pingtunnel/main.go

205 lines
6.4 KiB
Go
Raw Normal View History

2019-11-01 16:12:37 +03:00
package main
2018-12-16 08:56:40 +03:00
import (
"flag"
"fmt"
2019-10-25 15:06:13 +03:00
"github.com/esrrhs/go-engine/src/loggo"
2019-11-01 16:10:32 +03:00
"github.com/esrrhs/go-engine/src/pingtunnel"
2019-11-01 18:18:57 +03:00
"net/http"
_ "net/http/pprof"
2019-10-26 15:45:58 +03:00
"strconv"
2019-10-30 14:14:27 +03:00
"time"
2018-12-16 08:56:40 +03:00
)
var usage = `
2019-10-28 11:55:27 +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-11-01 18:54:44 +03:00
-tcp_bs tcp的发送接收缓冲区大小默认1MB
Tcp send and receive buffer size, default 1MB
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-28 11:55:27 +03:00
-tcp_gz 当数据包超过这个大小tcp将压缩数据0表示不压缩默认0
2019-10-27 14:20:35 +03:00
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 11:55:27 +03:00
-tcp_stat 打印tcp的监控默认0
2019-10-28 06:58:01 +03:00
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
2019-10-31 16:16:14 +03:00
-maxconn 最大连接数默认1000
the max num of connections, default 1000
2019-11-01 15:52:18 +03:00
-maxprt server最大处理线程数默认100
max process thread in server, default 100
-maxprb server最大处理线程buffer数默认1000
max process thread's buffer in server, default 1000
2019-11-01 18:18:57 +03:00
-profile 在端口开启性能检测默认0不开启
open profile on the port, 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-11-01 18:54:44 +03:00
tcpmode_buffersize := flag.Int("tcp_bs", 1*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")
2019-10-31 16:16:14 +03:00
maxconn := flag.Int("maxconn", 0, "max num of connections")
2019-11-01 15:54:55 +03:00
max_process_thread := flag.Int("maxprt", 100, "max process thread in server")
max_process_buffer := flag.Int("maxprb", 1000, "max process thread's buffer in server")
2019-11-01 18:18:57 +03:00
profile := flag.Int("profile", 0, "open profile")
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-11-01 15:52:18 +03:00
s, err := pingtunnel.NewServer(*key, *maxconn, *max_process_thread, *max_process_buffer)
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")
2019-10-30 14:14:27 +03:00
err = s.Run()
if err != nil {
loggo.Error("Run ERROR: %s", err.Error())
return
}
2019-10-31 16:16:14 +03:00
} else 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-29 15:39:12 +03:00
*tcpmode_compress = 0
*tcpmode_stat = 0
2019-10-25 15:42:51 +03:00
}
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-31 16:16:14 +03:00
*tcpmode_stat, *open_sock5, *maxconn)
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())
2019-10-30 14:14:27 +03:00
err = c.Run()
if err != nil {
loggo.Error("Run ERROR: %s", err.Error())
return
}
2019-10-31 16:16:14 +03:00
} else {
return
2019-10-30 14:14:27 +03:00
}
2019-11-01 18:18:57 +03:00
if *profile > 0 {
2019-11-01 18:22:24 +03:00
go http.ListenAndServe("0.0.0.0:"+strconv.Itoa(*profile), nil)
2019-11-01 18:18:57 +03:00
}
2019-10-30 14:14:27 +03:00
for {
time.Sleep(time.Hour)
2018-12-16 08:56:40 +03:00
}
}