Build your own ngrok service

1. Server preparation
I tried it on CentOS 6 and the compiler got stuck. It is said that it is. Git As a result of the low version, I want to upgrade git. Who knows there are a lot of problems, so I simply give up, install CentOS 7 and deploy it on it.
2. Go Environmental construction
Go environment installation can be installed by source code or EPEL extension source and then installed by yum. Because go installed by Yum can not be cross-compiled and compiled to generate Windows client, it is recommended to use source code installation.  
The detailed process of source code installation go is as follows:
1). Download the source code, you can http://www.golangtc.com/download Find the source code for your system. Because my vps system is centos, the download is go1.7.1. Linux-amd64.tar.gz.  
2. Unzip it into the / usr/local directory:

tar -C /usr/local  -xzf   go1.7.1.linux-amd64.tar.gz  
  • 1
  • 1

Pay attention to downloading for yourself operating system Version
3. Setting environment variables

mkdir $HOME/go  
echo 'export GOROOT=/usr/local/go'>> ~/.bashrc  
echo 'export GOPATH=$HOME/go'>> ~/.bashrc  
echo 'export PATH=$PATH:$GOROOT/bin'>> ~/.bashrc  
source  $HOME/.bashrc  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

4. Install the go get tool

yum install mercurial git bzr subversion 
  • 1
  • 1

2. Get ngrok source code
The git version needs to be over 1.7.9.5, and if it does not meet the requirements, it needs to be upgraded. My git version here is 1.8.3.1.  
Get the source code:

git clone https://github.com/inconshreveable/ngrok.git  
  • 1
  • 1

3. compilation
1. Configuring environment variables

export NGROK_DOMAIN="tunnel.ngrok.me"  
  • 1
  • 1

tunnel.ngrok.me is replaced by your own domain name.  
2. Generating self-signed ssl certificates

cd ngrok

openssl genrsa -out rootCA.key 2048

openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem

openssl genrsa -out device.key 2048

openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr

openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000

cp rootCA.pem assets/client/tls/ngrokroot.crt

cp device.crt assets/server/tls/snakeoil.crt

cp device.key assets/server/tls/snakeoil.key  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Set variables:

GOOS=linux GOARCH=amd64  #If it's a 32-bit system, here GOARCH=386  
  • 1
  • 1

Generating Server and Client

make release-server release-client  
  • 1
  • 1

After compilation, two executable files are generated in the bin directory of the ngrok source code: ngrokd and ngrok.  
ngrokd is the server program of ngrok, and ngrok is the client program of ngrok.  
Since the generated client ngrok can only run under linux, if you want to generate client programs under windows, you need to continue cross-compiling.  
4. Cross-compilation
1. Generating windows Client
Both the server and client generated by the above compilation process are under linux and cannot be used under windows. If you want to compile and generate windows clients, you need to reconfigure the environment and compile it.  
The cross-compilation process is as follows:

GOOS=windows GOARCH=amd64 make release-server release-client
  • 1
  • 1

After compilation, the windows_amd64 directory will be generated under the bin directory, which contains the server and client programs running under windows.  
There are articles on the Internet that say it is necessary to:__________

cd  /usr/local/go/src/

GOOS=windows GOARCH=amd64 CGO_ENABLED=0 ./make.bash  
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

I didn't recompile go, but I also generated a windows client. Maybe it's because of my high version of go?
2. Compile arm client

sudo GOOS=linux GOARCH=arm make release-client
  • 1
  • 1

3. Compiling mac version client

GOOS=darwin GOARCH=amd64 make  release-client
  • 1
  • 1

5. Setting up domain name resolution
Add two A records: tunnel.ngrok.me and *. tunnel.ngrok.me, pointing to the local Ngrok server ip.  
6. Startup and use of ngrokd services
1. Start ngrokd server

bin/ngrokd -domain="$NGROK_DOMAIN" 
  • 1
  • 1

The results are as follows:

[09:40:02 HKT 2016/10/21] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified
[09:40:02 HKT 2016/10/21] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:80
[09:40:02 HKT 2016/10/21] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443
[09:40:02 HKT 2016/10/21] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443
[09:40:02 HKT 2016/10/21] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

Default forwarding port 80 and 443, listening port 4443
If you want to change, you need to add parameters

-httpAddr=":8000" -httpsAddr=":44443"
  • 1
  • 1

NGROK_DOMAIN must be specified, otherwise the secondary domain name of ngrok.com will be generated, so it will not be used.
2. Start ngrok client
Client use, copy the newly generated ngrok.exe file to local, create ngrok.cfg configuration file, can be configured according to their own actual situation.

server_addr: "tunnel.ngrok.me:4443"  
trust_host_root_certs: false  
  • 1
  • 2
  • 1
  • 2

ngrok -config=ngrok.cfg -subdomain test 8080 
test is your secondary domain address.
Seeing this information shows that you have succeeded.

ngrok

Tunnel Status                 online
Version                       1.7/1.7
Forwarding                    http://test.tunnel.ngrok.me -> 127.0.0.1:80
Forwarding                    https://test.tunnel.ngrok.me -> 127.0.0.1:80
Web Interface                 127.0.0.1:4040
# Conn                        0
Avg Conn Time                 0.00ms
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

If something goes wrong, you can add parameters to see why.
-log=stdout

The cause of the error may be certificate or domain name. It must be the same as NGROK_DOMAIN.
If a firewall is opened, ports 80, 443, 4443 must also be opened.

Reference:
http://bbear.me/shi-yong-a-li-yun-da-jian-zi-ji-de-ngrokfu-wu/ 
http://chuansong.me/n/341281851829 
https://www.sunnyos.com/article-show-48.html 
http://tonybai.com/2015/03/14/selfhost-ngrok-service/

Tags: Windows git Linux OpenSSL

Posted on Sat, 20 Apr 2019 04:15:33 -0400 by jvanv8