内网穿透两种方法

两种方法,分别为ssh隧道和ngork。ssh隧道可能有超时断开的问题,ngork是开源的工具,本身有官网提供服务转发,我们可以自己搭建。

ssh隧道

设内网服务器:192.168.0.101,用户Alice

外网服务器:1.2.3.4,用户Bob

# 反向代理,在内网主机中执行,让远程主机的10012端口,代理内网主机的22端口
ssh -NfR 1.2.3.4:10012:192.168.0.101:22 Bob@1.2.3.4
​
# 正向代理,在外网主机中执行,将本地10013转发到localhost的10012
ssh -fgCNL 10013:localhost:10012 Bob@127.0.0.1

参数解析:

  • -C:压缩数据传输

  • -f:后台认证用户密码

  • -N:不执行脚本和命令,通常和-f一起用

  • -g:允许远程主机连接到转发的端口,否则只能本地连。

参考:https://www.cnblogs.com/kwongtai/p/6903420.html

https://www.cnblogs.com/pheye/p/8207378.html

ngork

必须准备一个域名!可以内网测试自己搭。

编译安装

apt install -y git golang
​
git clone https://github.com/inconshreveable/ngrok.git
​
mkdir ngork && cd ngork
​
# 指明转发服务器域名,关乎证书,非常重要
export NGROK_DOMAIN="abc.com"
​
# 生成密钥
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
​
# 编译服务器和客户端
GOOS=linux GOARCH=amd64 make release-server
GOOS=linux GOARCH=amd64 make release-client
#Linux 平台 32 位系统:GOOS=linux GOARCH=386
#Linux 平台 64 位系统:GOOS=linux GOARCH=amd64
#Windows 平台 32 位系统:GOOS=windows GOARCH=386
#Windows 平台 64 位系统:GOOS=windows GOARCH=amd64
#MAC 平台 32 位系统:GOOS=darwin GOARCH=386
#MAC 平台 64 位系统:GOOS=darwin GOARCH=amd64
#ARM 平台:GOOS=linux GOARCH=arm
​
# 启动,8083是要连接的
sudo ./bin/ngrokd -domain="abc.com"  -httpAddr=":80" -httpsAddr=":443" -tunnelAddr=":8083" &

  

配置文件一般如下,

server_addr: "abc.com:8083" // 8083 监控端口
​
trust_host_root_certs: false
​
tunnels:
    http:
        subdomain: "www"
    proto:
        http: "80"    // http 端口 httpAddr=":80"
​
    https:
        subdomain: "www"
    proto:
        https: "443"  // https 端口 httpsAddr=":443"
​
ssh:
    remote_port: 2222
    proto:
        tcp: "22"             
​
mstsc:
    remote_port: 52222  // 远程开启52222或其他,只要不冲突 
    proto:
        tcp: "192.168.1.7:3389" // 本地windows的ip以及远程访问端口3389(默认)

 

启动:

sudo ./bin/ngrokd -domain="abc.com"  -httpAddr=":80" -httpsAddr=":443" -tunnelAddr=":8083" &
​
# 在客户端配置好后启动即可,.ngork即为上面配置文件:
ngrok -config=.ngrok start http https ssh mstsc

  

参考:https://blog.csdn.net/lifuma/article/details/82349340

https://www.jianshu.com/p/796c3411f8eb

客户端验证:https://ask.csdn.net/questions/688161

官网:https://ngrok.com/

http://ngrok.cn/docs.html

posted on 2019-01-30 15:29  willaty  阅读(843)  评论(0编辑  收藏  举报

导航