n2n是一个二层的虚拟专网,允许用户开发网络中典型的P2P应用而不是在应用层开发。这就意味着用户可以透明的得到本地地址,只要新的IP地址在一个子网内,无论两台机器处于什么位置都能够ping通对方。
N2N网络的主要特点:
- n2n网络是基于P2P协议的二层加密网络
- 加密是在节点上完成的,而且使用的是用户的自定义密钥;你控制自己的安全,而不用让比如skype代表你(管理你的服务器节点)
- 每个n2n用户可以同时属于多个网络
- 能够反向的穿越NAT和防火墙,因此n2n节点即使在内网也是能够被访问到的,因此防火墙不再是IP级别直接通讯的障碍。
- n2n网络并没有设计成独立的(自包含的),但是可能能够在n2n和非n2n网络之间路由数据。
n2n构架是基于两个部分的:
终端节点:安装在用户计算机上的应用使n2n网络被创建。每个节点设备都会创建一个TUN/TAP虚拟网卡设备作为接入n2n网络的入口点。
超级节点:作用是使终端节点能够访问到对称防火墙后面的其他终端节点。这个程序是那些无法直接通讯节点的目录记录器和包路由器。
从图上可以看出,SuperNode作用是进行转发等通讯,因为其在公网,可以将两个内网的Edge节点连接起来。
- #!/bin/sh /etc/rc.common
- # Copyright (C) 2008-2012 OpenWrt.org
- START=90
- start_instance() {
- local cfg="$1"
- config_get type "$cfg" TYPE
- case "$type" in
- edge)
- config_get ipaddr "$cfg" 'ipaddr'
- [ -n "$ipaddr" ] || return 1
- config_get supernode "$cfg" 'supernode'
- config_get port "$cfg" 'port'
- config_get community "$cfg" 'community'
- config_get key "$cfg" 'key'
- config_get_bool route "$cfg" 'route' '0'
- [ "$route" = "1" ] && args='-r'
- service_start /usr/sbin/edge -f $args -a $ipaddr -c $community -k $key -l ${supernode}:${port}
- ;;
- supernode)
- config_get port "$cfg" port
- [ -n "$port" ] || return 1
- service_start /usr/sbin/supernode -l $port
- ;;
- esac
- }
- stop_instance() {
- local cfg="$1"
- config_get type "$cfg" TYPE
- case "$type" in
- edge)
- service_stop /usr/sbin/edge
- ;;
- supernode)
- service_stop /usr/sbin/supernode
- ;;
- esac
- }
- start() {
- config_load 'n2n'
- config_foreach start_instance 'edge'
- config_foreach start_instance 'supernode'
- }
- stop() {
- config_load 'n2n'
- config_foreach stop_instance 'edge'
- config_foreach stop_instance 'supernode'
- }
从对应的启动文件可以看出,使用非常的简单。
- config edge
- option ipaddr ''
- option supernode ''
- option port ''
- option community ''
- option key ''
- option route ''
ipaddr 本地虚拟网卡的地址
upsernode 超级节点的地址,可以是域名
port 想要使用连接的端口
community 对应的网络名称,同一个名称下才能互访
key 用户定义的需要访问这边SSH使用的key
下面的内容是我的两台主机和服务器之间的通讯输出Debug信息
- 10/Sep/2013 23:51:47 [ n2n.c: 49] Marshalling hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:7654
- 10/Sep/2013 23:51:47 [ n2n.c: 638] 84 bytes compressed into 87
- 10/Sep/2013 23:51:47 [ n2n.c: 681] ### Tx N2N Msg -> network
- 10/Sep/2013 23:52:17 [ n2n.c: 843] Purging old registrations
- 10/Sep/2013 23:52:17 [ n2n.c: 848] Remove 0 registrations
- 10/Sep/2013 23:52:22 [ n2n.c: 49] Unmarshalled hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:50000
- 10/Sep/2013 23:52:22 [ n2n.c: 540] +++ Received unreliable data packet [rcvd_from=60.215.250.204:50000][msg_type=MSG_TYPE_REGISTER][seq_id=0]
- 10/Sep/2013 23:52:22 [ n2n.c: 545] [src_mac=DE:AD:BE:EF:01:23][dst_mac=00:00:00:00:00:00][original_sender=0.0.0.0:0]
- 10/Sep/2013 23:52:22 [supernode.c: 312] Received message from node [60.215.250.204:-15536]
- 10/Sep/2013 23:52:22 [ n2n.c: 49] Unmarshalled hdr: public_ip=(2)60.215.250.204:50000, private_ip=(2)0.0.0.0:50000
- 10/Sep/2013 23:52:22 [ n2n.c: 49] Marshalling hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:7654
- 10/Sep/2013 23:52:22 [ n2n.c: 49] Unmarshalled hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:7654
- 10/Sep/2013 23:52:22 [ n2n.c: 736] Sent unreliable packet [msg_type=MSG_TYPE_REGISTER_ACK][seq_id=0][src_mac=00:00:00:00:00:00][dst_mac=DE:AD:BE:EF:01:23]
- 10/Sep/2013 23:52:22 [ n2n.c: 49] Marshalling hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:7654
- 10/Sep/2013 23:52:22 [ n2n.c: 638] 84 bytes compressed into 87
- 10/Sep/2013 23:52:22 [ n2n.c: 681] ### Tx N2N Msg -> network
- 10/Sep/2013 23:52:47 [ n2n.c: 49] Unmarshalled hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:50001
- 10/Sep/2013 23:52:47 [ n2n.c: 540] +++ Received unreliable data packet [rcvd_from=60.215.250.204:50001][msg_type=MSG_TYPE_REGISTER][seq_id=0]
- 10/Sep/2013 23:52:47 [ n2n.c: 545] [src_mac=DE:AD:BE:EF:01:24][dst_mac=00:00:00:00:00:00][original_sender=0.0.0.0:0]
- 10/Sep/2013 23:52:47 [supernode.c: 312] Received message from node [60.215.250.204:-15535]
- 10/Sep/2013 23:52:47 [ n2n.c: 49] Unmarshalled hdr: public_ip=(2)60.215.250.204:50001, private_ip=(2)0.0.0.0:50001
- 10/Sep/2013 23:52:47 [ n2n.c: 49] Marshalling hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:7654
- 10/Sep/2013 23:52:47 [ n2n.c: 49] Unmarshalled hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:7654
- 10/Sep/2013 23:52:47 [ n2n.c: 736] Sent unreliable packet [msg_type=MSG_TYPE_REGISTER_ACK][seq_id=0][src_mac=00:00:00:00:00:00][dst_mac=DE:AD:BE:EF:01:24]
- 10/Sep/2013 23:52:47 [ n2n.c: 49] Marshalling hdr: public_ip=(2)0.0.0.0:0, private_ip=(2)0.0.0.0:7654
- 10/Sep/2013 23:52:47 [ n2n.c: 638] 84 bytes compressed into 87
- 10/Sep/2013 23:52:47 [ n2n.c: 681] ### Tx N2N Msg -> network