From e26096085e73bfca1bd9bedcdd444d26b9a04a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 28 Sep 2023 15:51:33 +0800 Subject: [PATCH] Remove invalid code --- adapter/v2ray.go | 2 -- inbound/trojan.go | 7 ------- inbound/vless.go | 4 ---- inbound/vmess.go | 4 ---- transport/v2raygrpclite/server.go | 17 +++++------------ transport/v2rayhttp/server.go | 24 +++++++++--------------- transport/v2raywebsocket/server.go | 20 ++++++-------------- 7 files changed, 20 insertions(+), 58 deletions(-) diff --git a/adapter/v2ray.go b/adapter/v2ray.go index df6372a1..d1b420ee 100644 --- a/adapter/v2ray.go +++ b/adapter/v2ray.go @@ -5,7 +5,6 @@ import ( "net" E "github.com/sagernet/sing/common/exceptions" - M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" ) @@ -19,7 +18,6 @@ type V2RayServerTransport interface { type V2RayServerTransportHandler interface { N.TCPConnectionHandler E.Handler - FallbackConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error } type V2RayClientTransport interface { diff --git a/inbound/trojan.go b/inbound/trojan.go index 84ef9bcf..82432131 100644 --- a/inbound/trojan.go +++ b/inbound/trojan.go @@ -227,10 +227,3 @@ func (t *trojanTransportHandler) NewConnection(ctx context.Context, conn net.Con Destination: metadata.Destination, }) } - -func (t *trojanTransportHandler) FallbackConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { - return (*Trojan)(t).fallbackConnection(ctx, conn, adapter.InboundContext{ - Source: metadata.Source, - Destination: metadata.Destination, - }) -} diff --git a/inbound/vless.go b/inbound/vless.go index efb73551..11df86bf 100644 --- a/inbound/vless.go +++ b/inbound/vless.go @@ -189,7 +189,3 @@ func (t *vlessTransportHandler) NewConnection(ctx context.Context, conn net.Conn Destination: metadata.Destination, }) } - -func (t *vlessTransportHandler) FallbackConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { - return os.ErrInvalid -} diff --git a/inbound/vmess.go b/inbound/vmess.go index 89f938fc..77a8b28a 100644 --- a/inbound/vmess.go +++ b/inbound/vmess.go @@ -198,7 +198,3 @@ func (t *vmessTransportHandler) NewConnection(ctx context.Context, conn net.Conn Destination: metadata.Destination, }) } - -func (t *vmessTransportHandler) FallbackConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { - return os.ErrInvalid -} diff --git a/transport/v2raygrpclite/server.go b/transport/v2raygrpclite/server.go index bcf55def..a3025ca6 100644 --- a/transport/v2raygrpclite/server.go +++ b/transport/v2raygrpclite/server.go @@ -64,15 +64,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { return } if request.URL.Path != s.path { - s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) + s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) return } if request.Method != http.MethodPost { - s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad method: ", request.Method)) + s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad method: ", request.Method)) return } if ct := request.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/grpc") { - s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad content type: ", ct)) + s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad content type: ", ct)) return } writer.Header().Set("Content-Type", "application/grpc") @@ -85,18 +85,11 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { conn.CloseWrapper() } -func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter, request *http.Request, statusCode int, err error) { - conn := v2rayhttp.NewHTTPConn(request.Body, writer) - fErr := s.handler.FallbackConnection(ctx, &conn, M.Metadata{}) - if fErr == nil { - return - } else if fErr == os.ErrInvalid { - fErr = nil - } +func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) { if statusCode > 0 { writer.WriteHeader(statusCode) } - s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr)) + s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr)) } func (s *Server) Serve(listener net.Listener) error { diff --git a/transport/v2rayhttp/server.go b/transport/v2rayhttp/server.go index 962f0ab1..a635e8f3 100644 --- a/transport/v2rayhttp/server.go +++ b/transport/v2rayhttp/server.go @@ -85,15 +85,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { } host := request.Host if len(s.host) > 0 && !common.Contains(s.host, host) { - s.fallbackRequest(request.Context(), writer, request, http.StatusBadRequest, E.New("bad host: ", host)) + s.invalidRequest(writer, request, http.StatusBadRequest, E.New("bad host: ", host)) return } if !strings.HasPrefix(request.URL.Path, s.path) { - s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) + s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) return } if request.Method != s.method { - s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad method: ", request.Method)) + s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad method: ", request.Method)) return } @@ -113,7 +113,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { requestBody = buf.NewSize(contentLength) _, err := requestBody.ReadFullFrom(request.Body, contentLength) if err != nil { - s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "read request")) + s.invalidRequest(writer, request, 0, E.Cause(err, "read request")) return } } @@ -121,14 +121,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { writer.(http.Flusher).Flush() conn, reader, err := h.Hijack() if err != nil { - s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "hijack conn")) + s.invalidRequest(writer, request, 0, E.Cause(err, "hijack conn")) return } if cacheLen := reader.Reader.Buffered(); cacheLen > 0 { cache := buf.NewSize(cacheLen) _, err = cache.ReadFullFrom(reader.Reader, cacheLen) if err != nil { - s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "read cache")) + conn.Close() + s.invalidRequest(writer, request, 0, E.Cause(err, "read cache")) return } conn = bufio.NewCachedConn(conn, cache) @@ -148,18 +149,11 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { } } -func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter, request *http.Request, statusCode int, err error) { - conn := NewHTTPConn(request.Body, writer) - fErr := s.handler.FallbackConnection(ctx, &conn, M.Metadata{}) - if fErr == nil { - return - } else if fErr == os.ErrInvalid { - fErr = nil - } +func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) { if statusCode > 0 { writer.WriteHeader(statusCode) } - s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr)) + s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr)) } func (s *Server) Serve(listener net.Listener) error { diff --git a/transport/v2raywebsocket/server.go b/transport/v2raywebsocket/server.go index 73bf56ff..9d8bc69a 100644 --- a/transport/v2raywebsocket/server.go +++ b/transport/v2raywebsocket/server.go @@ -12,7 +12,6 @@ import ( "github.com/sagernet/sing-box/common/tls" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing-box/transport/v2rayhttp" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" @@ -69,7 +68,7 @@ var upgrader = websocket.Upgrader{ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { if s.maxEarlyData == 0 || s.earlyDataHeaderName != "" { if request.URL.Path != s.path { - s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) + s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) return } } @@ -83,7 +82,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { earlyDataStr := request.URL.RequestURI()[len(s.path):] earlyData, err = base64.RawURLEncoding.DecodeString(earlyDataStr) } else { - s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) + s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path)) return } } else { @@ -93,12 +92,12 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { } } if err != nil { - s.fallbackRequest(request.Context(), writer, request, http.StatusBadRequest, E.Cause(err, "decode early data")) + s.invalidRequest(writer, request, http.StatusBadRequest, E.Cause(err, "decode early data")) return } wsConn, err := upgrader.Upgrade(writer, request, nil) if err != nil { - s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "upgrade websocket connection")) + s.invalidRequest(writer, request, 0, E.Cause(err, "upgrade websocket connection")) return } var metadata M.Metadata @@ -110,18 +109,11 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { s.handler.NewConnection(request.Context(), conn, metadata) } -func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter, request *http.Request, statusCode int, err error) { - conn := v2rayhttp.NewHTTPConn(request.Body, writer) - fErr := s.handler.FallbackConnection(ctx, &conn, M.Metadata{}) - if fErr == nil { - return - } else if fErr == os.ErrInvalid { - fErr = nil - } +func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) { if statusCode > 0 { writer.WriteHeader(statusCode) } - s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr)) + s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr)) } func (s *Server) Network() []string {