From c39678032b50acc531948cbf2c4bcc84c05bf87c Mon Sep 17 00:00:00 2001 From: hahahrfool <75717694+hahahrfool@users.noreply.github.com> Date: Fri, 15 Apr 2022 15:10:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3trojan=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=85=BC=E5=AE=B9trojan-go=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 6 ++++-- proxy/proxy.go | 13 +++++++++++-- proxy/trojan/server.go | 8 ++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 2d0a4e5..fe29327 100644 --- a/main.go +++ b/main.go @@ -641,7 +641,7 @@ func handshakeInserver(iics *incomingInserverConnState) (wlc io.ReadWriteCloser, return } - session := inServer.GetServerInnerMuxSession(mh.ReadWriteCloser) + session := inServer.GetServerInnerMuxSession(mh) if session == nil { err = utils.ErrFailed @@ -660,7 +660,9 @@ func handshakeInserver(iics *incomingInserverConnState) (wlc io.ReadWriteCloser, stream, err := session.AcceptStream() if err != nil { - utils.Debug("inServer try accept stream err") + if ce := utils.CanLogDebug("mux inServer try accept stream failed"); ce != nil { + ce.Write(zap.Error(err)) + } session.Close() return diff --git a/proxy/proxy.go b/proxy/proxy.go index 2d218a3..c9c9c7d 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -37,7 +37,16 @@ func PrintAllClientNames() { //用于Server返回一个可被识别为innerMux的结构 type MuxConnHaser struct { io.ReadWriteCloser - IsMux bool + OptionalReader io.Reader + IsMux bool +} + +func (mc MuxConnHaser) Read(p []byte) (n int, err error) { + if r := mc.OptionalReader; r != nil { + return r.Read(p) + } else { + return mc.ReadWriteCloser.Read(p) + } } // Client 用于向 服务端 拨号. @@ -282,7 +291,7 @@ func (pcs *ProxyCommonStruct) HasInnerMux() (int, string) { return 0, "" } -func (pcs *ProxyCommonStruct) GetServerInnerMuxSession(wlc io.ReadWriteCloser) *smux.Session { +func (*ProxyCommonStruct) GetServerInnerMuxSession(wlc io.ReadWriteCloser) *smux.Session { smuxConfig := smux.DefaultConfig() smuxSession, err := smux.Server(wlc, smuxConfig) if err != nil { diff --git a/proxy/trojan/server.go b/proxy/trojan/server.go index 87b7411..a9abb2e 100644 --- a/proxy/trojan/server.go +++ b/proxy/trojan/server.go @@ -159,10 +159,14 @@ realPart: } if ismux { - return proxy.MuxConnHaser{ + mh := proxy.MuxConnHaser{ ReadWriteCloser: underlay, IsMux: true, - }, nil, targetAddr, nil + } + if readbuf.Len() > 0 { + mh.OptionalReader = io.MultiReader(readbuf, underlay) + } + return mh, nil, targetAddr, nil } if isudp {