内网穿透方案

1 介绍

我们写了一个网站,但是又苦于没有公网ip地址,就可以使用内网穿透,来让局域网内的机器上的,可以被外网访问到

这种软件有很多:

如开源的:frp、ngrok

商业的软件:花生壳(转发http需要收费6元)、神卓互联(收费)

基于Python3 我们自己写一个

2 花生壳演示

官网下载客户端:https://hsk.oray.com/, 无脑下一步安装

注册账号

打开软件

image-20210507181747759

image-20210507184408988

image-20210507184519636

3 ngrok演示

官网注册账号:https://ngrok.com/ (人机验证使用的是谷歌,所以要FQ才能注册成功)

登陆后,下载客户端

把客户端解压,启动你的项目,比如我的启动在8000端口上

执行命令:

ngrok http 8000

会看到如下界面,有个https地址和一个http地址

在外网浏览器访问:http://be979d1d5703.ngrok.io/

就能看到你的网站了

image-20210507185014191

image-20210507185713416

4 frp演示(需要有公网ip)

开源软件地址为:https://github.com/fatedier/frp

文档地址为:https://gofrp.org/docs/setup/

下载地址:https://github.com/fatedier/frp/releases

image-20210507190345492

第一步:在公网服务器上下载(我的是阿里云的centos7.5)

# 1 下载
wget https://github.com/fatedier/frp/releases/download/v0.36.2/frp_0.36.2_linux_amd64.tar.gz
# 2 解压
tar xzf frp_0.36.2_linux_amd64.tar.gz 
# 3 进入
cd frp_0.36.2_linux_amd64/
# frpc  frpc_full.ini  frpc.ini  frps  frps_full.ini  frps.ini  LICENSE  systemd
#4  frps_full.ini是服务端配置文件(不用修改)
'''
[common]
#frp服务器监听地址,如果是IPV6地址必须用中括号包围
bind_addr = 0.0.0.0
#frp服务器监听端口
bind_port = 7000
 
#kcp的udp监听端口,如果不设那就不启用
#kcp_bind_port = 7000
#指定使用的协议,默认tcp,可选kcp
#protocol = kcp
 
#如果要使用vitual host,就必须设置
#vhost_http_port = 80
#vhost_https_port = 443
 
#Web后台监听端口
dashboard_port = 7500
 
#Web后台的用户名和密码
dashboard_user = admin
dashboard_pwd = admin
 
#Web后台的静态资源目录,调试用的,一般不设
#assets_dir = ./static
 
#日志输出,可以设置为具体的日志文件或者console
log_file = /var/log/frps.log
 
#日志记录等级,有trace, debug, info, warn, error
log_level = info
#日志保留时间
log_max_days = 3
 
#启用特权模式,从v0.10.0版本开始默认启用特权模式,且目前只能使用特权模式
#privilege_mode = true
 
#特权模式Token,请尽量长点且复杂
privilege_token = 12345678
 
#特权模式允许分配的端口范围
privilege_allow_ports = 2000-3000,3001,3003,4000-50000
 
#心跳超时,不用改
#heartbeat_timeout = 90
 
#每个代理可以设置的连接池上限
#max_pool_count = 5
 
#认证超时时间,一般不用改
#authentication_timeout = 900
 
#如果配置了这个,当你的模式为http或https时,就能设置子域名subdomain
#subdomain_host = frps.com
 
#是否启用tcp多路复用,默认就是true,不用管
#tcp_mux = true


'''


# 5 修改frps.ini内容如下
[common]
bind_port = 7000
vhost_http_port = 8888  # 服务端监听8888端口

# 6 启动服务端即可
./frps -c ./frps.ini
'''
[root@lqz frp_0.36.2_linux_amd64]# ./frps -c ./frps.ini
2021/05/07 19:10:40 [I] [root.go:200] frps uses config file: ./frps.ini
2021/05/07 19:10:40 [I] [service.go:192] frps tcp listen on 0.0.0.0:7000
2021/05/07 19:10:40 [I] [root.go:209] frps started successfully
'''

第二步:在windows机器上下载并启动客户端

# 1 下载解压
# 2 frps_full.ini是客户端配置文件(不用修改)

'''
[common]
#frp服务器地址
server_addr = 1.2.3.4
#frp服务器端口
server_port = 7000
#特权模式Token
privilege_token = 12345678
#转发SSH
[ssh]
type = tcp
#可以指定为其它IP,默认是本地
#local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
#启用加密
use_encryption = true
#启用压缩
use_compression = true
 
#转发Web
[web]
type = http
local_port = 80
custom_domains = www.yourdomain.com
#修改header中的host
#host_header_rewrite = dev.yourdomain.com
#启用简单HTTP认证
#http_user = abc
#http_pwd = abc
#在服务端配置了subdomain_host的情况下用于自定义二级域名
#subdomain = test
#在存在多个相同域名的情况下通过请求的URL路由到不同的配置
#locations = /news,/about
 
#转发DNS请求
[dns]
type = udp
local_ip = 8.8.8.8
local_port = 53
remote_port = 6000
 
#转发Unix域套接字(这儿是Docker)
[unix_domain_socket]
type = tcp
remote_port = 6000
plugin = unix_domain_socket
plugin_unix_path = /var/run/docker.sock
 
#HTTP代理
[http_proxy]
type = tcp
remote_port = 6000
plugin = http_proxy
#配置http代理的简单认证
#plugin_http_user = abc
#plugin_http_passwd = abc

'''
# 3 修改frpc配置文件
'''
[common]
server_addr = 101.133.225.166
server_port = 7000

[web]
type = http
local_ip = 127.0.0.1
local_port = 8000
custom_domains = frp.liuqingzheng.top
'''


# 4 启动
frpc -c frpc.ini

# 5 在阿里云的域名解析配置
# 6 外网浏览器里访问
http://frp.liuqingzheng.top:8888/

image-20210507194934001

4 自己使用python实现

待续吧

posted @ 2021-05-16 18:24  silencio。  阅读(234)  评论(0编辑  收藏  举报