From c92219d18dd81d9a6683b79daaf5e4eebe2f1bd5 Mon Sep 17 00:00:00 2001 From: e1732a364fed <75717694+e1732a364fed@users.noreply.github.com> Date: Sat, 1 Jan 2000 00:00:00 +0000 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81,=E6=96=87?= =?UTF-8?q?=E6=A1=A3,=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/multi.server.toml | 9 +++++++++ examples/vless_tproxy.client.toml | 5 ++--- main.go | 8 ++++---- proxy/config_proxy.go | 16 +++++++++------- proxy/proxy.go | 6 +++--- proxy/shadowsocks/server.go | 4 ++-- .../tproxy/{tproxy_linux.go => server_linux.go} | 10 +++++----- 7 files changed, 34 insertions(+), 24 deletions(-) rename proxy/tproxy/{tproxy_linux.go => server_linux.go} (92%) diff --git a/examples/multi.server.toml b/examples/multi.server.toml index 080345ef..9cedcdf6 100644 --- a/examples/multi.server.toml +++ b/examples/multi.server.toml @@ -76,3 +76,12 @@ protocol = "reject" [[route]] toTag = "my_special_tag_for_this_guy" user = ["a684455c-b14f-11ea-bf0d-42010aaa0004"] #通过 listen 所得到 的 user的不同 来分流 + + +[[dial]] +tag = "my_special_direct" +protocol = "direct" +network = "dual" +sendThrough = "tcp:127.0.0.1:80\nudp:127.0.0.1:12345" +# 这里展示了一种特殊用法,direct设置sendThrough, 就能限制它发送数据的端口, 而且tcp和udp可以设为不同的地址 +# 注意这个写法 "tcp:ip:port\nudp:ip:port" diff --git a/examples/vless_tproxy.client.toml b/examples/vless_tproxy.client.toml index c8137312..5d090f2c 100644 --- a/examples/vless_tproxy.client.toml +++ b/examples/vless_tproxy.client.toml @@ -5,9 +5,7 @@ # 我们不像v2ray/xray一样 使用 dokodemo的额外配置来处理tproxy,而是单独使用 tproxy作为一个协议. -# tproxy无法在 交互模式/apiServer中配置 或 热加载, 只能用 标准toml配置文件启用. (因为涉及到 iptables) - -# tproxy只支持客户端, 且只支持linux系统, 一般用于软路由/树莓派等. +# tproxy只支持客户端, 且目前只支持linux系统, 一般用于软路由/树莓派等. [[listen]] protocol = "tproxy" @@ -16,6 +14,7 @@ port = 12345 # sockopt.tproxy = true # 不需要明示指明 tproxy这个 sockopt, 因为protocol指出tproxy后就会自动配置该项. # 不用指明network, 只要指明了tproxy, 就会同时自动监听 tcp和 dup. +# 但是如果你指明了 network = tcp, 就不会转发udp, 你指明了 network = udp 的话,就不会转发 tcp. #extra = { auto_iptables = true } # 如果给出了 auto_iptables, 且 【protocol 为 tproxy 的listen】【只有一个】, 则程序会自动在开始监听前配置好iptables,并在程序退出前 清除iptables中被程序改动的部分。 # auto_iptables 会一字不差地运行 上面 给出的 toutyrater 的教程中的 iptables命令。 diff --git a/main.go b/main.go index e0dc55a7..ef6bb3f2 100644 --- a/main.go +++ b/main.go @@ -76,11 +76,11 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy. var is, tcp, udp bool //tproxy 和 shadowsocks 都用到了 SelfListen if is, tcp, udp = inServer.SelfListen(); is { - var chantcp chan proxy.IncomeTCPInfo - var chanudp chan proxy.IncomeUDPInfo + var chantcp chan proxy.TCPRequestInfo + var chanudp chan proxy.UDPRequestInfo if tcp { - chantcp = make(chan proxy.IncomeTCPInfo, 2) + chantcp = make(chan proxy.TCPRequestInfo, 2) go func() { for tcpInfo := range chantcp { go passToOutClient(incomingInserverConnState{ @@ -95,7 +95,7 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy. } if udp { - chanudp = make(chan proxy.IncomeUDPInfo, 2) + chanudp = make(chan proxy.UDPRequestInfo, 2) go func() { for udpInfo := range chanudp { diff --git a/proxy/config_proxy.go b/proxy/config_proxy.go index cd133e7d..d01e5b06 100644 --- a/proxy/config_proxy.go +++ b/proxy/config_proxy.go @@ -8,7 +8,7 @@ import ( "github.com/e1732a364fed/v2ray_simple/utils" ) -//用于 tproxy 或 tun/tap 这种 只有 网络层 和传输层的情况 +// 用于 tproxy 或 tun/tap 这种 只有 网络层 和传输层的情况 type LesserConf struct { Addr string Tag string @@ -42,7 +42,7 @@ type CommonConf struct { Xver int `toml:"xver"` //可选,只能为0/1/2. 若不为0, 则使用 PROXY protocol 协议头. - Fullcone bool `toml:"fullcone"` //在direct会用到, fullcone的话因为不能关闭udp连接, 所以 时间长后, 可能会导致too many open files. fullcone 的话一般人是用不到的, 所以 有需要的人自行手动打开 即可 + Fullcone bool `toml:"fullcone"` //在udp会用到, fullcone的话因为不能关闭udp连接, 所以 时间长后, 可能会导致too many open files. fullcone 的话一般人是用不到的, 所以 有需要的人自行手动打开 即可 /////////////////// tls层 /////////////////// @@ -75,7 +75,7 @@ type CommonConf struct { } -//和 GetAddrStrForListenOrDial 的区别是,它优先使用host,其次再使用ip +// 和 GetAddrStrForListenOrDial 的区别是,它优先使用host,其次再使用ip func (cc *CommonConf) GetAddrStr() string { switch cc.Network { case "unix": @@ -94,7 +94,7 @@ func (cc *CommonConf) GetAddrStr() string { } -//if network is unix domain socket, return Host,or return ip:port / host:port; 和 GetAddr的区别是,它优先使用ip,其次再使用host +// if network is unix domain socket, return Host,or return ip:port / host:port; 和 GetAddr的区别是,它优先使用ip,其次再使用host func (cc *CommonConf) GetAddrStrForListenOrDial() string { switch cc.Network { case "unix": @@ -114,7 +114,8 @@ func (cc *CommonConf) GetAddrStrForListenOrDial() string { } // config for listening, the user can be called as listener or inServer. -// CommonConf.Host , CommonConf.IP, CommonConf.Port is the addr and port for listening +// +// CommonConf.Host , CommonConf.IP, CommonConf.Port is the addr and port for listening type ListenConf struct { CommonConf @@ -135,11 +136,12 @@ type ListenConf struct { } // config for dialing, user can be called dialer or outClient. -// CommonConf.Host , CommonConf.IP, CommonConf.Port are the addr and port for dialing. +// +// CommonConf.Host , CommonConf.IP, CommonConf.Port are the addr and port for dialing. type DialConf struct { CommonConf - SendThrough string `toml:"sendThrough"` //可选,用于发送数据的 IP 地址 + SendThrough string `toml:"sendThrough"` //可选,用于发送数据的 IP 地址, 可以是ip:port, 或者 tcp:ip:port\nudp:ip:port Utls bool `toml:"utls"` //是否使用 uTls 库 替换 go官方tls库 diff --git a/proxy/proxy.go b/proxy/proxy.go index a83a8862..25abeb5a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -92,19 +92,19 @@ type Server interface { SelfListen() (is, tcp, udp bool) } -type IncomeTCPInfo struct { +type TCPRequestInfo struct { net.Conn Target netLayer.Addr } -type IncomeUDPInfo struct { +type UDPRequestInfo struct { netLayer.MsgConn Target netLayer.Addr } type ListenerServer interface { Server - StartListen(chan<- IncomeTCPInfo, chan<- IncomeUDPInfo) io.Closer + StartListen(chan<- TCPRequestInfo, chan<- UDPRequestInfo) io.Closer } type UserServer interface { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 458f6d6a..5f8aef44 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -162,7 +162,7 @@ func (m *Server) removeUDPByHash(hash netLayer.HashableAddr) { m.Unlock() } -func (s *Server) StartListen(_ chan<- proxy.IncomeTCPInfo, udpInfoChan chan<- proxy.IncomeUDPInfo) io.Closer { +func (s *Server) StartListen(_ chan<- proxy.TCPRequestInfo, udpInfoChan chan<- proxy.UDPRequestInfo) io.Closer { uc, err := net.ListenUDP("udp", s.LUA) if err != nil { log.Panicln("shadowsocks listen udp failed", err) @@ -226,7 +226,7 @@ func (s *Server) StartListen(_ chan<- proxy.IncomeTCPInfo, udpInfoChan chan<- pr conn.readChan <- netLayer.AddrData{Data: readbuf.Bytes(), Addr: destAddr} if !found { - udpInfoChan <- proxy.IncomeUDPInfo{ + udpInfoChan <- proxy.UDPRequestInfo{ MsgConn: conn, Target: destAddr, } } diff --git a/proxy/tproxy/tproxy_linux.go b/proxy/tproxy/server_linux.go similarity index 92% rename from proxy/tproxy/tproxy_linux.go rename to proxy/tproxy/server_linux.go index de4d3604..a07bc0b0 100644 --- a/proxy/tproxy/tproxy_linux.go +++ b/proxy/tproxy/server_linux.go @@ -62,8 +62,8 @@ type Server struct { shouldSetIPTable bool - infoChan chan<- proxy.IncomeTCPInfo - udpInfoChan chan<- proxy.IncomeUDPInfo + infoChan chan<- proxy.TCPRequestInfo + udpInfoChan chan<- proxy.UDPRequestInfo tm *tproxy.Machine sync.Once } @@ -111,7 +111,7 @@ func (s *Server) Stop() { } -func (s *Server) StartListen(infoChan chan<- proxy.IncomeTCPInfo, udpInfoChan chan<- proxy.IncomeUDPInfo) io.Closer { +func (s *Server) StartListen(infoChan chan<- proxy.TCPRequestInfo, udpInfoChan chan<- proxy.UDPRequestInfo) io.Closer { tm := new(tproxy.Machine) @@ -124,7 +124,7 @@ func (s *Server) StartListen(infoChan chan<- proxy.IncomeTCPInfo, udpInfoChan ch tcpconn := conn.(*net.TCPConn) targetAddr := tproxy.HandshakeTCP(tcpconn) - info := proxy.IncomeTCPInfo{ + info := proxy.TCPRequestInfo{ Conn: tcpconn, Target: targetAddr, } @@ -186,7 +186,7 @@ func (s *Server) StartListen(infoChan chan<- proxy.IncomeTCPInfo, udpInfoChan ch return } - udpInfoChan <- proxy.IncomeUDPInfo{MsgConn: msgConn, Target: raddr} + udpInfoChan <- proxy.UDPRequestInfo{MsgConn: msgConn, Target: raddr} } }()