n2n部署

n2n

参考:
https://bugxia.com/2396.html
https://www.jianshu.com/p/559c1e582724
https://github.com/LomotHo/n2n-releases

服务器安装

  • centos
1
2
3
4
# RPM安装
yum install libzstd -y  
wget https://github.com/ntop/n2n/releases/download/3.0/n2n-3.0.0-1038.x86_64.rpm  
rpm -i n2n-3.0.0-1038.x86_64.rpm

启动服务器

1
2
3
启动服务端
#实际使用时,可以去掉 -f 参数,使其后台运行
supernode -p 端口 -f
  • 服务端(Supernode)附加参数
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    -p 端口 | Supernode监听端口,默认 7654
    -F federation名称 | supernode federation名称,默认为 *Federation
    -l 主机:端口 | 和 -F 配合,已知的一台Supernode地址和端口
    -M | 关闭非用户名密码认证的群组的MAC和IP地址欺骗保护功能
    -V 文本 | 自定义字符串(最长19位),用于在管理输出日志中展示
    -c 组名称配置文件路径 | 该配置文件中包含允许使用的组名称
    -a IP段 | 用于自动分配IP,格式如 -a 192.168.0.0-192.168.255.0/24
    -t 端口 | 用于管理supernode
    --management_password 文本 | 管理端的密码
    -v | 输出更多日志
    
  • 设置开机启动

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    # 修改配置文件
    sudo vim /etc/n2n/supernode.conf
    ====
    -p=端口
    # 服务端没有密码,为了避免被白嫖,添加组名称配置文件,一行一个
    -c=/home/n2n/community.list
    ====
    sudo systemctl enable supernode
    sudo systemctl start supernode
    sudo systemctl status supernode  
    journalctl -f -u supernode 
    # journalctl 查看日志
    # -f 实时最新
    # -u 指定服务
    

服务器端口放行

  • 腾讯云的防火墙
  • 若服务器开启了如iptables\firewalld\ufw等防火墙,需要放行supernode所设置的端口,否则客户端无法连接
1
2
3
4
5
6
7
8
9
#firewalld 
firewall-cmd --zone=public --add-port=9527/udp --permanent
firewall-cmd --reload
#iptables
iptables -I INPUT -p udp --dport 9527 -j ACCEPT
service iptables save
service iptables restart
#ufw
ufw allow 9527/udp

centos7 默认是firewall

1
2
3
4
5
6
7
8
# 查看状态
systemctl status firewalld
# 或者
firewall-cmd --state
# 查看所有开放端口
firewall-cmd --zone=public --list-ports
# 禁用端口
firewall-cmd --zone=public --remove-port=5672/tcp --permanent  #关闭5672端口

客户端组建N2N虚拟局域网

1
sudo edge -a 局域网ip -c n2n组名 -l [公网ip]:公网端口 -f

参数说明

 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
-a <mode:address>        | n2n网卡的ip地址,即本机的n2n的ip。直接填写IP或者使用 DHCP '-r -a dhcp:0.0.0.0'
-c <community>           | n2n组,只有相同组的edge之间才能进行通讯
-k <encrypt key>         | edge的加密密码
-s <netmask>             | 子网掩码 可选 (255.255.255.0).
-l <supernode host:port> | supernode的ip和端口
-i <reg_interval>        | 注册时间间隔,用于NAT打洞(默认为20秒)
-L <reg_ttl>             | 当通过超级节点UDP NAT打洞时,注册数据包的TTL(默认0不设置)
-p <local port>          | 固定本地UDP端口
-u <UID>                 | 如果不想用root来运行edge,则可指定本机账号的uid,windows下可忽略
-g <GID>                 | 如果不想用root来运行edge,则可指定本机账号的groupid,windows下可忽略
-f                       | 带-f时前台运行,不添加-f 参数时作为守护进程运行
-m <MAC address>         | n2n虚拟网卡的MAC地址,默认随机设置
                         | eg. -m 01:02:03:04:05:06
-M <mtu>                 | MTU 值,默认1290
-r                       | 启用数据转发功能,当需要访问该设备的局域网时需要设置
-A1                      | 禁用载荷加密。不能与key 同时使用(默认为Twofish)-A2 ... -A5 or -A        | 选择有效载荷加密的密码(和-k一起用),需要一个密钥:-A2 = Twofish(默认)                         | -A3 or -A (deprecated) = AES-CBC, -A5 = Speck-CTR.
-H                       | Enable full header encryption. Requires supernode with fixed community.
-z1 ... -z2 or -z        | Enable compression for outgoing data packets: -z1 or -z = lzo1x (default=disabled).
-E                       | 接受多播MAC地址(默认=drop)
-S                       | 不要连接P2P ,总是使用超级节点转发数据
-n <cidr:gateway>        | 通过gw路由IPv4网络。使用0.0.0.0/0作为默认gw。可多次设置。
-v                       | 详细日志
-t <port>                | 管理UDP端口(用于机器上的多个edges)
环境变量:
N2N_KEY                | 加密密钥(ASCII)

安装tuntap

1
2
brew tap happynclient/taps
brew install tuntap --cask

.查看系统信息,您会看到一个名为 tap0的虚拟网卡 sudo ifconfig

macos 设置开机启动项

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 在 /Library/LaunchDaemons 新增文件 lomotho.n2n-edge.plist,用于 launchd 控制的开机自启(类似 systemd)
# RunAtLoad 开机时是否运行
# KeepAlive 是否需要一直运行
====
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>Label</key>
    <string>lomotho.n2n.edge</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/bin/edge</string>
      <string>/usr/local/etc/n2n/edge.conf</string>
    </array>
  </dict>
</plist>
====
 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
# 客户端配置文件
# /usr/local/etc/n2n/edge.conf
#
#         The configuration file is similar to the command line, with one option per line. An equal
#        sign '=' should be used between key and value. Example: -c=mynetwork or --community=mynetwork
#        This file contains a basic configuration example, please refer to the help (-h) for the full
#        list of available options.
#
#       -d|--tun-device
#        Specifies the name of the TUN interface. 
#
#	-d=n2n0
#
#       -c|--community
#        Specifies the n2n community name the edge belongs to.
#
-c=xxx
#
#       -k
#        Sets the encryption key (ASCII). The environment variable N2N_KEY=<key> can also be used. 
#
#	-k=mypassword
#
#       -m
#        Specified the MAC address for the TAP interface (random otherwise).
#
# -m=DE:AD:BE:EF:99:99
#
#       -a
#        Sets the interface address. For DHCP use '-r -a dhcp:0.0.0.0'.
#
-a=x.x.x.x
#
#       -p
#        Sets the local UDP port to a fixed port.
#
# -p=50001
#
#       -l|--supernode-list
#        Specifies the supernode IP and port.
#
-l=x.x.x.x:x
# 访问局域网其他设置
-r
# -Z1为LZO(1x)压缩 -Z2为ZSTD压缩,默认没有
-Z2
#
1
2
3
4
5
# 开启服务
launchctl load /Library/LaunchDaemons/lomotho.n2n.edge.plist

# 关闭服务
launchctl unload /Library/LaunchDaemons/lomotho.n2n.edge.plist

linux客户端设置自启动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
sudo vim /etc/n2n/edge.conf
======
-c=组织名
-a=内网地址
-l=节点公网地址:端口
======
# 设置自启服务
sudo systemctl enable edge
sudo systemctl start edge
sudo systemctl status edge  
journalctl -f -u edge 
# journalctl 查看日志
# -f 实时最新
# -u 指定服务
Built with Hugo
Theme Stack designed by Jimmy