add binder for connector
This commit is contained in:
45
pkg/connector/bind.go
Normal file
45
pkg/connector/bind.go
Normal file
@ -0,0 +1,45 @@
|
||||
package connector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrBindUnsupported = errors.New("bind unsupported")
|
||||
)
|
||||
|
||||
type Accepter interface {
|
||||
Accept() (net.Conn, error)
|
||||
Addr() net.Addr
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Binder interface {
|
||||
Bind(ctx context.Context, conn net.Conn, network, address string, opts ...BindOption) (Accepter, error)
|
||||
}
|
||||
|
||||
type AcceptError struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func NewAcceptError(err error) error {
|
||||
return &AcceptError{err: err}
|
||||
}
|
||||
|
||||
func (e *AcceptError) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
func (e *AcceptError) Timeout() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *AcceptError) Temporary() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *AcceptError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
46
pkg/connector/forward/connector.go
Normal file
46
pkg/connector/forward/connector.go
Normal file
@ -0,0 +1,46 @@
|
||||
package forward
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.RegiserConnector("forward", NewConnector)
|
||||
}
|
||||
|
||||
type forwardConnector struct {
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func NewConnector(opts ...connector.Option) connector.Connector {
|
||||
options := &connector.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
return &forwardConnector{
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *forwardConnector) Init(md md.Metadata) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *forwardConnector) Connect(ctx context.Context, conn net.Conn, network, address string, opts ...connector.ConnectOption) (net.Conn, error) {
|
||||
c.logger = c.logger.WithFields(map[string]interface{}{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"network": network,
|
||||
"address": address,
|
||||
})
|
||||
c.logger.Infof("connect: %s/%s", address, network)
|
||||
|
||||
return conn, nil
|
||||
}
|
@ -20,3 +20,15 @@ type ConnectOptions struct {
|
||||
}
|
||||
|
||||
type ConnectOption func(opts *ConnectOptions)
|
||||
|
||||
type BindOptions struct {
|
||||
Mux bool
|
||||
}
|
||||
|
||||
type BindOption func(opts *BindOptions)
|
||||
|
||||
func MuxBindOption(mux bool) BindOption {
|
||||
return func(opts *BindOptions) {
|
||||
opts.Mux = mux
|
||||
}
|
||||
}
|
||||
|
117
pkg/connector/relay/conn.go
Normal file
117
pkg/connector/relay/conn.go
Normal file
@ -0,0 +1,117 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
"github.com/go-gost/relay"
|
||||
)
|
||||
|
||||
type conn struct {
|
||||
net.Conn
|
||||
udp bool
|
||||
wbuf bytes.Buffer
|
||||
once sync.Once
|
||||
headerSent bool
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
c.once.Do(func() {
|
||||
resp := relay.Response{}
|
||||
_, err = resp.ReadFrom(c.Conn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if resp.Version != relay.Version1 {
|
||||
err = relay.ErrBadVersion
|
||||
return
|
||||
}
|
||||
if resp.Status != relay.StatusOK {
|
||||
err = fmt.Errorf("status %d", resp.Status)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !c.udp {
|
||||
return c.Conn.Read(b)
|
||||
}
|
||||
|
||||
var bb [2]byte
|
||||
_, err = io.ReadFull(c.Conn, bb[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dlen := int(binary.BigEndian.Uint16(bb[:]))
|
||||
if len(b) >= dlen {
|
||||
return io.ReadFull(c.Conn, b[:dlen])
|
||||
}
|
||||
buf := make([]byte, dlen)
|
||||
_, err = io.ReadFull(c.Conn, buf)
|
||||
n = copy(b, buf)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *conn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||
n, err = c.Read(b)
|
||||
addr = c.Conn.RemoteAddr()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *conn) Write(b []byte) (n int, err error) {
|
||||
if len(b) > 0xFFFF {
|
||||
err = errors.New("write: data maximum exceeded")
|
||||
return
|
||||
}
|
||||
n = len(b) // force byte length consistent
|
||||
if c.wbuf.Len() > 0 {
|
||||
if c.udp {
|
||||
var bb [2]byte
|
||||
binary.BigEndian.PutUint16(bb[:2], uint16(len(b)))
|
||||
c.wbuf.Write(bb[:])
|
||||
c.headerSent = true
|
||||
}
|
||||
c.wbuf.Write(b) // append the data to the cached header
|
||||
// _, err = c.Conn.Write(c.wbuf.Bytes())
|
||||
// c.wbuf.Reset()
|
||||
_, err = c.wbuf.WriteTo(c.Conn)
|
||||
return
|
||||
}
|
||||
|
||||
if !c.udp {
|
||||
return c.Conn.Write(b)
|
||||
}
|
||||
if !c.headerSent {
|
||||
c.headerSent = true
|
||||
b2 := make([]byte, len(b)+2)
|
||||
copy(b2, b)
|
||||
_, err = c.Conn.Write(b2)
|
||||
return
|
||||
}
|
||||
nsize := 2 + len(b)
|
||||
var buf []byte
|
||||
if nsize <= mediumBufferSize {
|
||||
buf = mPool.Get().([]byte)
|
||||
defer mPool.Put(buf)
|
||||
} else {
|
||||
buf = make([]byte, nsize)
|
||||
}
|
||||
binary.BigEndian.PutUint16(buf[:2], uint16(len(b)))
|
||||
n = copy(buf[2:], b)
|
||||
_, err = c.Conn.Write(buf[:nsize])
|
||||
return
|
||||
}
|
||||
|
||||
func (c *relayConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||
return c.Write(b)
|
||||
}
|
101
pkg/connector/relay/connector.go
Normal file
101
pkg/connector/relay/connector.go
Normal file
@ -0,0 +1,101 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
"github.com/go-gost/relay"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.RegiserConnector("relay", NewConnector)
|
||||
}
|
||||
|
||||
type relayConnector struct {
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
}
|
||||
|
||||
func NewConnector(opts ...connector.Option) connector.Connector {
|
||||
options := &connector.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
return &relayConnector{
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *relayConnector) Init(md md.Metadata) (err error) {
|
||||
return c.parseMetadata(md)
|
||||
}
|
||||
|
||||
func (c *relayConnector) Connect(ctx context.Context, conn net.Conn, network, address string, opts ...connector.ConnectOption) (net.Conn, error) {
|
||||
c.logger = c.logger.WithFields(map[string]interface{}{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"network": network,
|
||||
"address": address,
|
||||
})
|
||||
c.logger.Infof("connect: %s/%s", address, network)
|
||||
|
||||
if c.md.connectTimeout > 0 {
|
||||
conn.SetDeadline(time.Now().Add(c.md.connectTimeout))
|
||||
defer conn.SetDeadline(time.Time{})
|
||||
}
|
||||
|
||||
var udpMode bool
|
||||
if network == "udp" || network == "udp4" || network == "udp6" {
|
||||
udpMode = true
|
||||
}
|
||||
|
||||
req := relay.Request{
|
||||
Version: relay.Version1,
|
||||
}
|
||||
if udpMode {
|
||||
req.Flags |= relay.FUDP
|
||||
}
|
||||
|
||||
if c.md.user != nil {
|
||||
pwd, _ := c.md.user.Password()
|
||||
req.Features = append(req.Features, &relay.UserAuthFeature{
|
||||
Username: c.md.user.Username(),
|
||||
Password: pwd,
|
||||
})
|
||||
}
|
||||
|
||||
if address != "" {
|
||||
host, port, _ := net.SplitHostPort(address)
|
||||
nport, _ := strconv.ParseUint(port, 10, 16)
|
||||
if host == "" {
|
||||
host = net.IPv4zero.String()
|
||||
}
|
||||
|
||||
if nport > 0 {
|
||||
var atype uint8
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
atype = relay.AddrDomain
|
||||
} else if ip.To4() == nil {
|
||||
atype = relay.AddrIPv6
|
||||
} else {
|
||||
atype = relay.AddrIPv4
|
||||
}
|
||||
|
||||
req.Features = append(req.Features, &relay.TargetAddrFeature{
|
||||
AType: atype,
|
||||
Host: host,
|
||||
Port: uint16(nport),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
36
pkg/connector/relay/metadata.go
Normal file
36
pkg/connector/relay/metadata.go
Normal file
@ -0,0 +1,36 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
connectTimeout time.Duration
|
||||
user *url.Userinfo
|
||||
nodelay bool
|
||||
}
|
||||
|
||||
func (c *relayConnector) parseMetadata(md md.Metadata) (err error) {
|
||||
const (
|
||||
auth = "auth"
|
||||
connectTimeout = "connectTimeout"
|
||||
nodelay = "nodelay"
|
||||
)
|
||||
|
||||
if v := md.GetString(auth); v != "" {
|
||||
ss := strings.SplitN(v, ":", 2)
|
||||
if len(ss) == 1 {
|
||||
c.md.user = url.User(ss[0])
|
||||
} else {
|
||||
c.md.user = url.UserPassword(ss[0], ss[1])
|
||||
}
|
||||
}
|
||||
c.md.connectTimeout = md.GetDuration(connectTimeout)
|
||||
c.md.nodelay = md.GetBool(nodelay)
|
||||
|
||||
return
|
||||
}
|
191
pkg/connector/socks/v5/accepter.go
Normal file
191
pkg/connector/socks/v5/accepter.go
Normal file
@ -0,0 +1,191 @@
|
||||
package v5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gosocks5"
|
||||
"github.com/go-gost/gost/pkg/common/bufpool"
|
||||
"github.com/go-gost/gost/pkg/common/util/mux"
|
||||
"github.com/go-gost/gost/pkg/common/util/udp"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
type tcpAccepter struct {
|
||||
addr net.Addr
|
||||
conn net.Conn
|
||||
logger logger.Logger
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (p *tcpAccepter) Accept() (net.Conn, error) {
|
||||
select {
|
||||
case <-p.done:
|
||||
return nil, io.EOF
|
||||
default:
|
||||
close(p.done)
|
||||
}
|
||||
|
||||
// second reply, peer connected
|
||||
rep, err := gosocks5.ReadReply(p.conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.logger.Debug(rep)
|
||||
|
||||
if rep.Rep != gosocks5.Succeeded {
|
||||
return nil, fmt.Errorf("peer connect failed")
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", rep.Addr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &bindConn{
|
||||
Conn: p.conn,
|
||||
localAddr: p.addr,
|
||||
remoteAddr: raddr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *tcpAccepter) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *tcpAccepter) Close() error {
|
||||
return p.conn.Close()
|
||||
}
|
||||
|
||||
type tcpMuxAccepter struct {
|
||||
addr net.Addr
|
||||
session *mux.Session
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) Accept() (net.Conn, error) {
|
||||
cc, err := p.session.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn, err := p.getPeerConn(cc)
|
||||
if err != nil {
|
||||
cc.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) getPeerConn(conn net.Conn) (net.Conn, error) {
|
||||
// second reply, peer connected
|
||||
rep, err := gosocks5.ReadReply(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.logger.Debug(rep)
|
||||
|
||||
if rep.Rep != gosocks5.Succeeded {
|
||||
err = fmt.Errorf("peer connect failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", rep.Addr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &bindConn{
|
||||
Conn: conn,
|
||||
localAddr: p.addr,
|
||||
remoteAddr: raddr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) Close() error {
|
||||
return p.session.Close()
|
||||
}
|
||||
|
||||
type udpAccepter struct {
|
||||
addr net.Addr
|
||||
conn net.PacketConn
|
||||
cqueue chan net.Conn
|
||||
connPool *udp.ConnPool
|
||||
readQueueSize int
|
||||
readBufferSize int
|
||||
closed chan struct{}
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (p *udpAccepter) Accept() (conn net.Conn, err error) {
|
||||
select {
|
||||
case conn = <-p.cqueue:
|
||||
return
|
||||
case <-p.closed:
|
||||
return nil, net.ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
func (p *udpAccepter) acceptLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-p.closed:
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
b := bufpool.Get(p.readBufferSize)
|
||||
|
||||
n, raddr, err := p.conn.ReadFrom(b)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
c := p.getConn(raddr)
|
||||
if c == nil {
|
||||
bufpool.Put(b)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := c.WriteQueue(b[:n]); err != nil {
|
||||
p.logger.Warn("data discarded: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *udpAccepter) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *udpAccepter) Close() error {
|
||||
select {
|
||||
case <-p.closed:
|
||||
default:
|
||||
close(p.closed)
|
||||
p.connPool.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *udpAccepter) getConn(raddr net.Addr) *udp.Conn {
|
||||
c, ok := p.connPool.Get(raddr.String())
|
||||
if !ok {
|
||||
c = udp.NewConn(p.conn, p.addr, raddr, p.readQueueSize)
|
||||
select {
|
||||
case p.cqueue <- c:
|
||||
p.connPool.Set(raddr.String(), c)
|
||||
default:
|
||||
c.Close()
|
||||
p.logger.Warnf("connection queue is full, client %s discarded", raddr)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
137
pkg/connector/socks/v5/bind.go
Normal file
137
pkg/connector/socks/v5/bind.go
Normal file
@ -0,0 +1,137 @@
|
||||
package v5
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gosocks5"
|
||||
"github.com/go-gost/gost/pkg/common/util/mux"
|
||||
"github.com/go-gost/gost/pkg/common/util/socks"
|
||||
"github.com/go-gost/gost/pkg/common/util/udp"
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
)
|
||||
|
||||
// Bind implements connector.Binder.
|
||||
func (c *socks5Connector) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (connector.Accepter, error) {
|
||||
c.logger = c.logger.WithFields(map[string]interface{}{
|
||||
"network": network,
|
||||
"address": address,
|
||||
})
|
||||
c.logger.Infof("bind: %s/%s", address, network)
|
||||
|
||||
options := connector.BindOptions{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
if options.Mux {
|
||||
return c.muxBindTCP(ctx, conn, network, address)
|
||||
}
|
||||
return c.bindTCP(ctx, conn, network, address)
|
||||
case "udp", "udp4", "udp6":
|
||||
return c.bindUDP(ctx, conn, network, address)
|
||||
default:
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
c.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (c *socks5Connector) bindTCP(ctx context.Context, conn net.Conn, network, address string) (connector.Accepter, error) {
|
||||
laddr, err := c.bind(conn, gosocks5.CmdBind, network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tcpAccepter{
|
||||
addr: laddr,
|
||||
conn: conn,
|
||||
logger: c.logger,
|
||||
done: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *socks5Connector) muxBindTCP(ctx context.Context, conn net.Conn, network, address string) (connector.Accepter, error) {
|
||||
laddr, err := c.bind(conn, socks.CmdMuxBind, network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session, err := mux.ServerSession(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tcpMuxAccepter{
|
||||
addr: laddr,
|
||||
session: session,
|
||||
logger: c.logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *socks5Connector) bindUDP(ctx context.Context, conn net.Conn, network, address string) (connector.Accepter, error) {
|
||||
laddr, err := c.bind(conn, socks.CmdUDPTun, network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accepter := &udpAccepter{
|
||||
addr: laddr,
|
||||
conn: socks.UDPTunClientPacketConn(conn),
|
||||
cqueue: make(chan net.Conn, c.md.backlog),
|
||||
connPool: udp.NewConnPool(c.md.ttl).WithLogger(c.logger),
|
||||
readQueueSize: c.md.readQueueSize,
|
||||
readBufferSize: c.md.readBufferSize,
|
||||
closed: make(chan struct{}),
|
||||
logger: c.logger,
|
||||
}
|
||||
go accepter.acceptLoop()
|
||||
|
||||
return accepter, nil
|
||||
}
|
||||
|
||||
func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string) (net.Addr, error) {
|
||||
laddr, err := net.ResolveTCPAddr(network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addr := gosocks5.Addr{}
|
||||
addr.ParseFrom(laddr.String())
|
||||
req := gosocks5.NewRequest(cmd, &addr)
|
||||
if err := req.Write(conn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.logger.Debug(req)
|
||||
|
||||
// first reply, bind status
|
||||
reply, err := gosocks5.ReadReply(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l.logger.Debug(reply)
|
||||
|
||||
if reply.Rep != gosocks5.Succeeded {
|
||||
return nil, fmt.Errorf("bind on %s/%s failed", laddr, laddr.Network())
|
||||
}
|
||||
|
||||
var baddr net.Addr
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
baddr, err = net.ResolveTCPAddr(network, reply.Addr.String())
|
||||
case "udp", "udp4", "udp6":
|
||||
baddr, err = net.ResolveUDPAddr(network, reply.Addr.String())
|
||||
default:
|
||||
err = fmt.Errorf("unknown network %s", network)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.logger.Debugf("bind on %s/%s OK", baddr, baddr.Network())
|
||||
|
||||
return laddr, nil
|
||||
}
|
17
pkg/connector/socks/v5/conn.go
Normal file
17
pkg/connector/socks/v5/conn.go
Normal file
@ -0,0 +1,17 @@
|
||||
package v5
|
||||
|
||||
import "net"
|
||||
|
||||
type bindConn struct {
|
||||
net.Conn
|
||||
localAddr net.Addr
|
||||
remoteAddr net.Addr
|
||||
}
|
||||
|
||||
func (c *bindConn) LocalAddr() net.Addr {
|
||||
return c.localAddr
|
||||
}
|
||||
|
||||
func (c *bindConn) RemoteAddr() net.Addr {
|
||||
return c.remoteAddr
|
||||
}
|
@ -9,11 +9,23 @@ import (
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTTL = 60 * time.Second
|
||||
defaultReadBufferSize = 4096
|
||||
defaultReadQueueSize = 128
|
||||
defaultBacklog = 128
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
connectTimeout time.Duration
|
||||
User *url.Userinfo
|
||||
tlsConfig *tls.Config
|
||||
noTLS bool
|
||||
|
||||
ttl time.Duration
|
||||
readBufferSize int
|
||||
readQueueSize int
|
||||
backlog int
|
||||
}
|
||||
|
||||
func (c *socks5Connector) parseMetadata(md md.Metadata) (err error) {
|
||||
@ -21,6 +33,11 @@ func (c *socks5Connector) parseMetadata(md md.Metadata) (err error) {
|
||||
connectTimeout = "timeout"
|
||||
auth = "auth"
|
||||
noTLS = "notls"
|
||||
|
||||
ttl = "ttl"
|
||||
readBufferSize = "readBufferSize"
|
||||
readQueueSize = "readQueueSize"
|
||||
backlog = "backlog"
|
||||
)
|
||||
|
||||
if v := md.GetString(auth); v != "" {
|
||||
@ -35,5 +52,23 @@ func (c *socks5Connector) parseMetadata(md md.Metadata) (err error) {
|
||||
c.md.connectTimeout = md.GetDuration(connectTimeout)
|
||||
c.md.noTLS = md.GetBool(noTLS)
|
||||
|
||||
c.md.ttl = md.GetDuration(ttl)
|
||||
if c.md.ttl <= 0 {
|
||||
c.md.ttl = defaultTTL
|
||||
}
|
||||
c.md.readBufferSize = md.GetInt(readBufferSize)
|
||||
if c.md.readBufferSize <= 0 {
|
||||
c.md.readBufferSize = defaultReadBufferSize
|
||||
}
|
||||
|
||||
c.md.readQueueSize = md.GetInt(readQueueSize)
|
||||
if c.md.readQueueSize <= 0 {
|
||||
c.md.readQueueSize = defaultReadQueueSize
|
||||
}
|
||||
|
||||
c.md.backlog = md.GetInt(backlog)
|
||||
if c.md.backlog <= 0 {
|
||||
c.md.backlog = defaultBacklog
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user