重庆邮电大学内网外入
By Gufeng Shen on · Reading time: 2 mins说明
- 本文是教你搭建自己的内网外入的教程,不是提供一个公共的内网外入服务。
- 文中的
endpoint.domain
代表你自己用于搭建内网外入的入口域名。当您需要使用内网外入时,请将其替换为真实的入口域名。
效果
- 如果你要访问的内网网站有域名: 将域名中的
cqupt.edu.cn
替换为endpoint.domain
即可。 - 如果你要访问的内网网站只有 IP 地址: 将 IP 地址放在
.endpoint.domain
域名之前。
举个例子
- 如果你要访问
jwzx.cqupt.edu.cn
,则在外网访问jwzx.endpoint.domain
- 使用
127.20.0.1.endpoint.domain
来访问127.20.0.1
Advanced Notices
- Some destination server requires an TLS connection, and
*.secure.endpoint.domain
is aimed to do that. Otherwise,*.endpoint.domain
will initiate a plain HTTP request to the destination. - Considering there will be many direct IP forwards, and there is no need to acquire a certificate for them. Thus, any domain access like
jwzx.endpoint.domain
is provided with a valid wildcard certificate, while IP accesses are not. - Destinations with unusual port(other than 80 and 443) are not supported and their link will not be overridden.
教程
- 在内网放置一个小服务器,它可以是个树莓派。
- 在树莓派上安装 NGINX,然后按照下文配置 NGINX。
- 配置内网穿透,将 NGINX 暴露在公网,并将
*.endpoint.domain
和*.secure.endpoint.domain
指向公网上的 NGINX。
覆盖重定向
对于使用 cqupt.edu.cn
为结尾的域名:
proxy_redirect ~^https?://cqupt.edu.cn/(.*) https://endpoint.domain/$1;
proxy_redirect ~^http://(.*).cqupt.edu.cn/(.*) https://$1.endpoint.domain/$2;
proxy_redirect ~^https://(.*).cqupt.edu.cn/(.*) https://$1.secure.endpoint.domain/$2;
对于 IP 地址:
proxy_redirect ~^http://([0-9.]+)/(.*) http://$1.endpoint.domain/$2;
proxy_redirect ~^https://([0-9.]+)/(.*) http://$1.secure.endpoint.domain/$2;
替换页面中的链接
nginx_substitutions_filter
是增强版的 sub_filter
,它可以实现多个正则表达式同时进行替换
访问 Substitutions | NGINX 来查看更多信息。
subs_filter 'https?://cqupt.edu.cn' https://endpoint.domain gir;
subs_filter 'http://(.*).cqupt.edu.cn' https://$1.endpoint.domain gir;
subs_filter 'https://(.*).cqupt.edu.cn' https://$1.secure.endpoint.domain gir;
subs_filter 'http://([0-9.]+)' http://$1.endpoint.domain gir;
subs_filter 'https://([0-9.]+)' http://$1.secure.endpoint.domain gir;
因为接下来会配置防火墙,我们不需要在此匹配到精确的 IP 地址,也不需要做过多检查。
使用下面的语句来从校内的 DNS 服务器解析内部专用的域名
resolver [ns1.ip] [ns2.ip] valid=3600s;
最后配置 iptables
防火墙来过滤非正常请求
# Generated by xtables-save v1.8.3 on Thu Dec 19 03:26:35 2019
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -d [network/mask] -p tcp -m owner --uid-owner 33 -j ACCEPT
...
-A OUTPUT -d [ns1.ip] -p udp -m owner --uid-owner 33 -j ACCEPT
-A OUTPUT -d [ns2.ip] -p udp -m owner --uid-owner 33 -j ACCEPT
-A OUTPUT -m owner --uid-owner 33 -j REJECT --reject-with icmp-port-unreachable
COMMIT
# Completed on Thu Dec 19 03:26:35 2019