good morning!!!!

Skip to content
Snippets Groups Projects
Verified Commit c67ddafb authored by a's avatar a
Browse files

ok

parent e940453a
Branches
Tags
No related merge requests found
...@@ -58,29 +58,14 @@ func (c *Codec) Reset(w http.ResponseWriter, r *http.Request) { ...@@ -58,29 +58,14 @@ func (c *Codec) Reset(w http.ResponseWriter, r *http.Request) {
ctx := c.r.Context() ctx := c.r.Context()
c.ctx, c.cn = context.WithCancel(ctx) c.ctx, c.cn = context.WithCancel(ctx)
c.peerInfo()
c.doRead() c.doRead()
c.peerInfo()
} }
func (c *Codec) peerInfo() { func (c *Codec) peerInfo() {
c.i.Transport = "http" c.i.Transport = "http"
c.i.RemoteAddr = c.r.RemoteAddr c.i.RemoteAddr = c.r.RemoteAddr
c.i.HTTP = jsonrpc.HttpInfo{ c.i.HTTP = &http.Request{}
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
}
} }
// gets the peer info // gets the peer info
......
...@@ -200,14 +200,14 @@ func TestHTTPPeerInfo(t *testing.T) { ...@@ -200,14 +200,14 @@ func TestHTTPPeerInfo(t *testing.T) {
if info.Transport != "http" { if info.Transport != "http" {
t.Errorf("wrong Transport %q", info.Transport) t.Errorf("wrong Transport %q", info.Transport)
} }
if info.HTTP.Version != "HTTP/1.1" { if info.HTTP.Proto != "HTTP/1.1" {
t.Errorf("wrong HTTP.Version %q", info.HTTP.Version) t.Errorf("wrong HTTP.Version %q", info.HTTP.Proto)
} }
if info.HTTP.UserAgent != "ua-testing" { if info.HTTP.UserAgent() != "ua-testing" {
t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent) t.Errorf("wrong HTTP.UserAgent %q", info.HTTP.UserAgent())
} }
if info.HTTP.Origin != "origin.example.com" { if info.HTTP.Host != "origin.example.com" {
t.Errorf("wrong HTTP.Origin %q", info.HTTP.UserAgent) t.Errorf("wrong HTTP.Origin %q", info.HTTP.Host)
} }
} }
func TestClientHTTP(t *testing.T) { func TestClientHTTP(t *testing.T) {
......
...@@ -38,7 +38,7 @@ func (c *Codec) PeerInfo() jsonrpc.PeerInfo { ...@@ -38,7 +38,7 @@ func (c *Codec) PeerInfo() jsonrpc.PeerInfo {
return jsonrpc.PeerInfo{ return jsonrpc.PeerInfo{
Transport: "ipc", Transport: "ipc",
RemoteAddr: "", RemoteAddr: "",
HTTP: jsonrpc.HttpInfo{}, HTTP: nil,
} }
} }
......
...@@ -30,7 +30,7 @@ type Codec struct { ...@@ -30,7 +30,7 @@ type Codec struct {
i jsonrpc.PeerInfo 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) conn.SetReadLimit(WsMessageSizeLimit)
c := &Codec{ c := &Codec{
closed: make(chan struct{}), closed: make(chan struct{}),
...@@ -38,19 +38,7 @@ func newWebsocketCodec(ctx context.Context, conn *websocket.Conn, host string, r ...@@ -38,19 +38,7 @@ func newWebsocketCodec(ctx context.Context, conn *websocket.Conn, host string, r
} }
c.i.Transport = "ws" c.i.Transport = "ws"
// Fill in connection details. // Fill in connection details.
c.i.HTTP.Host = host c.i.HTTP = req.Clone(req.Context())
// 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
// Start pinger. // Start pinger.
go heartbeat(ctx, conn, WsPingInterval) go heartbeat(ctx, conn, WsPingInterval)
return c return c
......
...@@ -22,7 +22,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -22,7 +22,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
c := newWebsocketCodec(r.Context(), conn, "", r.Header) c := newWebsocketCodec(r.Context(), conn, "", r)
err = s.Server.ServeCodec(r.Context(), c) err = s.Server.ServeCodec(r.Context(), c)
if err != nil { if err != nil {
// slog.Error("codec err", "error", err) // slog.Error("codec err", "error", err)
...@@ -43,7 +43,7 @@ func WebsocketHandler(s *server.Server, allowedOrigins []string) http.Handler { ...@@ -43,7 +43,7 @@ func WebsocketHandler(s *server.Server, allowedOrigins []string) http.Handler {
if err != nil { if err != nil {
return return
} }
codec := newWebsocketCodec(r.Context(), conn, r.Host, r.Header) codec := newWebsocketCodec(r.Context(), conn, r.Host, r)
err = s.ServeCodec(r.Context(), codec) err = s.ServeCodec(r.Context(), codec)
if err != nil { if err != nil {
// slog.Error("codec err", "error", err) // slog.Error("codec err", "error", err)
......
...@@ -121,11 +121,11 @@ func TestWebsocketPeerInfo(t *testing.T) { ...@@ -121,11 +121,11 @@ func TestWebsocketPeerInfo(t *testing.T) {
if connInfo.Transport != "ws" { if connInfo.Transport != "ws" {
t.Errorf("wrong Transport %q", connInfo.Transport) t.Errorf("wrong Transport %q", connInfo.Transport)
} }
if connInfo.HTTP.UserAgent != "Go-http-client/1.1" { if connInfo.HTTP.UserAgent() != "Go-http-client/1.1" {
t.Errorf("wrong HTTP.UserAgent %q", connInfo.HTTP.UserAgent) t.Errorf("wrong HTTP.UserAgent %q", connInfo.HTTP.UserAgent())
} }
if connInfo.HTTP.Origin != "http://origin.example.com" { if connInfo.HTTP.Host != "http://origin.example.com" {
t.Errorf("wrong HTTP.Origin %q", connInfo.HTTP.Origin) t.Errorf("wrong HTTP.Origin %q", connInfo.HTTP.Host)
} }
} }
......
...@@ -12,16 +12,5 @@ type PeerInfo struct { ...@@ -12,16 +12,5 @@ type PeerInfo struct {
RemoteAddr string RemoteAddr string
// Additional information for HTTP and WebSocket connections. // Additional information for HTTP and WebSocket connections.
HTTP HttpInfo HTTP *http.Request
}
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
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment