Total Pageviews

Saturday, 19 September 2015

tcptunnel(不实用!)

FROM http://www.vakuumverpackt.de/tcptunnel/

(项目地址 https://github.com/vakuum/tcptunnel)
Tcptunnel is a simple TCP port forwarder. This tool listens to a local TCP port and all the received data is sent to a remote host. It can be used to redirect TCP based protocols like HTTPIRCNNTP,SSH or SMTP.
The current version was successfully tested under GNU/LinuxMac OS X and Windows 7. Earlier versions were also reported to work withFreeBSDHP-UXOpenBSDSolarisWindows Server 2008 and Windows XP. If you have compiled and run it on another system or in another universe please let me know it.
The source code is released under the terms of the GNU General Public License.

help

$ tcptunnel --help
Usage: ./tcptunnel [options]

Options:
  --version
  --help

  --local-port=PORT    local port
  --remote-port=PORT   remote port
  --remote-host=HOST   remote host
  --bind-address=IP    bind address
  --buffer-size=BYTES  buffer size
  --fork               fork-based concurrency
  --log
  --stay-alive

download

Note: The MinGW32-based version of tcptunnel does not support the fork-based concurrent client handling. If you need this feature under Windows, then you should use the Cygwin-based version instead.

changelog

2013-10-27 tcptunnel-0.8
  • » Added Mac OS X support.
  • » Added buffer size option.
  • » Code cleanup.
2013-02-03 tcptunnel v0.7
  • » Added Cygwin-based fork support for Windows.
  • » Added SO_REUSEADDR socket option. Patch by Anders Norman.
  • » Fixed Make warnings. Patch by Sebastian Pipping.
  • » Code cleanup.
2012-11-24
  • » Updated Windows binary.
2012-10-20 tcptunnel v0.6
  • » Added fork option.
  • » Fixed code rot.
  • » Code cleanup.
2010-09-21
  • » Added fork-based concurrent client handling.
2010-09-16 tcptunnel v0.5
  • » Fixed version number.
2010-09-11 tcptunnel v0.4
  • » Increased receive buffer.
  • » Code cleanup.
2010-09-09 tcptunnel v0.3
  • » Added address binding patch from Newtral Human.
  • » Replaced strcpy with strncpy. Reported by Chris Benedict.
  • » Removed superfluous error messages. Reported by Chris Benedict.
  • » Updated Windows binary.
2010-08-15
  • » 10th Anniversary!
2009-02-21
  • » Updated Windows binary.
2004-12-28 tcptunnel v0.2
  • » Added support for Windows.
2000-09-14 tcptunnel v0.1g
  • » Added support for HP-UX.
  • » Added support for SunOS.
2000-09-11 tcptunnel v0.1f <smit@vossnet.de>
  • » Added getopt_long support for FreeBSD 2.2.2.
2000-08-15 tcptunnel v0.1
  • » First public release

--------------------------------------------------

将本地内网服务器映射到公网

tcptunnel

用于两种场景:

  1. 直接的端口转发,这个好理解
  2. 做内网服务器到公网的映射访问,用于解决内网服务器没有公网IP或者无法进行端口映射的场景

想要完成ngrok和lcx等类似的功能,对于lcx定义的slave啊,listen啊,tran啊我觉得很大歧义,半天理解不了。所以我发明了三个简单易懂的连接方式:

  • 公网服务器: publicserver,用于做转发的,监听一个对外开放的端口就行(对,我这里只要一个端口就行)
  • 内网服务器:natserver,也就是实际希望被外网访问的局域网服务器(或者是局域网代理)
  • 客户端:client,连接客户端,这个很好理解,实际的访问者,本地启动后,通过其他客户端连接本地监听的端口就相当于访问内网服务器

是不是更容易理解?是的话直接夸我。

注意:没有经过大量实际测试,请谨慎用于生产环境。

编译

由于用到了1.9才有的sync.Map, 所以编译环境必须是1.9+,见谅见谅 ;)

git clone https://github.com/LubyRuffy/tcptunnel
go get
go build

生成tcptunnel文件

跨平台编译,比如到NAS服务器或者树莓派等ARM平台,执行:

GOOS=linux GOARCH=arm GOARM=5 go build

运行

直接执行是读取config.toml配置文件中的内容,最主要的是Mode和对应的配置内容,后续在配置文件中说明。

./tcptunnel 

作为内网映射运行:

  • 作为publciserver执行,放到公网服务器
./tcptunnel -m publicserver
  • 作为natserver执行,放到内网的服务器
./tcptunnel -m natserver
  • 作为client执行,放到需要访问内网服务器的客户端
./tcptunnel -m client

然后连接本地端口就相当于连接natserver里面对应的服务器了

natserver 和 client 通信是通过约定好一致的唯一ID来进行。

作为tcpproxy执行,也就是端口转发

./tcptunnel -m tcpproxy

配置文件说明

默认读取config.toml文件,

# 模式: 支持publicserver,natserver,client,tcpproxy。可以通过命令行的-m参数覆盖
Mode = "publicserver"

# 连接公网服务器的地址,格式为 host:port
# 在Mode为 natserver 和 clientconnect 时有效
PublicServerAddr = "127.0.0.1:10011"

# 端口转发模式,仅仅在Mode为 tcpproxy 时有效
[TcpProxies]
    # 数组,可以多个映射关系
    [TcpProxies.proxy80]
    LocalBindAddr = "127.0.0.1:1234"
    RemoteServerAddr = "192.168.1.1:80"
    Type = "http"

    [TcpProxies.proxy22]
    LocalBindAddr = "127.0.0.1:1235"
    RemoteServerAddr = "192.168.1.1:22"

# 公网服务器监听的地址,仅仅在Mode为 publicserver 时有效,格式为 ip:port
[PublicServer]
LocalBindAddr = "127.0.0.1:10011"

# 端口转发模式,仅仅在Mode为 natserver 时有效
[NatServer]
    # 数组,可以多个映射关系,ID用于注册,客户端连接的时候直接通过ID来进行查找
    [NatServer.test]
    RemoteServerAddr = "192.168.1.1:80"
    ID = "test"
    Type = "http"

    [NatServer.test1]
    RemoteServerAddr = "192.168.1.1:22"
    ID = "test1"
    
# 端口转发模式,仅仅在Mode为 client 时有效
[ClientConnect]
    # 数组,可以多个映射关系,ID用于标示连接时指定NAT后的服务器对象
    [ClientConnect.test]
    LocalBindAddr = "127.0.0.1:1234"
    ID = "test"

流程说明

natserver    REGISTER   -> publicserver
                           publicserver <- CONNECT client <- application tcp connect
natserver NEWDATASTREAM <- publicserver
natserver   DATASTREAM  -> publicserver
 publicserver -> 200 OK client <-> application tcp connect
from https://github.com/LubyRuffy/tcptunnel