1. 程式人生 > >n2n內網穿透神器(一條命令實現穿透)(linux,安卓,win,openwrt全介紹)

n2n內網穿透神器(一條命令實現穿透)(linux,安卓,win,openwrt全介紹)

n2n簡介:

百度百科N2N,內衣服飾品牌於上世紀九十年代在美國誕生,經歷逾二十幾年的不斷拓展,現發展成為龐大的跨國內衣服飾品牌,其連鎖專賣店網路遍佈全球100多個國家大、中城市,數目逾萬間,無論品牌定位、潮流款式都能滿足全球廣大消費者對優質內衣服飾之需求,是享譽國際的內衣服飾品牌之一。隨著中國經濟的快速發展和中國消費者對高階內衣服飾需求量的日益增加,並結合N2N全球發展戰略的規劃,N2N於2012年正式登陸大中華…

什麼鬼?

googlen2n is an open source Layer 2 over Layer 3VPN application which utilises a peer-to-peer

 architecture for network membership and routing.Unlike many other VPN programs, n2n can also connect computers which reside behind NAT routers. These connections are set up with help from a third computer that both computers can reach. This computer, called a supernode, can then route the information between NATed nodes.
[2]It is free software licensed under the terms of the GNU General Public License v3。

好吧,進入正題!

Linux:

下載下來我們會發現2個版本,這裡預設介紹V1版本,已經在linux,安卓,win,openwrt上互通,v2版本未測

Linux下面:git clone https://github.com/meyerd/n2n.git以後,然後進入V1版本編譯執行即可

n2n編譯完了的命令不是n2n,而是edgesupernode

edge是....怎麼形容,就說客戶端吧,而supernode則是伺服器超級節點,用法也是很簡單的,首先看看edge的help:

  1. edge -d <tun device> -a <tun IP address> -c <community> -k <encrypt key> -s <netmask> [-u <uid> -g <gid>][-f][-m <MAC address>]  
  2. -l <supernode host:port> [-p <local port>] [-M <mtu>] [-t] [-r] [-v] [-b] [-h]  
  3. -d <tun device>          | tun device name  
  4. -a <tun IP address>      | n2n IP address  
  5. -c <community>           | n2n community name  
  6. -k <encrypt key>         | Encryption key (ASCII) - also N2N_KEY=<encrypt key>
  7. -s <netmask>             | Edge interface netmask in dotted decimal notation (255.255.255.0)  
  8. -l <supernode host:port> | Supernode IP:port  
  9. -b                       | Periodically resolve supernode IP  
  10.                          | (when supernodes are running on dynamic IPs)  
  11. -p <local port>          | Local port used for connecting to supernode  
  12. -u <UID>                 | User ID (numeric) to use when privileges are dropped  
  13. -g <GID>                 | Group ID (numeric) to use when privileges are dropped  
  14. -f                       | Fork and run as a daemon. Use syslog.  
  15. -m <MAC address>         | Choose a MAC address for the TAP interface  
  16.                          | eg. -m 01:02:03:04:05:06  
  17. -M <mtu>                 | Specify n2n MTU (default 1400)  
  18. -t                       | Use http tunneling (experimental)  
  19. -r                       | Enable packet forwarding through n2n community  
  20. -v                       | Verbose  
具體上面已經介紹的很清楚了,現在實戰怎麼開啟,一句話:
  1. sudo edge -d edge0 -a xx.xx.xx.xx -c username -u 1000 -g 1000 -M 1200 -k password -l xx.xx.xx.xx:xxxx -m 41:03:05:00:10:01 > /dev/null &  
現在簡單介紹下上面這條命令的含義,-d edge0就是建立一個虛擬的網絡卡裝置,edge0就是它的名稱,-c -k就是使用者名稱和密碼,-a虛擬的IP地址,隨便取,只要你的所有裝置在一個網段即可,然後 -u -g這個是設定UID和GID 也可以不設定,-m 虛擬mac地址,設定了有好處,只要不相同即可,不設定回隨機分配,最後有一個重要的地方,-M 1200,這裡是設定MTU,至於MTU是什麼,可以自行百度哈,沒設定之前,ssh,http等協議會卡住死活連不上,設定後就正常了,需要比1400小,安卓上不用設定,這樣設定以後就可以ping通你的其他機器了,只要它可以訪問到超級節點 -l xx.xx.xx.xx:xxxx

現在介紹supernode,這個是建立超級節點,root許可權都不需要,直接supernode -l 埠號,就行了。網上有免費的超級節點,不過都很慢,很卡,很蛋疼,建議自己買個阿里的vps,又便宜,100M寬頻一個月32塊錢,那速度,只有一個字,飛快,視訊線上傳輸都沒問題,我上班都可以通過n2n檢視家裡的攝像頭...

好了,linux下面的就這樣,客戶端就是那一句話,很方便,開機自動連線,超級節點自己搭建一個,其他機器都可以用這個節點

Windows:

下面介紹win下面怎麼搭建n2n:win下面其實也很簡單,下個軟體設定下就OK了

 

單擊advanced按鍵,勾選Enable packet forwarding through n2n community,然後其他的自己看,和Linux下的設定一樣

 

上面的圖借鑑網上的,因為我沒在windows下,但是我用的和這個圖一樣,這裡選擇V1版本。

把桌面圖示拖到選單啟動就可以開機啟動了,不要擔心,不佔記憶體不佔cpu不佔網速.....

Android:

下面該介紹安卓了:安卓和win一樣,百度上面搜n2n安卓客戶端,然後下載安裝,注意,需要root許可權

下載地址:

  1. http://download.csdn.net/detail/leekwen/8604177  

設定好了點選start,出現RX什麼ACK就表示連通了,可以用超級終端ping通,或者ssh連線



其實安卓上有arm linux,不過不好打字,我現在也沒怎麼用,在arm linux裡面你可以下載更新和pc一樣,還可以vnc進桌面。

好了,安卓版就說到這裡了。

OpenWrt:

下面介紹OpenWrt的n2n配置,這些都是很有價值的,具體價值自己去想

首先安裝n2n,可以編譯的時候編譯進去。make menuconfig裡面找到network->vpn->n2n選中即可,或者編輯.config,搜n2n改成=y

或者在板子裡面opkg update && opkg install n2n即可

下面看看n2n的配置檔案:

  1. config edge                                                                                                                                
  2.     option community 'username'                                                                                                               
  3.     option key 'password'                                                                                                                     
  4.     option port 'port'                                                                                                                                                                                                                                       
  5.     option supernode 'xx.xx.xx.xx'                                                                                                     
  6.     option ipaddr 'xx.xx.xx.xx'                                                                                                            
  7.     option route '1'   

好,現在分析一下配置檔案,community就是使用者名稱,key就是密碼對應edge命令的-c -k,supernode是超級節點的ip地址,port是埠,對應edge的-l,然後ipaddr是虛擬ip地址,對應-a,route對應-r引數,就是設定是否通過n2n轉發,然後我們來看看openwrt下面n2n的服務程式:
  1. start_instance() {  
  2.         local cfg="$1"
  3.         config_get type "$cfg" TYPE  
  4.         case "$type" in  
  5.           edge)  
  6.                 config_get ipaddr "$cfg" 'ipaddr'  
  7.                 [ -n "$ipaddr" ] || return 1  
  8.                 config_get supernode "$cfg" 'supernode'  
  9.                 config_get port "$cfg" 'port'  
  10.                 config_get community "$cfg" 'community'  
  11.                 config_get key "$cfg" 'key'  
  12.                 config_get_bool route "$cfg" 'route' '0'  
  13.                 [ "$route" = "1" ] && args='-r'
  14.                 service_start /usr/sbin/edge -f $args -a $ipaddr -c $community -k $key -l ${supernode}:${port}  
  15.                 ;;  
  16.           supernode)  
  17.                 config_get port "$cfg" port  
  18.                 [ -n "$port" ] || return 1  
  19.                 service_start /usr/sbin/supernode -l $port  
  20.                 ;;  
  21.         esac  
  22. }  
上面是啟動的主要函式:首先是讀取config 這個type,判斷是edge還是supernode如果是supernode則只讀取port ,執行命令 supernode -l port,如果type是edge,則讀取....等配置檔案,然後
  1. edge -f $args -a $ipaddr -c $community -k $key -l ${supernode}:${port}  
上面的 -f是作為一個服務啟動,寫入syslog

好了這樣配置好了就可以訪問到路由器的,只要路由器連了外網,就可以隨時訪問進去

: 經過長期使用,發現一個不美好的問題,如果長時間執行的話,會導致不間斷不通不穩定的情況,所以建議每天自動重啟一下edge程式

n2n的介紹到此結束,很好很強大的一個功能,基本上人手必備神器,小巧,開源,迅速,免費,無限穿透!