目录

异地多局域网互联(详细版)

背景

目前面临这么一个问题,有两个局域网需要互联。但是中间并没有直接的网线可以接入内部交换机直接链接。

以下将这两个地方简称为:机房,实验室。

具体网络信息如下:

  • 机房:10.12.0.0/16 (有公网ip。或者说,大内网ip。)
  • 实验室:10.10.0.0/16 (暂定无公网ip。)

目的是为了让实验室机器直接访问:10.12.0.0/16网段的机器。

方案1——异地组网

方案1.1——路由转发设置。

当机房和实验都有同一个网段的公网(大内网)IP的时候,可以针对两个WAN设置静态路由。

https://raw.githubusercontent.com/kengerlwl/kengerlwl.github.io/refs/heads/master/image/f9f1cb2f7df80be988d7865d15fc79d1/5e2e23ee52f02e744a6fdfa72747c522.png

效果:可以实现访问。

缺点:

  • 另一边不一定有公网ip。

静态路由:添加路由时,下一跳地址必须要在当前机器所拥有的某一个网络范围内,可以是当前机器所连接的某个网段,也可以是其他直接连通的网络。

方案1.2——VPN实现组网

当两个局域网并没有一根网线直接相连的时候,我们该如何实现两个内网无缝访问呢。

利用VPN实现近似的效果。使用zerotier加上中间moon节点

方案2——基于代理的伪组网

很简单的,服务器机房安装v2ray以及具备公网ip。

然后客户端路由器安装openclash做好代理转发。

实操

最终我选择了方案2,简单。

中间踩了很多坑。时间花的比较零散。所以断断续续搞了一周。

我的服务主要分为两端,需要两个路由器。架构图如下

https://raw.githubusercontent.com/kengerlwl/kengerlwl.github.io/refs/heads/master/image/f9f1cb2f7df80be988d7865d15fc79d1/d85bfa1f9d5d8bdd289c027e53896303.png

机房路由器

我用的esxi实现的软路由方案,没有搞多ip。

  • 用的校内静态公网ip。
  • 使用docker安装了v2ray
  • 做了一些基本的网络dns适配
  • 安装了openclash,但是选择不用
  • 网络默认route的wan是公网ip。lan口提供dhcp

用户端路由器

这里踩了很多踩,最终采用esir的最新高大全固件,物理裸机安装openwrt

  • 安装了openwrt(但是只代理指定的域名),其他全部走直连

  • 设置了多个wan口,通过校内dhcp获取ip登录上网。

  • 编写了针对多个wan口的登录脚本

    • 多个wan口是虚拟出来的

    • 注意,这里需要在开机以后,每个wan口都获取到了ip以后。通过多路由表,将每个wan口都添加出网络的路由规则(我这里一开没有做相关设置,导致出现了很多奇奇怪怪的问题,排查了很久的bug,例如使用某个wan口的ip能够ping通登录网关10.1.1.1.。但是不能curl通过http应用层协议访问通。)

    • 针对https://portal.csu.edu.cn/在hosts文件里面设置了ip映射10.1.1.1

    • 在完成路由表的操作后,可以运行登录脚本了。

    • 使用mwan做了负载均衡

    • 缺点,目前没有使用docker做封装。后面可以交给师弟完成。

  • 设置了openclash

    • 可以使用域名代理访问指定的域名流量
    • ip代理不可以。(经过排查,同样的配置如果是自己的个人PC上是可以使用的)。网上说openclash的bug。(后续可以考虑使用iptable命令手动实现)
      • 升级最新版固件后ok了
  • 针对多用户登录脚本,设置了定时任务,5min执行一次。/root/csu_net_keep

附录

开机自动虚拟化网口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 创建虚拟网口

base_interface="eth1"
i=1


while [ $i -lt 6 ]; do
  interface="vth$i"
  ip link add link $base_interface name $interface type macvlan
  ifconfig $interface up
  i=$((i + 1))
done

多wan口设置固定的mac地址

 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
26
27
28
29
30
31
32
33
34
config interface 'wan1'
        option proto 'dhcp'
        option macaddr 'd8:64:c7:57:02:1c'
        option _orig_ifname 'vth0'
        option _orig_bridge 'false'
        option ifname 'vth1'

config interface 'wan2'
        option proto 'dhcp'
        option macaddr 'd8:64:c7:57:02:2c'
        option _orig_ifname 'vth1'
        option _orig_bridge 'false'
        option ifname 'vth2'

config interface 'wan3'
        option proto 'dhcp'
        option macaddr 'd8:64:c7:57:02:3c'
        option _orig_ifname 'vth2'
        option _orig_bridge 'false'
        option ifname 'vth3'

config interface 'wan4'
        option proto 'dhcp'
        option macaddr 'd8:64:c7:57:02:4c'
        option _orig_ifname 'vth3'
        option _orig_bridge 'false'
        option ifname 'vth4'

config interface 'wan5'
        option proto 'dhcp'
        option macaddr 'd8:64:c7:57:02:5c'
        option _orig_ifname 'vth4'
        option _orig_bridge 'false'
        option ifname 'vth5'

入网申请mac地址

防火墙

1
2
3
4
5
6
7
8
9

config zone
        option name 'wan'
        option output 'ACCEPT'
        option masq '1'
        option mtu_fix '1'
        option input 'ACCEPT'
        option forward 'ACCEPT'
        option network 'wan wan6 wan2 wan1 wan3 wan4 wan5'

针对多wan口设置路由

多路由表设置/etc/iproute2/rt_tables

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#
# reserved values
#
128     prelocal
255     local
254     main
253     default
0       unspec

201     wan1
202     wan2
203     wan3
204     wan4
205     wan5

#
# local
#
#1      inr.ruhep

init_table_rule.sh使用从源IP发起请求来走路由表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# !/bin/bash
i=1


while [ $i -lt 6 ]; do


    wan_name="wan$i"
    eth_name="vth$i"

    gateway="183.169.79.254"


    wan_ip=$(ip -4 -br addr show dev $eth_name | awk '{split($3,a,"/"); print a[1]}')
    ip rule add from $wan_ip table $wan_name
    ip route add default via $gateway dev $eth_name table $wan_name

    echo $wan_ip
    echo #wan_name

  i=$((i + 1))
done
 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
26
# !/bin/bash
i=1


while [ $i -lt 6 ]; do

	
    wan_name="wan$i"
    eth_name="vth$i"

    gateway="183.169.79.254"
    
    # 删除该表
	ip route flush table $wan_name
	

    wan_ip=$(ip -4 -br addr show dev $eth_name | awk '{split($3,a,"/"); print a[1]}')
    ip rule add from all iif $eth_name lookup $wan_name
    ip route add default via $gateway dev $eth_name table $wan_name
    ip route add 10.11.0.0/16 dev eth0 proto kernel scope link src 10.11.20.20 table $wan_name
	ip route add 183.169.64.0/20 dev $eth_name proto kernel scope link src $wan_ip table $wan_name
    echo $wan_ip
    echo #wan_name

  i=$((i + 1))
done

curl 指定ip访问

1
2
3
4
5
6
target_url="baidu.com"
curl --interface vth1 -X GET $target_url
curl --interface vth2 -X GET $target_url
curl --interface vth3 -X GET $target_url
curl --interface vth4 -X GET $target_url
curl --interface vth5 -X GET $target_url

多wan自动登录脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/bash
cd /root/csu_net_keep

# 输出当前时间到日志
echo "" >> /root/csu_net_keep/run.log
echo "" >> /root/csu_net_keep/run.log
echo "---------------- Task ----------------" >> /root/csu_net_keep/run.log
echo "Current time: $(date)" >> /root/csu_net_keep/run.log

# 循环执行python main.py -i=0到4
for i in {0..4}; do
    # 输出任务分隔线
    echo "Executing python main.py -i=$i at $(date)" >> /root/csu_net_keep/run.log
    python main.py -i=$i >> /root/csu_net_keep/run.log 2>&1 &
done

# 等待所有后台任务完成
wait

其定时指定任务,5min执行一次

*/5 * * * * /bin/bash /root/csu_net_keep/net_rec.sh

关于多wan负载均衡

5个wan口负载均衡结果,学校是每个wan口限速20mbps

https://raw.githubusercontent.com/kengerlwl/kengerlwl.github.io/refs/heads/master/image/f9f1cb2f7df80be988d7865d15fc79d1/555fc97c1fb2be7de73621df105c19e0.png

mwan配置

接口

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
config interface 'wan1'
        option enabled '1'
        option initial_state 'online'
        option family 'ipv4'
        option track_method 'ping'
        option reliability '1'
        option count '1'
        option size '56'
        option check_quality '0'
        option timeout '2'
        option interval '5'
        option failure_interval '5'
        option recovery_interval '5'
        option down '3'
        option up '2'
        option flush_conntrack 'never'
        
config interface 'wan2'
        option enabled '1'
        option initial_state 'online'
        option family 'ipv4'
        option track_method 'ping'
        option reliability '1'
        option count '1'
        option size '56'
        option check_quality '0'
        option timeout '2'
        option interval '5'
        option failure_interval '5'
        option recovery_interval '5'
        option down '3'
        option up '2'
        option flush_conntrack 'never'
config interface 'wan3'
        option enabled '1'
        option initial_state 'online'
        option family 'ipv4'
        option track_method 'ping'
        option reliability '1'
        option count '1'
        option size '56'
        option check_quality '0'
        option timeout '2'
        option interval '5'
        option failure_interval '5'
        option recovery_interval '5'
        option down '3'
        option up '2'
        option flush_conntrack 'never'
config interface 'wan4'
        option enabled '1'
        option initial_state 'online'
        option family 'ipv4'
        option track_method 'ping'
        option reliability '1'
        option count '1'
        option size '56'
        option check_quality '0'
        option timeout '2'
        option interval '5'
        option failure_interval '5'
        option recovery_interval '5'
        option down '3'
        option up '2'
        option flush_conntrack 'never'        
config interface 'wan5'
        option enabled '1'
        option initial_state 'online'
        option family 'ipv4'
        option track_method 'ping'
        option reliability '1'
        option count '1'
        option size '56'
        option check_quality '0'
        option timeout '2'
        option interval '5'
        option failure_interval '5'
        option recovery_interval '5'
        option down '3'
        option up '2'
        option flush_conntrack 'never'    
        
        
        

成员

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
config member 'wan1_men'
        option interface 'wan1'

config member 'wan2_men'
        option interface 'wan2'
        
config member 'wan3_men'
        option interface 'wan3'
        
config member 'wan4_men'
        option interface 'wan4'
        
config member 'wan5_men'
        option interface 'wan5'        

修改dns

1
2
3

	echo "nameserver 119.29.29.29" > /tmp/resolv.conf
	echo "search lan." >> /tmp/resolv.conf