adapt newest go-shadowsocks2 lib
This commit is contained in:
+14
-20
@@ -2,6 +2,7 @@ package ss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
@@ -43,18 +44,20 @@ func (c *ssConnector) Init(md md.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if c.options.Auth != nil {
|
||||
method := c.options.Auth.Username()
|
||||
password, _ := c.options.Auth.Password()
|
||||
|
||||
clientConfig, err := utils.NewClientConfig(method, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.client = core.NewTCPClient(clientConfig)
|
||||
if c.options.Auth == nil {
|
||||
return errors.New("ss: auth is required")
|
||||
}
|
||||
|
||||
method := c.options.Auth.Username()
|
||||
password, _ := c.options.Auth.Password()
|
||||
|
||||
clientConfig, err := utils.NewClientConfig(method, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.client = core.NewTCPClient(clientConfig)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,14 +109,5 @@ func (c *ssConnector) Connect(ctx context.Context, conn net.Conn, network, addre
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var sc net.Conn
|
||||
if c.md.noDelay {
|
||||
err := conn.(core.TCPConn).ClientFirstWrite()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
sc = conn
|
||||
|
||||
return sc, nil
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package ss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/connector"
|
||||
@@ -23,11 +24,10 @@ func init() {
|
||||
}
|
||||
|
||||
type ssuConnector struct {
|
||||
client core.UDPClient
|
||||
tcpClient core.TCPClient
|
||||
md metadata
|
||||
options connector.Options
|
||||
sessionMap sync.Map
|
||||
clientCfg core.ClientConfig
|
||||
tcpClient core.TCPClient
|
||||
md metadata
|
||||
options connector.Options
|
||||
}
|
||||
|
||||
func NewConnector(opts ...connector.Option) connector.Connector {
|
||||
@@ -46,17 +46,26 @@ func (c *ssuConnector) Init(md md.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if c.options.Auth != nil {
|
||||
method := c.options.Auth.Username()
|
||||
password, _ := c.options.Auth.Password()
|
||||
clientConfig, err := utils.NewClientConfig(method, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.client = core.NewUDPClient(clientConfig, 60)
|
||||
c.tcpClient = core.NewTCPClient(clientConfig)
|
||||
if c.options.Auth == nil {
|
||||
return errors.New("ss: auth is required")
|
||||
}
|
||||
|
||||
method := c.options.Auth.Username()
|
||||
password, _ := c.options.Auth.Password()
|
||||
clientConfig, err := utils.NewClientConfig(method, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
clientConfig.UDPTimeout = time.Minute
|
||||
|
||||
tcpClientConfig, err := utils.NewClientConfig(method, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.clientCfg = clientConfig
|
||||
c.tcpClient = core.NewTCPClient(tcpClientConfig)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -87,15 +96,29 @@ func (c *ssuConnector) Connect(ctx context.Context, conn net.Conn, network, addr
|
||||
if taddr == nil {
|
||||
taddr = &net.UDPAddr{}
|
||||
}
|
||||
serverAddr, err := netip.ParseAddrPort(conn.RemoteAddr().String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clientCfg := c.clientCfg
|
||||
clientCfg.ServerAddr = serverAddr
|
||||
client := core.NewUDPClient(core.ClientConfig{
|
||||
Cipher: clientCfg.Cipher,
|
||||
ServerAddr: serverAddr,
|
||||
UDPTimeout: time.Minute,
|
||||
})
|
||||
if err := client.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pc, ok := conn.(net.PacketConn)
|
||||
if ok {
|
||||
// standard UDP relay
|
||||
return ss.UDPClientConn(pc, conn.RemoteAddr(), taddr, c.md.udpBufferSize, &c.client, &c.sessionMap), nil
|
||||
return ss.UDPClientConn(pc, taddr, &client), nil
|
||||
}
|
||||
|
||||
target := socks.ParseAddr(taddr.String())
|
||||
conn, err := c.tcpClient.WrapConn(conn, target)
|
||||
conn, err = c.tcpClient.WrapConn(conn, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user