add UDP support for reverse proxy tunnel
This commit is contained in:
parent
9750998940
commit
416405b1f0
@ -232,3 +232,10 @@ func (c *udpConn) SetDSCP(n int) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *udpConn) Metadata() metadata.Metadata {
|
||||||
|
if md, ok := c.PacketConn.(metadata.Metadatable); ok {
|
||||||
|
return md.Metadata()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
// Bind implements connector.Binder.
|
// Bind implements connector.Binder.
|
||||||
func (c *relayConnector) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
func (c *relayConnector) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||||
if !c.md.tunnelID.IsZero() {
|
if !c.md.tunnelID.IsZero() {
|
||||||
return c.tunnel(ctx, conn, c.options.Logger)
|
return c.bindTunnel(ctx, conn, network, c.options.Logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
log := c.options.Logger.WithFields(map[string]any{
|
log := c.options.Logger.WithFields(map[string]any{
|
||||||
@ -43,31 +43,36 @@ func (c *relayConnector) Bind(ctx context.Context, conn net.Conn, network, addre
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *relayConnector) tunnel(ctx context.Context, conn net.Conn, log logger.Logger) (net.Listener, error) {
|
func (c *relayConnector) bindTunnel(ctx context.Context, conn net.Conn, network string, log logger.Logger) (net.Listener, error) {
|
||||||
addr, cid, err := c.initTunnel(conn)
|
addr, cid, err := c.initTunnel(conn, network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Debugf("create tunnel %s connector %s OK", c.md.tunnelID.String(), cid)
|
log.Debugf("create tunnel %s connector %s/%s OK", c.md.tunnelID.String(), cid, network)
|
||||||
|
|
||||||
session, err := mux.ServerSession(conn)
|
session, err := mux.ServerSession(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &tcpListener{
|
return &bindListener{
|
||||||
|
network: network,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
session: session,
|
session: session,
|
||||||
logger: log,
|
logger: log,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *relayConnector) initTunnel(conn net.Conn) (addr net.Addr, cid relay.ConnectorID, err error) {
|
func (c *relayConnector) initTunnel(conn net.Conn, network string) (addr net.Addr, cid relay.ConnectorID, err error) {
|
||||||
req := relay.Request{
|
req := relay.Request{
|
||||||
Version: relay.Version1,
|
Version: relay.Version1,
|
||||||
Cmd: relay.CmdBind,
|
Cmd: relay.CmdBind,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if network == "udp" {
|
||||||
|
req.Cmd |= relay.FUDP
|
||||||
|
}
|
||||||
|
|
||||||
if c.options.Auth != nil {
|
if c.options.Auth != nil {
|
||||||
pwd, _ := c.options.Auth.Password()
|
pwd, _ := c.options.Auth.Password()
|
||||||
req.Features = append(req.Features, &relay.UserAuthFeature{
|
req.Features = append(req.Features, &relay.UserAuthFeature{
|
||||||
@ -77,7 +82,7 @@ func (c *relayConnector) initTunnel(conn net.Conn) (addr net.Addr, cid relay.Con
|
|||||||
}
|
}
|
||||||
|
|
||||||
req.Features = append(req.Features, &relay.TunnelFeature{
|
req.Features = append(req.Features, &relay.TunnelFeature{
|
||||||
ID: c.md.tunnelID,
|
ID: c.md.tunnelID.ID(),
|
||||||
})
|
})
|
||||||
if _, err = req.WriteTo(conn); err != nil {
|
if _, err = req.WriteTo(conn); err != nil {
|
||||||
return
|
return
|
||||||
@ -102,7 +107,7 @@ func (c *relayConnector) initTunnel(conn net.Conn) (addr net.Addr, cid relay.Con
|
|||||||
}
|
}
|
||||||
case relay.FeatureTunnel:
|
case relay.FeatureTunnel:
|
||||||
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
|
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
|
||||||
cid = feature.ID
|
cid = relay.NewConnectorID(feature.ID[:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,7 +127,7 @@ func (c *relayConnector) bindTCP(ctx context.Context, conn net.Conn, network, ad
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &tcpListener{
|
return &bindListener{
|
||||||
addr: laddr,
|
addr: laddr,
|
||||||
session: session,
|
session: session,
|
||||||
logger: log,
|
logger: log,
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/go-gost/core/common/bufpool"
|
||||||
mdata "github.com/go-gost/core/metadata"
|
mdata "github.com/go-gost/core/metadata"
|
||||||
"github.com/go-gost/relay"
|
"github.com/go-gost/relay"
|
||||||
)
|
)
|
||||||
@ -67,9 +68,11 @@ func (c *udpConn) Read(b []byte) (n int, err error) {
|
|||||||
if len(b) >= dlen {
|
if len(b) >= dlen {
|
||||||
return io.ReadFull(c.Conn, b[:dlen])
|
return io.ReadFull(c.Conn, b[:dlen])
|
||||||
}
|
}
|
||||||
buf := make([]byte, dlen)
|
|
||||||
_, err = io.ReadFull(c.Conn, buf)
|
buf := bufpool.Get(dlen)
|
||||||
n = copy(b, buf)
|
defer bufpool.Put(buf)
|
||||||
|
_, err = io.ReadFull(c.Conn, *buf)
|
||||||
|
n = copy(b, *buf)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -137,3 +140,72 @@ func (c *bindConn) RemoteAddr() net.Addr {
|
|||||||
func (c *bindConn) Metadata() mdata.Metadata {
|
func (c *bindConn) Metadata() mdata.Metadata {
|
||||||
return c.md
|
return c.md
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type bindUDPConn struct {
|
||||||
|
net.Conn
|
||||||
|
localAddr net.Addr
|
||||||
|
remoteAddr net.Addr
|
||||||
|
md mdata.Metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bindUDPConn) Read(b []byte) (n int, err error) {
|
||||||
|
// 2-byte data length header
|
||||||
|
var bh [2]byte
|
||||||
|
_, err = io.ReadFull(c.Conn, bh[:])
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dlen := int(binary.BigEndian.Uint16(bh[:]))
|
||||||
|
if len(b) >= dlen {
|
||||||
|
n, err = io.ReadFull(c.Conn, b[:dlen])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bufpool.Get(dlen)
|
||||||
|
defer bufpool.Put(buf)
|
||||||
|
|
||||||
|
_, err = io.ReadFull(c.Conn, *buf)
|
||||||
|
n = copy(b, *buf)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bindUDPConn) Write(b []byte) (n int, err error) {
|
||||||
|
if len(b) > math.MaxUint16 {
|
||||||
|
err = errors.New("write: data maximum exceeded")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2-byte data length header
|
||||||
|
var bh [2]byte
|
||||||
|
binary.BigEndian.PutUint16(bh[:], uint16(len(b)))
|
||||||
|
_, err = c.Conn.Write(bh[:])
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return c.Conn.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bindUDPConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||||
|
addr = c.remoteAddr
|
||||||
|
n, err = c.Read(b)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bindUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
|
return c.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bindUDPConn) LocalAddr() net.Addr {
|
||||||
|
return c.localAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bindUDPConn) RemoteAddr() net.Addr {
|
||||||
|
return c.remoteAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Metadata implements metadata.Metadatable interface.
|
||||||
|
func (c *bindUDPConn) Metadata() mdata.Metadata {
|
||||||
|
return c.md
|
||||||
|
}
|
||||||
|
@ -88,7 +88,7 @@ func (c *relayConnector) Connect(ctx context.Context, conn net.Conn, network, ad
|
|||||||
|
|
||||||
if !c.md.tunnelID.IsZero() {
|
if !c.md.tunnelID.IsZero() {
|
||||||
req.Features = append(req.Features, &relay.TunnelFeature{
|
req.Features = append(req.Features, &relay.TunnelFeature{
|
||||||
ID: c.md.tunnelID,
|
ID: c.md.tunnelID.ID(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,18 +6,20 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/go-gost/core/logger"
|
"github.com/go-gost/core/logger"
|
||||||
|
mdata "github.com/go-gost/core/metadata"
|
||||||
"github.com/go-gost/relay"
|
"github.com/go-gost/relay"
|
||||||
"github.com/go-gost/x/internal/util/mux"
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
mdx "github.com/go-gost/x/metadata"
|
mdx "github.com/go-gost/x/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tcpListener struct {
|
type bindListener struct {
|
||||||
|
network string
|
||||||
addr net.Addr
|
addr net.Addr
|
||||||
session *mux.Session
|
session *mux.Session
|
||||||
logger logger.Logger
|
logger logger.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *tcpListener) Accept() (net.Conn, error) {
|
func (p *bindListener) Accept() (net.Conn, error) {
|
||||||
cc, err := p.session.Accept()
|
cc, err := p.session.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -33,7 +35,7 @@ func (p *tcpListener) Accept() (net.Conn, error) {
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *tcpListener) getPeerConn(conn net.Conn) (net.Conn, error) {
|
func (p *bindListener) getPeerConn(conn net.Conn) (net.Conn, error) {
|
||||||
// second reply, peer connected
|
// second reply, peer connected
|
||||||
resp := relay.Response{}
|
resp := relay.Response{}
|
||||||
if _, err := resp.ReadFrom(conn); err != nil {
|
if _, err := resp.ReadFrom(conn); err != nil {
|
||||||
@ -64,21 +66,33 @@ func (p *tcpListener) getPeerConn(conn net.Conn) (net.Conn, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var md mdata.Metadata
|
||||||
|
if host != "" {
|
||||||
|
md = mdx.NewMetadata(map[string]any{"host": host})
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.network == "udp" {
|
||||||
|
return &bindUDPConn{
|
||||||
|
Conn: conn,
|
||||||
|
localAddr: p.addr,
|
||||||
|
remoteAddr: raddr,
|
||||||
|
md: md,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
cn := &bindConn{
|
cn := &bindConn{
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
localAddr: p.addr,
|
localAddr: p.addr,
|
||||||
remoteAddr: raddr,
|
remoteAddr: raddr,
|
||||||
}
|
md: md,
|
||||||
if host != "" {
|
|
||||||
cn.md = mdx.NewMetadata(map[string]any{"host": host})
|
|
||||||
}
|
}
|
||||||
return cn, nil
|
return cn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *tcpListener) Addr() net.Addr {
|
func (p *bindListener) Addr() net.Addr {
|
||||||
return p.addr
|
return p.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *tcpListener) Close() error {
|
func (p *bindListener) Close() error {
|
||||||
return p.session.Close()
|
return p.session.Close()
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ func (c *ssuConnector) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
if bs := mdutil.GetInt(md, bufferSize); bs > 0 {
|
if bs := mdutil.GetInt(md, bufferSize); bs > 0 {
|
||||||
c.md.bufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
c.md.bufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
||||||
} else {
|
} else {
|
||||||
c.md.bufferSize = 1500
|
c.md.bufferSize = 4096
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -92,11 +92,9 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
|||||||
var rw io.ReadWriter = conn
|
var rw io.ReadWriter = conn
|
||||||
var host string
|
var host string
|
||||||
var protocol string
|
var protocol string
|
||||||
if h.md.sniffing {
|
if network == "tcp" && h.md.sniffing {
|
||||||
if network == "tcp" {
|
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
||||||
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
||||||
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if protocol == forward.ProtoHTTP {
|
if protocol == forward.ProtoHTTP {
|
||||||
@ -107,19 +105,23 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
|||||||
if _, _, err := net.SplitHostPort(host); err != nil {
|
if _, _, err := net.SplitHostPort(host); err != nil {
|
||||||
host = net.JoinHostPort(host, "0")
|
host = net.JoinHostPort(host, "0")
|
||||||
}
|
}
|
||||||
target := &chain.Node{
|
|
||||||
Addr: host,
|
var target *chain.Node
|
||||||
|
if host != "" {
|
||||||
|
target = &chain.Node{
|
||||||
|
Addr: host,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if h.hop != nil {
|
if h.hop != nil {
|
||||||
target = h.hop.Select(ctx,
|
target = h.hop.Select(ctx,
|
||||||
chain.HostSelectOption(host),
|
chain.HostSelectOption(host),
|
||||||
chain.ProtocolSelectOption(protocol),
|
chain.ProtocolSelectOption(protocol),
|
||||||
)
|
)
|
||||||
if target == nil {
|
}
|
||||||
err := errors.New("target not available")
|
if target == nil {
|
||||||
log.Error(err)
|
err := errors.New("target not available")
|
||||||
return err
|
log.Error(err)
|
||||||
}
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log = log.WithFields(map[string]any{
|
log = log.WithFields(map[string]any{
|
||||||
@ -172,7 +174,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var target *chain.Node
|
target := &chain.Node{
|
||||||
|
Addr: req.Host,
|
||||||
|
}
|
||||||
if h.hop != nil {
|
if h.hop != nil {
|
||||||
target = h.hop.Select(ctx,
|
target = h.hop.Select(ctx,
|
||||||
chain.HostSelectOption(req.Host),
|
chain.HostSelectOption(req.Host),
|
||||||
@ -235,6 +239,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
|||||||
go func() {
|
go func() {
|
||||||
defer cc.Close()
|
defer cc.Close()
|
||||||
err := xnet.CopyBuffer(rw, cc, 8192)
|
err := xnet.CopyBuffer(rw, cc, 8192)
|
||||||
|
if err != nil {
|
||||||
|
resp.Write(rw)
|
||||||
|
}
|
||||||
log.Debugf("close connection to node %s(%s), reason: %v", target.Name, target.Addr, err)
|
log.Debugf("close connection to node %s(%s), reason: %v", target.Name, target.Addr, err)
|
||||||
connPool.Delete(target)
|
connPool.Delete(target)
|
||||||
}()
|
}()
|
||||||
@ -266,9 +273,12 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cc.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// log.Error(err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,11 +92,9 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
|||||||
var rw io.ReadWriter = conn
|
var rw io.ReadWriter = conn
|
||||||
var host string
|
var host string
|
||||||
var protocol string
|
var protocol string
|
||||||
if h.md.sniffing {
|
if network == "tcp" && h.md.sniffing {
|
||||||
if network == "tcp" {
|
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
||||||
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
||||||
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if protocol == forward.ProtoHTTP {
|
if protocol == forward.ProtoHTTP {
|
||||||
h.handleHTTP(ctx, rw, log)
|
h.handleHTTP(ctx, rw, log)
|
||||||
@ -109,6 +107,11 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var target *chain.Node
|
var target *chain.Node
|
||||||
|
if host != "" {
|
||||||
|
target = &chain.Node{
|
||||||
|
Addr: host,
|
||||||
|
}
|
||||||
|
}
|
||||||
if h.hop != nil {
|
if h.hop != nil {
|
||||||
target = h.hop.Select(ctx,
|
target = h.hop.Select(ctx,
|
||||||
chain.HostSelectOption(host),
|
chain.HostSelectOption(host),
|
||||||
@ -172,7 +175,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var target *chain.Node
|
target := &chain.Node{
|
||||||
|
Addr: req.Host,
|
||||||
|
}
|
||||||
if h.hop != nil {
|
if h.hop != nil {
|
||||||
target = h.hop.Select(ctx,
|
target = h.hop.Select(ctx,
|
||||||
chain.HostSelectOption(req.Host),
|
chain.HostSelectOption(req.Host),
|
||||||
@ -235,6 +240,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
|||||||
go func() {
|
go func() {
|
||||||
defer cc.Close()
|
defer cc.Close()
|
||||||
err := xnet.CopyBuffer(rw, cc, 8192)
|
err := xnet.CopyBuffer(rw, cc, 8192)
|
||||||
|
if err != nil {
|
||||||
|
resp.Write(rw)
|
||||||
|
}
|
||||||
log.Debugf("close connection to node %s(%s), reason: %v", target.Name, target.Addr, err)
|
log.Debugf("close connection to node %s(%s), reason: %v", target.Name, target.Addr, err)
|
||||||
connPool.Delete(target)
|
connPool.Delete(target)
|
||||||
}()
|
}()
|
||||||
@ -266,6 +274,8 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cc.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -193,20 +193,12 @@ func (h *relayHandler) serveTCPBind(ctx context.Context, conn net.Conn, ln net.L
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID relay.TunnelID, log logger.Logger) (err error) {
|
func (h *relayHandler) handleBindTunnel(ctx context.Context, conn net.Conn, network string, tunnelID relay.TunnelID, log logger.Logger) (err error) {
|
||||||
resp := relay.Response{
|
resp := relay.Response{
|
||||||
Version: relay.Version1,
|
Version: relay.Version1,
|
||||||
Status: relay.StatusOK,
|
Status: relay.StatusOK,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
if h.ep == nil {
|
|
||||||
resp.Status = relay.StatusServiceUnavailable
|
|
||||||
resp.WriteTo(conn)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
uuid, err := uuid.NewRandom()
|
uuid, err := uuid.NewRandom()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Status = relay.StatusInternalServerError
|
resp.Status = relay.StatusInternalServerError
|
||||||
@ -214,7 +206,10 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
connectorID := relay.NewTunnelID(uuid[:])
|
connectorID := relay.NewConnectorID(uuid[:])
|
||||||
|
if network == "udp" {
|
||||||
|
connectorID = relay.NewUDPConnectorID(uuid[:])
|
||||||
|
}
|
||||||
|
|
||||||
addr := ":0"
|
addr := ":0"
|
||||||
if h.ep != nil {
|
if h.ep != nil {
|
||||||
@ -227,7 +222,7 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
|
|||||||
}
|
}
|
||||||
resp.Features = append(resp.Features, af,
|
resp.Features = append(resp.Features, af,
|
||||||
&relay.TunnelFeature{
|
&relay.TunnelFeature{
|
||||||
ID: connectorID,
|
ID: connectorID.ID(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
resp.WriteTo(conn)
|
resp.WriteTo(conn)
|
||||||
@ -239,7 +234,7 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
|
|||||||
}
|
}
|
||||||
|
|
||||||
h.pool.Add(tunnelID, NewConnector(connectorID, session))
|
h.pool.Add(tunnelID, NewConnector(connectorID, session))
|
||||||
log.Debugf("tunnel %s connector %s established", tunnelID, connectorID)
|
log.Debugf("tunnel %s connector %s/%s established", tunnelID, connectorID, network)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,6 @@ type tcpConn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *tcpConn) Read(b []byte) (n int, err error) {
|
func (c *tcpConn) Read(b []byte) (n int, err error) {
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return c.Conn.Read(b)
|
return c.Conn.Read(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ func (h *relayHandler) handleConnectTunnel(ctx context.Context, conn net.Conn, n
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cc, err := getTunnelConn(h.pool, tunnelID, 3, log)
|
cc, err := getTunnelConn(network, h.pool, tunnelID, 3, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return err
|
return err
|
||||||
|
@ -128,7 +128,7 @@ func (h *epHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.H
|
|||||||
"tunnel": tunnelID.String(),
|
"tunnel": tunnelID.String(),
|
||||||
})
|
})
|
||||||
|
|
||||||
cc, err := getTunnelConn(h.pool, tunnelID, 3, log)
|
cc, err := getTunnelConn("tcp", h.pool, tunnelID, 3, log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return err
|
return err
|
||||||
|
@ -173,7 +173,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
|||||||
}
|
}
|
||||||
case relay.FeatureTunnel:
|
case relay.FeatureTunnel:
|
||||||
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
|
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
|
||||||
tunnelID = feature.ID
|
tunnelID = relay.NewTunnelID(feature.ID[:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -210,7 +210,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
|||||||
return h.handleConnect(ctx, conn, network, address, log)
|
return h.handleConnect(ctx, conn, network, address, log)
|
||||||
case relay.CmdBind:
|
case relay.CmdBind:
|
||||||
if !tunnelID.IsZero() {
|
if !tunnelID.IsZero() {
|
||||||
return h.handleTunnel(ctx, conn, tunnelID, log)
|
return h.handleBindTunnel(ctx, conn, network, tunnelID, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
@ -38,7 +38,7 @@ func (h *relayHandler) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
if bs := mdutil.GetInt(md, udpBufferSize); bs > 0 {
|
if bs := mdutil.GetInt(md, udpBufferSize); bs > 0 {
|
||||||
h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
||||||
} else {
|
} else {
|
||||||
h.md.udpBufferSize = 1500
|
h.md.udpBufferSize = 4096
|
||||||
}
|
}
|
||||||
|
|
||||||
h.md.hash = mdutil.GetString(md, hash)
|
h.md.hash = mdutil.GetString(md, hash)
|
||||||
|
@ -81,16 +81,27 @@ func (t *Tunnel) AddConnector(c *Connector) {
|
|||||||
t.connectors = append(t.connectors, c)
|
t.connectors = append(t.connectors, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) GetConnector() *Connector {
|
func (t *Tunnel) GetConnector(network string) *Connector {
|
||||||
t.mu.RLock()
|
t.mu.RLock()
|
||||||
defer t.mu.RUnlock()
|
defer t.mu.RUnlock()
|
||||||
|
|
||||||
if len(t.connectors) == 0 {
|
var connectors []*Connector
|
||||||
|
for _, c := range t.connectors {
|
||||||
|
if network == "udp" {
|
||||||
|
if c.id.IsUDP() {
|
||||||
|
connectors = append(connectors, c)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !c.id.IsUDP() {
|
||||||
|
connectors = append(connectors, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(connectors) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
n := atomic.AddUint64(&t.n, 1) - 1
|
n := atomic.AddUint64(&t.n, 1) - 1
|
||||||
return t.connectors[n%uint64(len(t.connectors))]
|
return connectors[n%uint64(len(connectors))]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) clean() {
|
func (t *Tunnel) clean() {
|
||||||
@ -137,7 +148,7 @@ func (p *ConnectorPool) Add(tid relay.TunnelID, c *Connector) {
|
|||||||
t.AddConnector(c)
|
t.AddConnector(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ConnectorPool) Get(tid relay.TunnelID) *Connector {
|
func (p *ConnectorPool) Get(network string, tid relay.TunnelID) *Connector {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -150,7 +161,7 @@ func (p *ConnectorPool) Get(tid relay.TunnelID) *Connector {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.GetConnector()
|
return t.GetConnector(network)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTunnelID(s string) (tid relay.TunnelID) {
|
func parseTunnelID(s string) (tid relay.TunnelID) {
|
||||||
@ -170,12 +181,12 @@ func parseTunnelID(s string) (tid relay.TunnelID) {
|
|||||||
return relay.NewTunnelID(uuid[:])
|
return relay.NewTunnelID(uuid[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTunnelConn(pool *ConnectorPool, tunnelID relay.TunnelID, retry int, log logger.Logger) (conn net.Conn, err error) {
|
func getTunnelConn(network string, pool *ConnectorPool, tunnelID relay.TunnelID, retry int, log logger.Logger) (conn net.Conn, err error) {
|
||||||
if retry <= 0 {
|
if retry <= 0 {
|
||||||
retry = 1
|
retry = 1
|
||||||
}
|
}
|
||||||
for i := 0; i < retry; i++ {
|
for i := 0; i < retry; i++ {
|
||||||
c := pool.Get(tunnelID)
|
c := pool.Get(network, tunnelID)
|
||||||
if c == nil {
|
if c == nil {
|
||||||
err = fmt.Errorf("tunnel %s not available", tunnelID.String())
|
err = fmt.Errorf("tunnel %s not available", tunnelID.String())
|
||||||
break
|
break
|
||||||
|
@ -37,7 +37,7 @@ func (h *socks5Handler) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
if bs := mdutil.GetInt(md, udpBufferSize); bs > 0 {
|
if bs := mdutil.GetInt(md, udpBufferSize); bs > 0 {
|
||||||
h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
||||||
} else {
|
} else {
|
||||||
h.md.udpBufferSize = 1500
|
h.md.udpBufferSize = 4096
|
||||||
}
|
}
|
||||||
|
|
||||||
h.md.compatibilityMode = mdutil.GetBool(md, compatibilityMode)
|
h.md.compatibilityMode = mdutil.GetBool(md, compatibilityMode)
|
||||||
|
@ -27,7 +27,7 @@ func (h *ssuHandler) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
if bs := mdutil.GetInt(md, bufferSize); bs > 0 {
|
if bs := mdutil.GetInt(md, bufferSize); bs > 0 {
|
||||||
h.md.bufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
h.md.bufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
||||||
} else {
|
} else {
|
||||||
h.md.bufferSize = 1500
|
h.md.bufferSize = 4096
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ func (h *tapHandler) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
h.md.key = mdutil.GetString(md, key)
|
h.md.key = mdutil.GetString(md, key)
|
||||||
h.md.bufferSize = mdutil.GetInt(md, bufferSize)
|
h.md.bufferSize = mdutil.GetInt(md, bufferSize)
|
||||||
if h.md.bufferSize <= 0 {
|
if h.md.bufferSize <= 0 {
|
||||||
h.md.bufferSize = 1500
|
h.md.bufferSize = 4096
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,18 @@ import (
|
|||||||
"github.com/go-gost/core/common/bufpool"
|
"github.com/go-gost/core/common/bufpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
bufferSize = 32 * 1024
|
||||||
|
)
|
||||||
|
|
||||||
func Transport(rw1, rw2 io.ReadWriter) error {
|
func Transport(rw1, rw2 io.ReadWriter) error {
|
||||||
errc := make(chan error, 1)
|
errc := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
errc <- CopyBuffer(rw1, rw2, 8192)
|
errc <- CopyBuffer(rw1, rw2, bufferSize)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
errc <- CopyBuffer(rw2, rw1, 8192)
|
errc <- CopyBuffer(rw2, rw1, bufferSize)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := <-errc; err != nil && err != io.EOF {
|
if err := <-errc; err != nil && err != io.EOF {
|
||||||
@ -26,7 +30,7 @@ func Transport(rw1, rw2 io.ReadWriter) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CopyBuffer(dst io.Writer, src io.Reader, bufSize int) error {
|
func CopyBuffer(dst io.Writer, src io.Reader, bufSize int) error {
|
||||||
buf := bufpool.Get(8192)
|
buf := bufpool.Get(bufSize)
|
||||||
defer bufpool.Put(buf)
|
defer bufpool.Put(buf)
|
||||||
|
|
||||||
_, err := io.CopyBuffer(dst, src, *buf)
|
_, err := io.CopyBuffer(dst, src, *buf)
|
||||||
|
@ -41,7 +41,7 @@ func (r *Relay) SetBufferSize(n int) {
|
|||||||
func (r *Relay) Run() (err error) {
|
func (r *Relay) Run() (err error) {
|
||||||
bufSize := r.bufferSize
|
bufSize := r.bufferSize
|
||||||
if bufSize <= 0 {
|
if bufSize <= 0 {
|
||||||
bufSize = 1500
|
bufSize = 4096
|
||||||
}
|
}
|
||||||
|
|
||||||
errc := make(chan error, 2)
|
errc := make(chan error, 2)
|
||||||
|
@ -15,8 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
readBufferSize = 1500
|
readBufferSize = 4096
|
||||||
writeBufferSize = 1500
|
writeBufferSize = 4096
|
||||||
magicNumber = 0x474F5354
|
magicNumber = 0x474F5354
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -449,3 +449,10 @@ func (c *udpConn) SetDSCP(n int) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *udpConn) Metadata() metadata.Metadata {
|
||||||
|
if md, ok := c.PacketConn.(metadata.Metadatable); ok {
|
||||||
|
return md.Metadata()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTTL = 60 * time.Second
|
defaultTTL = 60 * time.Second
|
||||||
defaultReadBufferSize = 1500
|
defaultReadBufferSize = 4096
|
||||||
defaultReadQueueSize = 128
|
defaultReadQueueSize = 1024
|
||||||
defaultBacklog = 128
|
defaultBacklog = 128
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTTL = 30 * time.Second
|
defaultTTL = 30 * time.Second
|
||||||
defaultReadBufferSize = 1500
|
defaultReadBufferSize = 4096
|
||||||
)
|
)
|
||||||
|
|
||||||
type metadata struct {
|
type metadata struct {
|
||||||
|
@ -104,7 +104,7 @@ func (l *rtcpListener) Close() error {
|
|||||||
close(l.closed)
|
close(l.closed)
|
||||||
if l.ln != nil {
|
if l.ln != nil {
|
||||||
l.ln.Close()
|
l.ln.Close()
|
||||||
l.ln = nil
|
// l.ln = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +110,7 @@ func (l *rudpListener) Close() error {
|
|||||||
close(l.closed)
|
close(l.closed)
|
||||||
if l.ln != nil {
|
if l.ln != nil {
|
||||||
l.ln.Close()
|
l.ln.Close()
|
||||||
l.ln = nil
|
// l.ln = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTTL = 5 * time.Second
|
defaultTTL = 5 * time.Second
|
||||||
defaultReadBufferSize = 1500
|
defaultReadBufferSize = 4096
|
||||||
defaultReadQueueSize = 128
|
defaultReadQueueSize = 1024
|
||||||
defaultBacklog = 128
|
defaultBacklog = 128
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTTL = 5 * time.Second
|
defaultTTL = 5 * time.Second
|
||||||
defaultReadBufferSize = 1500
|
defaultReadBufferSize = 4096
|
||||||
defaultReadQueueSize = 128
|
defaultReadQueueSize = 1024
|
||||||
defaultBacklog = 128
|
defaultBacklog = 128
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -286,3 +286,10 @@ func (c *udpConn) SetDSCP(n int) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *udpConn) Metadata() metadata.Metadata {
|
||||||
|
if md, ok := c.PacketConn.(metadata.Metadatable); ok {
|
||||||
|
return md.Metadata()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user