From ac554f670c346d7d5749f389fe70a3375456c5d1 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Thu, 18 Aug 2022 20:03:03 +0800 Subject: [PATCH] add proxyAgent options for http/http2 handler --- README.md | 2 +- README_en.md | 2 +- cmd/gost/route.go | 1 + gost.go | 2 ++ handler.go | 8 ++++++++ http.go | 9 +++++++-- http2.go | 6 +++++- selector.go | 2 +- 8 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1d111eb5..85f2e83e 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ go build #### Docker ```bash -docker pull ginuerzh/gost +docker run --rm ginuerzh/gost -V ``` #### Homebrew diff --git a/README_en.md b/README_en.md index 3fe6b9a8..74ffa317 100644 --- a/README_en.md +++ b/README_en.md @@ -53,7 +53,7 @@ go build #### Docker ```bash -docker pull ginuerzh/gost +docker run --rm ginuerzh/gost -V ``` #### Homebrew diff --git a/cmd/gost/route.go b/cmd/gost/route.go index a65b0aed..deb71cfc 100644 --- a/cmd/gost/route.go +++ b/cmd/gost/route.go @@ -664,6 +664,7 @@ func (r *route) GenRouters() ([]router, error) { gost.IPsHandlerOption(ips), gost.TCPModeHandlerOption(node.GetBool("tcp")), gost.IPRoutesHandlerOption(tunRoutes...), + gost.ProxyAgentHandlerOption(node.Get("proxyAgent")), ) rt := router{ diff --git a/gost.go b/gost.go index 31d3d568..a17c827b 100644 --- a/gost.go +++ b/gost.go @@ -80,6 +80,8 @@ var ( // DefaultUserAgent is the default HTTP User-Agent header used by HTTP and websocket. DefaultUserAgent = "Chrome/78.0.3904.106" + DefaultProxyAgent = "gost/" + Version + // DefaultMTU is the default mtu for tun/tap device DefaultMTU = 1350 ) diff --git a/handler.go b/handler.go index eee515c3..81f505f3 100644 --- a/handler.go +++ b/handler.go @@ -42,6 +42,7 @@ type HandlerOptions struct { IPs []string TCPMode bool IPRoutes []IPRoute + ProxyAgent string } // HandlerOption allows a common way to set handler options. @@ -211,6 +212,13 @@ func IPRoutesHandlerOption(routes ...IPRoute) HandlerOption { } } +// ProxyAgentHandlerOption sets the proxy agent for http handler. +func ProxyAgentHandlerOption(agent string) HandlerOption { + return func(opts *HandlerOptions) { + opts.ProxyAgent = agent + } +} + type autoHandler struct { options *HandlerOptions } diff --git a/http.go b/http.go index 1b17e6fd..3275daa6 100644 --- a/http.go +++ b/http.go @@ -173,7 +173,12 @@ func (h *httpHandler) handleRequest(conn net.Conn, req *http.Request) { ProtoMinor: 1, Header: http.Header{}, } - resp.Header.Add("Proxy-Agent", "gost/"+Version) + + proxyAgent := DefaultProxyAgent + if h.options.ProxyAgent != "" { + proxyAgent = h.options.ProxyAgent + } + resp.Header.Add("Proxy-Agent", proxyAgent) if !Can("tcp", host, h.options.Whitelist, h.options.Blacklist) { log.Logf("[http] %s - %s : Unauthorized to tcp connect to %s", @@ -287,7 +292,7 @@ func (h *httpHandler) handleRequest(conn net.Conn, req *http.Request) { if req.Method == http.MethodConnect { b := []byte("HTTP/1.1 200 Connection established\r\n" + - "Proxy-Agent: gost/" + Version + "\r\n\r\n") + "Proxy-Agent: " + proxyAgent + "\r\n\r\n") if Debug { log.Logf("[http] %s <- %s\n%s", conn.RemoteAddr(), conn.LocalAddr(), string(b)) } diff --git a/http2.go b/http2.go index 1c3cf5a9..72c48906 100644 --- a/http2.go +++ b/http2.go @@ -365,7 +365,11 @@ func (h *http2Handler) roundTrip(w http.ResponseWriter, r *http.Request) { log.Logf("[http2] %s - %s\n%s", r.RemoteAddr, laddr, string(dump)) } - w.Header().Set("Proxy-Agent", "gost/"+Version) + proxyAgent := DefaultProxyAgent + if h.options.ProxyAgent != "" { + proxyAgent = h.options.ProxyAgent + } + w.Header().Set("Proxy-Agent", proxyAgent) if !Can("tcp", host, h.options.Whitelist, h.options.Blacklist) { log.Logf("[http2] %s - %s : Unauthorized to tcp connect to %s", diff --git a/selector.go b/selector.go index 12545aca..bff6d11f 100644 --- a/selector.go +++ b/selector.go @@ -12,7 +12,7 @@ import ( var ( // ErrNoneAvailable indicates there is no node available. - ErrNoneAvailable = errors.New("none available") + ErrNoneAvailable = errors.New("none node available") ) // NodeSelector as a mechanism to pick nodes and mark their status.