Skip to content

Commit

Permalink
add proxyAgent options for http/http2 handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Aug 18, 2022
1 parent b9e61dc commit ac554f6
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ go build
#### Docker

```bash
docker pull ginuerzh/gost
docker run --rm ginuerzh/gost -V
```

#### Homebrew
Expand Down
2 changes: 1 addition & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ go build
#### Docker

```bash
docker pull ginuerzh/gost
docker run --rm ginuerzh/gost -V
```

#### Homebrew
Expand Down
1 change: 1 addition & 0 deletions cmd/gost/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions gost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
8 changes: 8 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type HandlerOptions struct {
IPs []string
TCPMode bool
IPRoutes []IPRoute
ProxyAgent string
}

// HandlerOption allows a common way to set handler options.
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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))
}
Expand Down
6 changes: 5 additions & 1 deletion http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ac554f6

Please sign in to comment.