diff --git a/contrib/codecs/http/codec.go b/contrib/codecs/http/codec.go index a6e543c50d2be83d70eb1c7dcaed7cddde57949c..b3fb38d127b2973b61921853351aae8aaab8ea5a 100644 --- a/contrib/codecs/http/codec.go +++ b/contrib/codecs/http/codec.go @@ -58,29 +58,14 @@ func (c *Codec) Reset(w http.ResponseWriter, r *http.Request) { ctx := c.r.Context() c.ctx, c.cn = context.WithCancel(ctx) - c.peerInfo() c.doRead() + c.peerInfo() } func (c *Codec) peerInfo() { c.i.Transport = "http" c.i.RemoteAddr = c.r.RemoteAddr - c.i.HTTP = jsonrpc.HttpInfo{ - Version: c.r.Proto, - UserAgent: c.r.UserAgent(), - Host: c.r.Host, - Headers: c.r.Header.Clone(), - } - c.i.HTTP.Origin = c.r.Header.Get("X-Real-Ip") - if c.i.HTTP.Origin == "" { - c.i.HTTP.Origin = c.r.Header.Get("X-Forwarded-For") - } - if c.i.HTTP.Origin == "" { - c.i.HTTP.Origin = c.r.Header.Get("Origin") - } - if c.i.HTTP.Origin == "" { - c.i.HTTP.Origin = c.r.RemoteAddr - } + c.i.HTTP = &http.Request{} } // gets the peer info diff --git a/contrib/codecs/http/http_test.go b/contrib/codecs/http/http_test.go index a73fd139e709f7bcd2794274426af0dc6a961f34..c13b8acc59b0a02da773de810945c78d5557ac40 100644 --- a/contrib/codecs/http/http_test.go +++ b/contrib/codecs/http/http_test.go @@ -200,14 +200,14 @@ func TestHTTPPeerInfo(t *testing.T) { if info.Transport != "http" { t.Errorf("wrong Transport %q", info.Transport) } - if info.HTTP.Version != "HTTP/1.1" { - t.Errorf("wrong HTTP.Version %q", info.HTTP.Version) + if info.HTTP.Proto != "HTTP/1.1" { + t.Errorf("wrong HTTP.Version %q", info.HTTP.Proto) } - if info.HTTP.UserAgent != "ua-testing" { - t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent) + if info.HTTP.UserAgent() != "ua-testing" { + t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent()) } - if info.HTTP.Origin != "origin.example.com" { - t.Errorf("wrong HTTP.Origin %q", info.HTTP.UserAgent) + if info.HTTP.Host != "origin.example.com" { + t.Errorf("wrong HTTP.Origin %q", info.HTTP.Host) } } func TestClientHTTP(t *testing.T) { diff --git a/contrib/codecs/rdwr/codec.go b/contrib/codecs/rdwr/codec.go index e75a245a11c5e9ae497b852f1e94330187b535b8..c6661a9ccef091d4bcdc85b3351d5cadc23f46a4 100644 --- a/contrib/codecs/rdwr/codec.go +++ b/contrib/codecs/rdwr/codec.go @@ -38,7 +38,7 @@ func (c *Codec) PeerInfo() jsonrpc.PeerInfo { return jsonrpc.PeerInfo{ Transport: "ipc", RemoteAddr: "", - HTTP: jsonrpc.HttpInfo{}, + HTTP: nil, } } diff --git a/contrib/codecs/websocket/codec.go b/contrib/codecs/websocket/codec.go index 3ad099fe5be580fc7832bcd11bec678aef4f51cc..325c4452120d908ae5cc510f28f8909ae8666572 100644 --- a/contrib/codecs/websocket/codec.go +++ b/contrib/codecs/websocket/codec.go @@ -30,7 +30,7 @@ type Codec struct { i jsonrpc.PeerInfo } -func newWebsocketCodec(ctx context.Context, conn *websocket.Conn, host string, req http.Header) *Codec { +func newWebsocketCodec(ctx context.Context, conn *websocket.Conn, host string, req *http.Request) *Codec { conn.SetReadLimit(WsMessageSizeLimit) c := &Codec{ closed: make(chan struct{}), @@ -38,19 +38,7 @@ func newWebsocketCodec(ctx context.Context, conn *websocket.Conn, host string, r } c.i.Transport = "ws" // Fill in connection details. - c.i.HTTP.Host = host - // traefik proxy protocol headers - c.i.HTTP.Origin = req.Get("X-Real-Ip") - if c.i.HTTP.Origin == "" { - c.i.HTTP.Origin = req.Get("X-Forwarded-For") - } - // origin header fallback - if c.i.HTTP.Origin == "" { - c.i.HTTP.Origin = req.Get("origin") - } - c.i.RemoteAddr = c.i.HTTP.Origin - c.i.HTTP.UserAgent = req.Get("User-Agent") - c.i.HTTP.Headers = req + c.i.HTTP = req.Clone(req.Context()) // Start pinger. go heartbeat(ctx, conn, WsPingInterval) return c diff --git a/contrib/codecs/websocket/handler.go b/contrib/codecs/websocket/handler.go index 7eaf816d597ad22f47a4eade6a71dd32ecf3f41b..2458a85bb5eb1f6cc4a8cfda280bcd515bb2d883 100644 --- a/contrib/codecs/websocket/handler.go +++ b/contrib/codecs/websocket/handler.go @@ -22,7 +22,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - c := newWebsocketCodec(r.Context(), conn, "", r.Header) + c := newWebsocketCodec(r.Context(), conn, "", r) err = s.Server.ServeCodec(r.Context(), c) if err != nil { // slog.Error("codec err", "error", err) @@ -43,7 +43,7 @@ func WebsocketHandler(s *server.Server, allowedOrigins []string) http.Handler { if err != nil { return } - codec := newWebsocketCodec(r.Context(), conn, r.Host, r.Header) + codec := newWebsocketCodec(r.Context(), conn, r.Host, r) err = s.ServeCodec(r.Context(), codec) if err != nil { // slog.Error("codec err", "error", err) diff --git a/contrib/codecs/websocket/websocket_test.go b/contrib/codecs/websocket/websocket_test.go index cd68fa8c4fb72f2e052eca4ceb573abc3a89aada..b5e449795a72b1fc106de68ab58517556fee4fc0 100644 --- a/contrib/codecs/websocket/websocket_test.go +++ b/contrib/codecs/websocket/websocket_test.go @@ -121,11 +121,11 @@ func TestWebsocketPeerInfo(t *testing.T) { if connInfo.Transport != "ws" { t.Errorf("wrong Transport %q", connInfo.Transport) } - if connInfo.HTTP.UserAgent != "Go-http-client/1.1" { - t.Errorf("wrong HTTP.UserAgent %q", connInfo.HTTP.UserAgent) + if connInfo.HTTP.UserAgent() != "Go-http-client/1.1" { + t.Errorf("wrong HTTP.UserAgent %q", connInfo.HTTP.UserAgent()) } - if connInfo.HTTP.Origin != "http://origin.example.com" { - t.Errorf("wrong HTTP.Origin %q", connInfo.HTTP.Origin) + if connInfo.HTTP.Host != "http://origin.example.com" { + t.Errorf("wrong HTTP.Origin %q", connInfo.HTTP.Host) } } diff --git a/pkg/jsonrpc/peer.go b/pkg/jsonrpc/peer.go index dcb2915034a46f0898c29c8ae96af02662578d07..36bf95f66bb8fa93b856e5a380bbb0fdc40ded8b 100644 --- a/pkg/jsonrpc/peer.go +++ b/pkg/jsonrpc/peer.go @@ -12,16 +12,5 @@ type PeerInfo struct { RemoteAddr string // Additional information for HTTP and WebSocket connections. - HTTP HttpInfo -} - -type HttpInfo struct { - // Protocol version, i.e. "HTTP/1.1". This is not set for WebSocket. - Version string - // Header values sent by the client. - UserAgent string - Origin string - Host string - - Headers http.Header + HTTP *http.Request }