Browse Source

add user-agent option for http/http2

pull/417/head
ginuerzh 7 years ago
parent
commit
e996e7c35b
  1. 5
      chain.go
  2. 16
      client.go
  3. 4
      cmd/gost/route.go
  4. 6
      http.go
  5. 7
      http2.go
  6. 6
      node.go

5
chain.go

@ -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
@ -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()

16
client.go

@ -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.
@ -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
cmd/gost/route.go

@ -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
}

6
http.go

@ -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{})
@ -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

7
http2.go

@ -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 {
@ -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 {

6
node.go

@ -28,6 +28,7 @@ type Node struct {
Values url.Values
DialOptions []DialOption
HandshakeOptions []HandshakeOption
ConnectOptions []ConnectOption
Client *Client
marker *failMarker
Bypass *Bypass
@ -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

Loading…
Cancel
Save