n2n_v2: upgrade to v3 (#9675)

Co-authored-by: Tianling Shen <cnsztl@immortalwrt.org>
Signed-off-by: Tianling Shen <cnsztl@immortalwrt.org>

Co-authored-by: He Cheng <57560866+hecheng337@users.noreply.github.com>
Co-authored-by: Tianling Shen <cnsztl@immortalwrt.org>
This commit is contained in:
Beginner 2022-07-01 01:22:41 +08:00 committed by GitHub
parent 80125b864a
commit 886a9fd300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 230 additions and 173 deletions

82
package/lean/n2n/Makefile Normal file
View File

@ -0,0 +1,82 @@
# SPDX-License-Identifer: GPL-3.0-only
#
# Copyright (C) 2020 - ntop.org and contributors
# Copyright (C) 2021-2022 ImmortalWrt.org
include $(TOPDIR)/rules.mk
PKG_NAME:=n2n
PKG_VERSION:=3.0
PKG_RELEASE:=$(AUTORELEASE)
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/ntop/n2n/tar.gz/$(PKG_VERSION)?
PKG_HASH:=25fcabba7bfcf25f4c9cd7fecc7ce11de48beb0b0f3506053d8485604ea8f50d
PKG_LICENSE:=GPL-3.0
PKG_LICENSE_FILE:=LICENSE
PKG_MAINTAINER:=Emanuele Faranda <faranda@ntop.org>
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
define Package/n2n/template
SECTION:=net
CATEGORY:=Network
SUBMENU:=VPN
TITLE:=N2N Peer-to-peer VPN
URL:=http://www.ntop.org/n2n
DEPENDS:=+libcap +libopenssl +libpthread +libzstd
endef
define Package/n2n
$(call Package/n2n/template)
DEPENDS+=+kmod-tun +resolveip
endef
define Package/n2n/description
This package contains client node and supernode for the N2N infrastructure.
endef
define Package/n2n/conffiles
/etc/config/n2n
endef
define Package/n2n-utils
$(call Package/n2n/template)
DEPENDS+=+n2n +libpcap
endef
define Package/n2n-utils/description
This package contains extend utilities for the N2N infrastructure.
endef
CMAKE_OPTIONS+= \
-DCMAKE_BUILD_TYPE=Release \
-DN2N_OPTION_USE_PTHREAD=ON \
-DN2N_OPTION_USE_OPENSSL=ON \
-DN2N_OPTION_USE_PCAPLIB=ON \
-DN2N_OPTION_USE_ZSTD=ON
define Package/n2n/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/edge $(1)/usr/bin/n2n-edge
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/supernode $(1)/usr/bin/n2n-supernode
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_CONF) ./files/n2n.config $(1)/etc/config/n2n
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) ./files/n2n.init $(1)/etc/init.d/n2n
$(INSTALL_DIR) $(1)/etc/uci-defaults
$(INSTALL_BIN) ./files/n2n-migrate-conf.sh $(1)/etc/uci-defaults/50-n2n-migrate-conf
endef
define Package/n2n-utils/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/n2n-benchmark $(1)/usr/bin/
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/n2n-decode $(1)/usr/bin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/n2n-keygen $(1)/usr/bin/
endef
$(eval $(call BuildPackage,n2n))
$(eval $(call BuildPackage,n2n-utils))

View File

@ -0,0 +1,4 @@
#!/bin/sh
[ ! -e "/etc/config/n2n_v2" ] || mv "/etc/config/n2n_v2" "/etc/config/n2n"
exit 0

View File

@ -10,6 +10,7 @@ config edge
option community 'example' option community 'example'
option key 'password' option key 'password'
option route '1' option route '1'
option masquerade '0'
config supernode config supernode
option enabled '0' option enabled '0'

View File

@ -0,0 +1,143 @@
#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2020 OpenWrt.org
# Copyright (C) 2022 ImmortalWrt.org
START=99
USE_PROCD=1
start_instance() {
local cfg="$1"
local type
config_get type "$cfg" TYPE
case "$type" in
edge)
local enabled
config_get_bool enabled "$cfg" 'enabled' '0'
[ "$enabled" = "1" ] || return 1
local masquerade tunname mode ipaddr prefix mtu
local supernode port second_supernode second_port
local community key route
config_get_bool masquerade "$cfg" 'masquerade' '0'
config_get tunname "$cfg" 'tunname'
config_get mode "$cfg" 'mode'
config_get ipaddr "$cfg" 'ipaddr'
config_get prefix "$cfg" 'prefix'
config_get mtu "$cfg" 'mtu'
config_get supernode "$cfg" 'supernode'
config_get port "$cfg" 'port'
config_get second_supernode "$cfg" 'second_supernode'
config_get second_port "$cfg" 'second_port'
config_get community "$cfg" 'community'
config_get key "$cfg" 'key'
config_get_bool route "$cfg" 'route' '0'
local address
address="$ipaddr/$prefix"
[ "$mode" != 'dhcp' ] || address='0.0.0.0'
procd_open_instance "edge_$cfg"
procd_set_param command /usr/bin/n2n-edge -f
procd_append_param command -u 0 -g 0
procd_append_param command -d "$tunname"
procd_append_param command -a "$mode:$address"
procd_append_param command -c "$community"
procd_append_param command -l "$supernode:$port"
[ -z "$key" ] || procd_append_param command -k "$key"
[ -z "$mtu" ] || procd_append_param command -M "$mtu"
[ -z "$second_supernode" -o -z "$second_port" ] || procd_append_param command -l "$second_supernode:$second_port"
[ "$route" = "0" ] || procd_append_param command -r
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn
procd_close_instance
iptables -I FORWARD -i "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth'
iptables -I FORWARD -o "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth'
[ "$masquerade" = "0" ] || iptables -t nat -I POSTROUTING -o "$tunname" -j MASQUERADE -m comment --comment 'n2n edge net'
;;
supernode)
local enabled
config_get_bool enabled "$cfg" 'enabled' '0'
[ "$enabled" = "1" ] || return 1
local port subnet
config_get port "$cfg" 'port'
config_get subnet "$cfg" 'subnet'
procd_open_instance "supernode_$cfg"
procd_set_param command /usr/bin/n2n-supernode -f
procd_append_param command -p "$port"
procd_append_param command -a "$subnet"
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn
procd_close_instance
iptables -I INPUT -p udp --dport $port -j ACCEPT -m comment --comment 'n2n supernode port'
;;
route)
local enabled
config_get_bool enabled "$cfg" 'enabled' '0'
[ "$enabled" = "1" ] || return 1
local ip mask gw
config_get ip "$cfg" 'ip'
config_get mask "$cfg" 'mask'
config_get gw "$cfg" 'gw'
route add -net "$ip/$mask" gw "$gw"
;;
esac
}
stop_instance() {
local cfg="$1"
local type
config_get type "$cfg" TYPE
case "$type" in
edge)
local tunname masquerade
config_get tunname "$cfg" 'tunname'
config_get_bool masquerade "$cfg" 'masquerade' '0'
iptables -D FORWARD -i "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth' 2>/dev/null
iptables -D FORWARD -o "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth' 2>/dev/null
iptables -t nat -D POSTROUTING -o "$tunname" -j MASQUERADE -m comment --comment 'n2n edge net' 2>"/dev/null"
;;
supernode)
local port
config_get port "$cfg" 'port'
iptables -D INPUT -p udp --dport "$port" -j ACCEPT -m comment --comment 'n2n supernode port' 2>"/dev/null"
;;
esac
}
start_service() {
config_load 'n2n'
config_foreach start_instance 'edge'
config_foreach start_instance 'supernode'
sleep 2
config_foreach start_instance 'route'
}
stop_service() {
config_load 'n2n'
config_foreach stop_instance 'edge'
config_foreach stop_instance 'supernode'
}
reload_service() {
stop
start
}
service_triggers() {
procd_add_reload_trigger "n2n"
}

View File

@ -1,81 +0,0 @@
#
# Copyright (C) 2020 - ntop.org and contributors
#
include $(TOPDIR)/rules.mk
PKG_NAME:=n2n
PKG_VERSION:=3.0
PKG_RELEASE:=1
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/ntop/n2n/tar.gz/$(PKG_VERSION)?
PKG_HASH:=25fcabba7bfcf25f4c9cd7fecc7ce11de48beb0b0f3506053d8485604ea8f50d
PKG_LICENSE:=GPL-3.0
PKG_LICENSE_FILE:=LICENSE
PKG_MAINTAINER:=Emanuele Faranda <faranda@ntop.org>
PKG_BUILD_PARALLEL:=1
PKG_FIXUP:=autoreconf
include $(INCLUDE_DIR)/package.mk
define Package/n2n/Default
SECTION:=net
CATEGORY:=Network
TITLE:=N2N Peer-to-peer VPN
URL:=http://www.ntop.org/n2n
SUBMENU:=VPN
DEPENDS:=+libcap +libopenssl +libzstd +resolveip
endef
define Package/n2n-edge
$(call Package/n2n/Default)
TITLE+= client (edge node)
DEPENDS+=+kmod-tun
endef
define Package/n2n-edge/description
The client node for the N2N infrastructure
endef
define Package/n2n-edge/conffiles
/etc/config/n2n-edge.conf
endef
define Package/n2n-edge/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/edge $(1)/usr/bin/
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) ./files/n2n_v2.init $(1)/etc/init.d/n2n_v2
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_DATA) ./files/n2n_v2.config $(1)/etc/config/n2n_v2
endef
define Package/n2n-supernode
$(call Package/n2n/Default)
TITLE+= server (supernode)
endef
define Package/n2n-supernode/description
The supernode for the N2N infrastructure
endef
define Package/n2n-supernode/conffiles
/etc/config/n2n-supernode.conf
endef
define Package/n2n-supernode/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/supernode $(1)/usr/bin/
endef
define Build/Configure
( cd $(PKG_BUILD_DIR); \
./autogen.sh; \
./configure CFLAGS="-O3" )
endef
$(eval $(call BuildPackage,n2n-edge))
$(eval $(call BuildPackage,n2n-supernode))

View File

@ -1,92 +0,0 @@
#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2020 OpenWrt.org
START=99
start_instance() {
local cfg="$1"
config_get type "$cfg" TYPE
case "$type" in
edge)
config_get_bool enabled "$cfg" 'enabled' '0'
[ "$enabled" = "0" ] && return 1
config_get tunname "$cfg" 'tunname'
config_get mode "$cfg" 'mode'
config_get ipaddr "$cfg" 'ipaddr'
config_get prefix "$cfg" 'prefix'
config_get mtu "$cfg" 'mtu'
config_get supernode "$cfg" 'supernode'
config_get port "$cfg" 'port'
config_get second_supernode "$cfg" 'second_supernode'
config_get second_port "$cfg" 'second_port'
config_get community "$cfg" 'community'
config_get key "$cfg" 'key'
config_get_bool route "$cfg" 'route' '0'
address="$ipaddr/$prefix"
supernode_bak=""
[ "$second_supernode" -a "$second_port" ] && supernode_bak=" -l ${second_supernode}:${second_port}"
[ "$route" = "1" ] && args='-r'
[ "$mode" = 'dhcp' ] && address='0.0.0.0'
[ "-$mtu" != "-" ] && mtu="-M $mtu"
# eval "$(ipcalc.sh "$ipaddr/$prefix")"
# netmask="$NETMASK"
/usr/bin/edge -u 0 -g 0 -d $tunname -a ${mode}:${address} -c $community $([ -n "$key" ] && echo -k $key) -l ${supernode}:${port}$supernode_bak $args $mtu
iptables -I FORWARD -i "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth'
iptables -I FORWARD -o "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth'
iptables -t nat -I POSTROUTING -o "$tunname" -j MASQUERADE -m comment --comment 'n2n edge net'
;;
supernode)
config_get_bool enabled "$cfg" 'enabled' '0'
[ "$enabled" = "0" ] && return 1
config_get port "$cfg" 'port'
config_get subnet "$cfg" 'subnet'
/usr/bin/supernode -p $port -a $subnet &
iptables -I INPUT -p udp --dport $port -j ACCEPT -m comment --comment 'n2n supernode port'
;;
route)
config_get_bool enabled "$cfg" 'enabled' '0'
[ "$enabled" = "0" ] && return 1
config_get ip "$cfg" 'ip'
config_get mask "$cfg" 'mask'
config_get gw "$cfg" 'gw'
route add -net $ip/$mask gw $gw
;;
esac
}
stop_instance() {
local cfg="$1"
config_get type "$cfg" TYPE
case "$type" in
edge)
config_get tunname "$cfg" 'tunname'
iptables -D FORWARD -i "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth' 2>/dev/null
iptables -D FORWARD -o "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth' 2>/dev/null
iptables -t nat -D POSTROUTING -o "$tunname" -j MASQUERADE -m comment --comment 'n2n edge net' 2>/dev/null
killall -9 edge
;;
supernode)
config_get port "$cfg" 'port'
iptables -D INPUT -p udp --dport $port -j ACCEPT -m comment --comment 'n2n supernode port' 2>/dev/null
ps | grep supernode | grep -v grep 2>&1 >/dev/null && killall -9 supernode
;;
esac
}
start() {
config_load 'n2n_v2'
config_foreach start_instance 'edge'
config_foreach start_instance 'supernode'
sleep 2
config_foreach start_instance 'route'
}
stop() {
config_load 'n2n_v2'
config_foreach stop_instance 'edge'
config_foreach stop_instance 'supernode'
}