From 7ed04c07ff638b75f86f42e3e8209a4dd179c715 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?=E4=BF=AE=E8=AE=A2=E4=BB=A3=E7=A0=81,=E6=96=87?= =?UTF-8?q?=E6=A1=A3;=20=E4=BF=AE=E6=94=B9SelfListen=E7=AD=BE=E5=90=8D;?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=8B=E5=8A=A8auto=20route=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SelfListen返回一个bool和两个int; windows auto route中sleep4秒 --- examples/tun.client.toml | 10 +++---- main.go | 16 ++++++----- proxy/base.go | 2 +- proxy/proxy.go | 2 +- proxy/shadowsocks/server.go | 8 +++--- proxy/tproxy/server_darwin.go | 11 +++++--- proxy/tproxy/server_linux.go | 18 +++++++------ proxy/tun/route_windows.go | 49 +++++++++++++++++++--------------- proxy/tun/tun.go | 50 ++++++++++++++++++----------------- 9 files changed, 90 insertions(+), 76 deletions(-) diff --git a/examples/tun.client.toml b/examples/tun.client.toml index d34f0a4..fb59f5d 100644 --- a/examples/tun.client.toml +++ b/examples/tun.client.toml @@ -55,23 +55,19 @@ [[listen]] protocol = "tun" -# tun Server使用 host 配置作为 tun device name (macos不设此项) +# tun Server使用 host 配置作为 tun device name # 使用 ip 配置作为 gateway 的ip , 若不给出,默认为 10.1.0.20 -# 使用 extra.tun_selfip 作为 tun向外拨号的ip, 若不给出, 默认为 10.1.0.10 -# 使用 extra.tun_mask 作为 子网掩码, 若不给出, 默认为 255.255.255.0 - +# 使用 extra.tun_selfip 作为 tun向外拨号的ip, 若不给出, 默认为 10.1.0.10 (windows上不配置该项) # 如果 extra.tun_auto_route 给出,vs_gui会试图自动配置路由表. # 此时必须额外给出需要 直连的ip列表, 比如你的 代理服务器的ip地址; 如果不给出, 则不会自动配置路由表 -# 在windows上,还可给出 tun_dns_list 否则windows上,vs默认会使用 114.114.114.114 - # 目前的自动配置逻辑 完全仿照上面的路由配置指导。 extra.tun_auto_route = true extra.tun_auto_route_direct_list = [ "127.0.0.1" ] -extra.tun_dns_list = [ "223.5.5.5" ] #阿里 +# extra.tun_auto_route_manual = true # 若这项开启,则生成路由命令但不执行,由你自行拷贝到终端并执行。 [[dial]] protocol = "vlesss" diff --git a/main.go b/main.go index 87b023f..5cd1af3 100644 --- a/main.go +++ b/main.go @@ -74,7 +74,8 @@ non-blocking. closer used to stop listening. It means listening failed if closer func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.RoutingEnv, gi *GlobalInfo) (closer io.Closer) { var extraCloser io.Closer - var is, tcp, udp bool + var is bool + var tcp, udp int //tproxy,tun/tap 和 shadowsocks(udp) 都用到了 SelfListen if is, tcp, udp = inServer.SelfListen(); is { var chantcp chan netLayer.TCPRequestInfo @@ -91,7 +92,7 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy. ) } - if tcp { + if tcp == 1 { chantcp = make(chan netLayer.TCPRequestInfo, 2) go func() { for tcpInfo := range chantcp { @@ -107,7 +108,7 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy. }() } - if udp { + if udp == 1 { chanudp = make(chan netLayer.UDPRequestInfo, 2) go func() { @@ -125,7 +126,9 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy. } closer = inServer.(proxy.ListenerServer).StartListen(chantcp, chanudp) - if tcp && udp { + //可以直接return的值: 1,1 1,-1, -1,1 ; + //还需继续监听的值: 1,0 0,1 + if tcp+udp != 1 { return } else { @@ -197,8 +200,9 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy. network := inServer.Network() if network == netLayer.DualNetworkName && is { - if is != tcp && udp { - if tcp { + //设置这里实际监听的传输层协议 + if is != (tcp == 1 && udp == 1) { + if tcp == 1 { network = "udp" } else { network = "tcp" diff --git a/proxy/base.go b/proxy/base.go index 787d2a9..87271e5 100644 --- a/proxy/base.go +++ b/proxy/base.go @@ -492,6 +492,6 @@ func (d *Base) DialUDP(target netLayer.Addr) (mc *netLayer.UDPMsgConn, err error return } -func (d *Base) SelfListen() (is, tcp, udp bool) { +func (d *Base) SelfListen() (is bool, tcp, udp int) { return } diff --git a/proxy/proxy.go b/proxy/proxy.go index bfbbc7b..1895f9f 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -91,7 +91,7 @@ type Server interface { //get/listen a useable inner mux GetServerInnerMuxSession(wlc io.ReadWriteCloser) *smux.Session - SelfListen() (is, tcp, udp bool) + SelfListen() (is bool, tcp, udp int) //is表示开启自监听; 此时若 tcp=1, 表示监听tcp, 若tcp=0, 表示自己不监听tcp, 但需要vs进行监听; 若tcp<0, 则表示自己不监听, 也不要vs监听; udp同理 } type ListenerServer interface { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 126e435..c0c789f 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -98,18 +98,18 @@ func (s *Server) Network() string { } } -func (s *Server) SelfListen() (is, _, udp bool) { +func (s *Server) SelfListen() (is bool, _, udp int) { switch n := s.Network(); n { case "", netLayer.DualNetworkName: - udp = true + udp = 1 case "tcp": case "udp": - udp = true + udp = 1 } - is = udp + is = udp > 0 return } diff --git a/proxy/tproxy/server_darwin.go b/proxy/tproxy/server_darwin.go index 69f2253..d392f94 100644 --- a/proxy/tproxy/server_darwin.go +++ b/proxy/tproxy/server_darwin.go @@ -69,9 +69,12 @@ func NewServer() (proxy.Server, error) { } func (*Server) Name() string { return name } -func (s *Server) SelfListen() (is, tcp, udp bool) { - return true, true, true //darwin的tproxy不支持udp,但是如果传入false,则v2ray_simple包会试图自行监听udp。 - //所以暂时的解决方案是欺骗它告诉他我们都监听 +func (s *Server) SelfListen() (is bool, tcp, udp int) { + udp = -1 + tcp = 1 + is = true + + return } func (s *Server) Close() error { @@ -96,7 +99,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha tm := new(tproxy.Machine) _, lt, _ := s.SelfListen() - if lt { + if lt > 0 { s.infoChan = infoChan lis, err := netLayer.ListenAndAccept("tcp", s.Addr, s.Sockopt, 0, func(conn net.Conn) { diff --git a/proxy/tproxy/server_linux.go b/proxy/tproxy/server_linux.go index 141354f..0a042b3 100644 --- a/proxy/tproxy/server_linux.go +++ b/proxy/tproxy/server_linux.go @@ -72,19 +72,21 @@ func NewServer() (proxy.Server, error) { } func (*Server) Name() string { return name } -func (s *Server) SelfListen() (is, tcp, udp bool) { +func (s *Server) SelfListen() (is bool, tcp, udp int) { switch n := s.Network(); n { case "", netLayer.DualNetworkName: - tcp = true - udp = true + tcp = 1 + udp = 1 case "tcp": - tcp = true + tcp = 1 + udp = -1 case "udp": - udp = true + udp = 1 + tcp = -1 } - is = tcp || udp + is = true return } @@ -115,7 +117,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha _, lt, lu := s.SelfListen() - if lt { + if lt > 0 { s.infoChan = infoChan lis, err := netLayer.ListenAndAccept("tcp", s.Addr, s.Sockopt, 0, func(conn net.Conn) { @@ -144,7 +146,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha } - if lu { + if lu > 0 { s.udpInfoChan = udpInfoChan ad, err := netLayer.NewAddr(s.Addr) diff --git a/proxy/tun/route_windows.go b/proxy/tun/route_windows.go index 211c4dd..22fcf5d 100644 --- a/proxy/tun/route_windows.go +++ b/proxy/tun/route_windows.go @@ -14,13 +14,17 @@ import ( var rememberedRouterIP string func init() { - //经过测试发现,完全一样的路由命令,自动执行和 手动在控制台输入执行,效果竟然不一样; 手动的能正常运行, 自动的就不行, 怪 - //似乎是需要等待几秒钟 /* - netsh interface ip set address name="vs_wintun" source=static addr=192.168.123.1 mask=255.255.255.0 gateway=none + 经过测试发现,完全一样的路由命令,自动执行和 手动在控制台输入执行,效果竟然不一样; 手动的能正常运行, 自动的就不行, 怪 + 后发现,是需要等待4秒钟;3秒都不够; - route add vps_ip router_ip - route add 0.0.0.0 mask 0.0.0.0 vps_ip metric 5 + 要确保wintun的 Gateway显示为 On-link, Interface显示为 设置好的地址; + 错误时显示的是 Geteway 是 设置好的地址,Interface为原始路由器的地址 + + netsh interface ip set address name="vs_wintun" source=static addr=192.168.123.1 mask=255.255.255.0 gateway=none + + route add vps_ip router_ip + route add 0.0.0.0 mask 0.0.0.0 vps_ip metric 5 而且wintun的自动执行行为 和 go-tun2socks 的 tap的行为还是不一样。 @@ -82,27 +86,30 @@ func init() { strs = append(strs, fmt.Sprintf("route add 0.0.0.0 mask 0.0.0.0 %s metric 6", tunGateway)) - // utils.Warn("Please try run these commands manually(Administrator):") - // for _, s := range strs { - // utils.Warn(s) - // } + if manualRoute { + utils.Warn("Please try run these commands manually(Administrator):") + for _, s := range strs { + utils.Warn(s) + } - // if AddManualRunCmdsListFunc != nil { - // AddManualRunCmdsListFunc(strs) - // } + if AddManualRunCmdsListFunc != nil { + AddManualRunCmdsListFunc(strs) + } + } else { + if e := utils.ExecCmdList(strs[:len(strs)-1]); e != nil { + if ce := utils.CanLogErr("recover auto route failed"); ce != nil { + ce.Write(zap.Error(e)) + } + } - if e := utils.ExecCmdList(strs[:len(strs)-1]); e != nil { - if ce := utils.CanLogErr("recover auto route failed"); ce != nil { - ce.Write(zap.Error(e)) + time.Sleep(time.Second * 4) + if e := utils.ExecCmd(strs[len(strs)-1]); e != nil { + if ce := utils.CanLogErr("recover auto route failed"); ce != nil { + ce.Write(zap.Error(e)) + } } } - time.Sleep(time.Second * 2) - if e := utils.ExecCmd(strs[len(strs)-1]); e != nil { - if ce := utils.CanLogErr("recover auto route failed"); ce != nil { - ce.Write(zap.Error(e)) - } - } } autoRouteDownFunc = func(tunDevName, tunGateway, tunIP string, directList []string) { diff --git a/proxy/tun/tun.go b/proxy/tun/tun.go index 86d19c1..52e1f86 100644 --- a/proxy/tun/tun.go +++ b/proxy/tun/tun.go @@ -3,7 +3,9 @@ tun Server使用 host 配置作为 tun device name 使用 ip 配置作为 gateway 的ip 使用 extra.tun_selfip 作为 tun向外拨号的ip - 使用 extra.tun_mask 作为 子网掩码 + + mac默认 utun5, windows 默认 vs_wintun + */ package tun @@ -21,6 +23,7 @@ import ( ) var AddManualRunCmdsListFunc func([]string) +var manualRoute bool const name = "tun" @@ -54,11 +57,6 @@ func (ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error) { s.selfip = str } } - if thing := lc.Extra["tun_mask"]; thing != nil { - if str, ok := thing.(string); ok { - s.mask = str - } - } if thing := lc.Extra["tun_auto_route"]; thing != nil { if auto, autoOk := utils.AnyToBool(thing); autoOk && auto { @@ -81,14 +79,10 @@ func (ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error) { } - if thing := lc.Extra["tun_dns_list"]; thing != nil { + if thing := lc.Extra["tun_auto_route_manual"]; thing != nil { - if list, ok := thing.([]any); ok { - for _, v := range list { - if str, ok := v.(string); ok && str != "" { - s.tun_dnsList = append(s.tun_dnsList, str) - } - } + if manual, ok := utils.AnyToBool(thing); ok && manual { + manualRoute = true } } } @@ -113,9 +107,6 @@ func (ServerCreator) AfterCommonConfServer(ps proxy.Server) (err error) { if s.selfip == "" { s.selfip = defaultSelfIP } - if s.mask == "" { - s.mask = defaultMask - } return } @@ -129,17 +120,29 @@ type Server struct { udpRequestChan chan<- netLayer.UDPRequestInfo lwipCloser io.Closer - devName, realIP, selfip, mask string - autoRoute bool - autoRouteDirectList, tun_dnsList []string + devName, realIP, selfip string + autoRoute bool + autoRouteDirectList []string } func (*Server) Name() string { return name } -func (s *Server) SelfListen() (is, tcp, udp bool) { +func (s *Server) SelfListen() (is bool, tcp, udp int) { + switch n := s.Network(); n { + case "", netLayer.DualNetworkName: + tcp = 1 + udp = 1 + + case "tcp": + tcp = 1 + udp = -1 + case "udp": + udp = 1 + tcp = -1 + } + is = true - tcp = true - udp = true + return } @@ -167,7 +170,7 @@ func (s *Server) Stop() { func (s *Server) StartListen(tcpRequestChan chan<- netLayer.TCPRequestInfo, udpRequestChan chan<- netLayer.UDPRequestInfo) io.Closer { s.stopped = false - //log.Println(s.devName, s.selfip, s.realIP, s.mask) + if s.devName == "" { switch runtime.GOOS { case "darwin": @@ -202,7 +205,6 @@ func (s *Server) StartListen(tcpRequestChan chan<- netLayer.TCPRequestInfo, udpR return nil } - //newTchan, newUchan, lwipcloser := tun.Listen(tunDev) go func() { for tr := range newTchan { if s.stopped {