基于docker搭建ngrok私服

2023-11-04

经常开发微信的同学们,肯定会经常用到内网穿透~

大部分人选择网上寻找各种现成的,比如ngrok官网、natapp、sunny-ngrok或者花生壳之类的。但是世界上没有免费的午餐,要不就是收费,要不就是免费但是偶尔会出现连接失败的问题(当然大多数时间是没有问题的)。

偶然,正在测试微信的某些功能,但是正在使用的ngrok连接失败了。导致测试无法进行,最终萌生出自己搭建一个ngrok服务器的想法。

闲话少说,直接上干货:

备料

要有云服务器一台;

公网域名一个,可以是一级域名也可以是二级域名,我这里使用二级域名ngrok.mydomain.com进行举例。

解析域名

    A记录 解析ngrok.mydomain.com到服务器ip

    CNAME 解析*.ngrok.mydomain.com 到 ngrok.mydomain.com

准备环境

    云服务器安装docker环境, 具体方法见 传送门, 这里不展开讲

   新建一个文件夹,内建两个文件 Dockerfile 和 build.sh

    Dockerfile

FROM golang:1.7.1-alpine
#切换国内源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

ADD build.sh /
RUN apk add --no-cache git make openssl
RUN git clone https://gitee.com/julywind/ngrok.git --depth=1 /ngrok
RUN sh /build.sh
ENV DOMAIN=ngrok.mydomain.com
EXPOSE 80
EXPOSE 443
EXPOSE 4443
VOLUME [ "/ngrok" ]
CMD [ "sh", "-c", "/ngrok/bin/ngrokd -domain ${DOMAIN}"]

build.sh 这里主要是用于生成自建证书,并且编译出服务器端ngrokd和各个平台下的客户端,  生成的文件都存在于容器内的 /ngrok/bin下

export NGROK_DOMAIN="ngrok.mydomain.com"
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

make release-server
GOOS=linux GOARCH=386 make release-client
GOOS=linux GOARCH=amd64 make release-client
GOOS=windows GOARCH=386 make release-client
GOOS=windows GOARCH=amd64 make release-client
GOOS=darwin GOARCH=386 make release-client
GOOS=darwin GOARCH=amd64 make release-client
GOOS=linux GOARCH=arm make release-client

构建并且启动镜像

#构建镜像
docker build -t ngrok .

#启动镜像, 为了不影响服务器上其他的服务,这里不占用80和443端口,同样默认的4443端口也使用了nginx代理
docker run -itd --name=ngrok -p880:80 -p8443:443 -p4444:4443 ngrok

配置访问代理

安装nginx后,修改其配置:

修改/etc/nginx/nginx.conf,  在http标签同级,添加tcp端口代理

#这里纯粹是为了记录如何配置tcp代理,如果嫌麻烦,也可以直接在启动docker容器的时候 直接暴露4443端口
stream {
    server {
        listen 4443;
        proxy_pass localhost:4444;
    }
}

  添加vhost,   centos下:  vi /etc/nginx/conf.d/ngrok.conf

server {
        listen 80;
        listen [::]:80;

        keepalive_timeout 70;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name ngrok.mydomain.com *.ngrok.mydomain.com;
        location / {
            proxy_pass http://localhost:880/;
            proxy_redirect off;
            proxy_connect_timeout 1;
            proxy_send_timeout 120;
            proxy_read_timeout 120;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #千万不能带80,后面我会告诉你为何
            #proxy_set_header Host  $http_host:80;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
        }
}


server {
        listen 443;
        listen [::]:443;

        server_name ngrok.mydomain.com *.ngrok.mydomain.com;

        root /var/www/html;
        index index.html;

        location / {
            proxy_pass https://localhost:8443/;
            proxy_redirect off;
            proxy_connect_timeout 1;
            proxy_send_timeout 120;
            proxy_read_timeout 120;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #千万不能带443,后面我会告诉你为何
            #proxy_set_header Host  $http_host:443;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
        }
}

检查nginx的配置项,没问题即可加载

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload

访问 http://ngrok.mydomain.com,提示 tunel not found 即表示服务端已经就绪

配置客户端

这里需要特别说明一下:

   构建镜像的时候,服务器端和客户端一并生成了,并且客户端内已经内置了使用的证书,所以千万要注意: “服务器端和客户端是一一对应的,一旦服务器端的证书变了,就需要重新取新构建好的客户端”​​​

通用客户端并不能连上使用自建证书的服务器端,获取客户端的方法如下:

查看docker内文件的所在目录:

 docker inspect ngrok | grep volume

可以看到在服务器上的目录是 /var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data,

# ls -l /var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data
total 64
drwxr-xr-x 4 root root 4096 Jan 31 22:16 assets
drwxr-xr-x 8 root root 4096 Jan 31 22:16 bin
drwxr-xr-x 2 root root 4096 Jan 31 22:16 contrib
-rw-r--r-- 1 root root  199 Jan 31 21:58 CONTRIBUTORS
-rw-r--r-- 1 root root  993 Jan 31 21:58 device.crt
-rw-r--r-- 1 root root  899 Jan 31 21:58 device.csr
-rw-r--r-- 1 root root 1675 Jan 31 21:58 device.key
drwxr-xr-x 2 root root 4096 Jan 31 22:16 docs
-rw-r--r-- 1 root root  551 Jan 31 21:58 LICENSE
-rw-r--r-- 1 root root 1433 Jan 31 21:58 Makefile
drwxr-xr-x 9 root root 4096 Jan 31 22:16 pkg
-rw-r--r-- 1 root root 2725 Jan 31 21:58 README.md
-rw-r--r-- 1 root root 1679 Jan 31 21:58 rootCA.key
-rw-r--r-- 1 root root 1111 Jan 31 21:58 rootCA.pem
-rw-r--r-- 1 root root   17 Jan 31 21:58 rootCA.srl
drwxr-xr-x 5 root root 4096 Jan 31 22:16 src

客户端就在bin目录下,选择对应的端安装下载到本地即可

# ls -l bin
total 21120
drwxr-xr-x 2 root root     4096 Jan 31 22:16 darwin_386
drwxr-xr-x 2 root root     4096 Jan 31 22:16 darwin_amd64
-rwxr-xr-x 1 root root  2655629 Jan 31 21:58 go-bindata
drwxr-xr-x 2 root root     4096 Jan 31 22:16 linux_386
drwxr-xr-x 2 root root     4096 Jan 31 22:16 linux_arm
-rwxr-xr-x 1 root root 10775527 Jan 31 21:59 ngrok
-rwxr-xr-x 1 root root  8134110 Jan 31 21:59 ngrokd
drwxr-xr-x 2 root root     4096 Jan 31 22:16 windows_386
drwxr-xr-x 2 root root     4096 Jan 31 22:16 windows_amd64

下载客户端可以用scp,也可以用sz

#scp方式
scp root@服务器IP:/var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data ./

#sz方式,需要服务器上安装了lrzsz(支持yum和apt直接安装)
sz /var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data/bin/windows_amd64/ngrok.exe

在下载到的客户端同级别的目录下,新建一个文件 config.cfg,内容如下:

server_addr: "ngrok.mydomain.com:4443"
trust_host_root_certs: false
#这里面可以直接配置,也可以不配置,启动的时候再传入要用的端口
tunnels:
    node:
        proto:
            https: 3000
        subdomain: node
    #http-wx:
    #    proto:
    #        http: 3000
    #    subdomain: wx
    #http:
    #    proto:
    #        http: 80
    #    subdomain: pi
    #https:
    #    proto:
    #        https: 443
    #    subdomain: web
    #ssh:
    #    remote_port: 5000
    #    proto:
    #        tcp: 22

启动客户端:

#方法一,启动预设配置
#ngrok.exe -config=config.cfg -log a.log start-all 


#方法二,启动临时端口,自动分配子域名
#ngrok.exe -config=config.fg -proto http 3000

#启动临时端口, 指定子域名 node.ngrok.mydomain.com
#ngrok.exe -subdomain node -config=config.fg -proto http 3000

正常情况下,看到这个界面,就已经可以愉快滴玩耍了。

当然也不排除有问题的可能,因为我也踩了一个大坑:访问外网地址一直提示 

Tunnel xxx.ngrok.mydomain not found

具体情况见另外一篇帖子(传送门)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于docker搭建ngrok私服 的相关文章

随机推荐

  • java连接kafka测试

    进入到kafka文件夹中修改配置文件 vim config server properties 启动zookeeper bin zookeeper server start sh config zookeeper properties 端口
  • python mssql数据库开发_Python实现的连接mssql数据库操作示例

    本文实例讲述了Python实现的连接mssql数据库操作 分享给大家供大家参考 具体如下 1 目标数据sql2008 R2 ComPrject gt TestModel 2 安装python 连接mssql 模块 运行 pip instal
  • 小程序hover-class点击态效果——小程序体验

    微信小程序设置 hover class 实现点击态效果 增强小程序触感 提高用户交互感知度 概念及注意事项 微信小程序中 可以用 hover class 属性来指定元素的点击态效果 但是在在使用中要注意 大部分组件是不支持该属性的 目前支持
  • Spring Cache

    Spring Cache Spring Cache使用方法与Spring对事务管理的配置相似 Spring Cache的核心就是对某个方法进行缓存 其实质就是缓存该方法的返回结果 并把方法参数和结果用键值对的方式存放到缓存中 当再次调用该方
  • Cisco Voip实验

    http hackerjx blog 51cto com 383839 248031 转载于 https blog 51cto com markyan 1043695
  • Java基础-I/O流(文件字节流)

    字符串常见的字符底层组成是什么样的 英文和数字等在任何国家的字符集中都占1个字节 GBK字符中一个中文字符占2个字节 UTF 8编码中一个中文字符占3个字节 注意 编码前的字符集和编码好的字符集要必须一致 否则会出现中文字符乱码 英文和数字
  • NullPointerException : HiveAuthorizerImpl.checkPrivileges(HiveAuthorizerImpl.java:85)

    背景 做hive sentry LDAP授权 1 jdbc hive2 localhost 10000 gt connect jdbc hive2 localhost 10000 Connecting to jdbc hive2 local
  • Gitlab中Pipeline语法三

    Pipeline语法三 only except rules workflow only和except 用分支策略来限制jobs构建 only 定义哪些分支和标签的git项目将会被job执行 except定义哪些分支和标签的git项目将不会被
  • 腾讯云对象存储的创建和S3 Browser的使用

    简述 想想第一次接触对象存储的时候还是很兴奋的 同时也是一脸懵逼 然后开始网上疯狂的找资料 但因为客户当时给的文档写的是关于Amazon S3之类的 所以自以为的就只有Amazon S3这一家 接着开始查资料 经过一番努力最后在Amazon
  • ubuntu下下载安装dstat

    如果直接安装采用s sudo apt install dstat 或者通过下载方式 https launchpad net ubuntu source dstat 0 7 4 6 1 下载这三个包 然后里面直接运行 dstat就可以了 前提
  • I3Net: Implicit Instance-Invariant Network for Adapting One-Stage Object Detectors

    摘要 最近的两阶段跨域检测工作广泛探索了局部特征模式 以获得更准确的适配结果 这些方法在很大程度上依赖于区域建议机制和基于ROI的实例级特性来设计关于前景目标的细粒度特性对齐模块 然而 对于一阶段检测检测器 在检测流程中很难甚至不可能获得显
  • 【Scapy】使用Python的Scapy包对Wirshark捕获的Http数据包进行解析(格式输出)

    通过Anaconda安装scapy pip install scapy http Python源码如下 实现功能 1 读取本地pcap文件 文件内容为Wirshark捕获的数据二进制流 2 通过scapy将二进制数据流解析为有结构的pake
  • 贪心算法解决集合覆盖问题

    问题描述 假设存在下面需要付费的广播台 以及广播台需要覆盖的地区 如何选择最少的广播台 让所有的地区都可以接受到信号 广播台 覆盖地区 k1 北京 上海 天津 k2 广州 北京 深圳 k3 成都 上海 杭州 k4 上海 天津 k5 杭州 大
  • python折线图设置标题

    在 Python 中使用 Matplotlib 绘制折线图时 可以使用 title 函数来设置图表的标题 例如 import matplotlib pyplot as plt 绘制折线图 plt plot x y 设置标题 plt titl
  • Android系统目录树建立过程

    一 文件系统类型 了解Android系统目录树的建立之前 有必要了解文件系统类型 Linux内核中将文件系统类型抽象为结构体struct file system type 其中name为文件系统名称 例如ext4 f2fs rootfs等
  • 学习笔记-接口测试(postman、jmeter)

    一 什么是接口测试 通常做的接口测试指的是系统对外的接口 比如你需要从别的系统来获取到或者同步资源与信息 他们会提供给你一个写好的接口方法供你调用 比如常用的app 用户同步这些在处理数据的时候需要通过接口进行调用 webService接口
  • 让HR眼前一亮:30个APP项目软件测试经验,点燃你的简历

    在求职过程中 我们都希望自己的简历能够吸引面试官的眼球 从而获得更多的面试机会 作为一名软件测试人员 丰富的实战经验是让自己脱颖而出的关键之一 在我多年从事APP项目软件测试的工作中 我积累了大量的实践经验 并成功将这些经验写入简历中 让自
  • 剑指 Offer 05. 替换空格

    题目链接 05 替换空格 思路分析 遍历判断即可 class Solution public string replaceSpace string s std ios sync with stdio false string tmp for
  • 谈谈ChatGPT对中国教育的影响与挑战,我们该怎么办?

    ChatGPT对中国教育的影响 1 个性化教学 通过分析学生的学习习惯和问题 AI可以为每个学生提供个性化的学习路径 例如 如果一个学生在数学上表现出困难 AI可以提供更多的数学练习和教学资源 2 在线教育 AI可以作为在线课程的一部分提供
  • 基于docker搭建ngrok私服

    经常开发微信的同学们 肯定会经常用到内网穿透 大部分人选择网上寻找各种现成的 比如ngrok官网 natapp sunny ngrok或者花生壳之类的 但是世界上没有免费的午餐 要不就是收费 要不就是免费但是偶尔会出现连接失败的问题 当然大