Deploy your own ngrok in 7 minutes

Keywords: Windows Linux git yum

Deploy your own ngrok in 7 minutes

Article Background

Recently, to develop the WeChat Public Number, we need to use the external network ip. However, the free version on the Internet is either slow or changing IP regularly, so we decided to deploy a ngrok by ourselves using the linux server and domain name.
Note: Looking back at the pin development documentation, I found that he has an intranet penetration tool, but I have deployed my own ngrok. Can a partner without a domain name tell me in the comments that it's good to use? Nail Inner Mesh Penetration Tool

Pre-preparation

Linux Server 1 (Nginx installed)
Domain name one (test hosts file c:\windows\system32\drivers\etc which can modify computer directly)

Test here I direct the domain names: ngrok.test.com and wx.ngrok.test.com to my server ip
If the ngrok domain name points to ngrok.test.com, you can specify any prefix later, such as wx, then the final domain name points to wx.ngrok.test.com

Install software

First enter the / usr/local / directory, where all subsequent operations will take place

  1. Install git and go

    yum install gcc -y
    yum install git -y
    yum install mercurial git bzr subversion golang golang-pkg-windows-amd64 golang-pkg-windows-386 -y
    yum install epel-release -y
    yum install golang -y
    
  2. Install ngrok

    git clone https://github.com/tutumcloud/ngrok.git ngrok
    
  3. Generate a file and overwrite the original certificate

    cd ngrok
    
    openssl genrsa -out base.key 2048
    
    openssl req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=ngrok.test.com" -out base.pem
    
    openssl genrsa -out server.key 2048
    
    openssl req -new -key server.key -subj "/CN=ngrok.test.com" -out server.csr
    
    openssl x509 -req -in server.csr -CA base.pem -CAkey base.key -CAcreateserial -days 10000 -out server.crt
    

    Replace files (all require y to confirm overwrite)

    cp base.pem /usr/local/ngrok/assets/client/tls/ngrokroot.crt
    
    cp server.crt /usr/local/ngrok/assets/server/tls/snakeoil.crt
    
    cp server.key /usr/local/ngrok/assets/server/tls/snakeoil.key
    
  4. Build server (long compilation time, please be patient)

    make release-server
    
  5. Generate clients on demand (client generation path/ngrok/bin/xxx (corresponding version), note that if linux64 bits are generated, /ngrok/bin/ngrok)
    Note: New servers and clients need to be regenerated if the domain name is subsequently modified

    GOOS=windows GOARCH=amd64 make release-client
    #After successful compilation, a windows_amd64 directory with ngrok.exe will be generated under ngrok/bin/on demand
     
    #Linux platform 32-bit system:
    GOOS=linux GOARCH=386 make release-client
    #64-bit system on Linux platform:
    GOOS=linux GOARCH=amd64 make release-client
    #Windows Platform 32-bit System:
    GOOS=windows GOARCH=386 make release-client
    #Windows Platform 64-bit System:
    GOOS=windows GOARCH=amd64 make release-client
    #MAC platform 32-bit system:
    GOOS=darwin GOARCH=386 make release-client
    #MAC platform 64-bit system:
    GOOS=darwin GOARCH=amd64 make release-client
    #ARM platform:
    GOOS=linux GOARCH=arm make release-client
    
  6. Background startup server (any three port numbers can be specified)

    setsid ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="ngrok.test.com"  -httpAddr=":8001" -httpsAddr=":8002" -tunnelAddr=":4443"
    

    Check if the server is started properly

    netstat -tunple | grep 4443
    
  7. Modify nginx configuration file and restart

        server{
    		listen 80;
            server_name *.ngrok.test.com;
           
            location / {
                  proxy_pass  http://127.0.0.1:8001;
                  proxy_redirect off;
                  proxy_set_header   Host $http_host:8001;
                  #proxy_set_header   Host             $host:8001;
                  proxy_set_header   X-Real-IP        $remote_addr;
                  #proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                  proxy_connect_timeout 1; 
                  proxy_send_timeout 120; 
                  proxy_read_timeout 120;
            }
           
    	}
    
  8. Start the client (note if the cloud server's security group policy and firewall block the three ports above)

    1. Download client and add profile
      Download/ngrok/bin client files from the server locally and add the configuration file ngrok.cfg to the directory at the same level

      server_addr: ngrok.test.com:4443
      trust_host_root_certs: false
      
    2. cmd enters the current directory to start

      Log file records may not be specified if they can be started properly, where subdomain s specify any prefix specified
      The entire command is to specify the prefix wx, map the local port 80, and save the log file to the peer directory ngrok.log

      ngrok -config=./ngrok.cfg -log=./ngrok.log -subdomain=wx 80
      
    3. Visit the web address: wx.ngrok.test.com, just visit it normally

14 original articles published. 2. 10,000 visits+
Private letter follow

Posted by baze on Thu, 16 Jan 2020 18:00:42 -0800