From c67ddafb25a2df5ca5207f784f78de499f9ce5e9 Mon Sep 17 00:00:00 2001 From: a <a@tuxpa.in> Date: Thu, 23 Nov 2023 23:20:31 -0600 Subject: [PATCH] ok --- contrib/codecs/http/codec.go | 19 ++----------------- contrib/codecs/http/http_test.go | 12 ++++++------ contrib/codecs/rdwr/codec.go | 2 +- contrib/codecs/websocket/codec.go | 16 ++-------------- contrib/codecs/websocket/handler.go | 4 ++-- contrib/codecs/websocket/websocket_test.go | 8 ++++---- pkg/jsonrpc/peer.go | 13 +------------ 7 files changed, 18 insertions(+), 56 deletions(-) diff --git a/contrib/codecs/http/codec.go b/contrib/codecs/http/codec.go index a6e543c..b3fb38d 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 a73fd13..c13b8ac 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 e75a245..c6661a9 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 3ad099f..325c445 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 7eaf816..2458a85 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 cd68fa8..b5e4497 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 dcb2915..36bf95f 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 } -- GitLab