initial commit
This commit is contained in:
111
connector/ss/connector.go
Normal file
111
connector/ss/connector.go
Normal file
@ -0,0 +1,111 @@
|
||||
package ss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gosocks5"
|
||||
"github.com/go-gost/gost/v3/pkg/common/bufpool"
|
||||
"github.com/go-gost/gost/v3/pkg/connector"
|
||||
md "github.com/go-gost/gost/v3/pkg/metadata"
|
||||
"github.com/go-gost/gost/v3/pkg/registry"
|
||||
"github.com/go-gost/x/internal/util/ss"
|
||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.ConnectorRegistry().Register("ss", NewConnector)
|
||||
}
|
||||
|
||||
type ssConnector struct {
|
||||
cipher core.Cipher
|
||||
md metadata
|
||||
options connector.Options
|
||||
}
|
||||
|
||||
func NewConnector(opts ...connector.Option) connector.Connector {
|
||||
options := connector.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &ssConnector{
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ssConnector) Init(md md.Metadata) (err error) {
|
||||
if err = c.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if c.options.Auth != nil {
|
||||
method := c.options.Auth.Username()
|
||||
password, _ := c.options.Auth.Password()
|
||||
c.cipher, err = ss.ShadowCipher(method, password, c.md.key)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *ssConnector) Connect(ctx context.Context, conn net.Conn, network, address string, opts ...connector.ConnectOption) (net.Conn, error) {
|
||||
log := c.options.Logger.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"network": network,
|
||||
"address": address,
|
||||
})
|
||||
log.Infof("connect %s/%s", address, network)
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
if _, ok := conn.(net.PacketConn); ok {
|
||||
err := fmt.Errorf("tcp over udp is unsupported")
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addr := gosocks5.Addr{}
|
||||
if err := addr.ParseFrom(address); err != nil {
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
rawaddr := bufpool.Get(512)
|
||||
defer bufpool.Put(rawaddr)
|
||||
|
||||
n, err := addr.Encode(*rawaddr)
|
||||
if err != nil {
|
||||
log.Error("encoding addr: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.md.connectTimeout > 0 {
|
||||
conn.SetDeadline(time.Now().Add(c.md.connectTimeout))
|
||||
defer conn.SetDeadline(time.Time{})
|
||||
}
|
||||
|
||||
if c.cipher != nil {
|
||||
conn = c.cipher.StreamConn(conn)
|
||||
}
|
||||
|
||||
var sc net.Conn
|
||||
if c.md.noDelay {
|
||||
sc = ss.ShadowConn(conn, nil)
|
||||
// write the addr at once.
|
||||
if _, err := sc.Write((*rawaddr)[:n]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// cache the header
|
||||
sc = ss.ShadowConn(conn, (*rawaddr)[:n])
|
||||
}
|
||||
|
||||
return sc, nil
|
||||
}
|
27
connector/ss/metadata.go
Normal file
27
connector/ss/metadata.go
Normal file
@ -0,0 +1,27 @@
|
||||
package ss
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/gost/v3/pkg/metadata"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
key string
|
||||
connectTimeout time.Duration
|
||||
noDelay bool
|
||||
}
|
||||
|
||||
func (c *ssConnector) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
key = "key"
|
||||
connectTimeout = "timeout"
|
||||
noDelay = "nodelay"
|
||||
)
|
||||
|
||||
c.md.key = mdata.GetString(md, key)
|
||||
c.md.connectTimeout = mdata.GetDuration(md, connectTimeout)
|
||||
c.md.noDelay = mdata.GetBool(md, noDelay)
|
||||
|
||||
return
|
||||
}
|
95
connector/ss/udp/connector.go
Normal file
95
connector/ss/udp/connector.go
Normal file
@ -0,0 +1,95 @@
|
||||
package ss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/v3/pkg/connector"
|
||||
md "github.com/go-gost/gost/v3/pkg/metadata"
|
||||
"github.com/go-gost/gost/v3/pkg/registry"
|
||||
"github.com/go-gost/x/internal/util/relay"
|
||||
"github.com/go-gost/x/internal/util/ss"
|
||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.ConnectorRegistry().Register("ssu", NewConnector)
|
||||
}
|
||||
|
||||
type ssuConnector struct {
|
||||
cipher core.Cipher
|
||||
md metadata
|
||||
options connector.Options
|
||||
}
|
||||
|
||||
func NewConnector(opts ...connector.Option) connector.Connector {
|
||||
options := connector.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &ssuConnector{
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ssuConnector) Init(md md.Metadata) (err error) {
|
||||
if err = c.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if c.options.Auth != nil {
|
||||
method := c.options.Auth.Username()
|
||||
password, _ := c.options.Auth.Password()
|
||||
c.cipher, err = ss.ShadowCipher(method, password, c.md.key)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *ssuConnector) Connect(ctx context.Context, conn net.Conn, network, address string, opts ...connector.ConnectOption) (net.Conn, error) {
|
||||
log := c.options.Logger.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"network": network,
|
||||
"address": address,
|
||||
})
|
||||
log.Infof("connect %s/%s", address, network)
|
||||
|
||||
switch network {
|
||||
case "udp", "udp4", "udp6":
|
||||
default:
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.md.connectTimeout > 0 {
|
||||
conn.SetDeadline(time.Now().Add(c.md.connectTimeout))
|
||||
defer conn.SetDeadline(time.Time{})
|
||||
}
|
||||
|
||||
taddr, _ := net.ResolveUDPAddr(network, address)
|
||||
if taddr == nil {
|
||||
taddr = &net.UDPAddr{}
|
||||
}
|
||||
|
||||
pc, ok := conn.(net.PacketConn)
|
||||
if ok {
|
||||
if c.cipher != nil {
|
||||
pc = c.cipher.PacketConn(pc)
|
||||
}
|
||||
|
||||
// standard UDP relay
|
||||
return ss.UDPClientConn(pc, conn.RemoteAddr(), taddr, c.md.bufferSize), nil
|
||||
}
|
||||
|
||||
if c.cipher != nil {
|
||||
conn = ss.ShadowConn(c.cipher.StreamConn(conn), nil)
|
||||
}
|
||||
|
||||
// UDP over TCP
|
||||
return relay.UDPTunClientConn(conn, taddr), nil
|
||||
}
|
33
connector/ss/udp/metadata.go
Normal file
33
connector/ss/udp/metadata.go
Normal file
@ -0,0 +1,33 @@
|
||||
package ss
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/gost/v3/pkg/metadata"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
key string
|
||||
connectTimeout time.Duration
|
||||
bufferSize int
|
||||
}
|
||||
|
||||
func (c *ssuConnector) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
key = "key"
|
||||
connectTimeout = "timeout"
|
||||
bufferSize = "bufferSize" // udp buffer size
|
||||
)
|
||||
|
||||
c.md.key = mdata.GetString(md, key)
|
||||
c.md.connectTimeout = mdata.GetDuration(md, connectTimeout)
|
||||
|
||||
if bs := mdata.GetInt(md, bufferSize); bs > 0 {
|
||||
c.md.bufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
||||
} else {
|
||||
c.md.bufferSize = 1024
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user