package redirect import ( "bufio" "bytes" "context" "crypto/tls" "encoding/binary" "fmt" "io" "net" "net/http" "net/http/httputil" "strings" "time" "github.com/go-gost/core/bypass" "github.com/go-gost/core/handler" "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" "github.com/go-gost/core/recorder" dissector "github.com/go-gost/tls-dissector" xbypass "github.com/go-gost/x/bypass" ctxvalue "github.com/go-gost/x/ctx" xio "github.com/go-gost/x/internal/io" netpkg "github.com/go-gost/x/internal/net" xhttp "github.com/go-gost/x/internal/net/http" tls_util "github.com/go-gost/x/internal/util/tls" rate_limiter "github.com/go-gost/x/limiter/rate" xrecorder "github.com/go-gost/x/recorder" "github.com/go-gost/x/registry" ) const ( defaultBodySize = 1024 * 1024 // 1MB ) func init() { registry.HandlerRegistry().Register("red", NewHandler) registry.HandlerRegistry().Register("redir", NewHandler) registry.HandlerRegistry().Register("redirect", NewHandler) } type redirectHandler struct { md metadata options handler.Options recorder recorder.RecorderObject } func NewHandler(opts ...handler.Option) handler.Handler { options := handler.Options{} for _, opt := range opts { opt(&options) } return &redirectHandler{ options: options, } } func (h *redirectHandler) Init(md md.Metadata) (err error) { if err = h.parseMetadata(md); err != nil { return } for _, ro := range h.options.Recorders { if ro.Record == xrecorder.RecorderServiceHandler { h.recorder = ro break } } return } func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) (err error) { defer conn.Close() start := time.Now() ro := &xrecorder.HandlerRecorderObject{ Service: h.options.Service, Network: "tcp", RemoteAddr: conn.RemoteAddr().String(), LocalAddr: conn.LocalAddr().String(), Time: start, SID: string(ctxvalue.SidFromContext(ctx)), } ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String()) log := h.options.Logger.WithFields(map[string]any{ "remote": conn.RemoteAddr().String(), "local": conn.LocalAddr().String(), "sid": ctxvalue.SidFromContext(ctx), }) log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr()) defer func() { if !ro.Time.IsZero() { if err != nil { ro.Err = err.Error() } ro.Duration = time.Since(start) if err := ro.Record(ctx, h.recorder.Recorder); err != nil { log.Errorf("record: %v", err) } } log.WithFields(map[string]any{ "duration": time.Since(start), }).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr()) }() if !h.checkRateLimit(conn.RemoteAddr()) { return rate_limiter.ErrRateLimit } var dstAddr net.Addr if h.md.tproxy { dstAddr = conn.LocalAddr() } else { dstAddr, err = h.getOriginalDstAddr(conn) if err != nil { log.Error(err) return } } ro.Host = dstAddr.String() log = log.WithFields(map[string]any{ "dst": fmt.Sprintf("%s/%s", dstAddr, dstAddr.Network()), }) var rw io.ReadWriter = conn if h.md.sniffing { if h.md.sniffingTimeout > 0 { conn.SetReadDeadline(time.Now().Add(h.md.sniffingTimeout)) } // try to sniff TLS traffic var hdr [dissector.RecordHeaderLen]byte n, err := io.ReadFull(rw, hdr[:]) if h.md.sniffingTimeout > 0 { conn.SetReadDeadline(time.Time{}) } rw = xio.NewReadWriter(io.MultiReader(bytes.NewReader(hdr[:n]), rw), rw) tlsVersion := binary.BigEndian.Uint16(hdr[1:3]) if err == nil && hdr[0] == dissector.Handshake && (tlsVersion >= tls.VersionTLS10 && tlsVersion <= tls.VersionTLS13) { return h.handleHTTPS(ctx, rw, conn.RemoteAddr(), dstAddr, ro, log) } // try to sniff HTTP traffic if isHTTP(string(hdr[:])) { return h.handleHTTP(ctx, rw, conn.RemoteAddr(), dstAddr, ro, log) } } log.Debugf("%s >> %s", conn.RemoteAddr(), dstAddr) if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, dstAddr.Network(), dstAddr.String()) { log.Debug("bypass: ", dstAddr) return xbypass.ErrBypass } cc, err := h.options.Router.Dial(ctx, dstAddr.Network(), dstAddr.String()) if err != nil { log.Error(err) return err } defer cc.Close() t := time.Now() log.Infof("%s <-> %s", conn.RemoteAddr(), dstAddr) netpkg.Transport(rw, cc) log.WithFields(map[string]any{ "duration": time.Since(t), }).Infof("%s >-< %s", conn.RemoteAddr(), dstAddr) return nil } func (h *redirectHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, raddr, dstAddr net.Addr, ro *xrecorder.HandlerRecorderObject, log logger.Logger) error { req, err := http.ReadRequest(bufio.NewReader(rw)) if err != nil { return err } ro.HTTP = &xrecorder.HTTPRecorderObject{ Host: req.Host, Proto: req.Proto, Scheme: req.URL.Scheme, Method: req.Method, URI: req.RequestURI, Request: xrecorder.HTTPRequestRecorderObject{ ContentLength: req.ContentLength, Header: req.Header, }, } if clientIP := xhttp.GetClientIP(req); clientIP != nil { ro.ClientIP = clientIP.String() } if log.IsLevelEnabled(logger.TraceLevel) { dump, _ := httputil.DumpRequest(req, false) log.Trace(string(dump)) } ro.Host = req.Host host := req.Host if _, _, err := net.SplitHostPort(host); err != nil { host = net.JoinHostPort(strings.Trim(host, "[]"), "80") } log = log.WithFields(map[string]any{ "host": host, }) if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, "tcp", host, bypass.WithPathOption(req.RequestURI)) { log.Debugf("bypass: %s %s", host, req.RequestURI) return xbypass.ErrBypass } cc, err := h.options.Router.Dial(ctx, "tcp", host) if err != nil { log.Error(err) if !h.md.sniffingFallback { return err } } if cc == nil { cc, err = h.options.Router.Dial(ctx, "tcp", dstAddr.String()) if err != nil { log.Error(err) return err } } defer cc.Close() t := time.Now() log.Infof("%s <-> %s", raddr, host) defer func() { log.WithFields(map[string]any{ "duration": time.Since(t), }).Infof("%s >-< %s", raddr, host) }() var reqBody *xhttp.Body if opts := h.recorder.Options; opts != nil && opts.HTTPBody { if req.Body != nil { maxSize := opts.MaxBodySize if maxSize <= 0 { maxSize = defaultBodySize } reqBody = xhttp.NewBody(req.Body, maxSize) req.Body = reqBody } } if err := req.Write(cc); err != nil { log.Error(err) return err } if reqBody != nil { ro.HTTP.Request.Body = reqBody.Content() ro.HTTP.Request.ContentLength = reqBody.Length() } br := bufio.NewReader(cc) resp, err := http.ReadResponse(br, req) if err != nil { log.Error(err) return err } defer resp.Body.Close() ro.HTTP.Response.Header = resp.Header ro.HTTP.StatusCode = resp.StatusCode ro.HTTP.Response.ContentLength = resp.ContentLength if log.IsLevelEnabled(logger.TraceLevel) { dump, _ := httputil.DumpResponse(resp, false) log.Trace(string(dump)) } var respBody *xhttp.Body if opts := h.recorder.Options; opts != nil && opts.HTTPBody { maxSize := opts.MaxBodySize if maxSize <= 0 { maxSize = defaultBodySize } respBody = xhttp.NewBody(resp.Body, maxSize) resp.Body = respBody } if err := resp.Write(rw); err != nil { log.Error(err) return err } if respBody != nil { ro.HTTP.Response.Body = respBody.Content() ro.HTTP.Response.ContentLength = resp.ContentLength } netpkg.Transport(rw, xio.NewReadWriter(br, cc)) return nil } func (h *redirectHandler) handleHTTPS(ctx context.Context, rw io.ReadWriter, raddr, dstAddr net.Addr, ro *xrecorder.HandlerRecorderObject, log logger.Logger) error { buf := new(bytes.Buffer) clientHello, err := dissector.ParseClientHello(io.TeeReader(rw, buf)) if err != nil { return err } ro.TLS = &xrecorder.TLSRecorderObject{ ServerName: clientHello.ServerName, } if len(clientHello.SupportedProtos) > 0 { ro.TLS.Proto = clientHello.SupportedProtos[0] } var cc io.ReadWriteCloser host := clientHello.ServerName if host != "" { if _, _, err := net.SplitHostPort(host); err != nil { _, port, _ := net.SplitHostPort(dstAddr.String()) if port == "" { port = "443" } host = net.JoinHostPort(strings.Trim(host, "[]"), port) } log = log.WithFields(map[string]any{ "host": host, }) ro.Host = host if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, "tcp", host) { log.Debug("bypass: ", host) return xbypass.ErrBypass } cc, err = h.options.Router.Dial(ctx, "tcp", host) if err != nil { log.Error(err) if !h.md.sniffingFallback { return err } } } if cc == nil { cc, err = h.options.Router.Dial(ctx, "tcp", dstAddr.String()) if err != nil { log.Error(err) return err } } defer cc.Close() if _, err := buf.WriteTo(cc); err != nil { return err } if serverHello, _ := dissector.ParseServerHello(io.TeeReader(cc, buf)); serverHello != nil { ro.TLS.CipherSuite = tls_util.CipherSuite(serverHello.CipherSuite).String() ro.TLS.CompressionMethod = serverHello.CompressionMethod if serverHello.Proto != "" { ro.TLS.Proto = serverHello.Proto } if serverHello.Version > 0 { ro.TLS.Version = tls_util.Version(serverHello.Version).String() } } if _, err := buf.WriteTo(rw); err != nil { return err } t := time.Now() log.Infof("%s <-> %s", raddr, host) netpkg.Transport(rw, cc) log.WithFields(map[string]any{ "duration": time.Since(t), }).Infof("%s >-< %s", raddr, host) return nil } func (h *redirectHandler) checkRateLimit(addr net.Addr) bool { if h.options.RateLimiter == nil { return true } host, _, _ := net.SplitHostPort(addr.String()) if limiter := h.options.RateLimiter.Limiter(host); limiter != nil { return limiter.Allow(1) } return true } func isHTTP(s string) bool { return strings.HasPrefix(http.MethodGet, s[:3]) || strings.HasPrefix(http.MethodPost, s[:4]) || strings.HasPrefix(http.MethodPut, s[:3]) || strings.HasPrefix(http.MethodDelete, s) || strings.HasPrefix(http.MethodOptions, s) || strings.HasPrefix(http.MethodPatch, s) || strings.HasPrefix(http.MethodHead, s[:4]) || strings.HasPrefix(http.MethodConnect, s) || strings.HasPrefix(http.MethodTrace, s) }