此文由 Mix Space 同步更新至 xLog
为获得最佳浏览体验,建议访问原始链接
https://www.do1e.cn/posts/others/nju-ipv4
动机#
动机来自于搭建的网页。由于校内和公网都有搭建,一方面为了降低服务器流量,另一方面也可以为校内用户提供更快的速度,因此想获取南大的IP地址范围,在nginx里配置302重定向。
十分讨厌微信和 QQ 发送文件在每一个设备上都下载一份的行为,在手机上更是难以找到保存路径。
因此我使用vastsa/FileCodeBox搭建了一个临时的文件分析站。
其中 https://filebox.nju.do1e.cn 搭建在我校内的小主机上,因此只能在南大访问。而 https://filebox.cloud.do1e.cn 是使用rapiz1/rathole映射到我在新加坡的 VPS 上,方便公网访问。
另外我也用alist搭建了一个文件分享站。
校内: https://alist.nju.do1e.cn
公网: https://alist.do1e.cn
这个不是用端口映射,前者文件放在我校内主机的硬盘上,后者则是放在 OneDrive 上以减轻服务器带宽压力。OneDrive 和主机硬盘上的文件每天定时同步。
不过如果我只给别人发公网链接,在校内的人享受不到千兆校内网的速度,而我也不能每次发链接前问一下对方在校内还是校外。
因此想到了在 nginx 中判断源 IP 地址在校内的话就 302 重定向到校内网址就行。这样给别人链接的时候就只要给一个了。妙啊!
虽然在 ITSC 中询问了南大所有的 IP 地址范围,但他们不给。/_ \
旧版数据#
旧版数据来自于 https://github.com/FW27623/qqwry ,最新数据更新于**2024年09月25日**,但由于[纯真IP](https://cz88.net/geo-public)走向商业化,需要申请才行。但是你怎么知道我申请成功了?因此有了日更数据。
通过自行搜索发现纯真 IP给的 IPv4 地址位置还挺准的,但要获取全部数据库还挺麻烦,总不能每次有用户先请求一个 API 获取用户位置再返回吧。
最终还是找到了一份 2024 年 09 月 25 日的数据库,并解析出了所有地址为 “南京大学” 的 IPv4 段,这里分享给各位。
数据更新于2024 年 09 月 25 日,无法保证完全准确,请谨慎使用。
58.192.32.0 - 58.192.55.255
58.193.224.0 - 58.193.255.255
58.240.127.3 - 58.240.127.3
114.212.0.0 - 114.212.255.255
180.209.0.0 - 180.209.15.255
202.38.2.0 - 202.38.3.255
202.119.32.0 - 202.119.63.255
210.28.128.0 - 210.28.143.255
210.29.240.0 - 210.29.255.255
218.94.9.35 - 218.94.9.38
218.94.36.211 - 218.94.36.211
218.94.142.6 - 218.94.142.6
219.219.112.0 - 219.219.127.255
221.226.2.0 - 221.226.3.25
最终 nginx 中的配置如下:
|| 可能还有更优雅的写法,但是直接用 GPT 生成的不香吗?||
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name filebox.cloud.do1e.cn;
location / {
set $nju_ip 0;
if ($remote_addr ~ ^58\.192\.(3[2-9]|4[0-9]|5[0-5])\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^58\.193\.(22[4-9]|2[3-4][0-9]|25[0-5])\.) {
set $nju_ip 1;
}
if ($remote_addr = 58.240.127.3) {
set $nju_ip 1;
}
if ($remote_addr ~ ^114\.212\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^180\.209\.(0|1[0-5])\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^202\.38\.(2|3)\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^202\.119\.(3[2-9]|[4-5][0-9]|6[0-3])\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^210\.28\.(12[8-9]|1[3-4][0-9])\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^210\.29\.(24[0-9]|25[0-5])\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^218\.94\.9\.(3[5-8])$) {
set $nju_ip 1;
}
if ($remote_addr = 218.94.36.211) {
set $nju_ip 1;
}
if ($remote_addr = 218.94.142.6) {
set $nju_ip 1;
}
if ($remote_addr ~ ^219\.219\.(11[2-9]|12[0-7])\.) {
set $nju_ip 1;
}
if ($remote_addr ~ ^221\.226\.2\.(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25)$) {
set $nju_ip 1;
}
if ($nju_ip) {
return 302 https://filebox.nju.do1e.cn$request_uri;
}
proxy_pass http://127.0.0.1:3465;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
日更数据#
由于拿到了纯真 IP的授权,因此我可以在这里每天更新 IP 范围了(当然,纯真并不是日更,下面会注明更新日期)。
|| 应该也没有必要每天就是了,不过反正都自动化,我最喜欢自动化了。||
数据更新于,无法保证完全准确,请谨慎使用。
你的 IP 为,所属区域:,
。start | end | mask | mask_len | region |
---|
IP 地址位置数据由纯真 CZ88提供支持
用法#
下述配置文件为手动更新,最新使用的数据库日期为 2024 年 12 月 18 日
nginx 分流#
判断源地址,如果属于南大 IP,则重定向到在南大内的服务器。
在/etc/nginx/nginx.conf
中定义 geo:
http {
...
geo $njuip {
default 0;
58.192.32.0/20 1;
58.192.48.0/21 1;
58.193.224.0/19 1;
58.240.127.3 1;
114.212.0.0/16 1;
180.209.0.0/20 1;
202.38.2.0/23 1;
202.119.32.0/19 1;
210.28.128.0/20 1;
210.29.240.0/20 1;
218.94.9.35 1;
218.94.9.36/31 1;
218.94.9.38 1;
218.94.36.211 1;
218.94.142.6 1;
219.219.112.0/20 1;
221.226.2.0/25 1;
221.226.2.128/27 1;
221.226.2.160/28 1;
221.226.2.176/29 1;
221.226.2.184/31 1;
221.226.2.186 1;
221.226.2.187 1;
221.226.2.188/30 1;
221.226.2.192/26 1;
221.226.3.0/28 1;
221.226.3.16/29 1;
221.226.3.24/31 1;
221.226.3.27 1;
221.226.3.28/30 1;
221.226.3.32/27 1;
221.226.3.64/26 1;
221.226.3.128/25 1;
}
...
}
在需要重定向的server
中使用:
# filecodebox
server {
...
server_name example.com;
location / {
if ($njuip) {
return 302 https://nju.example.com$request_uri;
}
...
}
}
openvpn 分流#
判断访问的目的地址是否为南大地址,仅在访问南大地址的资源是从才走 VPN。
在已有的.ovpn
文件的dev tun
下面添加下述内容,删除 #注释
route-nopull # 不适用服务器下发的路由
route 10.8.0.0 255.255.0.0 vpn_gateway # 此处应修改为openvpn客户端网段
route 172.16.1.0 255.255.255.0 vpn_gateway
route 58.192.32.0 255.255.240.0 vpn_gateway
route 58.192.48.0 255.255.248.0 vpn_gateway
route 58.193.224.0 255.255.224.0 vpn_gateway
route 58.240.127.3 255.255.255.255 vpn_gateway
route 114.212.0.0 255.255.0.0 vpn_gateway
route 180.209.0.0 255.255.240.0 vpn_gateway
route 202.38.2.0 255.255.254.0 vpn_gateway
route 202.119.32.0 255.255.224.0 vpn_gateway
route 210.28.128.0 255.255.240.0 vpn_gateway
route 210.29.240.0 255.255.240.0 vpn_gateway
route 218.94.9.35 255.255.255.255 vpn_gateway
route 218.94.9.36 255.255.255.254 vpn_gateway
route 218.94.9.38 255.255.255.255 vpn_gateway
route 218.94.36.211 255.255.255.255 vpn_gateway
route 218.94.142.6 255.255.255.255 vpn_gateway
route 219.219.112.0 255.255.240.0 vpn_gateway
route 221.226.2.0 255.255.255.128 vpn_gateway
route 221.226.2.128 255.255.255.224 vpn_gateway
route 221.226.2.160 255.255.255.240 vpn_gateway
route 221.226.2.176 255.255.255.248 vpn_gateway
route 221.226.2.184 255.255.255.254 vpn_gateway
route 221.226.2.186 255.255.255.255 vpn_gateway
route 221.226.2.187 255.255.255.255 vpn_gateway
route 221.226.2.188 255.255.255.252 vpn_gateway
route 221.226.2.192 255.255.255.192 vpn_gateway
route 221.226.3.0 255.255.255.240 vpn_gateway
route 221.226.3.16 255.255.255.248 vpn_gateway
route 221.226.3.24 255.255.255.254 vpn_gateway
route 221.226.3.27 255.255.255.255 vpn_gateway
route 221.226.3.28 255.255.255.252 vpn_gateway
route 221.226.3.32 255.255.255.224 vpn_gateway
route 221.226.3.64 255.255.255.192 vpn_gateway
route 221.226.3.128 255.255.255.128 vpn_gateway