add host for client handshake
This commit is contained in:
parent
637e642341
commit
da30584df1
14
chain.go
14
chain.go
@ -100,7 +100,7 @@ func (c *Chain) IsEmpty() bool {
|
|||||||
// If the chain is empty, it will use the net.Dial directly.
|
// If the chain is empty, it will use the net.Dial directly.
|
||||||
func (c *Chain) Dial(addr string) (net.Conn, error) {
|
func (c *Chain) Dial(addr string) (net.Conn, error) {
|
||||||
if c.IsEmpty() {
|
if c.IsEmpty() {
|
||||||
return net.Dial("tcp", addr)
|
return net.DialTimeout("tcp", addr, DialTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
route, err := c.selectRoute()
|
route, err := c.selectRoute()
|
||||||
@ -108,7 +108,7 @@ func (c *Chain) Dial(addr string) (net.Conn, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := c.getConn(route)
|
conn, err := route.getConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -128,16 +128,16 @@ func (c *Chain) Conn() (conn net.Conn, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn, err = c.getConn(route)
|
conn, err = route.getConn()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Chain) getConn(route *Chain) (conn net.Conn, err error) {
|
func (c *Chain) getConn() (conn net.Conn, err error) {
|
||||||
if route.IsEmpty() {
|
if c.IsEmpty() {
|
||||||
err = ErrEmptyChain
|
err = ErrEmptyChain
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
nodes := route.Nodes()
|
nodes := c.Nodes()
|
||||||
node := nodes[0]
|
node := nodes[0]
|
||||||
|
|
||||||
cn, err := node.Client.Dial(node.Addr, node.DialOptions...)
|
cn, err := node.Client.Dial(node.Addr, node.DialOptions...)
|
||||||
@ -206,7 +206,7 @@ func (c *Chain) selectRoute() (route *Chain, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func selectIP(node *Node) (string, error) {
|
func selectIP(node *Node) (string, error) {
|
||||||
s := node.IPSelector
|
s := node.Selector
|
||||||
if s == nil {
|
if s == nil {
|
||||||
s = &RandomIPSelector{}
|
s = &RandomIPSelector{}
|
||||||
}
|
}
|
||||||
|
@ -116,6 +116,7 @@ func ChainDialOption(chain *Chain) DialOption {
|
|||||||
// HandshakeOptions describes the options for handshake.
|
// HandshakeOptions describes the options for handshake.
|
||||||
type HandshakeOptions struct {
|
type HandshakeOptions struct {
|
||||||
Addr string
|
Addr string
|
||||||
|
Host string
|
||||||
User *url.Userinfo
|
User *url.Userinfo
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
@ -136,6 +137,13 @@ func AddrHandshakeOption(addr string) HandshakeOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HostHandshakeOption specifies the hostname
|
||||||
|
func HostHandshakeOption(host string) HandshakeOption {
|
||||||
|
return func(opts *HandshakeOptions) {
|
||||||
|
opts.Host = host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// UserHandshakeOption specifies the user used by Transporter.Handshake
|
// UserHandshakeOption specifies the user used by Transporter.Handshake
|
||||||
func UserHandshakeOption(user *url.Userinfo) HandshakeOption {
|
func UserHandshakeOption(user *url.Userinfo) HandshakeOption {
|
||||||
return func(opts *HandshakeOptions) {
|
return func(opts *HandshakeOptions) {
|
||||||
|
@ -140,7 +140,7 @@ func parseChainNode(ns string) (node gost.Node, err error) {
|
|||||||
node.IPs[i] = ip + ":" + sport
|
node.IPs[i] = ip + ":" + sport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
node.IPSelector = &gost.RoundRobinIPSelector{}
|
node.Selector = &gost.RoundRobinIPSelector{}
|
||||||
|
|
||||||
users, err := parseUsers(node.Values.Get("secrets"))
|
users, err := parseUsers(node.Values.Get("secrets"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -265,6 +265,7 @@ func parseChainNode(ns string) (node gost.Node, err error) {
|
|||||||
retry, _ := strconv.Atoi(node.Values.Get("retry"))
|
retry, _ := strconv.Atoi(node.Values.Get("retry"))
|
||||||
node.HandshakeOptions = append(node.HandshakeOptions,
|
node.HandshakeOptions = append(node.HandshakeOptions,
|
||||||
gost.AddrHandshakeOption(node.Addr),
|
gost.AddrHandshakeOption(node.Addr),
|
||||||
|
gost.HostHandshakeOption(node.Host),
|
||||||
gost.UserHandshakeOption(node.User),
|
gost.UserHandshakeOption(node.User),
|
||||||
gost.TLSConfigHandshakeOption(tlsCfg),
|
gost.TLSConfigHandshakeOption(tlsCfg),
|
||||||
gost.IntervalHandshakeOption(time.Duration(interval)*time.Second),
|
gost.IntervalHandshakeOption(time.Duration(interval)*time.Second),
|
||||||
|
4
node.go
4
node.go
@ -11,6 +11,7 @@ type Node struct {
|
|||||||
ID int
|
ID int
|
||||||
Addr string
|
Addr string
|
||||||
IPs []string
|
IPs []string
|
||||||
|
Host string
|
||||||
Protocol string
|
Protocol string
|
||||||
Transport string
|
Transport string
|
||||||
Remote string // remote address, used by tcp/udp port forwarding
|
Remote string // remote address, used by tcp/udp port forwarding
|
||||||
@ -19,7 +20,7 @@ type Node struct {
|
|||||||
DialOptions []DialOption
|
DialOptions []DialOption
|
||||||
HandshakeOptions []HandshakeOption
|
HandshakeOptions []HandshakeOption
|
||||||
Client *Client
|
Client *Client
|
||||||
IPSelector IPSelector
|
Selector IPSelector
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseNode parses the node info.
|
// ParseNode parses the node info.
|
||||||
@ -40,6 +41,7 @@ func ParseNode(s string) (node Node, err error) {
|
|||||||
|
|
||||||
node = Node{
|
node = Node{
|
||||||
Addr: u.Host,
|
Addr: u.Host,
|
||||||
|
Host: u.Host,
|
||||||
Remote: strings.Trim(u.EscapedPath(), "/"),
|
Remote: strings.Trim(u.EscapedPath(), "/"),
|
||||||
Values: u.Query(),
|
Values: u.Query(),
|
||||||
User: u.User,
|
User: u.User,
|
||||||
|
5
obfs.go
5
obfs.go
@ -35,7 +35,7 @@ func (tr *obfsHTTPTransporter) Handshake(conn net.Conn, options ...HandshakeOpti
|
|||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(opts)
|
option(opts)
|
||||||
}
|
}
|
||||||
return &obfsHTTPConn{Conn: conn}, nil
|
return &obfsHTTPConn{Conn: conn, host: opts.Host}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type obfsHTTPListener struct {
|
type obfsHTTPListener struct {
|
||||||
@ -66,6 +66,7 @@ func (l *obfsHTTPListener) Accept() (net.Conn, error) {
|
|||||||
|
|
||||||
type obfsHTTPConn struct {
|
type obfsHTTPConn struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
|
host string
|
||||||
request *http.Request
|
request *http.Request
|
||||||
response *http.Response
|
response *http.Response
|
||||||
rbuf []byte
|
rbuf []byte
|
||||||
@ -151,7 +152,7 @@ func (c *obfsHTTPConn) clientHandshake() (err error) {
|
|||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
ProtoMajor: 1,
|
ProtoMajor: 1,
|
||||||
ProtoMinor: 1,
|
ProtoMinor: 1,
|
||||||
URL: &url.URL{Scheme: "http", Host: "www.baidu.com"},
|
URL: &url.URL{Scheme: "http", Host: c.host},
|
||||||
Header: make(http.Header),
|
Header: make(http.Header),
|
||||||
}
|
}
|
||||||
r.Header.Set("Connection", "keep-alive")
|
r.Header.Set("Connection", "keep-alive")
|
||||||
|
8
ws.go
8
ws.go
@ -129,7 +129,7 @@ func (tr *wsTransporter) Handshake(conn net.Conn, options ...HandshakeOption) (n
|
|||||||
if opts.WSOptions != nil {
|
if opts.WSOptions != nil {
|
||||||
wsOptions = opts.WSOptions
|
wsOptions = opts.WSOptions
|
||||||
}
|
}
|
||||||
url := url.URL{Scheme: "ws", Host: opts.Addr, Path: "/ws"}
|
url := url.URL{Scheme: "ws", Host: opts.Host, Path: "/ws"}
|
||||||
return websocketClientConn(url.String(), conn, nil, wsOptions)
|
return websocketClientConn(url.String(), conn, nil, wsOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,7 +210,7 @@ func (tr *mwsTransporter) initSession(addr string, conn net.Conn, opts *Handshak
|
|||||||
if opts.WSOptions != nil {
|
if opts.WSOptions != nil {
|
||||||
wsOptions = opts.WSOptions
|
wsOptions = opts.WSOptions
|
||||||
}
|
}
|
||||||
url := url.URL{Scheme: "ws", Host: opts.Addr, Path: "/ws"}
|
url := url.URL{Scheme: "ws", Host: opts.Host, Path: "/ws"}
|
||||||
conn, err := websocketClientConn(url.String(), conn, nil, wsOptions)
|
conn, err := websocketClientConn(url.String(), conn, nil, wsOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -252,7 +252,7 @@ func (tr *wssTransporter) Handshake(conn net.Conn, options ...HandshakeOption) (
|
|||||||
if opts.TLSConfig == nil {
|
if opts.TLSConfig == nil {
|
||||||
opts.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
opts.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
}
|
}
|
||||||
url := url.URL{Scheme: "wss", Host: opts.Addr, Path: "/ws"}
|
url := url.URL{Scheme: "wss", Host: opts.Host, Path: "/ws"}
|
||||||
return websocketClientConn(url.String(), conn, opts.TLSConfig, wsOptions)
|
return websocketClientConn(url.String(), conn, opts.TLSConfig, wsOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,7 +337,7 @@ func (tr *mwssTransporter) initSession(addr string, conn net.Conn, opts *Handsha
|
|||||||
if tlsConfig == nil {
|
if tlsConfig == nil {
|
||||||
tlsConfig = &tls.Config{InsecureSkipVerify: true}
|
tlsConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
}
|
}
|
||||||
url := url.URL{Scheme: "wss", Host: opts.Addr, Path: "/ws"}
|
url := url.URL{Scheme: "wss", Host: opts.Host, Path: "/ws"}
|
||||||
conn, err := websocketClientConn(url.String(), conn, tlsConfig, wsOptions)
|
conn, err := websocketClientConn(url.String(), conn, tlsConfig, wsOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user