mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 00:21:30 +00:00
Remove invalid code
This commit is contained in:
parent
2f1b2199c5
commit
e26096085e
|
@ -5,7 +5,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,7 +18,6 @@ type V2RayServerTransport interface {
|
||||||
type V2RayServerTransportHandler interface {
|
type V2RayServerTransportHandler interface {
|
||||||
N.TCPConnectionHandler
|
N.TCPConnectionHandler
|
||||||
E.Handler
|
E.Handler
|
||||||
FallbackConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type V2RayClientTransport interface {
|
type V2RayClientTransport interface {
|
||||||
|
|
|
@ -227,10 +227,3 @@ func (t *trojanTransportHandler) NewConnection(ctx context.Context, conn net.Con
|
||||||
Destination: metadata.Destination,
|
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
@ -189,7 +189,3 @@ func (t *vlessTransportHandler) NewConnection(ctx context.Context, conn net.Conn
|
||||||
Destination: metadata.Destination,
|
Destination: metadata.Destination,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *vlessTransportHandler) FallbackConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
|
|
|
@ -198,7 +198,3 @@ func (t *vmessTransportHandler) NewConnection(ctx context.Context, conn net.Conn
|
||||||
Destination: metadata.Destination,
|
Destination: metadata.Destination,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *vmessTransportHandler) FallbackConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
|
|
|
@ -64,15 +64,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if request.URL.Path != s.path {
|
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
|
return
|
||||||
}
|
}
|
||||||
if request.Method != http.MethodPost {
|
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
|
return
|
||||||
}
|
}
|
||||||
if ct := request.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/grpc") {
|
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
|
return
|
||||||
}
|
}
|
||||||
writer.Header().Set("Content-Type", "application/grpc")
|
writer.Header().Set("Content-Type", "application/grpc")
|
||||||
|
@ -85,18 +85,11 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
conn.CloseWrapper()
|
conn.CloseWrapper()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
|
func (s *Server) invalidRequest(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
|
|
||||||
}
|
|
||||||
if statusCode > 0 {
|
if statusCode > 0 {
|
||||||
writer.WriteHeader(statusCode)
|
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 {
|
func (s *Server) Serve(listener net.Listener) error {
|
||||||
|
|
|
@ -85,15 +85,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
}
|
}
|
||||||
host := request.Host
|
host := request.Host
|
||||||
if len(s.host) > 0 && !common.Contains(s.host, 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
|
return
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(request.URL.Path, s.path) {
|
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
|
return
|
||||||
}
|
}
|
||||||
if request.Method != s.method {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
requestBody = buf.NewSize(contentLength)
|
requestBody = buf.NewSize(contentLength)
|
||||||
_, err := requestBody.ReadFullFrom(request.Body, contentLength)
|
_, err := requestBody.ReadFullFrom(request.Body, contentLength)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,14 +121,15 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
writer.(http.Flusher).Flush()
|
writer.(http.Flusher).Flush()
|
||||||
conn, reader, err := h.Hijack()
|
conn, reader, err := h.Hijack()
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
if cacheLen := reader.Reader.Buffered(); cacheLen > 0 {
|
if cacheLen := reader.Reader.Buffered(); cacheLen > 0 {
|
||||||
cache := buf.NewSize(cacheLen)
|
cache := buf.NewSize(cacheLen)
|
||||||
_, err = cache.ReadFullFrom(reader.Reader, cacheLen)
|
_, err = cache.ReadFullFrom(reader.Reader, cacheLen)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
conn = bufio.NewCachedConn(conn, cache)
|
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) {
|
func (s *Server) invalidRequest(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
|
|
||||||
}
|
|
||||||
if statusCode > 0 {
|
if statusCode > 0 {
|
||||||
writer.WriteHeader(statusCode)
|
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 {
|
func (s *Server) Serve(listener net.Listener) error {
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"github.com/sagernet/sing-box/common/tls"
|
"github.com/sagernet/sing-box/common/tls"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/sagernet/sing-box/transport/v2rayhttp"
|
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
@ -69,7 +68,7 @@ var upgrader = websocket.Upgrader{
|
||||||
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
if s.maxEarlyData == 0 || s.earlyDataHeaderName != "" {
|
if s.maxEarlyData == 0 || s.earlyDataHeaderName != "" {
|
||||||
if request.URL.Path != s.path {
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +82,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
earlyDataStr := request.URL.RequestURI()[len(s.path):]
|
earlyDataStr := request.URL.RequestURI()[len(s.path):]
|
||||||
earlyData, err = base64.RawURLEncoding.DecodeString(earlyDataStr)
|
earlyData, err = base64.RawURLEncoding.DecodeString(earlyDataStr)
|
||||||
} else {
|
} 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
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -93,12 +92,12 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
wsConn, err := upgrader.Upgrade(writer, request, nil)
|
wsConn, err := upgrader.Upgrade(writer, request, nil)
|
||||||
if err != 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
|
return
|
||||||
}
|
}
|
||||||
var metadata M.Metadata
|
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)
|
s.handler.NewConnection(request.Context(), conn, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) fallbackRequest(ctx context.Context, writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
|
func (s *Server) invalidRequest(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
|
|
||||||
}
|
|
||||||
if statusCode > 0 {
|
if statusCode > 0 {
|
||||||
writer.WriteHeader(statusCode)
|
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 {
|
func (s *Server) Network() []string {
|
||||||
|
|
Loading…
Reference in a new issue