Xray-core/transport/internet/http/hub.go

253 lines
6.4 KiB
Go
Raw Normal View History

2020-11-25 14:01:53 +03:00
package http
import (
"context"
2024-09-26 04:29:41 +03:00
gotls "crypto/tls"
2020-11-25 14:01:53 +03:00
"io"
"net/http"
"strings"
"time"
2024-09-26 04:29:41 +03:00
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
goreality "github.com/xtls/reality"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net/cnc"
2020-12-04 04:36:16 +03:00
http_proto "github.com/xtls/xray-core/common/protocol/http"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/transport/internet/tls"
2022-05-18 10:29:01 +03:00
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
2020-11-25 14:01:53 +03:00
)
type Listener struct {
2024-09-26 04:29:41 +03:00
server *http.Server
h3server *http3.Server
handler internet.ConnHandler
local net.Addr
config *Config
isH3 bool
2020-11-25 14:01:53 +03:00
}
func (l *Listener) Addr() net.Addr {
return l.local
}
func (l *Listener) Close() error {
2024-09-26 04:29:41 +03:00
if l.h3server != nil {
if err := l.h3server.Close(); err != nil {
return err
}
} else if l.server != nil {
return l.server.Close()
}
return errors.New("listener does not have an HTTP/3 server or h2 server")
2020-11-25 14:01:53 +03:00
}
type flushWriter struct {
w io.Writer
d *done.Instance
}
func (fw flushWriter) Write(p []byte) (n int, err error) {
if fw.d.Done() {
return 0, io.ErrClosedPipe
}
defer func() {
if recover() != nil {
fw.d.Close()
err = io.ErrClosedPipe
}
}()
2020-11-25 14:01:53 +03:00
n, err = fw.w.Write(p)
if f, ok := fw.w.(http.Flusher); ok && err == nil {
2020-11-25 14:01:53 +03:00
f.Flush()
}
return
}
func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
host := request.Host
if !l.config.isValidHost(host) {
writer.WriteHeader(404)
return
}
path := l.config.getNormalizedPath()
if !strings.HasPrefix(request.URL.Path, path) {
writer.WriteHeader(404)
return
}
writer.Header().Set("Cache-Control", "no-store")
for _, httpHeader := range l.config.Header {
for _, httpHeaderValue := range httpHeader.Value {
writer.Header().Set(httpHeader.Name, httpHeaderValue)
}
}
2020-11-25 14:01:53 +03:00
writer.WriteHeader(200)
if f, ok := writer.(http.Flusher); ok {
f.Flush()
}
remoteAddr := l.Addr()
dest, err := net.ParseDestination(request.RemoteAddr)
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to parse request remote addr: ", request.RemoteAddr)
2020-11-25 14:01:53 +03:00
} else {
remoteAddr = &net.TCPAddr{
IP: dest.Address.IP(),
Port: int(dest.Port),
}
}
2021-01-22 06:35:56 +03:00
forwardedAddress := http_proto.ParseXForwardedFor(request.Header)
if len(forwardedAddress) > 0 && forwardedAddress[0].Family().IsIP() {
2020-11-25 14:01:53 +03:00
remoteAddr = &net.TCPAddr{
2021-01-22 06:35:56 +03:00
IP: forwardedAddress[0].IP(),
Port: 0,
2020-11-25 14:01:53 +03:00
}
}
done := done.New()
conn := cnc.NewConnection(
cnc.ConnectionOutput(request.Body),
cnc.ConnectionInput(flushWriter{w: writer, d: done}),
cnc.ConnectionOnClose(common.ChainedClosable{done, request.Body}),
cnc.ConnectionLocalAddr(l.Addr()),
cnc.ConnectionRemoteAddr(remoteAddr),
2020-11-25 14:01:53 +03:00
)
l.handler(conn)
<-done.Wait()
}
func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
httpSettings := streamSettings.ProtocolSettings.(*Config)
config := tls.ConfigFromStreamSettings(streamSettings)
2024-09-26 04:29:41 +03:00
var tlsConfig *gotls.Config
2020-11-25 14:01:53 +03:00
if config == nil {
2024-09-26 04:29:41 +03:00
tlsConfig = &gotls.Config{}
} else {
tlsConfig = config.GetTLSConfig()
}
isH3 := len(tlsConfig.NextProtos) == 1 && tlsConfig.NextProtos[0] == "h3"
listener := &Listener{
handler: handler,
2024-11-09 14:16:11 +03:00
config: httpSettings,
isH3: isH3,
2024-09-26 04:29:41 +03:00
}
if port == net.Port(0) { // unix
listener.local = &net.UnixAddr{
Name: address.Domain(),
Net: "unix",
}
} else if isH3 { // udp
listener.local = &net.UDPAddr{
IP: address.IP(),
Port: int(port),
2020-11-25 14:01:53 +03:00
}
} else {
2024-09-26 04:29:41 +03:00
listener.local = &net.TCPAddr{
IP: address.IP(),
Port: int(port),
2020-11-25 14:01:53 +03:00
}
}
if streamSettings.SocketSettings != nil && streamSettings.SocketSettings.AcceptProxyProtocol {
errors.LogWarning(ctx, "accepting PROXY protocol")
2020-11-25 14:01:53 +03:00
}
2024-09-26 04:29:41 +03:00
if isH3 {
Conn, err := internet.ListenSystemPacket(context.Background(), listener.local, streamSettings.SocketSettings)
if err != nil {
2024-11-09 14:16:11 +03:00
return nil, errors.New("failed to listen UDP(for SH3) on ", address, ":", port).Base(err)
2020-11-25 14:01:53 +03:00
}
2024-09-26 04:29:41 +03:00
h3listener, err := quic.ListenEarly(Conn, tlsConfig, nil)
if err != nil {
return nil, errors.New("failed to listen QUIC(for SH3) on ", address, ":", port).Base(err)
}
errors.LogInfo(ctx, "listening QUIC(for SH3) on ", address, ":", port)
2020-11-25 14:01:53 +03:00
2024-09-26 04:29:41 +03:00
listener.h3server = &http3.Server{
Handler: listener,
}
go func() {
if err := listener.h3server.ServeListener(h3listener); err != nil {
errors.LogWarningInner(ctx, err, "failed to serve http3 for splithttp")
}
2024-09-26 04:29:41 +03:00
}()
} else {
var server *http.Server
if config == nil {
h2s := &http2.Server{}
2024-11-09 14:16:11 +03:00
2024-09-26 04:29:41 +03:00
server = &http.Server{
Addr: serial.Concat(address, ":", port),
Handler: h2c.NewHandler(listener, h2s),
ReadHeaderTimeout: time.Second * 4,
2020-11-25 14:01:53 +03:00
}
} else {
2024-09-26 04:29:41 +03:00
server = &http.Server{
Addr: serial.Concat(address, ":", port),
TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")),
Handler: listener,
ReadHeaderTimeout: time.Second * 4,
2020-11-25 14:01:53 +03:00
}
}
2024-11-09 14:16:11 +03:00
2024-09-26 04:29:41 +03:00
listener.server = server
go func() {
var streamListener net.Listener
var err error
if port == net.Port(0) { // unix
streamListener, err = internet.ListenSystem(ctx, &net.UnixAddr{
Name: address.Domain(),
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
errors.LogErrorInner(ctx, err, "failed to listen on ", address)
return
}
} else { // tcp
streamListener, err = internet.ListenSystem(ctx, &net.TCPAddr{
IP: address.IP(),
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
errors.LogErrorInner(ctx, err, "failed to listen on ", address, ":", port)
return
}
}
2024-11-09 14:16:11 +03:00
2024-09-26 04:29:41 +03:00
if config == nil {
if config := reality.ConfigFromStreamSettings(streamSettings); config != nil {
streamListener = goreality.NewListener(streamListener, config.GetREALITYConfig())
}
err = server.Serve(streamListener)
if err != nil {
errors.LogInfoInner(ctx, err, "stopping serving H2C or REALITY H2")
}
} else {
err = server.ServeTLS(streamListener, "", "")
if err != nil {
errors.LogInfoInner(ctx, err, "stopping serving TLS H2")
}
}
2024-11-09 14:16:11 +03:00
}()
2024-09-26 04:29:41 +03:00
}
2020-11-25 14:01:53 +03:00
return listener, nil
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, Listen))
}