add binder for connector
This commit is contained in:
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
|
||||
}
|
Reference in New Issue
Block a user