Skip to content

Latest commit

 

History

History
61 lines (53 loc) · 1.19 KB

README.md

File metadata and controls

61 lines (53 loc) · 1.19 KB

httpserver

http or https server framework with go

api document site for example https://github.com/ktpswjz/httpdoc

To get real report ip behind tcp proxy, need modify the following source code:

crypto/tls/conn.go

type Conn struct {
	// constant
	conn     net.Conn
	isClient bool
	...
	// store the remote address behind proxy
	ProxyRemoteAddr string
}

func (c *Conn) RawConn() net.Conn {

	return c.conn
}
...

net/http/server.go

type Server struct {
	Addr    string  // TCP address to listen on, ":http" if empty
	Handler Handler // handler to invoke, http.DefaultServeMux if nil
	...
	// get the remote address behind proxy
	ProxyRemoteAddr func(net.Conn) string
}

...

func (c *conn) serve(ctx context.Context) {
	c.remoteAddr = c.rwc.RemoteAddr().String()
	remoteAddr := ""
	if c.server.ProxyRemoteAddr != nil {
		remoteAddr = c.server.ProxyRemoteAddr(c.rwc)
		c.remoteAddr = remoteAddr;
	}
	...
	if tlsConn, ok := c.rwc.(*tls.Conn); ok {
		tlsConn.ProxyRemoteAddr = remoteAddr
    	...
        }
}

...

func (h initNPNRequest) ServeHTTP(rw ResponseWriter, req *Request) {
	...
	if h.c.ProxyRemoteAddr != "" {
		req.RemoteAddr = h.c.ProxyRemoteAddr
	}
	h.h.ServeHTTP(rw, req)
}