2022-08-22 22:19:25 +08:00
|
|
|
package v2rayhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-03-11 15:49:02 +08:00
|
|
|
"time"
|
2022-08-22 22:19:25 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-09-09 18:45:10 +08:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2022-08-22 22:19:25 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
"github.com/sagernet/sing/common"
|
2023-04-07 18:20:07 +08:00
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2022-08-22 22:19:25 +08: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 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"
|
2022-09-11 10:22:52 +08:00
|
|
|
|
|
|
|
"golang.org/x/net/http2"
|
2022-11-09 11:49:01 +08:00
|
|
|
"golang.org/x/net/http2/h2c"
|
2022-08-22 22:19:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-02-13 05:53:03 +08:00
|
|
|
ctx context.Context
|
2023-03-11 15:05:07 +08:00
|
|
|
tlsConfig tls.ServerConfig
|
2023-02-13 05:53:03 +08:00
|
|
|
handler adapter.V2RayServerTransportHandler
|
|
|
|
httpServer *http.Server
|
|
|
|
h2Server *http2.Server
|
|
|
|
h2cHandler http.Handler
|
|
|
|
host []string
|
|
|
|
path string
|
|
|
|
method string
|
|
|
|
headers http.Header
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Network() []string {
|
|
|
|
return []string{N.NetworkTCP}
|
|
|
|
}
|
|
|
|
|
2023-02-13 05:53:03 +08:00
|
|
|
func NewServer(ctx context.Context, options option.V2RayHTTPOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (*Server, error) {
|
2022-08-22 22:19:25 +08:00
|
|
|
server := &Server{
|
2023-03-11 15:05:07 +08:00
|
|
|
ctx: ctx,
|
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
handler: handler,
|
2023-03-11 15:49:02 +08:00
|
|
|
h2Server: &http2.Server{
|
|
|
|
IdleTimeout: time.Duration(options.IdleTimeout),
|
|
|
|
},
|
|
|
|
host: options.Host,
|
|
|
|
path: options.Path,
|
|
|
|
method: options.Method,
|
|
|
|
headers: make(http.Header),
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
|
|
|
if server.method == "" {
|
|
|
|
server.method = "PUT"
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(server.path, "/") {
|
|
|
|
server.path = "/" + server.path
|
|
|
|
}
|
|
|
|
for key, value := range options.Headers {
|
2023-04-07 18:20:07 +08:00
|
|
|
server.headers[key] = value
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
|
|
|
server.httpServer = &http.Server{
|
|
|
|
Handler: server,
|
|
|
|
ReadHeaderTimeout: C.TCPTimeout,
|
|
|
|
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
|
|
|
|
}
|
2022-11-09 11:49:01 +08:00
|
|
|
server.h2cHandler = h2c.NewHandler(server, server.h2Server)
|
2022-09-09 18:45:10 +08:00
|
|
|
return server, nil
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
2022-11-09 11:49:01 +08: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-22 22:19:25 +08:00
|
|
|
host := request.Host
|
|
|
|
if len(s.host) > 0 && !common.Contains(s.host, host) {
|
2023-02-13 05:53:03 +08:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, http.StatusBadRequest, E.New("bad host: ", host))
|
2022-08-22 22:19:25 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(request.URL.Path, s.path) {
|
2023-02-13 05:53:03 +08:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad path: ", request.URL.Path))
|
2022-08-22 22:19:25 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.Method != s.method {
|
2023-02-13 05:53:03 +08:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, http.StatusNotFound, E.New("bad method: ", request.Method))
|
2022-08-22 22:19:25 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("Cache-Control", "no-store")
|
|
|
|
|
|
|
|
for key, values := range s.headers {
|
|
|
|
for _, value := range values {
|
|
|
|
writer.Header().Set(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 19:44:40 +08:00
|
|
|
var metadata M.Metadata
|
|
|
|
metadata.Source = sHttp.SourceAddress(request)
|
2022-08-22 22:19:25 +08:00
|
|
|
if h, ok := writer.(http.Hijacker); ok {
|
2023-04-11 10:30:10 +08:00
|
|
|
var requestBody *buf.Buffer
|
|
|
|
if contentLength := int(request.ContentLength); contentLength > 0 {
|
|
|
|
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"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
writer.(http.Flusher).Flush()
|
2023-04-07 18:20:07 +08:00
|
|
|
conn, reader, err := h.Hijack()
|
2022-08-22 22:19:25 +08:00
|
|
|
if err != nil {
|
2023-04-09 19:31:28 +08:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "hijack conn"))
|
2022-08-22 22:19:25 +08:00
|
|
|
return
|
|
|
|
}
|
2023-04-07 18:20:07 +08:00
|
|
|
if cacheLen := reader.Reader.Buffered(); cacheLen > 0 {
|
|
|
|
cache := buf.NewSize(cacheLen)
|
|
|
|
_, err = cache.ReadFullFrom(reader.Reader, cacheLen)
|
|
|
|
if err != nil {
|
2023-04-09 19:31:28 +08:00
|
|
|
s.fallbackRequest(request.Context(), writer, request, 0, E.Cause(err, "read cache"))
|
2023-04-07 18:20:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
conn = bufio.NewCachedConn(conn, cache)
|
|
|
|
}
|
2023-04-11 10:30:10 +08:00
|
|
|
if requestBody != nil {
|
|
|
|
conn = bufio.NewCachedConn(conn, requestBody)
|
|
|
|
}
|
2022-08-23 19:44:40 +08:00
|
|
|
s.handler.NewConnection(request.Context(), conn, metadata)
|
2022-08-22 22:19:25 +08:00
|
|
|
} else {
|
2023-04-11 10:30:10 +08:00
|
|
|
writer.WriteHeader(http.StatusOK)
|
2023-01-04 15:19:39 +08:00
|
|
|
conn := NewHTTP2Wrapper(&ServerHTTPConn{
|
2023-02-13 05:53:03 +08:00
|
|
|
NewHTTPConn(request.Body, writer),
|
2022-08-22 22:19:25 +08:00
|
|
|
writer.(http.Flusher),
|
2023-01-04 15:19:39 +08:00
|
|
|
})
|
2022-08-23 19:44:40 +08:00
|
|
|
s.handler.NewConnection(request.Context(), conn, metadata)
|
2023-01-04 15:19:39 +08:00
|
|
|
conn.CloseWrapper()
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 05:53:03 +08:00
|
|
|
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
|
|
|
|
}
|
2023-04-09 19:31:28 +08:00
|
|
|
if statusCode > 0 {
|
|
|
|
writer.WriteHeader(statusCode)
|
|
|
|
}
|
2023-02-13 05:53:03 +08:00
|
|
|
s.handler.NewError(request.Context(), E.Cause(E.Errors(err, E.Cause(fErr, "fallback connection")), "process connection from ", request.RemoteAddr))
|
2022-08-22 22:19:25 +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 22:19:25 +08:00
|
|
|
}
|
2023-03-11 15:05:07 +08:00
|
|
|
return s.httpServer.Serve(listener)
|
2022-08-22 22:19:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServePacket(listener net.PacketConn) error {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return common.Close(common.PtrOrNil(s.httpServer))
|
|
|
|
}
|