From d05e426ccc46f5dedaec856c97f5f35058e0ec2d Mon Sep 17 00:00:00 2001 From: lwch Date: Thu, 12 Aug 2021 17:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0README=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=92=8C=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 35 ++++++++++++++- conf/client.yaml | 2 +- init.d/np-cli | 115 +++++++++++++++++++++++++++++++++++++++++++++++ init.d/np-svr | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 265 insertions(+), 2 deletions(-) create mode 100644 init.d/np-cli create mode 100644 init.d/np-svr diff --git a/README.md b/README.md index 7b50b47..16efc52 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,36 @@ # natpass -内网穿透工具 \ No newline at end of file +内网穿透工具 + +## 实现原理 + +基于tls链接,server端转发流量 + +## 编译 + + CGO_ENABLED=0 go build -o bin/np-svr code/server/*.go + CGO_ENABLED=0 go build -o bin/np-cli code/client/*.go + +## 配置 + +server端配置如下: + + listen: 6154 # 监听端口号 + secret: 0123456789 # 预共享密钥 + tls: + key: /dir/to/tls/key/file # tls密钥 + crt: /dir/to/tls/crt/file # tls证书 + +client端配置如下: + + id: this # 客户端ID + server: 127.0.0.1:6154 # 服务器地址 + secret: 0123456789 # 预共享密钥 + tunnel: # 远端tunnel列表可为空 + - name: rdp # 链路名称 + target: that # 目标客户端ID + type: tcp # 连接类型tcp或udp + local_addr: 0.0.0.0 # 本地监听地址 + local_port: 3389 # 本地监听端口号 + remote_addr: 127.0.0.1 # 目标客户端连接地址 + remote_port: 3389 # 目标客户端连接端口号 \ No newline at end of file diff --git a/conf/client.yaml b/conf/client.yaml index 2eabcca..1d597de 100644 --- a/conf/client.yaml +++ b/conf/client.yaml @@ -1,7 +1,7 @@ id: this # 客户端ID server: 127.0.0.1:6154 # 服务器地址 secret: 0123456789 # 预共享密钥 -tunnel: +tunnel: # 远端tunnel列表可为空 - name: rdp # 链路名称 target: that # 目标客户端ID type: tcp # 连接类型tcp或udp diff --git a/init.d/np-cli b/init.d/np-cli new file mode 100644 index 0000000..bd8ce7c --- /dev/null +++ b/init.d/np-cli @@ -0,0 +1,115 @@ +#!/bin/sh +# +# chkconfig: 2345 12 88 +# description: natpass client +### BEGIN INIT INFO +# Provides: lwch748@gmail.com +# Required-Start: $network +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: np-cli +# Description: natpass client +### END INIT INFO + +NAME=np-cli +WORKDIR=/opt/natpass +DAEMON=/opt/natpass/bin/$NAME +CONFIG=/opt/natpass/conf/client.yaml +PIDFILE=/var/run/$NAME.pid + +wait_for_pid () { + try=0 + while test $try -lt 35 ; do + case "$1" in + 'created') + if [ -f "$2" ] ; then + try='' + break + fi + ;; + 'removed') + if [ ! -f "$2" ] ; then + try='' + break + fi + ;; + esac + + echo -n . + try=`expr $try + 1` + sleep 1 + done +} + +case "$1" in + start) + echo -n "Starting $NAME " + cd $WORKDIR + nohup $DAEMON -d -pid $PIDFILE -u nobody -conf $CONFIG > /dev/null 2>&1 & + if [ "$?" != 0 ] ; then + echo " failed" + exit 1 + fi + wait_for_pid created $PIDFILE + if [ -n "$try" ] ; then + echo " failed" + exit 1 + else + echo " done" + fi + ;; + stop) + echo -n "Gracefully shutting down $NAME " + if [ ! -r $PIDFILE ] ; then + echo "warning, no pid file found - $NAME is not running ?" + exit 1 + fi + kill -QUIT `cat $PIDFILE` + wait_for_pid removed $PIDFILE + if [ -n "$try" ] ; then + echo " failed. Use force-quit" + exit 1 + else + echo " done" + fi + ;; + status) + if [ ! -r $PIDFILE ] ; then + echo "$NAME is stopped" + exit 0 + fi + + PID=`cat $PIDFILE` + if ps -p $PID | grep -q $PID; then + echo "$NAME (pid $PID) is running..." + else + echo "$NAME dead but pid file exists" + fi + ;; + force-quit) + echo -n "Terminating $NAME " + if [ ! -r $PIDFILE ] ; then + echo "warning, no pid file found - $NAME is not running ?" + exit 1 + fi + kill -TERM `cat $PIDFILE` + wait_for_pid removed $PIDFILE + if [ -n "$try" ] ; then + echo " failed" + exit 1 + else + echo " done" + fi + ;; + restart) + $0 stop + $0 start + ;; + *) + echo "Usage: $0 {start|stop|status|force-quit|restart}" + exit 1 + ;; +esac + +exit $? \ No newline at end of file diff --git a/init.d/np-svr b/init.d/np-svr new file mode 100644 index 0000000..18fa078 --- /dev/null +++ b/init.d/np-svr @@ -0,0 +1,115 @@ +#!/bin/sh +# +# chkconfig: 2345 12 88 +# description: natpass server +### BEGIN INIT INFO +# Provides: lwch748@gmail.com +# Required-Start: $network +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: np-svr +# Description: natpass server +### END INIT INFO + +NAME=np-svr +WORKDIR=/opt/natpass +DAEMON=/opt/natpass/bin/$NAME +CONFIG=/opt/natpass/conf/server.yaml +PIDFILE=/var/run/$NAME.pid + +wait_for_pid () { + try=0 + while test $try -lt 35 ; do + case "$1" in + 'created') + if [ -f "$2" ] ; then + try='' + break + fi + ;; + 'removed') + if [ ! -f "$2" ] ; then + try='' + break + fi + ;; + esac + + echo -n . + try=`expr $try + 1` + sleep 1 + done +} + +case "$1" in + start) + echo -n "Starting $NAME " + cd $WORKDIR + nohup $DAEMON -d -pid $PIDFILE -u nobody -conf $CONFIG > /dev/null 2>&1 & + if [ "$?" != 0 ] ; then + echo " failed" + exit 1 + fi + wait_for_pid created $PIDFILE + if [ -n "$try" ] ; then + echo " failed" + exit 1 + else + echo " done" + fi + ;; + stop) + echo -n "Gracefully shutting down $NAME " + if [ ! -r $PIDFILE ] ; then + echo "warning, no pid file found - $NAME is not running ?" + exit 1 + fi + kill -QUIT `cat $PIDFILE` + wait_for_pid removed $PIDFILE + if [ -n "$try" ] ; then + echo " failed. Use force-quit" + exit 1 + else + echo " done" + fi + ;; + status) + if [ ! -r $PIDFILE ] ; then + echo "$NAME is stopped" + exit 0 + fi + + PID=`cat $PIDFILE` + if ps -p $PID | grep -q $PID; then + echo "$NAME (pid $PID) is running..." + else + echo "$NAME dead but pid file exists" + fi + ;; + force-quit) + echo -n "Terminating $NAME " + if [ ! -r $PIDFILE ] ; then + echo "warning, no pid file found - $NAME is not running ?" + exit 1 + fi + kill -TERM `cat $PIDFILE` + wait_for_pid removed $PIDFILE + if [ -n "$try" ] ; then + echo " failed" + exit 1 + else + echo " done" + fi + ;; + restart) + $0 stop + $0 start + ;; + *) + echo "Usage: $0 {start|stop|status|force-quit|restart}" + exit 1 + ;; +esac + +exit $? \ No newline at end of file