add clientIP for handler recorder object

This commit is contained in:
ginuerzh
2024-09-15 18:28:24 +08:00
parent f681f7aa03
commit b39aa63f75
12 changed files with 77 additions and 8 deletions
+6
View File
@@ -27,6 +27,7 @@ import (
ctxvalue "github.com/go-gost/x/ctx" ctxvalue "github.com/go-gost/x/ctx"
xio "github.com/go-gost/x/internal/io" xio "github.com/go-gost/x/internal/io"
xnet "github.com/go-gost/x/internal/net" xnet "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
"github.com/go-gost/x/internal/util/forward" "github.com/go-gost/x/internal/util/forward"
tls_util "github.com/go-gost/x/internal/util/tls" tls_util "github.com/go-gost/x/internal/util/tls"
rate_limiter "github.com/go-gost/x/limiter/rate" rate_limiter "github.com/go-gost/x/limiter/rate"
@@ -90,6 +91,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
Time: start, Time: start,
SID: string(ctxvalue.SidFromContext(ctx)), SID: string(ctxvalue.SidFromContext(ctx)),
} }
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{ log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(), "remote": conn.RemoteAddr().String(),
@@ -252,6 +254,10 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriteCloser,
Header: req.Header.Clone(), Header: req.Header.Clone(),
}, },
} }
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
defer func() { defer func() {
if err != nil { if err != nil {
ro.HTTP.StatusCode = resp.StatusCode ro.HTTP.StatusCode = resp.StatusCode
+6
View File
@@ -28,6 +28,7 @@ import (
ctxvalue "github.com/go-gost/x/ctx" ctxvalue "github.com/go-gost/x/ctx"
xio "github.com/go-gost/x/internal/io" xio "github.com/go-gost/x/internal/io"
xnet "github.com/go-gost/x/internal/net" xnet "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
"github.com/go-gost/x/internal/net/proxyproto" "github.com/go-gost/x/internal/net/proxyproto"
"github.com/go-gost/x/internal/util/forward" "github.com/go-gost/x/internal/util/forward"
tls_util "github.com/go-gost/x/internal/util/tls" tls_util "github.com/go-gost/x/internal/util/tls"
@@ -91,6 +92,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
Time: start, Time: start,
SID: string(ctxvalue.SidFromContext(ctx)), SID: string(ctxvalue.SidFromContext(ctx)),
} }
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{ log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(), "remote": conn.RemoteAddr().String(),
@@ -252,6 +254,10 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriteCloser,
Header: req.Header.Clone(), Header: req.Header.Clone(),
}, },
} }
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
defer func() { defer func() {
if err != nil { if err != nil {
ro.HTTP.StatusCode = resp.StatusCode ro.HTTP.StatusCode = resp.StatusCode
+7 -1
View File
@@ -29,6 +29,7 @@ import (
ctxvalue "github.com/go-gost/x/ctx" ctxvalue "github.com/go-gost/x/ctx"
xio "github.com/go-gost/x/internal/io" xio "github.com/go-gost/x/internal/io"
netpkg "github.com/go-gost/x/internal/net" netpkg "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
limiter_util "github.com/go-gost/x/internal/util/limiter" limiter_util "github.com/go-gost/x/internal/util/limiter"
stats_util "github.com/go-gost/x/internal/util/stats" stats_util "github.com/go-gost/x/internal/util/stats"
rate_limiter "github.com/go-gost/x/limiter/rate" rate_limiter "github.com/go-gost/x/limiter/rate"
@@ -101,6 +102,7 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
Time: start, Time: start,
SID: string(ctxvalue.SidFromContext(ctx)), SID: string(ctxvalue.SidFromContext(ctx)),
} }
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{ log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(), "remote": conn.RemoteAddr().String(),
@@ -133,6 +135,10 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
} }
defer req.Body.Close() defer req.Body.Close()
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
return h.handleRequest(ctx, conn, req, ro, log) return h.handleRequest(ctx, conn, req, ro, log)
} }
@@ -179,7 +185,7 @@ func (h *httpHandler) handleRequest(ctx context.Context, conn net.Conn, req *htt
if u, _, _ := h.basicProxyAuth(req.Header.Get("Proxy-Authorization")); u != "" { if u, _, _ := h.basicProxyAuth(req.Header.Get("Proxy-Authorization")); u != "" {
fields["user"] = u fields["user"] = u
ro.Client = u ro.ClientID = u
} }
log = log.WithFields(fields) log = log.WithFields(fields)
+7 -1
View File
@@ -29,6 +29,7 @@ import (
ctxvalue "github.com/go-gost/x/ctx" ctxvalue "github.com/go-gost/x/ctx"
xio "github.com/go-gost/x/internal/io" xio "github.com/go-gost/x/internal/io"
netpkg "github.com/go-gost/x/internal/net" netpkg "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
limiter_util "github.com/go-gost/x/internal/util/limiter" limiter_util "github.com/go-gost/x/internal/util/limiter"
stats_util "github.com/go-gost/x/internal/util/stats" stats_util "github.com/go-gost/x/internal/util/stats"
rate_limiter "github.com/go-gost/x/limiter/rate" rate_limiter "github.com/go-gost/x/limiter/rate"
@@ -102,6 +103,7 @@ func (h *http2Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
Time: start, Time: start,
SID: string(ctxvalue.SidFromContext(ctx)), SID: string(ctxvalue.SidFromContext(ctx)),
} }
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{ log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(), "remote": conn.RemoteAddr().String(),
@@ -165,6 +167,10 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
} }
} }
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
ro.Host = req.Host ro.Host = req.Host
addr := req.Host addr := req.Host
if _, port, _ := net.SplitHostPort(addr); port == "" { if _, port, _ := net.SplitHostPort(addr); port == "" {
@@ -176,7 +182,7 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
} }
if u, _, _ := h.basicProxyAuth(req.Header.Get("Proxy-Authorization")); u != "" { if u, _, _ := h.basicProxyAuth(req.Header.Get("Proxy-Authorization")); u != "" {
fields["user"] = u fields["user"] = u
ro.Client = u ro.ClientID = u
} }
log = log.WithFields(fields) log = log.WithFields(fields)
+5
View File
@@ -24,6 +24,7 @@ import (
ctxvalue "github.com/go-gost/x/ctx" ctxvalue "github.com/go-gost/x/ctx"
xio "github.com/go-gost/x/internal/io" xio "github.com/go-gost/x/internal/io"
netpkg "github.com/go-gost/x/internal/net" netpkg "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
rate_limiter "github.com/go-gost/x/limiter/rate" rate_limiter "github.com/go-gost/x/limiter/rate"
xrecorder "github.com/go-gost/x/recorder" xrecorder "github.com/go-gost/x/recorder"
"github.com/go-gost/x/registry" "github.com/go-gost/x/registry"
@@ -80,6 +81,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
Time: start, Time: start,
SID: string(ctxvalue.SidFromContext(ctx)), SID: string(ctxvalue.SidFromContext(ctx)),
} }
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{ log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(), "remote": conn.RemoteAddr().String(),
@@ -190,6 +192,9 @@ func (h *redirectHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, radd
Header: req.Header, Header: req.Header,
}, },
} }
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
if log.IsLevelEnabled(logger.TraceLevel) { if log.IsLevelEnabled(logger.TraceLevel) {
dump, _ := httputil.DumpRequest(req, false) dump, _ := httputil.DumpRequest(req, false)
+1 -1
View File
@@ -164,7 +164,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
} }
if user != "" { if user != "" {
ro.Client = user ro.ClientID = user
log = log.WithFields(map[string]any{"user": user}) log = log.WithFields(map[string]any{"user": user})
} }
+5
View File
@@ -26,6 +26,7 @@ import (
ctxvalue "github.com/go-gost/x/ctx" ctxvalue "github.com/go-gost/x/ctx"
xio "github.com/go-gost/x/internal/io" xio "github.com/go-gost/x/internal/io"
netpkg "github.com/go-gost/x/internal/net" netpkg "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
rate_limiter "github.com/go-gost/x/limiter/rate" rate_limiter "github.com/go-gost/x/limiter/rate"
xrecorder "github.com/go-gost/x/recorder" xrecorder "github.com/go-gost/x/recorder"
"github.com/go-gost/x/registry" "github.com/go-gost/x/registry"
@@ -82,6 +83,7 @@ func (h *sniHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
Time: start, Time: start,
SID: string(ctxvalue.SidFromContext(ctx)), SID: string(ctxvalue.SidFromContext(ctx)),
} }
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{ log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(), "remote": conn.RemoteAddr().String(),
@@ -139,6 +141,9 @@ func (h *sniHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, raddr net
Header: req.Header, Header: req.Header,
}, },
} }
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
if log.IsLevelEnabled(logger.TraceLevel) { if log.IsLevelEnabled(logger.TraceLevel) {
dump, _ := httputil.DumpRequest(req, false) dump, _ := httputil.DumpRequest(req, false)
+3 -2
View File
@@ -135,13 +135,14 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
conn.SetReadDeadline(time.Time{}) conn.SetReadDeadline(time.Time{})
if h.options.Auther != nil { if h.options.Auther != nil {
id, ok := h.options.Auther.Authenticate(ctx, string(req.Userid), "") clientID, ok := h.options.Auther.Authenticate(ctx, string(req.Userid), "")
if !ok { if !ok {
resp := gosocks4.NewReply(gosocks4.RejectedUserid, nil) resp := gosocks4.NewReply(gosocks4.RejectedUserid, nil)
log.Trace(resp) log.Trace(resp)
return resp.Write(conn) return resp.Write(conn)
} }
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(id)) ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
ro.ClientID = clientID
} }
switch req.Cmd { switch req.Cmd {
+1 -1
View File
@@ -136,7 +136,7 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
if clientID := sc.ID(); clientID != "" { if clientID := sc.ID(); clientID != "" {
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID)) ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
log = log.WithFields(map[string]any{"user": clientID}) log = log.WithFields(map[string]any{"user": clientID})
ro.Client = clientID ro.ClientID = clientID
} }
conn = sc conn = sc
+8 -1
View File
@@ -23,6 +23,7 @@ import (
admission "github.com/go-gost/x/admission/wrapper" admission "github.com/go-gost/x/admission/wrapper"
xio "github.com/go-gost/x/internal/io" xio "github.com/go-gost/x/internal/io"
xnet "github.com/go-gost/x/internal/net" xnet "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
"github.com/go-gost/x/internal/net/proxyproto" "github.com/go-gost/x/internal/net/proxyproto"
climiter "github.com/go-gost/x/limiter/conn/wrapper" climiter "github.com/go-gost/x/limiter/conn/wrapper"
metrics "github.com/go-gost/x/metrics/wrapper" metrics "github.com/go-gost/x/metrics/wrapper"
@@ -102,6 +103,12 @@ func (ep *entrypoint) handle(ctx context.Context, conn net.Conn) error {
}, },
}, },
} }
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
} else {
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
}
defer func() { defer func() {
if err != nil { if err != nil {
d := time.Since(start) d := time.Since(start)
@@ -140,7 +147,7 @@ func (ep *entrypoint) handle(ctx context.Context, conn net.Conn) error {
return err return err
} }
ro.Client = tunnelID.String() ro.ClientID = tunnelID.String()
if tunnelID.IsPrivate() { if tunnelID.IsPrivate() {
err = fmt.Errorf("access denied: tunnel %s is private for host %s", tunnelID, req.Host) err = fmt.Errorf("access denied: tunnel %s is private for host %s", tunnelID, req.Host)
+26
View File
@@ -0,0 +1,26 @@
package http
import (
"net"
"net/http"
"strings"
)
func GetClientIP(req *http.Request) net.IP {
if req == nil {
return nil
}
// cloudflare CDN
sip := req.Header.Get("CF-Connecting-IP")
if sip == "" {
ss := strings.Split(req.Header.Get("X-Forwarded-For"), ",")
if len(ss) > 0 && ss[0] != "" {
sip = ss[0]
}
}
if sip == "" {
sip = req.Header.Get("X-Real-Ip")
}
return net.ParseIP(sip)
}
+2 -1
View File
@@ -53,7 +53,8 @@ type HandlerRecorderObject struct {
RemoteAddr string `json:"remote"` RemoteAddr string `json:"remote"`
LocalAddr string `json:"local"` LocalAddr string `json:"local"`
Host string `json:"host"` Host string `json:"host"`
Client string `json:"client,omitempty"` ClientIP string `json:"clientIP"`
ClientID string `json:"clientID,omitempty"`
HTTP *HTTPRecorderObject `json:"http,omitempty"` HTTP *HTTPRecorderObject `json:"http,omitempty"`
DNS *DNSRecorderObject `json:"dns,omitempty"` DNS *DNSRecorderObject `json:"dns,omitempty"`
Err string `json:"err,omitempty"` Err string `json:"err,omitempty"`