2022-08-22 14:19:25 +00:00
|
|
|
package v2rayhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-09-09 10:45:10 +00:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2022-08-22 14:19:25 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-08-23 11:44:40 +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-22 14:19:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.V2RayServerTransport = (*Server)(nil)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
ctx context.Context
|
|
|
|
handler N.TCPConnectionHandler
|
|
|
|
errorHandler E.Handler
|
|
|
|
httpServer *http.Server
|
2022-11-09 03:49:01 +00:00
|
|
|
h2Server *http2.Server
|
|
|
|
h2cHandler http.Handler
|
2022-08-22 14:19:25 +00:00
|
|
|
host []string
|
|
|
|
path string
|
|
|
|
method string
|
|
|
|
headers http.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Network() []string {
|
|
|
|
return []string{N.NetworkTCP}
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:43:03 +00:00
|
|
|
func NewServer(ctx context.Context, options option.V2RayHTTPOptions, tlsConfig tls.ServerConfig, handler N.TCPConnectionHandler, errorHandler E.Handler) (*Server, error) {
|
2022-08-22 14:19:25 +00:00
|
|
|
server := &Server{
|
|
|
|
ctx: ctx,
|
|
|
|
handler: handler,
|
|
|
|
errorHandler: errorHandler,
|
2022-11-09 03:49:01 +00:00
|
|
|
h2Server: new(http2.Server),
|
2022-08-22 14:19:25 +00:00
|
|
|
host: options.Host,
|
|
|
|
path: options.Path,
|
|
|
|
method: options.Method,
|
|
|
|
headers: make(http.Header),
|
|
|
|
}
|
|
|
|
if server.method == "" {
|
|
|
|
server.method = "PUT"
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(server.path, "/") {
|
|
|
|
server.path = "/" + server.path
|
|
|
|
}
|
|
|
|
for key, value := range options.Headers {
|
|
|
|
server.headers.Set(key, value)
|
|
|
|
}
|
|
|
|
server.httpServer = &http.Server{
|
|
|
|
Handler: server,
|
|
|
|
ReadHeaderTimeout: C.TCPTimeout,
|
|
|
|
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
|
|
|
|
}
|
2022-11-09 03:49:01 +00:00
|
|
|
server.h2cHandler = h2c.NewHandler(server, server.h2Server)
|
2022-09-09 10:45:10 +00:00
|
|
|
if tlsConfig != nil {
|
|
|
|
stdConfig, err := tlsConfig.Config()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
server.httpServer.TLSConfig = stdConfig
|
|
|
|
}
|
|
|
|
return server, nil
|
2022-08-22 14:19:25 +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-22 14:19:25 +00:00
|
|
|
host := request.Host
|
|
|
|
if len(s.host) > 0 && !common.Contains(s.host, host) {
|
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
|
|
|
s.badRequest(request, E.New("bad host: ", host))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(request.URL.Path, s.path) {
|
|
|
|
writer.WriteHeader(http.StatusNotFound)
|
|
|
|
s.badRequest(request, E.New("bad path: ", request.URL.Path))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.Method != s.method {
|
|
|
|
writer.WriteHeader(http.StatusNotFound)
|
|
|
|
s.badRequest(request, E.New("bad method: ", request.Method))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("Cache-Control", "no-store")
|
|
|
|
|
|
|
|
for key, values := range s.headers {
|
|
|
|
for _, value := range values {
|
|
|
|
writer.Header().Set(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
2022-08-26 00:41:45 +00:00
|
|
|
writer.(http.Flusher).Flush()
|
|
|
|
|
2022-08-23 11:44:40 +00:00
|
|
|
var metadata M.Metadata
|
|
|
|
metadata.Source = sHttp.SourceAddress(request)
|
2022-08-22 14:19:25 +00:00
|
|
|
if h, ok := writer.(http.Hijacker); ok {
|
2022-08-22 14:51:08 +00:00
|
|
|
conn, _, err := h.Hijack()
|
2022-08-22 14:19:25 +00:00
|
|
|
if err != nil {
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
s.badRequest(request, E.Cause(err, "hijack conn"))
|
|
|
|
return
|
|
|
|
}
|
2022-08-23 11:44:40 +00:00
|
|
|
s.handler.NewConnection(request.Context(), conn, metadata)
|
2022-08-22 14:19:25 +00:00
|
|
|
} else {
|
|
|
|
conn := &ServerHTTPConn{
|
2022-09-11 02:22:52 +00:00
|
|
|
newHTTPConn(request.Body, writer),
|
2022-08-22 14:19:25 +00:00
|
|
|
writer.(http.Flusher),
|
|
|
|
}
|
2022-08-23 11:44:40 +00:00
|
|
|
s.handler.NewConnection(request.Context(), conn, metadata)
|
2022-08-22 14:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) badRequest(request *http.Request, err error) {
|
|
|
|
s.errorHandler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Serve(listener net.Listener) error {
|
2022-11-09 03:49:01 +00:00
|
|
|
err := http2.ConfigureServer(s.httpServer, s.h2Server)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-22 14:19:25 +00:00
|
|
|
if s.httpServer.TLSConfig == nil {
|
|
|
|
return s.httpServer.Serve(listener)
|
|
|
|
} else {
|
|
|
|
return s.httpServer.ServeTLS(listener, "", "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServePacket(listener net.PacketConn) error {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return common.Close(common.PtrOrNil(s.httpServer))
|
|
|
|
}
|