[关闭]
@cyysu 2017-10-06T07:04:36.000000Z 字数 2729 阅读 1077

内网穿透-ngrok

  • 时间:2017年9月6日
  • 作者:MJ-DZ chen yuan
  • 邮箱:chenyuan@ypmingjiang.cn
  • 描述:ngrok server以及client编译以及嵌入式ARM平台移植
  • 版本:V2.0

内网映射


简介以及软件下载

  1. ngrok 是一个反向代理,通过在公共的端点和本地运行的Web服务器之间建立一个安全的通道。软件下载地址以及go,这里官方建议是1.4,本软件提供1.6版本,在生成客户端环境时需要注意一下如何配置。
  2. 链接:http://pan.baidu.com/s/1mhAWJLQ 密码:y40c

go环境设置

  1. export GOROOT=/usr/local/go
  2. export GOBIN=/usr/local/go/bin
  3. export PATH=$PATH:$GOBIN
  4. export GOPATH=/usr/local/src/ngrok/
  5. export NGROK_DOMAIN="ngrok.mj.com"
  6. 这里有一点需要注意的是:
  7. 如果我们采用源码安装方式安装go时,需要将
  8. sudo cp /usr/local/go/bin/go /usr/bin/go

根域证书生成

  1. openssl genrsa -out rootCA.key 2048
  2. openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
  3. openssl genrsa -out device.key 2048
  4. openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
  5. openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
  6. cp rootCA.pem assets/client/tls/ngrokroot.crt
  7. cp device.crt assets/server/tls/snakeoil.crt
  8. cp device.key assets/server/tls/snakeoil.key

server编译

  1. # 在/usr/local/src/ngrok/Makefile中修改GOOS以及对应的GOARCH
  2. sudo make release-server
  3. # 正常情况下是完全可以编译过去的,也不是报错,报错的可能性是权限的问题。

client编译

  1. #首先生成一下对应目标平台的环境
  2. sudo GOOS=windows GOARCH=amd64 ./make.bash
  3. # 对应ARM平台客户端生成
  4. sudo GOOS=linux GOARCH=arm ./make.bash
  5. 重点来了!!!!!!!
  6. 重点来了!!!!!!!
  7. 重点来了!!!!!!!
  8. 如果你的版本是1.4以上的那么这个时候肯定会报错的。报错信息如下:
  9. mj@DZ:/usr/local/go/src$ sudo GOOS=windows GOARCH=amd64 ./make.bash
  10. [sudo] mj 的密码:
  11. ##### Building Go bootstrap tool.
  12. cmd/dist
  13. ERROR: Cannot find /home/mj/go1.4/bin/go.
  14. Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
  15. 解决方案!!!!!!
  16. 解决方案!!!!!!
  17. 解决方案!!!!!!
  18. cp -rf /usr/local/go /usr/local/go1.4
  19. export GOROOT_BOOTSTRAP=/usr/local/go1.4/
  20. GOOS=windows GOARCH=amd64 ./make.bash
  21. #然后就可以进入ngrok中编译我们的客户端了。
  22. sudo make release-client

生成目录

  1. mj@DZ:/usr/local/ngrok/bin$ tree .
  2. .
  3. ├── go-bindata
  4. ├── linux_arm
  5.    └── ngrok
  6. ├── ngrok
  7. ├── ngrokd
  8. └── windows_amd64
  9. └── ngrok.exe
  10. 2 directories, 5 files

环境测试

  1. # 服务端运行程序
  2. mj@DZ:/usr/local/ngrok$ sudo bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":8000"
  3. [14:25:14 CST 2017/09/06] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified
  4. [14:25:14 CST 2017/09/06] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds
  5. [14:25:14 CST 2017/09/06] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8000
  6. [14:25:14 CST 2017/09/06] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443
  7. [14:25:14 CST 2017/09/06] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443
  8. # 客户端生成配置文件
  9. server_addr: "ngrok.cyysu.com:4443"
  10. trust_host_root_certs: false
  11. # 客户端运行
  12. ./ngrok -config=./ngrok.cfg -subdomain=test 80
  13. # 参数解析
  14. -config 就是上面配置文件ngrok.cfg的路径
  15. -subdomain 就是需要分配域名的前缀部分
  16. 80 就是本机websever的端口,比如apache监听的端口,一会ngrok会将请求映射到该端口上。
  17. # 运行正常会出现如下
  18. Tunnel Status online
  19. Version 1.7/1.7
  20. Forwarding http://test.ngrok.cyysu.com:8000 -> 127.0.0.1:80
  21. Forwarding https://test.ngrok.cyysu.com:8000 -> 127.0.0.1:80
  22. Web Interface 127.0.0.1:4040
  23. # Conn 0
  24. Avg Conn Time 0.00ms
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注