在openwrt路由中加入n2n,并在ubuntu里加入n2n服务,实现开机联网互通互访

在openwrt路由中加入n2n,并在ubuntu里加入n2n服务,实现开机联网互通互访

1、openwrt的硬件环境:

将自己的WR703N刷入openwrt(这里需要修改FLASH芯片,换个8M的FLASH);

刷机后,安装openwrt,此教程网上一大堆,无需说明了,TELNET进入,并开启SSH后,root登陆。

2、安装n2n到openwrt中:

[email protected]:~# opkg update <-----如果不能升级,请运行下条命令,查看源是否正确:

[email protected]:~# cat /etc/opkg.conf

[email protected]:~# opkg install n2n

运行上述命令即可安装n2n。

3、修改n2n的配置文件:

[email protected]:~# cat /etc/config/n2n
config edge
        option ipaddr           '10.10.100.191'
        option supernode        '106.186.30.16'
        option port             '9876'
        option community        'leekwen'
        option key              'leekwen'
        option route            '1'

4、修改n2n服务的启动脚本:

[email protected]:~# vi /etc/init.d/n2n
#!/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} -M 1300
                ;;
          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'
}

5、将n2n加入openwrt系统启动项:

[email protected]:~# /etc/init.d/n2n enable

6、启动openwrt中的n2n服务:

[email protected]:~# /etc/init.d/n2n start

[email protected]:~# ifconfig edge0
edge0     Link encap:Ethernet  HWaddr 86:CC:1B:E9:1A:DC
          inet addr:10.10.100.191  Bcast:10.10.100.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1300  Metric:1
          RX packets:1117 errors:0 dropped:22 overruns:0 frame:0
          TX packets:698 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:104415 (101.9 KiB)  TX bytes:98732 (96.4 KiB)

7、互通测试:与我的手机端 n2n互ping下

[email protected]:~# ping 10.10.100.103
PING 10.10.100.103 (10.10.100.103): 56 data bytes
64 bytes from 10.10.100.103: seq=0 ttl=64 time=31.738 ms
64 bytes from 10.10.100.103: seq=1 ttl=64 time=25.347 ms
64 bytes from 10.10.100.103: seq=2 ttl=64 time=22.565 ms
64 bytes from 10.10.100.103: seq=3 ttl=64 time=40.063 ms
64 bytes from 10.10.100.103: seq=4 ttl=64 time=23.577 ms
^C
--- 10.10.100.103 ping statistics ---
6 packets transmitted, 5 packets received, 16% packet loss
round-trip min/avg/max = 22.565/19.838/31.738 ms

在ubuntu系统中增加n2n服务:

1、我已经在上篇博客中有写如何在ubuntu安装n2n

(未安装过的用户请移驾http://blog.csdn.net/leekwen/article/details/45095887)

这里只说明增加n2n开机服务,具体步骤如下:

[email protected]:~$ svn co https://svn.ntop.org/svn/ntop/trunk/n2n
[email protected]:~$ cd n2n/
[email protected]:~/n2n$ ls
n2n_v1  n2n_v2
[email protected]:~/n2n$ cd n2n_v1/
[email protected]:~/n2n/n2n_v1$ ls
COPYING  edge.8  HACKING  lzoconf.h  Makefile   minilzo.h  n2n.h     openwrt  scripts      supernode.c       tuntap_linux.c  twofish.c  win32
debian   edge.c  INSTALL  lzodefs.h  minilzo.c  n2n.c      n2n.spec  README   supernode.1  tuntap_freebsd.c  tuntap_osx.c    twofish.h
[email protected]:~/n2n/n2n_v1$ make
[email protected]:~/n2n/n2n_v1$ ./edge -h
Welcome to n2n v.1.3.2 for i686
Built on 04/23/15 06:11:56 PM
Copyright 2007-08 - http://www.ntop.org

edge -d <tun device> -a <tun IP address> -c <community> -k <encrypt key> -s <netmask> [-u <uid> -g <gid>][-f][-m <MAC address>]
-l <supernode host:port> [-p <local port>] [-M <mtu>] [-t] [-r] [-v] [-b] [-h]

-d <tun device>          | tun device name
-a <tun IP address>      | n2n IP address
-c <community>           | n2n community name
-k <encrypt key>         | Encryption key (ASCII) - also N2N_KEY=<encrypt key>
-s <netmask>             | Edge interface netmask in dotted decimal notation (255.255.255.0)
-l <supernode host:port> | Supernode IP:port
-b                       | Periodically resolve supernode IP
                         | (when supernodes are running on dynamic IPs)
-p <local port>          | Local port used for connecting to supernode
-u <UID>                 | User ID (numeric) to use when privileges are dropped
-g <GID>                 | Group ID (numeric) to use when privileges are dropped
-f                       | Fork and run as a daemon. Use syslog.
-m <MAC address>         | Choose a MAC address for the TAP interface
                         | eg. -m 01:02:03:04:05:06
-M <mtu>                 | Specify n2n MTU (default 1400)
-t                       | Use http tunneling (experimental)
-r                       | Enable packet forwarding through n2n community
-v                       | Verbose

Environment variables:
  N2N_KEY                | Encryption key (ASCII)

[email protected]:~/n2n/n2n_v1$ pwd
/home/Leekwen/n2n/n2n_v1

2、下载并编译完毕后,编写启动服务文件n2n:

[email protected]:~/n2n/n2n_v1$ vi n2n

#!/bin/bash
##Some Variable define here ##
PROG="edge"
n2n_IP="10.10.100.103"
n2n_cname="leekwen"
n2n_pass="leekwen"
SNODE_Host="106.186.30.16"
SNODE_Port="9876"
LOGS="/var/log/$PROG.log"
PROG_PATH="/home/leekwen/n2n/n2n_v1"  # edge binary put on here for testing this script, Not Needed if the system binary.
PROG_ARGS="-f -d edge0 -a $n2n_IP -c $n2n_cname -k $n2n_pass -l $SNODE_Host:$SNODE_Port -M 1300"
PID_PATH="/var/run"

RETVAL=0

start() {
    pid=`ps ax | grep $PROG | grep -v "grep" | awk '{print $1}'`
    if [ ! -z "$pid" ]; then
        ## Program is running, exit with error.
        echo "Error! $PROG is currently running!" 1>&2
        exit 1
    else
        ## Change from /dev/null to something like /var/log/$PROG if you want to save output.
        $PROG_PATH/$PROG $PROG_ARGS 2>&1 > $LOGS &

        echo "$PROG started"
        echo $pid > "$PID_PATH/$PROG.pid"
    fi

   RETVAL=$?
   if [ $RETVAL -eq 0 ]; then
      echo -e "\\033[60G\c"
      echo -e "[ \\033[1;32m\c"
      echo -e "OK\c"
      echo -e "\\033[0;39m\c"
      echo " ]"
   else
      echo -e "\\033[60G\c"
      echo -e "[ \\033[1;31m\c"
      echo -e "FAILED\c"
      echo -e "\\033[0;39m\c"
      echo " ]"
   fi
   return $RETVAL
}

stop() {
    pid=`ps ax | grep $PROG | grep -v "grep"| awk '{print $1}'`
    if [ ! -z "$pid" ]; then
        ## Program is running, so stop it
	kill $pid

        rm -f  "$PID_PATH/$PROG.pid"
        echo "$PROG stopped"
    else
        ## Program is not running, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1
    fi

   RETVAL=$?
   if [ $RETVAL -eq 0 ]; then
      echo -e "\\033[60G\c"
      echo -e "[ \\033[1;32m\c"
      echo -e "OK\c"
      echo -e "\\033[0;39m\c"
      echo " ]"
   else
      echo -e "\\033[60G\c"
      echo -e "[ \\033[1;31m\c"
      echo -e "FAILED\c"
      echo -e "\\033[0;39m\c"
      echo " ]"
   fi
   return $RETVAL

}

## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root, you can use sudo command to run it." 1>&2
    exit 1
fi

case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
        exit 0
    ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
    ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac

3、增加可执行权限,移动n2n服务到/etc/init.d/,并更新rc.d

[email protected]:~/n2n/n2n_v1$ chmod 755 n2n
[email protected]:~/n2n/n2n_v1$ sudo mv n2n /etc/init.d/
[email protected]:~/n2n/n2n_v1$ sudo update-rc.d n2n defaults

4、对n2n服务进行测试:

[email protected]:~/n2n/n2n_v1$ /etc/init.d/n2n start
[email protected]:~/n2n/n2n_v1$ ifconfig edge0

[email protected]:~/n2n/n2n_v1$ /etc/init.d/n2n stop
[email protected]:~/n2n/n2n_v1$ ifconfig edge0

[email protected]:~/n2n/n2n_v1$ /etc/init.d/n2n restart
[email protected]:~/n2n/n2n_v1$ ifconfig edge0

5、对n2n服务进行Ping连通性测试:

[email protected]:~/n2n/n2n_v1$ ping 10.10.100.103
PING 10.10.100.103 (10.10.100.103): 56 data bytes
64 bytes from 10.10.100.103: seq=0 ttl=64 time=11.738 ms
64 bytes from 10.10.100.103: seq=1 ttl=64 time=15.347 ms
64 bytes from 10.10.100.103: seq=2 ttl=64 time=12.565 ms
64 bytes from 10.10.100.103: seq=3 ttl=64 time=10.063 ms
64 bytes from 10.10.100.103: seq=4 ttl=64 time=13.577 ms
^C
--- 10.10.100.103 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 10.063/12.838/15.347 ms

到此客户端的已经安装成功,在网络连接的情况下,每次重启ubuntu及openwrt路由后,他们都会自动连接上supernode节点,以构建一个p2p的网络。

时间: 2024-08-04 13:30:27

在openwrt路由中加入n2n,并在ubuntu里加入n2n服务,实现开机联网互通互访的相关文章

OpenWRT 路由配置 PPTP VPN + 断线自动重连 + chnroutes国内路由表

随着最近 Google 在国内已经完全无法访问,对我来说连接 VPN 已经成为了联网之后要做的第一件事. chnroutes 路由表 这个路由表集中了所有分配到中国大陆的 IP 段,根据 http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest 每天自动更新,可使得在访问国内地址时不经过 VPN. 想想如果能够让家里的路由直接连接 VPN,在家连接 WiFi 的所有设备直接达到Fan墙的效果,应该很 Cool,所以最近在某宝整了一

OpenWRT 路由系统上抓包

前言: 做路由器开发,难免会遇到抓包的情况,但是抓包需要有Hub或者无线抓包网卡,调试无线问题,且目前手中没有无线抓包网卡,怎么办?那不能分析无线连接建立的过程,我们就来分析下,其传输的数据,肯定是从ra0无线接口来进行了. 准备: 编译tcpdump.ipk, libpcap.ipk并且安装到OpenWRT系统中. 可选传输方式scp, wget等. wget http://<web server>/target_file opkg install <package_name>.

OpenWRT 路由配置技巧

随着最近 Google 在国内已经完全无法访问,使得通过 VPN 访问网络的需求更加强烈,本文介绍的方法可以使一个普通的路由具备稳定连接 VPN 的能力,并能够根据目标访问网站选择国内外线路,从而得到一个既没有限制,也不会影响速度的 Wifi 环境,连接到这个网络的设备不需要任何配置即可安全上网,完全感觉不到任何不便. chnroutes 路由表 这个路由表集中了所有分配到中国大陆的 IP 段,根据 http://ftp.apnic.net/apnic/stats/apnic/delegated

MVC路由中的一个问题

最近项目在使用MVC. 项目中有个地址为:www.xxx.com/search/search.html,从这个页面点击QQ登录时,地址为:www.xxx.com/passport/QQLogin.html?returl=/search/search.html 此时,QQ登录总是不成功. 但是同样的情况,如果把页面换成www.xxx.com/search/s.html,从这个页面点击QQ登录时,地址为:www.xxx.com/passport/QQLogin.html?returl=/search

静态路由中的递归路由原理与冗余备份链路的实现

如图1-4的拓扑中, R1去往R4的10.1.1.0/24网段,可以配置静态路由并以R2的fa0/1接口的IP地址为下一跳,但当R2的fa0/1端口为down时,需手动把静态路由切换到R3的fa0/1接口的IP地址,反之亦然. 图1-4 当链路数量很大时,在静态路由中关联直连网段中的下一跳地址,配置工作将非常巨大.此时可以在静态路由中关联非直连网段中的下一跳地址.例如当R1要访问R4上的直连网段10.1.1.0/24时,可以将R1的静态路由直接关联R4的fa0/0接口的IP地址 (ip rout

ionic的学习-02路由中的几个参数

Part1  路由中的几个参数 第一步:看几个参数的位置 ①ionic中是通过<ion-nav-view></ion-nav-view>实现不同模板渲染跳转的. ②ionic中的<ion-nav-view></ion-nav-view>中的name属性对应位置关系 ===================华丽丽的分割线=================== 第二步:路由跳转的三种方式 ①$state.go(url)方式,括号里的参数为需要跳转的url地址 at

.NetCore MVC中的路由(2)在路由中使用约束

.NetCore MVC中的路由(2)在路由中使用约束 0x00 路由模板中的约束 在配置路由模板时,除了以静态字符和变量的形式外,还可以通过配置对变量进行约束.实际如果不嫌麻烦的话在路由到的Action中对变量进行检查也是一种方法,不过对于变量的通用的约束当然是放在路由层面更加合适.而且这样做会简化Action的代码,使Action更加专注于自身业务,符合AOP的思路.这篇文章主要介绍路由模板中的约束. 0x01 在路由模板中使用约束 在路由模板中使用约束是很简单的,如下所示: {contro

node.js 模块和读取路由中获取信息

模块很类似于Java中的类,也是可以继承的,有自己的变量和方法 nj_dome.js var http = require('http'); var User = require("./model/User");//导入user的模块 var Teacher = require("./model/Teacher");//导入teacher的模块 http.createServer(function (request, response) { response.wri

asp.net core 系列 6 路由(中)

一.URL 生成 接着上篇讲MVC的路由,MVC 应用程序可以使用路由的 URL 生成功能,生成指向操作的 URL 链接. 生成 URL 可消除硬编码 URL,使代码更稳定.更易维护. 此部分重点介绍 MVC 提供的 URL 生成功能,并且仅涵盖 URL 生成工作原理的基础知识. IUrlHelper 接口用于生成 URL,是 MVC 与路由之间的基础结构的基础部分. 在控制器.视图和视图组件中,可通过 Url 属性找到 IUrlHelper 的实例. // // mvc 框架的Controll