N2N是一个开放源代码的2层跨越3层的×××程序,该程序利用了点对点的架构来处理网络间的成员关系和路由,N2N的原理如下图,在搭建的过程中需要一个super节点和多个edge节点,super节点建立一个通信中心,用来路由edge之间的通讯,对于×××使用来说,super node节点必须有一个公网的IP地址
本文章以阿里云服务器和局域网服务器为例子,其中
阿里云服务器地址:X.55.150.X,操作系统centos6.8 此处称为A
局域网服务器IP地址:10.221.249.106,操作系统centos6.8,此处成为B
1、安装
不管你是什么系统,下面的代码都是一样的,我们使用v2
协议。由于git clone https://github.com/meyerd/n2n.git 源在国内同步不成功所以使用ntop的源。以下配置需要在supernode所在服务器A,edge服务器B都需要执行
yum install subversion gcc-c++ openssl-devel svn co https://svn.ntop.org/svn/ntop/trunk/n2n cd n2n/n2n_v2 make make install
2、配置super node
在后台启动super node服务,TCP端口为1000,详细的用法可以用supernode -h查看
supernode -l 1000 -v >/dev/null &
3、配置edge
节点1:
可以和super在一个服务器上面,此处,supernode和节点1在一个服务器上面,下面的命令是在后台启动一个edge节点,配置ip地址为10.0.0.1,supernode节点IP地址为X.55.150.X(公网IP地址),supernode端口为1000
edge -d n2n0 -c mynetwork -k encryptme -a 10.0.0.1 -l X.55.150.X:1000 >/dev/null &
节点2
edge -d n2n0 -c mynetwork -k encryptme -a 10.0.0.2 -l X.55.150.X:1000 >/dev/null &
执行edge之后,可以通过ifconfig查看网卡信息,发现多了一块网卡,如下图:
4、测试完了连通性
一键搭建N2N脚本如下
#!/bin/bash #####此脚本用来实现安装N2N的客户端,实现内网之间的穿透 ####应用场景: ####客服的服务器有A、B、C三台,其中有一台可以上外网,此处以A为例子,ABC之间的SSH互通 ####N2N的server,即super node为114.114.114.114,端口1000,在阿里云端,可以实现外网访问 ####此脚本用来在客户的内网搭建N2N的client,可以实现和阿里云supernode的通信,这样通过阿里云端就可以SSH到客户服务器内网 N2N_super_node_ip=114.114.114.114 N2N_super_node_port=1000 ###N2N_edge_ip为搭建的edge的IP,需要设置,网段为10.10.10.* ####但有一个前提,设置的这个IP地址在虚拟局域网中不能冲突,所以需要先判断IP地址是否冲突 N2N_edge_ip=10.10.10.2 judge_ip_confilct() { if `ping -c 2 ${N2N_edge_ip} &>/dev/null`;then echo -e "\033[32m ${N2N_edge_ip} can ping,has client used,please motified N2N_edge_ip,系统退出\033[0m" exit 0 else echo -e "\033[31m ${N2N_edge_ip} not can ping,N2N_edge_ip can be userd\033[0m" fi } check_super_node_service() { ping -c 6 ${N2N_super_node_ip} if `ping -c 2 ${N2N_super_node_ip} &>/dev/null`;then echo -e "\033[32m super node :${N2N_edge_ip} can ping, N2N server can be used\033[0m" else echo -e "\033[31m super node :${N2N_edge_ip} can not ping ,n2n server can not be used ,please check system quit\033[0m" exit fi } n2n_install_super_node() { if `yum install bc &>/dev/null`;then echo -e "\033[32m yum can be use,starting yum install n2n relative paket:\033[0m" yum -y install subversion gcc-c++ openssl-devel echo "svn co install n2n:" svn co https://svn.ntop.org/svn/ntop/trunk/n2n if [ -e n2n ];then echo "n2n file download successful,beging install n2n" cd n2n/n2n_v2 make make install else echo "n2n file download failed ,has some problems ,please check" exit 0 fi echo "n2n install over,beginging start n2n services" supernode -l 1000 -v >/dev/null & echo "查看 ps -ef | grep supernode" ps -ef | grep supernode echo "supernode -l 1000 -v >/dev/null &" >> /etc/rc.local else echo -e "\033[31m yum not can be use,yum install n2 has some problem,please check\033[0m" exit fi } n2n_install_edge_node() { if `yum install bc &>/dev/null`;then echo -e "\033[32m yum can be use,starting yum install n2n relative paket:\033[0m" yum -y install subversion gcc-c++ openssl-devel echo "svn co install n2n:" svn co https://svn.ntop.org/svn/ntop/trunk/n2n if [ -e n2n ];then echo "n2n file download successful,beging install n2n" cd n2n/n2n_v2 make make install else echo "n2n file download failed ,has some problems ,please check" exit 0 fi echo "check super node server is ok or not" check_super_node_service echo "n2n install over,begining start edge node" edge -d n2n0 -c mynetwork -k encryptme -a $N2N_edge_ip -l $N2N_super_node_ip:$N2N_super_node_port >/dev/null & echo "查看 ps -ef | grep edge,进程是否启动OK" ps -ef | grep edge echo "edge -d n2n0 -c mynetwork -k encryptme -a $N2N_edge_ip -l $N2N_super_node_ip:$N2N_super_node_port" >> /etc/rc.local else echo -e "\033[31m yum not can be use,yum install n2 has some problem,please check\033[0m" exit fi } real=`grep -l '\^H' /root/.bash_profile` if [ $? -eq 1 ];then echo 'stty erase ^H' >> /root/.bash_profile source /root/.bash_profile #这几行主要就是让在使用read键时能使用回删键。写错了,回删了,重启写。不用这段的话,回删键会变成乱码。 fi echo -e '\033[0;33;1m #################nagios################## \033[0m' #让echo能弄点颜色出来好看点。。。 echo "n2n supernode install please input : 1" echo "n2n edgenode install please input : 2" echo -e '\033[0;33;1m ######################################### \033[0m' read -p "please chose : " frist #定义输入的值 if [ $frist -eq 1 ];then n2n_install_super_node else n2n_install_edge_node fi