server.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package v2raygrpclite
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "time"
  9. "github.com/sagernet/sing-box/adapter"
  10. "github.com/sagernet/sing-box/common/tls"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing-box/transport/v2rayhttp"
  13. "github.com/sagernet/sing/common"
  14. E "github.com/sagernet/sing/common/exceptions"
  15. M "github.com/sagernet/sing/common/metadata"
  16. N "github.com/sagernet/sing/common/network"
  17. aTLS "github.com/sagernet/sing/common/tls"
  18. sHttp "github.com/sagernet/sing/protocol/http"
  19. "golang.org/x/net/http2"
  20. "golang.org/x/net/http2/h2c"
  21. )
  22. var _ adapter.V2RayServerTransport = (*Server)(nil)
  23. type Server struct {
  24. tlsConfig tls.ServerConfig
  25. handler adapter.V2RayServerTransportHandler
  26. errorHandler E.Handler
  27. httpServer *http.Server
  28. h2Server *http2.Server
  29. h2cHandler http.Handler
  30. path string
  31. }
  32. func (s *Server) Network() []string {
  33. return []string{N.NetworkTCP}
  34. }
  35. func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
  36. server := &Server{
  37. tlsConfig: tlsConfig,
  38. handler: handler,
  39. path: "/" + options.ServiceName + "/Tun",
  40. h2Server: &http2.Server{
  41. IdleTimeout: time.Duration(options.IdleTimeout),
  42. },
  43. }
  44. server.httpServer = &http.Server{
  45. Handler: server,
  46. BaseContext: func(net.Listener) context.Context {
  47. return ctx
  48. },
  49. }
  50. server.h2cHandler = h2c.NewHandler(server, server.h2Server)
  51. return server, nil
  52. }
  53. func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
  54. if request.Method == "PRI" && len(request.Header) == 0 && request.URL.Path == "*" && request.Proto == "HTTP/2.0" {
  55. s.h2cHandler.ServeHTTP(writer, request)
  56. return
  57. }
  58. if request.URL.Path != s.path {
  59. s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
  60. return
  61. }
  62. if request.Method != http.MethodPost {
  63. s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad method: ", request.Method))
  64. return
  65. }
  66. if ct := request.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/grpc") {
  67. s.invalidRequest(writer, request, http.StatusNotFound, E.New("bad content type: ", ct))
  68. return
  69. }
  70. writer.Header().Set("Content-Type", "application/grpc")
  71. writer.Header().Set("TE", "trailers")
  72. writer.WriteHeader(http.StatusOK)
  73. var metadata M.Metadata
  74. metadata.Source = sHttp.SourceAddress(request)
  75. conn := v2rayhttp.NewHTTP2Wrapper(newGunConn(request.Body, writer, writer.(http.Flusher)))
  76. s.handler.NewConnection(request.Context(), conn, metadata)
  77. conn.CloseWrapper()
  78. }
  79. func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) {
  80. if statusCode > 0 {
  81. writer.WriteHeader(statusCode)
  82. }
  83. s.handler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
  84. }
  85. func (s *Server) Serve(listener net.Listener) error {
  86. if s.tlsConfig != nil {
  87. if !common.Contains(s.tlsConfig.NextProtos(), http2.NextProtoTLS) {
  88. s.tlsConfig.SetNextProtos(append([]string{"h2"}, s.tlsConfig.NextProtos()...))
  89. }
  90. listener = aTLS.NewListener(listener, s.tlsConfig)
  91. }
  92. return s.httpServer.Serve(listener)
  93. }
  94. func (s *Server) ServePacket(listener net.PacketConn) error {
  95. return os.ErrInvalid
  96. }
  97. func (s *Server) Close() error {
  98. return common.Close(common.PtrOrNil(s.httpServer))
  99. }