Skip to content

Commit

Permalink
add user-agent option for http/http2
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Jun 20, 2019
1 parent bea815e commit e996e7c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
5 changes: 3 additions & 2 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func (c *Chain) dialWithOptions(addr string, options *ChainOptions) (net.Conn, e
return nil, err
}

cc, err := route.LastNode().Client.Connect(conn, ipAddr, AddrConnectOption(addr))
cOpts := append([]ConnectOption{AddrConnectOption(addr)}, route.LastNode().ConnectOptions...)
cc, err := route.LastNode().Client.Connect(conn, ipAddr, cOpts...)
if err != nil {
conn.Close()
return nil, err
Expand Down Expand Up @@ -233,7 +234,7 @@ func (c *Chain) getConn() (conn net.Conn, err error) {
preNode := node
for _, node := range nodes[1:] {
var cc net.Conn
cc, err = preNode.Client.Connect(cn, node.Addr)
cc, err = preNode.Client.Connect(cn, node.Addr, preNode.ConnectOptions...)
if err != nil {
cn.Close()
node.MarkDead()
Expand Down
16 changes: 12 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ func QUICConfigHandshakeOption(config *QUICConfig) HandshakeOption {

// ConnectOptions describes the options for Connector.Connect.
type ConnectOptions struct {
Addr string
Timeout time.Duration
User *url.Userinfo
Selector gosocks5.Selector
Addr string
Timeout time.Duration
User *url.Userinfo
Selector gosocks5.Selector
UserAgent string
}

// ConnectOption allows a common way to set ConnectOptions.
Expand Down Expand Up @@ -274,3 +275,10 @@ func SelectorConnectOption(s gosocks5.Selector) ConnectOption {
opts.Selector = s
}
}

// UserAgentConnectOption specifies the HTTP user-agent header.
func UserAgentConnectOption(ua string) ConnectOption {
return func(opts *ConnectOptions) {
opts.UserAgent = ua
}
}
4 changes: 4 additions & 0 deletions cmd/gost/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ func parseChainNode(ns string) (nodes []gost.Node, err error) {
gost.TimeoutDialOption(time.Duration(timeout)*time.Second),
)

node.ConnectOptions = []gost.ConnectOption{
gost.UserAgentConnectOption(node.Get("agent")),
}

if host == "" {
host = node.Host
}
Expand Down
6 changes: 5 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (c *httpConnector) Connect(conn net.Conn, addr string, options ...ConnectOp
if timeout <= 0 {
timeout = ConnectTimeout
}
ua := opts.UserAgent
if ua == "" {
ua = DefaultUserAgent
}

conn.SetDeadline(time.Now().Add(timeout))
defer conn.SetDeadline(time.Time{})
Expand All @@ -49,7 +53,7 @@ func (c *httpConnector) Connect(conn net.Conn, addr string, options ...ConnectOp
ProtoMinor: 1,
Header: make(http.Header),
}
req.Header.Set("User-Agent", DefaultUserAgent)
req.Header.Set("User-Agent", ua)
req.Header.Set("Proxy-Connection", "keep-alive")

user := opts.User
Expand Down
7 changes: 5 additions & 2 deletions http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (c *http2Connector) Connect(conn net.Conn, addr string, options ...ConnectO
for _, option := range options {
option(opts)
}
ua := opts.UserAgent
if ua == "" {
ua = DefaultUserAgent
}

cc, ok := conn.(*http2ClientConn)
if !ok {
Expand All @@ -56,8 +60,7 @@ func (c *http2Connector) Connect(conn net.Conn, addr string, options ...ConnectO
Host: addr,
ContentLength: -1,
}
// DEPRECATED
//req.Header.Set("Gost-Target", addr)
req.Header.Set("User-Agent", ua)

user := opts.User
if user == nil {
Expand Down
6 changes: 4 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Node struct {
Values url.Values
DialOptions []DialOption
HandshakeOptions []HandshakeOption
ConnectOptions []ConnectOption
Client *Client
marker *failMarker
Bypass *Bypass
Expand Down Expand Up @@ -129,18 +130,19 @@ func (node *Node) Get(key string) string {
return node.Values.Get(key)
}

// GetBool likes Get, but convert parameter value to bool.
// GetBool converts node parameter value to bool.
func (node *Node) GetBool(key string) bool {
b, _ := strconv.ParseBool(node.Values.Get(key))
return b
}

// GetInt likes Get, but convert parameter value to int.
// GetInt converts node parameter value to int.
func (node *Node) GetInt(key string) int {
n, _ := strconv.Atoi(node.Values.Get(key))
return n
}

// GetDuration converts node parameter value to time.Duration.
func (node *Node) GetDuration(key string) time.Duration {
d, _ := time.ParseDuration(node.Values.Get(key))
return d
Expand Down

0 comments on commit e996e7c

Please sign in to comment.