2018-12-16 08:56:40 +03:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
2019-10-25 15:06:13 +03:00
|
|
|
|
"github.com/esrrhs/go-engine/src/loggo"
|
2018-12-18 10:47:45 +03:00
|
|
|
|
"github.com/esrrhs/pingtunnel"
|
2019-10-26 15:45:58 +03:00
|
|
|
|
"strconv"
|
2018-12-16 08:56:40 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var usage = `
|
2019-10-28 11:36:32 +03:00
|
|
|
|
通过伪造ping,把tcp/udp/sock5流量通过远程服务器转发到目的服务器上。用于突破某些运营商封锁TCP/UDP流量。
|
|
|
|
|
By forging ping, the tcp/udp/sock5 traffic is forwarded to the destination server through the remote server. Used to break certain operators to block TCP/UDP traffic.
|
2018-12-18 10:36:59 +03:00
|
|
|
|
|
2018-12-16 08:56:40 +03:00
|
|
|
|
Usage:
|
|
|
|
|
|
2019-10-28 11:33:44 +03:00
|
|
|
|
// server
|
2018-12-18 06:39:16 +03:00
|
|
|
|
pingtunnel -type server
|
2018-12-16 08:56:40 +03:00
|
|
|
|
|
2019-10-28 11:33:44 +03:00
|
|
|
|
// client, Forward udp
|
|
|
|
|
pingtunnel -type client -l LOCAL_IP:4455 -s SERVER_IP -t SERVER_IP:4455
|
|
|
|
|
|
|
|
|
|
// client, Forward tcp
|
2019-10-27 15:04:51 +03:00
|
|
|
|
pingtunnel -type client -l LOCAL_IP:4455 -s SERVER_IP -t SERVER_IP:4455 -tcp 1
|
2018-12-18 10:36:59 +03:00
|
|
|
|
|
2019-10-28 11:33:44 +03:00
|
|
|
|
// client, Forward sock5, implicitly open tcp, so no target server is needed
|
|
|
|
|
pingtunnel -type client -l LOCAL_IP:4455 -s SERVER_IP -sock5 1
|
|
|
|
|
|
2018-12-18 10:36:59 +03:00
|
|
|
|
-type 服务器或者客户端
|
|
|
|
|
client or server
|
|
|
|
|
|
|
|
|
|
-l 本地的地址,发到这个端口的流量将转发到服务器
|
|
|
|
|
Local address, traffic sent to this port will be forwarded to the server
|
|
|
|
|
|
|
|
|
|
-s 服务器的地址,流量将通过隧道转发到这个服务器
|
|
|
|
|
The address of the server, the traffic will be forwarded to this server through the tunnel
|
|
|
|
|
|
|
|
|
|
-t 远端服务器转发的目的地址,流量将转发到这个地址
|
|
|
|
|
Destination address forwarded by the remote server, traffic will be forwarded to this address
|
2018-12-16 08:56:40 +03:00
|
|
|
|
|
2018-12-19 06:48:48 +03:00
|
|
|
|
-timeout 本地记录连接超时的时间,单位是秒,默认60s
|
|
|
|
|
The time when the local record connection timed out, in seconds, 60 seconds by default
|
|
|
|
|
|
2019-01-08 05:43:31 +03:00
|
|
|
|
-key 设置的密码,默认0
|
|
|
|
|
Set password, default 0
|
2019-10-16 16:09:21 +03:00
|
|
|
|
|
2019-10-27 14:06:55 +03:00
|
|
|
|
-tcp 设置是否转发tcp,默认0
|
|
|
|
|
Set the switch to forward tcp, the default is 0
|
2019-10-25 15:04:52 +03:00
|
|
|
|
|
2019-10-27 07:25:17 +03:00
|
|
|
|
-tcp_bs tcp的发送接收缓冲区大小,默认10MB
|
|
|
|
|
Tcp send and receive buffer size, default 10MB
|
2019-10-25 15:04:52 +03:00
|
|
|
|
|
2019-10-26 14:54:49 +03:00
|
|
|
|
-tcp_mw tcp的最大窗口,默认10000
|
2019-10-26 15:02:37 +03:00
|
|
|
|
The maximum window of tcp, the default is 10000
|
2019-10-25 15:04:52 +03:00
|
|
|
|
|
2019-10-26 14:54:49 +03:00
|
|
|
|
-tcp_rst tcp的超时发送时间,默认400ms
|
|
|
|
|
Tcp timeout resend time, default 400ms
|
2019-10-27 14:06:55 +03:00
|
|
|
|
|
2019-10-27 14:20:35 +03:00
|
|
|
|
-tcp_gz 当数据包超过这个大小,tcp将压缩数据,0表示不压缩,默认0
|
|
|
|
|
Tcp will compress data when the packet exceeds this size, 0 means no compression, default 0
|
2019-10-27 14:41:56 +03:00
|
|
|
|
|
2019-10-28 06:58:01 +03:00
|
|
|
|
-tcp_stat 打印tcp的监控,默认0
|
|
|
|
|
Print tcp connection statistic, default 0 is off
|
|
|
|
|
|
2019-10-27 14:41:56 +03:00
|
|
|
|
-nolog 不写日志文件,只打印标准输出,默认0
|
|
|
|
|
Do not write log files, only print standard output, default 0 is off
|
|
|
|
|
|
2019-10-28 07:11:04 +03:00
|
|
|
|
-loglevel 日志文件等级,默认info
|
|
|
|
|
log level, default is info
|
2019-10-28 11:33:44 +03:00
|
|
|
|
|
|
|
|
|
-sock5 开启sock5转发,默认0
|
|
|
|
|
Turn on sock5 forwarding, default 0 is off
|
2018-12-16 08:56:40 +03:00
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
2018-12-18 10:36:59 +03:00
|
|
|
|
t := flag.String("type", "", "client or server")
|
|
|
|
|
listen := flag.String("l", "", "listen addr")
|
|
|
|
|
target := flag.String("t", "", "target addr")
|
|
|
|
|
server := flag.String("s", "", "server addr")
|
|
|
|
|
timeout := flag.Int("timeout", 60, "conn timeout")
|
2019-01-08 05:32:26 +03:00
|
|
|
|
key := flag.Int("key", 0, "key")
|
2019-10-20 11:27:03 +03:00
|
|
|
|
tcpmode := flag.Int("tcp", 0, "tcp mode")
|
2019-10-27 07:25:17 +03:00
|
|
|
|
tcpmode_buffersize := flag.Int("tcp_bs", 10*1024*1024, "tcp mode buffer size")
|
2019-10-26 15:02:37 +03:00
|
|
|
|
tcpmode_maxwin := flag.Int("tcp_mw", 10000, "tcp mode max win")
|
2019-10-26 14:54:49 +03:00
|
|
|
|
tcpmode_resend_timems := flag.Int("tcp_rst", 400, "tcp mode resend time ms")
|
2019-10-27 14:06:55 +03:00
|
|
|
|
tcpmode_compress := flag.Int("tcp_gz", 0, "tcp data compress")
|
2019-10-27 14:41:56 +03:00
|
|
|
|
nolog := flag.Int("nolog", 0, "write log file")
|
2019-10-28 06:58:01 +03:00
|
|
|
|
tcpmode_stat := flag.Int("tcp_stat", 0, "print tcp stat")
|
2019-10-28 07:11:04 +03:00
|
|
|
|
loglevel := flag.String("loglevel", "info", "log level")
|
2019-10-28 11:33:44 +03:00
|
|
|
|
open_sock5 := flag.Int("sock5", 0, "sock5 mode")
|
2018-12-16 08:56:40 +03:00
|
|
|
|
flag.Usage = func() {
|
|
|
|
|
fmt.Printf(usage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
2019-10-28 11:33:44 +03:00
|
|
|
|
if *t != "client" && *t != "server" {
|
2018-12-16 08:56:40 +03:00
|
|
|
|
flag.Usage()
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-10-28 11:33:44 +03:00
|
|
|
|
if *t == "client" {
|
|
|
|
|
if len(*listen) == 0 || len(*server) == 0 {
|
|
|
|
|
flag.Usage()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if *open_sock5 == 0 && len(*target) == 0 {
|
|
|
|
|
flag.Usage()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if *open_sock5 != 0 {
|
|
|
|
|
*tcpmode = 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-26 15:45:58 +03:00
|
|
|
|
if *tcpmode_maxwin*10 > pingtunnel.FRAME_MAX_ID {
|
2019-10-26 16:00:15 +03:00
|
|
|
|
fmt.Println("set tcp win to big, max = " + strconv.Itoa(pingtunnel.FRAME_MAX_ID/10))
|
2019-10-26 15:45:58 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-12-16 08:56:40 +03:00
|
|
|
|
|
2019-10-28 07:11:04 +03:00
|
|
|
|
level := loggo.LEVEL_INFO
|
|
|
|
|
if loggo.NameToLevel(*loglevel) >= 0 {
|
|
|
|
|
level = loggo.NameToLevel(*loglevel)
|
|
|
|
|
}
|
2019-10-27 14:41:56 +03:00
|
|
|
|
loggo.Ini(loggo.Config{
|
2019-10-28 07:11:04 +03:00
|
|
|
|
Level: level,
|
2019-10-27 14:41:56 +03:00
|
|
|
|
Prefix: "pingtunnel",
|
|
|
|
|
MaxDay: 3,
|
|
|
|
|
NoLogFile: *nolog > 0,
|
|
|
|
|
})
|
2019-10-25 15:06:13 +03:00
|
|
|
|
loggo.Info("start...")
|
|
|
|
|
loggo.Info("key %d", *key)
|
2018-12-16 08:56:40 +03:00
|
|
|
|
|
|
|
|
|
if *t == "server" {
|
2019-10-26 07:01:30 +03:00
|
|
|
|
s, err := pingtunnel.NewServer(*key)
|
2018-12-16 08:56:40 +03:00
|
|
|
|
if err != nil {
|
2019-10-25 15:06:13 +03:00
|
|
|
|
loggo.Error("ERROR: %s", err.Error())
|
2018-12-16 08:56:40 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-10-25 15:06:13 +03:00
|
|
|
|
loggo.Info("Server start")
|
2018-12-16 08:56:40 +03:00
|
|
|
|
s.Run()
|
|
|
|
|
}
|
|
|
|
|
if *t == "client" {
|
2018-12-18 10:36:59 +03:00
|
|
|
|
|
2019-10-25 15:06:13 +03:00
|
|
|
|
loggo.Info("type %s", *t)
|
|
|
|
|
loggo.Info("listen %s", *listen)
|
|
|
|
|
loggo.Info("server %s", *server)
|
|
|
|
|
loggo.Info("target %s", *target)
|
2018-12-18 10:36:59 +03:00
|
|
|
|
|
2019-10-25 15:42:51 +03:00
|
|
|
|
if *tcpmode == 0 {
|
|
|
|
|
*tcpmode_buffersize = 0
|
|
|
|
|
*tcpmode_maxwin = 0
|
|
|
|
|
*tcpmode_resend_timems = 0
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 15:04:52 +03:00
|
|
|
|
c, err := pingtunnel.NewClient(*listen, *server, *target, *timeout, *key,
|
2019-10-28 06:58:01 +03:00
|
|
|
|
*tcpmode, *tcpmode_buffersize, *tcpmode_maxwin, *tcpmode_resend_timems, *tcpmode_compress,
|
2019-10-28 11:33:44 +03:00
|
|
|
|
*tcpmode_stat, *open_sock5)
|
2018-12-16 08:56:40 +03:00
|
|
|
|
if err != nil {
|
2019-10-25 15:06:13 +03:00
|
|
|
|
loggo.Error("ERROR: %s", err.Error())
|
2018-12-16 08:56:40 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-10-25 15:06:13 +03:00
|
|
|
|
loggo.Info("Client Listen %s (%s) Server %s (%s) TargetPort %s:", c.Addr(), c.IPAddr(),
|
2018-12-18 10:36:59 +03:00
|
|
|
|
c.ServerAddr(), c.ServerIPAddr(), c.TargetAddr())
|
2018-12-17 10:21:15 +03:00
|
|
|
|
c.Run()
|
2018-12-16 08:56:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|