From f2297721e412c0cd4f9b0878460c22fdd757ed32 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?fix=20#141,=20=E4=BF=AE=E8=AE=A2=E4=BB=A3?= =?UTF-8?q?=E7=A0=81;=20direct=E8=8B=A5=E9=85=8D=E7=BD=AE=E4=BA=86sendThou?= =?UTF-8?q?gh=EF=BC=8C=E5=88=99=E4=BC=9A=E6=89=93=E5=8D=B0=E8=AF=A5?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 8 ++++---- netLayer/dial.go | 4 +++- proxy/direct.go | 7 +++++++ proxy/proxy.go | 21 +++++++++++++++------ proxy/socks5/server.go | 7 ++++--- proxy/vless/client.go | 6 +++--- proxy/vless/server.go | 4 ++-- proxy/vless/tcpconn.go | 12 ++++++------ proxy/vless/udpConn.go | 4 ++-- 9 files changed, 46 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 00e601c..716f1e0 100644 --- a/main.go +++ b/main.go @@ -254,7 +254,7 @@ func handleNewIncomeConnection(inServer proxy.Server, defaultClientForThis proxy addrstr := wrappedConn.RemoteAddr().String() ce.Write( zap.String("from", addrstr), - zap.String("handler", proxy.GetVSI_url(inServer)), + zap.String("handler", proxy.GetVSI_url(inServer, "")), ) iics.cachedRemoteAddr = addrstr @@ -1130,7 +1130,7 @@ func dialClient(iics incomingInserverConnState, targetAddr netLayer.Addr, ce.Write( zap.String("Fallback from", iics.cachedRemoteAddr), zap.String("Target", targetAddr.UrlString()), - zap.String("through", proxy.GetVSI_url(client)), + zap.String("through", proxy.GetVSI_url(client, targetAddr.Network)), zap.Int("with xver", iics.fallbackXver), ) @@ -1138,7 +1138,7 @@ func dialClient(iics incomingInserverConnState, targetAddr netLayer.Addr, ce.Write( zap.String("Fallback from", iics.cachedRemoteAddr), zap.String("Target", targetAddr.UrlString()), - zap.String("through", proxy.GetVSI_url(client)), + zap.String("through", proxy.GetVSI_url(client, targetAddr.Network)), ) } @@ -1146,7 +1146,7 @@ func dialClient(iics incomingInserverConnState, targetAddr netLayer.Addr, ce.Write( zap.String("From", iics.cachedRemoteAddr), zap.String("Target", targetAddr.UrlString()), - zap.String("through", proxy.GetVSI_url(client)), + zap.String("through", proxy.GetVSI_url(client, targetAddr.Network)), ) } diff --git a/netLayer/dial.go b/netLayer/dial.go index 2776005..ff63743 100644 --- a/netLayer/dial.go +++ b/netLayer/dial.go @@ -113,7 +113,8 @@ defaultPart: } else { //一般情况下,unix domain socket 会到达这里,其他情况则都被前面代码捕获到了 - if sockopt == nil { + + if sockopt == nil && localAddr == nil { resultConn, err = net.DialTimeout(a.Network, a.String(), DialTimeout) } else { resultConn, err = a.DialWithOpt(sockopt, localAddr) @@ -161,6 +162,7 @@ func (a Addr) DialWithOpt(sockopt *Sockopt, localAddr net.Addr) (net.Conn, error } else { a.Network = "tcp4" } + dialer.FallbackDelay = -1 } case "udp": if ta, ok := localAddr.(*net.UDPAddr); ok && ta != nil { diff --git a/proxy/direct.go b/proxy/direct.go index 7a03b8b..4cb3c60 100644 --- a/proxy/direct.go +++ b/proxy/direct.go @@ -57,6 +57,13 @@ func (*DirectClient) GetCreator() ClientCreator { return DirectCreator{} } +func (d *DirectClient) AddrStr() string { + if d.LocalTCPAddr() != nil { + return d.LocalTCPAddr().String() + } + return "" +} + // 若 underlay 为nil,则会对target进行拨号, 否则返回underlay本身 func (d *DirectClient) Handshake(underlay net.Conn, firstPayload []byte, target netLayer.Addr) (result io.ReadWriteCloser, err error) { if d.Network() == "udp" { diff --git a/proxy/proxy.go b/proxy/proxy.go index 25abeb5..454f49d 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -117,21 +117,30 @@ type UserServer interface { // // An Example of a full name: tcp+tls+ws+vless. func GetFullName(pc BaseInterface) string { - if n := pc.Name(); n == DirectName { - return n - } else { - return getFullNameBuilder(pc, n).String() - } + return getFullNameBuilder(pc, pc.Name()).String() } // return GetFullName(pc) + "://" + pc.AddrStr() (+ #tag) -func GetVSI_url(pc BaseInterface) string { +func GetVSI_url(pc BaseInterface, targetNetwork string) string { n := pc.Name() sb := getFullNameBuilder(pc, n) sb.WriteString("://") sb.WriteString(pc.AddrStr()) + if n == DirectName { + if targetNetwork == "tcp" { + if lta := pc.GetBase().LTA; lta != nil { + sb.WriteString(lta.String()) + } + } else if targetNetwork == "udp" { + if lua := pc.GetBase().LUA; lua != nil { + sb.WriteString(lua.String()) + } + } + + } + if t := pc.GetTag(); t != "" { sb.WriteByte('#') sb.WriteString(t) diff --git a/proxy/socks5/server.go b/proxy/socks5/server.go index ddde1a1..c92ba5e 100644 --- a/proxy/socks5/server.go +++ b/proxy/socks5/server.go @@ -405,9 +405,10 @@ For: } targetAddr = netLayer.Addr{ - IP: theIP, - Name: theName, - Port: thePort, + IP: theIP, + Name: theName, + Port: thePort, + Network: "tcp", } return underlay, nil, targetAddr, nil diff --git a/proxy/vless/client.go b/proxy/vless/client.go index 7820c6a..1393caf 100644 --- a/proxy/vless/client.go +++ b/proxy/vless/client.go @@ -80,7 +80,7 @@ func (ClientCreator) URLToDialConf(url *url.URL, dc *proxy.DialConf, format int) type Client struct { proxy.Base - version int + version byte user utils.V2rayUser @@ -95,12 +95,12 @@ func (c *Client) Name() string { if c.version == 0 { return Name } - return Name + "_" + strconv.Itoa(c.version) + return Name + "_" + strconv.Itoa(int(c.version)) // 根据 https://forum.golangbridge.org/t/fmt-sprintf-vs-string-concatenation/23006 // 直接 + 比 fmt.Sprintf 快不少. } -func (c *Client) Version() int { return c.version } +func (c *Client) Version() int { return int(c.version) } func (c *Client) GetUser() utils.User { return c.user } diff --git a/proxy/vless/server.go b/proxy/vless/server.go index a8fb5b5..80aec78 100644 --- a/proxy/vless/server.go +++ b/proxy/vless/server.go @@ -275,7 +275,7 @@ realPart: return nil, &UDPConn{ Conn: underlay, V2rayUser: thisUUIDBytes, - version: int(version), + version: version, optionalReader: io.MultiReader(readbuf, underlay), raddr: targetAddr, remainFirstBufLen: readbuf.Len(), @@ -287,7 +287,7 @@ realPart: uc := &UserTCPConn{ Conn: underlay, V2rayUser: thisUUIDBytes, - version: int(version), + version: version, optionalReader: io.MultiReader(readbuf, underlay), remainFirstBufLen: readbuf.Len(), underlayIsBasic: netLayer.IsBasicConn(underlay), diff --git a/proxy/vless/tcpconn.go b/proxy/vless/tcpconn.go index 880cc5a..d0a3b1b 100644 --- a/proxy/vless/tcpconn.go +++ b/proxy/vless/tcpconn.go @@ -11,7 +11,7 @@ import ( "github.com/e1732a364fed/v2ray_simple/utils" ) -//实现 net.Conn, io.ReaderFrom, utils.User, utils.MultiWriter, utils.MultiReader, netLayer.Splicer, netLayer.ConnWrapper +// 实现 net.Conn, io.ReaderFrom, utils.User, utils.MultiWriter, utils.MultiReader, netLayer.Splicer, netLayer.ConnWrapper type UserTCPConn struct { net.Conn @@ -23,7 +23,7 @@ type UserTCPConn struct { underlayIsBasic bool - version int + version byte isServerEnd bool //for v0 isntFirstPacket bool //for v0 @@ -33,14 +33,14 @@ type UserTCPConn struct { } func (u *UserTCPConn) GetProtocolVersion() int { - return u.version + return int(u.version) } func (c *UserTCPConn) GetRawConn() net.Conn { return c.Conn } -//当前连接状态是否可以直接写入底层Conn而不经任何改动/包装 +// 当前连接状态是否可以直接写入底层Conn而不经任何改动/包装 func (c *UserTCPConn) canDirectWrite() bool { return c.version == 1 || (c.version == 0 && !(c.isServerEnd && !c.isntFirstPacket)) } @@ -154,13 +154,13 @@ func (c *UserTCPConn) Write(p []byte) (int, error) { } } -//专门适用于 裸奔splice的情况 +// 专门适用于 裸奔splice的情况 func (c *UserTCPConn) ReadFrom(r io.Reader) (written int64, err error) { return netLayer.TryReadFrom_withSplice(c, c.Conn, r, c.canDirectWrite) } -//如果是udp,则是多线程不安全的,如果是tcp,则安不安全看底层的链接。 +// 如果是udp,则是多线程不安全的,如果是tcp,则安不安全看底层的链接。 // 这里规定,如果是UDP,则 每次 Read 得到的都是一个 完整的UDP 数据包,除非p给的太小…… func (c *UserTCPConn) Read(p []byte) (int, error) { diff --git a/proxy/vless/udpConn.go b/proxy/vless/udpConn.go index 8fb58de..8489b4e 100644 --- a/proxy/vless/udpConn.go +++ b/proxy/vless/udpConn.go @@ -24,7 +24,7 @@ type UDPConn struct { remainFirstBufLen int - version int + version byte udp_multi bool isClientEnd bool @@ -47,7 +47,7 @@ func (u *UDPConn) Fullcone() bool { } func (u *UDPConn) GetProtocolVersion() int { - return u.version + return int(u.version) } func (u *UDPConn) WriteMsgTo(p []byte, raddr netLayer.Addr) error {