Centos7快速搭建Ngrok服务器及配置文件详解

准备工作:

  1. 一台具有公网IP的服务器(服务器端)
  2. 一个已经解析到此服务器的域名
  3. 内网服务器(客户端)

步骤:

一、服务器端的操作

  • 安装git(先卸载旧版本)
yum remove git
yum install epel-release
yum install https://centos7.iuscommunity.org/ius-release.rpm
yum install git2u
git --version  # 返回git version 2.5.0 则安装成功
  • 安装go语言环境
yum install -y golang
  • 下载ngrok源码
mkdir ~/ngrok
cd ngrok
git clone https://github.com/inconshreveable/ngrok.git
export GOPATH=~/ngrok/ngrok
  • 生成自签名证书
cd ngrok
export DOMAIN_NAME='example.com'  # 这里的域名写你自己的
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$DOMAIN_NAME" -days 5000 -out rootCA.pem
openssl genrsa -out device.key 2048
openssl req -new -key device.key -subj "/CN=$DOMAIN_NAME" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
  • 把生成的证书分别替换到 assets/client/tlsassets/server/tls中, 这两个目录分别存放着ngrok和ngrokd的默认证书。在cp前加上' \ '强制覆盖不提示
\cp rootCA.pem assets/client/tls/ngrokroot.crt
\cp device.crt assets/server/tls/snakeoil.crt
\cp device.key assets/server/tls/snakeoil.key
  • 编译ngrokd和ngrok
make release-server

需要等待一会儿,接下来根据你客户端的系统种类来执行ngrok的编译

GOOS=linux GOARCH=amd64 make release-client  # linux的客户端
GOOS=windows GOARCH=amd64 make release-client  # windows的客户端
GOOS=darwin GOARCH=amd64 make release-server release-client  # mac的客户端

编译完成之后,可以在~/ngrok/ngork/bin目录下看到ngrok与ngrokd两个文件

把ngrok下载到你的客户端,也就是内网服务器,稍后使用

先启动服务端

/root/ngork/ngrok/bin/ngrokd -domain=example.com -httpAddr=:5442 -httpsAddr=:5443

二、客户端操作

  • 创建ngrok配置文件,并且写入配置
vi ~/.ngrok

# 写入以下内容

server_addr: "example.com:4443"  # 此处域名换成你自己的
trust_host_root_certs: false
tunnels:
  ssh:
    proto:
      tcp: 22
    remote_port: 12345  # 这个为绑定在服务器端的端口号
  • 启动ngrok客户端

进入之前下载了ngrok客户端的目录

ngrok start ssh

出现以下内容则表示成功连接到你的Ngrok服务器,并且此时已经可以通过域名 example.com 加 12345 端口与你的内网服务器建立ssh连接了

ngrok                                                                                                   (Ctrl+C to quit)
                                                                                                                        
Tunnel Status                 online                                                                                    
Version                       1.7/1.7                                                                                   
Forwarding                    tcp://example.com:12345 -> 127.0.0.1:22                                                 
Web Interface                 127.0.0.1:4040                                                                            
# Conn                        0                                                                                         
Avg Conn Time                 0.00ms 

三、.ngrok配置文件详解

  • ngrok 的配置文件默认从 ~/.ngrok 加载。你可以通过 -config 参数重写配置文件的地址
  • 在配置文件中使用tunnels参数可以添加多个隧道,例如
tunnels:
  client:
    auth: "user:password"
    proto:
      https: 8080
  ssh:
    proto:
      tcp: 22
  hacks.inconshreveable.com:
    proto:
      http: 9090

启动客户端

ngrok start client ssh hacks.inconshreveable.com  # 一次性启动多个隧道
  • 添加 subdomain属性可以设置子域名
tunnels:
  client:
    subdomain: "example"
    auth: "user:password"
    proto:
      https: 8080
  •  针对TCP隧道可以设置remote_port改变服务器端绑定的端口号
tunnels:
  ssh:
    remote_port: 60123
    proto:
      tcp: 22

ngrok中文文档:http://ngrok.cn/docs.html

ngrok更多配置详解:https://imlonghao.com/28.html

个人网站:mew.wiki  期待您的来访!

发布了9 篇原创文章 · 获赞 4 · 访问量 2822

猜你喜欢

转载自blog.csdn.net/weixin_44129672/article/details/88560311