add interface xnet.SrcAddr/DstAddr
This commit is contained in:
@@ -148,12 +148,8 @@ func (h *dnsHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
|
||||
ro.ClientIP = conn.RemoteAddr().String()
|
||||
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
|
||||
ro.ClientIP = string(clientAddr)
|
||||
}
|
||||
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
|
||||
ro.ClientIP = h
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/go-gost/core/handler"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
"github.com/go-gost/core/recorder"
|
||||
ctxvalue "github.com/go-gost/x/ctx"
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
"github.com/go-gost/x/registry"
|
||||
)
|
||||
@@ -65,7 +66,13 @@ func (h *fileHandler) Init(md md.Metadata) (err error) {
|
||||
}
|
||||
|
||||
func (h *fileHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) error {
|
||||
var clientAddr string
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
clientAddr = srcAddr.String()
|
||||
}
|
||||
|
||||
h.options.Logger.WithFields(map[string]any{
|
||||
"client": clientAddr,
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
}).Infof("%s - %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
@@ -101,7 +108,6 @@ func (h *fileHandler) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
},
|
||||
Time: start,
|
||||
}
|
||||
ro.ClientIP, _, _ = net.SplitHostPort(r.RemoteAddr)
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
"remote": r.RemoteAddr,
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/go-gost/core/recorder"
|
||||
ctxvalue "github.com/go-gost/x/ctx"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
"github.com/go-gost/x/internal/net/proxyproto"
|
||||
"github.com/go-gost/x/internal/util/forwarder"
|
||||
"github.com/go-gost/x/internal/util/sniffing"
|
||||
tls_util "github.com/go-gost/x/internal/util/tls"
|
||||
@@ -89,22 +90,16 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
|
||||
ro.ClientIP = conn.RemoteAddr().String()
|
||||
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
|
||||
ro.ClientIP = string(clientAddr)
|
||||
} else {
|
||||
ctx = ctxvalue.ContextWithClientAddr(ctx, ctxvalue.ClientAddr(conn.RemoteAddr().String()))
|
||||
}
|
||||
|
||||
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
|
||||
ro.ClientIP = h
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ro.SID,
|
||||
"client": ro.ClientIP,
|
||||
"network": ro.Network,
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ro.SID,
|
||||
"client": ro.ClientIP,
|
||||
})
|
||||
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
|
||||
@@ -157,6 +152,13 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
var buf bytes.Buffer
|
||||
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
|
||||
ro.Route = buf.String()
|
||||
|
||||
cc = proxyproto.WrapClientConn(
|
||||
h.md.proxyProtocol,
|
||||
ctxvalue.SrcAddrFromContext(ctx),
|
||||
ctxvalue.DstAddrFromContext(ctx),
|
||||
cc)
|
||||
|
||||
return cc, err
|
||||
}
|
||||
sniffer := &forwarder.Sniffer{
|
||||
@@ -246,6 +248,12 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
cc = proxyproto.WrapClientConn(
|
||||
h.md.proxyProtocol,
|
||||
ctxvalue.SrcAddrFromContext(ctx),
|
||||
ctxvalue.DstAddrFromContext(ctx),
|
||||
cc)
|
||||
|
||||
log = log.WithFields(map[string]any{"src": cc.LocalAddr().String(), "dst": cc.RemoteAddr().String()})
|
||||
ro.Src = cc.LocalAddr().String()
|
||||
ro.Dst = cc.RemoteAddr().String()
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
type metadata struct {
|
||||
readTimeout time.Duration
|
||||
httpKeepalive bool
|
||||
proxyProtocol int
|
||||
|
||||
sniffing bool
|
||||
sniffingTimeout time.Duration
|
||||
@@ -34,6 +35,7 @@ func (h *forwardHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
}
|
||||
|
||||
h.md.httpKeepalive = mdutil.GetBool(md, "http.keepalive")
|
||||
h.md.proxyProtocol = mdutil.GetInt(md, "proxyProtocol")
|
||||
|
||||
h.md.sniffing = mdutil.GetBool(md, "sniffing")
|
||||
h.md.sniffingTimeout = mdutil.GetDuration(md, "sniffing.timeout")
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/chain"
|
||||
@@ -91,15 +90,8 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
|
||||
ro.ClientIP = conn.RemoteAddr().String()
|
||||
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
|
||||
ro.ClientIP = string(clientAddr)
|
||||
} else {
|
||||
ctx = ctxvalue.ContextWithClientAddr(ctx, ctxvalue.ClientAddr(conn.RemoteAddr().String()))
|
||||
}
|
||||
|
||||
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
|
||||
ro.ClientIP = h
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
@@ -166,6 +158,13 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
var buf bytes.Buffer
|
||||
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
|
||||
ro.Route = buf.String()
|
||||
|
||||
cc = proxyproto.WrapClientConn(
|
||||
h.md.proxyProtocol,
|
||||
ctxvalue.SrcAddrFromContext(ctx),
|
||||
ctxvalue.DstAddrFromContext(ctx),
|
||||
cc)
|
||||
|
||||
return cc, err
|
||||
}
|
||||
sniffer := &forwarder.Sniffer{
|
||||
@@ -260,7 +259,11 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
ro.Src = cc.LocalAddr().String()
|
||||
ro.Dst = cc.RemoteAddr().String()
|
||||
|
||||
cc = proxyproto.WrapClientConn(h.md.proxyProtocol, conn.RemoteAddr(), convertAddr(conn.LocalAddr()), cc)
|
||||
cc = proxyproto.WrapClientConn(
|
||||
h.md.proxyProtocol,
|
||||
ctxvalue.SrcAddrFromContext(ctx),
|
||||
ctxvalue.DstAddrFromContext(ctx),
|
||||
cc)
|
||||
|
||||
t := time.Now()
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
|
||||
@@ -284,27 +287,3 @@ func (h *forwardHandler) checkRateLimit(addr net.Addr) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func convertAddr(addr net.Addr) net.Addr {
|
||||
host, sp, _ := net.SplitHostPort(addr.String())
|
||||
ip := net.ParseIP(host)
|
||||
port, _ := strconv.Atoi(sp)
|
||||
|
||||
if ip == nil || ip.Equal(net.IPv6zero) {
|
||||
ip = net.IPv4zero
|
||||
}
|
||||
|
||||
switch addr.Network() {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
return &net.TCPAddr{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
}
|
||||
|
||||
default:
|
||||
return &net.UDPAddr{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-17
@@ -133,12 +133,8 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
|
||||
ro.ClientIP = conn.RemoteAddr().String()
|
||||
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
|
||||
ro.ClientIP = string(clientAddr)
|
||||
}
|
||||
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
|
||||
ro.ClientIP = h
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
@@ -516,16 +512,6 @@ func (h *httpHandler) proxyRoundTrip(ctx context.Context, rw io.ReadWriteCloser,
|
||||
Header: req.Header.Clone(),
|
||||
},
|
||||
}
|
||||
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
|
||||
ro.ClientIP = clientIP.String()
|
||||
}
|
||||
|
||||
clientAddr := ro.RemoteAddr
|
||||
if ro.ClientIP != "" {
|
||||
if _, port, _ := net.SplitHostPort(ro.RemoteAddr); port != "" {
|
||||
clientAddr = net.JoinHostPort(ro.ClientIP, port)
|
||||
}
|
||||
}
|
||||
|
||||
// HTTP/1.0
|
||||
http10 := req.ProtoMajor == 1 && req.ProtoMinor == 0
|
||||
@@ -579,7 +565,6 @@ func (h *httpHandler) proxyRoundTrip(ctx context.Context, rw io.ReadWriteCloser,
|
||||
}
|
||||
}
|
||||
|
||||
ctx = ctxvalue.ContextWithClientAddr(ctx, ctxvalue.ClientAddr(clientAddr))
|
||||
ctx = ctx_internal.ContextWithRecorderObject(ctx, ro)
|
||||
ctx = ctxvalue.ContextWithLogger(ctx, log)
|
||||
|
||||
|
||||
@@ -108,7 +108,9 @@ func (h *http2Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
Time: start,
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
|
||||
@@ -15,15 +15,7 @@ type metadata struct {
|
||||
}
|
||||
|
||||
func (h *http3Handler) parseMetadata(md mdata.Metadata) error {
|
||||
const (
|
||||
header = "header"
|
||||
probeResistKey = "probeResistance"
|
||||
probeResistKeyX = "probe_resist"
|
||||
knock = "knock"
|
||||
hash = "hash"
|
||||
)
|
||||
|
||||
if m := mdutil.GetStringMapString(md, header); len(m) > 0 {
|
||||
if m := mdutil.GetStringMapString(md, "header"); len(m) > 0 {
|
||||
hd := http.Header{}
|
||||
for k, v := range m {
|
||||
hd.Add(k, v)
|
||||
@@ -31,20 +23,17 @@ func (h *http3Handler) parseMetadata(md mdata.Metadata) error {
|
||||
h.md.header = hd
|
||||
}
|
||||
|
||||
pr := mdutil.GetString(md, probeResistKey)
|
||||
if pr == "" {
|
||||
pr = mdutil.GetString(md, probeResistKeyX)
|
||||
}
|
||||
pr := mdutil.GetString(md, "probeResistance", "probe_resist")
|
||||
if pr != "" {
|
||||
if ss := strings.SplitN(pr, ":", 2); len(ss) == 2 {
|
||||
h.md.probeResistance = &probeResistance{
|
||||
Type: ss[0],
|
||||
Value: ss[1],
|
||||
Knock: mdutil.GetString(md, knock),
|
||||
Knock: mdutil.GetString(md, "knock"),
|
||||
}
|
||||
}
|
||||
}
|
||||
h.md.hash = mdutil.GetString(md, hash)
|
||||
h.md.hash = mdutil.GetString(md, "hash")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -81,13 +81,17 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
|
||||
Time: start,
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
|
||||
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
"network": ro.Network,
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ctxvalue.SidFromContext(ctx),
|
||||
"network": ro.Network,
|
||||
"client": ro.ClientIP,
|
||||
})
|
||||
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
|
||||
|
||||
@@ -112,12 +112,8 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
|
||||
ro.ClientIP = conn.RemoteAddr().String()
|
||||
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
|
||||
ro.ClientIP = string(clientAddr)
|
||||
}
|
||||
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
|
||||
ro.ClientIP = h
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
|
||||
@@ -81,12 +81,8 @@ func (h *sniHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
|
||||
SID: string(ctxvalue.SidFromContext(ctx)),
|
||||
}
|
||||
|
||||
ro.ClientIP = conn.RemoteAddr().String()
|
||||
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
|
||||
ro.ClientIP = string(clientAddr)
|
||||
}
|
||||
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
|
||||
ro.ClientIP = h
|
||||
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
|
||||
ro.ClientIP = srcAddr.String()
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
@@ -131,6 +127,7 @@ func (h *sniHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
|
||||
var buf bytes.Buffer
|
||||
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
|
||||
ro.Route = buf.String()
|
||||
|
||||
return cc, err
|
||||
}
|
||||
dialTLS := func(ctx context.Context, network, address string, cfg *tls.Config) (net.Conn, error) {
|
||||
|
||||
Reference in New Issue
Block a user