add IP list for wss client
This commit is contained in:
parent
feb1b6428c
commit
2446f5d0a2
18
client.go
18
client.go
@ -93,6 +93,18 @@ func (tr *tcpTransporter) Multiplex() bool {
|
|||||||
type DialOptions struct {
|
type DialOptions struct {
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Chain *Chain
|
Chain *Chain
|
||||||
|
IPs []string
|
||||||
|
// count uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *DialOptions) getIP() string {
|
||||||
|
n := len(o.IPs)
|
||||||
|
if n == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return o.IPs[int(time.Now().Nanosecond())%n]
|
||||||
|
// count := atomic.AddUint32(&o.count, 1)
|
||||||
|
//return o.IPs[int(count)%n]
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialOption allows a common way to set dial options.
|
// DialOption allows a common way to set dial options.
|
||||||
@ -110,6 +122,12 @@ func ChainDialOption(chain *Chain) DialOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IPDialOption(ips ...string) DialOption {
|
||||||
|
return func(opts *DialOptions) {
|
||||||
|
opts.IPs = ips
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// HandshakeOptions describes the options for handshake.
|
// HandshakeOptions describes the options for handshake.
|
||||||
type HandshakeOptions struct {
|
type HandshakeOptions struct {
|
||||||
Addr string
|
Addr string
|
||||||
|
@ -112,6 +112,11 @@ func initChain() (*gost.Chain, error) {
|
|||||||
wsOpts.UserAgent = node.Values.Get("agent")
|
wsOpts.UserAgent = node.Values.Get("agent")
|
||||||
tr = gost.WSTransporter(wsOpts)
|
tr = gost.WSTransporter(wsOpts)
|
||||||
case "wss":
|
case "wss":
|
||||||
|
ips := strings.Split(node.Values.Get("ip"), ",")
|
||||||
|
node.DialOptions = append(node.DialOptions,
|
||||||
|
gost.IPDialOption(ips...),
|
||||||
|
)
|
||||||
|
|
||||||
wsOpts := &gost.WSOptions{}
|
wsOpts := &gost.WSOptions{}
|
||||||
wsOpts.EnableCompression = toBool(node.Values.Get("compression"))
|
wsOpts.EnableCompression = toBool(node.Values.Get("compression"))
|
||||||
wsOpts.ReadBufferSize, _ = strconv.Atoi(node.Values.Get("rbuf"))
|
wsOpts.ReadBufferSize, _ = strconv.Atoi(node.Values.Get("rbuf"))
|
||||||
|
18
ws.go
18
ws.go
@ -139,6 +139,24 @@ func WSSTransporter(opts *WSOptions) Transporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tr *wssTransporter) Dial(addr string, options ...DialOption) (net.Conn, error) {
|
||||||
|
opts := &DialOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
if ip := opts.getIP(); ip != "" {
|
||||||
|
_, sport, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
addr = ip + ":" + sport
|
||||||
|
}
|
||||||
|
if opts.Chain == nil {
|
||||||
|
return net.DialTimeout("tcp", addr, opts.Timeout)
|
||||||
|
}
|
||||||
|
return opts.Chain.Dial(addr)
|
||||||
|
}
|
||||||
|
|
||||||
func (tr *wssTransporter) Handshake(conn net.Conn, options ...HandshakeOption) (net.Conn, error) {
|
func (tr *wssTransporter) Handshake(conn net.Conn, options ...HandshakeOption) (net.Conn, error) {
|
||||||
opts := &HandshakeOptions{}
|
opts := &HandshakeOptions{}
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
|
Loading…
Reference in New Issue
Block a user