2022-08-27 13:05:15 +00:00
|
|
|
package v2raygrpclite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-03-11 07:49:02 +00:00
|
|
|
"time"
|
2022-08-27 13:05:15 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-09-09 10:45:10 +00:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2022-08-27 13:05:15 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-01-04 07:19:39 +00:00
|
|
|
"github.com/sagernet/sing-box/transport/v2rayhttp"
|
2022-08-27 13:05:15 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2023-04-08 14:37:10 +00:00
|
|
|
"github.com/sagernet/sing/common/bufio/deadline"
|
2022-08-27 13:05:15 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2023-03-11 07:05:07 +00:00
|
|
|
aTLS "github.com/sagernet/sing/common/tls"
|
2022-08-27 13:05:15 +00:00
|
|
|
sHttp "github.com/sagernet/sing/protocol/http"
|
2022-09-11 02:22:52 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/http2"
|
2022-11-09 03:49:01 +00:00
|
|
|
"golang.org/x/net/http2/h2c"
|
2022-08-27 13:05:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-03-11 07:05:07 +00:00
|
|
|
tlsConfig tls.ServerConfig
|
2023-02-12 21:53:03 +00:00
|
|
|
handler adapter.V2RayServerTransportHandler
|
2022-08-27 13:05:15 +00:00
|
|
|
errorHandler E.Handler
|
|
|
|
httpServer *http.Server
|
2022-11-09 03:49:01 +00:00
|
|
|
h2Server *http2.Server
|
|
|
|
h2cHandler http.Handler
|
2022-08-27 13:05:15 +00:00
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Network() []string {
|
|
|
|
return []string{N.NetworkTCP}
|
|
|
|
}
|
|
|
|
|
2023-02-12 21:53:03 +00:00
|
|
|
func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
|
2022-08-27 13:05:15 +00:00
|
|
|
server := &Server{
|
2023-03-11 07:05:07 +00:00
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
handler: handler,
|
2023-04-08 09:44:11 +00:00
|
|
|
path: "/" + options.ServiceName + "/Tun",
|
2023-03-11 07:49:02 +00:00
|
|
|
h2Server: &http2.Server{
|
|
|
|
IdleTimeout: time.Duration(options.IdleTimeout),
|
|
|
|
},
|
2022-08-27 13:05:15 +00:00
|
|
|
}
|
2022-09-09 10:45:10 +00:00
|
|
|
server.httpServer = &http.Server{
|
|
|
|
Handler: server,
|
2023-04-08 10:46:35 +00:00
|
|
|
BaseContext: func(net.Listener) context.Context {
|
|
|
|
return ctx
|
|
|
|
},
|
2022-09-09 10:45:10 +00:00
|
|
|
}
|
2022-11-09 03:49:01 +00:00
|
|
|
server.h2cHandler = h2c.NewHandler(server, server.h2Server)
|
2022-09-09 10:45:10 +00:00
|
|
|
return server, nil
|
2022-08-27 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
2022-11-09 03:49:01 +00:00
|
|
|
if request.Method == "PRI" && len(request.Header) == 0 && request.URL.Path == "*" && request.Proto == "HTTP/2.0" {
|
|
|
|
s.h2cHandler.ServeHTTP(writer, request)
|
|
|
|
return
|
|
|
|
}
|
2022-08-27 13:05:15 +00:00
|
|
|
if request.URL.Path != s.path {
|
2023-02-12 21:53:03 +00:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
|
2022-08-27 13:05:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.Method != http.MethodPost {
|
2023-02-12 21:53:03 +00:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad method: ", request.Method))
|
2022-08-27 13:05:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ct := request.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/grpc") {
|
2023-02-12 21:53:03 +00:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad content type: ", ct))
|
2022-08-27 13:05:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
writer.Header().Set("Content-Type", "application/grpc")
|
|
|
|
writer.Header().Set("TE", "trailers")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
var metadata M.Metadata
|
|
|
|
metadata.Source = sHttp.SourceAddress(request)
|
2023-01-04 07:19:39 +00:00
|
|
|
conn := v2rayhttp.NewHTTP2Wrapper(newGunConn(request.Body, writer, writer.(http.Flusher)))
|
2023-04-08 14:37:10 +00:00
|
|
|
s.handler.NewConnection(request.Context(), deadline.NewConn(conn), metadata)
|
2023-01-04 07:19:39 +00:00
|
|
|
conn.CloseWrapper()
|
2022-08-27 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 21:53:03 +00:00
|
|
|
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
|
|
|
|
}
|
2023-04-09 11:31:28 +00:00
|
|
|
if statusCode > 0 {
|
|
|
|
writer.WriteHeader(statusCode)
|
|
|
|
}
|
2023-02-12 21:53:03 +00:00
|
|
|
s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr))
|
2022-08-27 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Serve(listener net.Listener) error {
|
2023-03-11 07:05:07 +00:00
|
|
|
if s.tlsConfig != nil {
|
2023-04-11 03:38:53 +00:00
|
|
|
if !common.Contains(s.tlsConfig.NextProtos(), http2.NextProtoTLS) {
|
|
|
|
s.tlsConfig.SetNextProtos(append([]string{"h2"}, s.tlsConfig.NextProtos()...))
|
2023-03-11 07:05:07 +00:00
|
|
|
}
|
|
|
|
listener = aTLS.NewListener(listener, s.tlsConfig)
|
2022-08-27 13:05:15 +00:00
|
|
|
}
|
2023-03-11 07:05:07 +00:00
|
|
|
return s.httpServer.Serve(listener)
|
2022-08-27 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServePacket(listener net.PacketConn) error {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return common.Close(common.PtrOrNil(s.httpServer))
|
|
|
|
}
|