sing-box/transport/v2raywebsocket/server.go

145 lines
4.3 KiB
Go
Raw Normal View History

2022-08-22 20:20:56 +08:00
package v2raywebsocket
import (
"context"
"encoding/base64"
"net"
"net/http"
2022-08-22 21:20:05 +08:00
"os"
2022-08-22 20:20:56 +08:00
"strings"
"github.com/sagernet/sing-box/adapter"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
2022-08-22 20:20:56 +08:00
C "github.com/sagernet/sing-box/constant"
2024-10-21 23:38:34 +08:00
"github.com/sagernet/sing-box/log"
2022-08-22 20:20:56 +08:00
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
2024-10-21 23:38:34 +08:00
"github.com/sagernet/sing/common/logger"
2022-08-22 20:20:56 +08:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2023-03-11 15:05:07 +08:00
aTLS "github.com/sagernet/sing/common/tls"
2022-08-23 19:44:40 +08:00
sHttp "github.com/sagernet/sing/protocol/http"
2023-11-09 16:59:44 +08:00
"github.com/sagernet/ws"
2022-08-22 20:20:56 +08:00
)
var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
2024-10-21 23:38:34 +08:00
logger logger.ContextLogger
2023-03-11 15:05:07 +08:00
tlsConfig tls.ServerConfig
handler adapter.V2RayServerTransportHandler
2022-08-22 20:20:56 +08:00
httpServer *http.Server
path string
maxEarlyData uint32
earlyDataHeaderName string
upgrader ws.HTTPUpgrader
2022-08-22 20:20:56 +08:00
}
2024-10-21 23:38:34 +08:00
func NewServer(ctx context.Context, logger logger.ContextLogger, options option.V2RayWebsocketOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
2022-08-22 20:20:56 +08:00
server := &Server{
ctx: ctx,
2024-10-21 23:38:34 +08:00
logger: logger,
2023-03-11 15:05:07 +08:00
tlsConfig: tlsConfig,
2022-08-22 20:20:56 +08:00
handler: handler,
path: options.Path,
maxEarlyData: options.MaxEarlyData,
earlyDataHeaderName: options.EarlyDataHeaderName,
upgrader: ws.HTTPUpgrader{
Timeout: C.TCPTimeout,
Header: options.Headers.Build(),
},
2022-08-22 20:20:56 +08:00
}
if !strings.HasPrefix(server.path, "/") {
server.path = "/" + server.path
}
server.httpServer = &http.Server{
Handler: server,
ReadHeaderTimeout: C.TCPTimeout,
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
2023-04-08 18:46:35 +08:00
BaseContext: func(net.Listener) context.Context {
return ctx
},
2024-10-21 23:38:34 +08:00
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return log.ContextWithNewID(ctx)
},
2022-08-22 20:20:56 +08:00
}
2022-09-09 18:45:10 +08:00
return server, nil
2022-08-22 20:20:56 +08:00
}
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if s.maxEarlyData == 0 || s.earlyDataHeaderName != "" {
if request.URL.Path != s.path {
2023-09-28 15:51:33 +08:00
s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
2022-08-22 20:20:56 +08:00
return
}
}
var (
earlyData []byte
err error
conn net.Conn
)
if s.earlyDataHeaderName == "" {
if strings.HasPrefix(request.URL.RequestURI(), s.path) {
earlyDataStr := request.URL.RequestURI()[len(s.path):]
earlyData, err = base64.RawURLEncoding.DecodeString(earlyDataStr)
} else {
2023-09-28 15:51:33 +08:00
s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
2022-08-22 20:20:56 +08:00
return
}
} else {
if request.URL.Path != s.path {
s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
return
}
2022-08-22 20:20:56 +08:00
earlyDataStr := request.Header.Get(s.earlyDataHeaderName)
if earlyDataStr != "" {
earlyData, err = base64.RawURLEncoding.DecodeString(earlyDataStr)
}
}
if err != nil {
2023-09-28 15:51:33 +08:00
s.invalidRequest(writer, request, http.StatusBadRequest, E.Cause(err, "decode early data"))
2022-08-22 20:20:56 +08:00
return
}
2023-12-01 12:24:29 +08:00
wsConn, _, _, err := ws.UpgradeHTTP(request, writer)
2022-08-22 20:20:56 +08:00
if err != nil {
2023-09-28 15:51:33 +08:00
s.invalidRequest(writer, request, 0, E.Cause(err, "upgrade websocket connection"))
2022-08-22 20:20:56 +08:00
return
}
2024-10-21 23:38:34 +08:00
source := sHttp.SourceAddress(request)
conn = NewConn(wsConn, source, ws.StateServerSide)
2022-08-22 20:20:56 +08:00
if len(earlyData) > 0 {
conn = bufio.NewCachedConn(conn, buf.As(earlyData))
}
2024-10-21 23:38:34 +08:00
s.handler.NewConnectionEx(request.Context(), conn, source, M.Socksaddr{}, nil)
2022-08-22 20:20:56 +08:00
}
2023-09-28 15:51:33 +08:00
func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
if statusCode > 0 {
writer.WriteHeader(statusCode)
}
2024-10-21 23:38:34 +08:00
s.logger.ErrorContext(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
2022-08-22 20:20:56 +08:00
}
2022-08-22 21:20:05 +08:00
func (s *Server) Network() []string {
return []string{N.NetworkTCP}
}
2022-08-22 20:20:56 +08:00
func (s *Server) Serve(listener net.Listener) error {
2023-03-11 15:05:07 +08:00
if s.tlsConfig != nil {
listener = aTLS.NewListener(listener, s.tlsConfig)
2022-08-22 20:20:56 +08:00
}
2023-03-11 15:05:07 +08:00
return s.httpServer.Serve(listener)
2022-08-22 20:20:56 +08:00
}
2022-08-22 21:20:05 +08:00
func (s *Server) ServePacket(listener net.PacketConn) error {
return os.ErrInvalid
}
2022-08-22 20:20:56 +08:00
func (s *Server) Close() error {
2022-08-22 22:19:25 +08:00
return common.Close(common.PtrOrNil(s.httpServer))
2022-08-22 20:20:56 +08:00
}