update forward handler
This commit is contained in:
@ -10,7 +10,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/chain"
|
||||
@ -181,7 +180,6 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
|
||||
func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log logger.Logger) (err error) {
|
||||
br := bufio.NewReader(rw)
|
||||
var connPool sync.Map
|
||||
|
||||
for {
|
||||
resp := &http.Response{
|
||||
@ -230,45 +228,28 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
ctx = auth_util.ContextWithID(ctx, auth_util.ID(id))
|
||||
}
|
||||
|
||||
var cc net.Conn
|
||||
if v, ok := connPool.Load(target); ok {
|
||||
cc = v.(net.Conn)
|
||||
log.Debugf("connection to node %s(%s) found in pool", target.Name, target.Addr)
|
||||
}
|
||||
if cc == nil {
|
||||
cc, err = h.router.Dial(ctx, "tcp", target.Addr)
|
||||
if err != nil {
|
||||
// TODO: the router itself may be failed due to the failed node in the router,
|
||||
// the dead marker may be a wrong operation.
|
||||
if marker := target.Marker(); marker != nil {
|
||||
marker.Mark()
|
||||
}
|
||||
log.Warnf("connect to node %s(%s) failed: %v", target.Name, target.Addr, err)
|
||||
return resp.Write(rw)
|
||||
}
|
||||
cc, err := h.router.Dial(ctx, "tcp", target.Addr)
|
||||
if err != nil {
|
||||
// TODO: the router itself may be failed due to the failed node in the router,
|
||||
// the dead marker may be a wrong operation.
|
||||
if marker := target.Marker(); marker != nil {
|
||||
marker.Reset()
|
||||
marker.Mark()
|
||||
}
|
||||
log.Warnf("connect to node %s(%s) failed: %v", target.Name, target.Addr, err)
|
||||
return resp.Write(rw)
|
||||
}
|
||||
if marker := target.Marker(); marker != nil {
|
||||
marker.Reset()
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
if tlsSettings := target.Options().TLS; tlsSettings != nil {
|
||||
cc = tls.Client(cc, &tls.Config{
|
||||
ServerName: tlsSettings.ServerName,
|
||||
InsecureSkipVerify: !tlsSettings.Secure,
|
||||
})
|
||||
}
|
||||
log.Debugf("new connection to node %s(%s)", target.Name, target.Addr)
|
||||
|
||||
connPool.Store(target, cc)
|
||||
log.Debugf("new connection to node %s(%s)", target.Name, target.Addr)
|
||||
|
||||
go func() {
|
||||
defer cc.Close()
|
||||
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)
|
||||
connPool.Delete(target)
|
||||
}()
|
||||
if tlsSettings := target.Options().TLS; tlsSettings != nil {
|
||||
cc = tls.Client(cc, &tls.Config{
|
||||
ServerName: tlsSettings.ServerName,
|
||||
InsecureSkipVerify: !tlsSettings.Secure,
|
||||
})
|
||||
}
|
||||
|
||||
if httpSettings := target.Options().HTTP; httpSettings != nil {
|
||||
@ -285,21 +266,18 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
log.Trace(string(dump))
|
||||
}
|
||||
if err := req.Write(cc); err != nil {
|
||||
log.Warnf("send request to node %s(%s) failed: %v", target.Name, target.Addr, err)
|
||||
log.Warnf("send request to node %s(%s): %v", target.Name, target.Addr, err)
|
||||
return resp.Write(rw)
|
||||
}
|
||||
|
||||
if req.Header.Get("Upgrade") == "websocket" {
|
||||
err := xnet.CopyBuffer(cc, br, 8192)
|
||||
if err == nil {
|
||||
err = io.EOF
|
||||
}
|
||||
return err
|
||||
res, err := http.ReadResponse(bufio.NewReader(cc), req)
|
||||
if err != nil {
|
||||
log.Warnf("read response from node %s(%s): %v", target.Name, target.Addr, err)
|
||||
return resp.Write(rw)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
// cc.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||
|
||||
return nil
|
||||
return res.Write(rw)
|
||||
}()
|
||||
if err != nil {
|
||||
// log.Error(err)
|
||||
@ -307,13 +285,6 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
}
|
||||
}
|
||||
|
||||
connPool.Range(func(key, value any) bool {
|
||||
if value != nil {
|
||||
value.(net.Conn).Close()
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -94,6 +95,8 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
|
||||
ctx = auth_util.ContextWithClientAddr(ctx, auth_util.ClientAddr(conn.RemoteAddr().String()))
|
||||
|
||||
localAddr := convertAddr(conn.LocalAddr())
|
||||
|
||||
var rw io.ReadWriter = conn
|
||||
var host string
|
||||
var protocol string
|
||||
@ -108,7 +111,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
}
|
||||
}
|
||||
if protocol == forward.ProtoHTTP {
|
||||
h.handleHTTP(ctx, rw, conn.RemoteAddr(), conn.LocalAddr(), log)
|
||||
h.handleHTTP(ctx, rw, conn.RemoteAddr(), localAddr, log)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -166,12 +169,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
marker.Reset()
|
||||
}
|
||||
|
||||
if dst, ok := conn.LocalAddr().(*net.TCPAddr); ok {
|
||||
if dst.IP.Equal(net.IPv6zero) {
|
||||
dst.IP = net.IPv4zero
|
||||
}
|
||||
}
|
||||
cc = proxyproto.WrapClientConn(h.md.proxyProtocol, conn.RemoteAddr(), conn.LocalAddr(), cc)
|
||||
cc = proxyproto.WrapClientConn(h.md.proxyProtocol, conn.RemoteAddr(), localAddr, cc)
|
||||
|
||||
t := time.Now()
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
|
||||
@ -334,3 +332,27 @@ func (h *forwardHandler) checkRateLimit(addr net.Addr) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func convertAddr(addr net.Addr) net.Addr {
|
||||
host, sp, _ := net.SplitHostPort(addr.String())
|
||||
ip := net.ParseIP(host)
|
||||
port, _ := strconv.Atoi(sp)
|
||||
|
||||
if ip == nil || ip.Equal(net.IPv6zero) {
|
||||
ip = net.IPv4zero
|
||||
}
|
||||
|
||||
switch addr.Network() {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
return &net.TCPAddr{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
}
|
||||
|
||||
default:
|
||||
return &net.UDPAddr{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
|
||||
|
||||
// try to sniff HTTP traffic
|
||||
if isHTTP(string(hdr[:])) {
|
||||
return h.handleHTTP(ctx, rw, conn.RemoteAddr(), log)
|
||||
return h.handleHTTP(ctx, rw, conn.RemoteAddr(), dstAddr, log)
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *redirectHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, raddr net.Addr, log logger.Logger) error {
|
||||
func (h *redirectHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, raddr, dstAddr net.Addr, log logger.Logger) error {
|
||||
req, err := http.ReadRequest(bufio.NewReader(rw))
|
||||
if err != nil {
|
||||
return err
|
||||
@ -171,7 +171,14 @@ func (h *redirectHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, radd
|
||||
cc, err := h.router.Dial(ctx, "tcp", host)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if cc == nil {
|
||||
cc, err = h.router.Dial(ctx, "tcp", dstAddr.String())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
@ -216,9 +223,10 @@ func (h *redirectHandler) handleHTTPS(ctx context.Context, rw io.ReadWriter, rad
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
if host == "" {
|
||||
host = dstAddr.String()
|
||||
} else {
|
||||
|
||||
var cc io.ReadWriteCloser
|
||||
|
||||
if host != "" {
|
||||
if _, _, err := net.SplitHostPort(host); err != nil {
|
||||
_, port, _ := net.SplitHostPort(dstAddr.String())
|
||||
if port == "" {
|
||||
@ -226,21 +234,27 @@ func (h *redirectHandler) handleHTTPS(ctx context.Context, rw io.ReadWriter, rad
|
||||
}
|
||||
host = net.JoinHostPort(host, port)
|
||||
}
|
||||
log = log.WithFields(map[string]any{
|
||||
"host": host,
|
||||
})
|
||||
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, "tcp", host) {
|
||||
log.Debug("bypass: ", host)
|
||||
return nil
|
||||
}
|
||||
|
||||
cc, err = h.router.Dial(ctx, "tcp", host)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"host": host,
|
||||
})
|
||||
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, "tcp", host) {
|
||||
log.Debug("bypass: ", host)
|
||||
return nil
|
||||
}
|
||||
|
||||
cc, err := h.router.Dial(ctx, "tcp", host)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
if cc == nil {
|
||||
cc, err = h.router.Dial(ctx, "tcp", dstAddr.String())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
|
@ -150,7 +150,7 @@ func (h *relayHandler) handleConnectTunnel(ctx context.Context, conn net.Conn, n
|
||||
return err
|
||||
}
|
||||
|
||||
cc, _, err := getTunnelConn(network, h.pool, tid, 3, log)
|
||||
cc, _, err := getTunnelConn(network, h.pool, tunnelID, 3, log)
|
||||
if err != nil {
|
||||
resp.Status = relay.StatusServiceUnavailable
|
||||
resp.WriteTo(conn)
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/logger"
|
||||
@ -12,37 +11,54 @@ import (
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
)
|
||||
|
||||
func (h *tunnelHandler) handleConnect(ctx context.Context, conn net.Conn, network, address string, tunnelID relay.TunnelID, log logger.Logger) error {
|
||||
func (h *tunnelHandler) handleConnect(ctx context.Context, conn net.Conn, network, srcAddr string, dstAddr string, tunnelID relay.TunnelID, log logger.Logger) error {
|
||||
log = log.WithFields(map[string]any{
|
||||
"dst": fmt.Sprintf("%s/%s", address, network),
|
||||
"dst": fmt.Sprintf("%s/%s", dstAddr, network),
|
||||
"cmd": "connect",
|
||||
"tunnel": tunnelID.String(),
|
||||
})
|
||||
|
||||
log.Debugf("%s >> %s/%s", conn.RemoteAddr(), address, network)
|
||||
|
||||
resp := relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
|
||||
host, sp, _ := net.SplitHostPort(address)
|
||||
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, network, address) {
|
||||
log.Debug("bypass: ", address)
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, network, dstAddr) {
|
||||
log.Debug("bypass: ", dstAddr)
|
||||
resp.Status = relay.StatusForbidden
|
||||
_, err := resp.WriteTo(conn)
|
||||
return err
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(dstAddr)
|
||||
|
||||
var tid relay.TunnelID
|
||||
if ingress := h.md.ingress; ingress != nil {
|
||||
if ingress := h.md.ingress; ingress != nil && host != "" {
|
||||
tid = parseTunnelID(ingress.Get(ctx, host))
|
||||
}
|
||||
|
||||
// client is not an public entrypoint.
|
||||
if h.md.entryPointID.IsZero() || !tunnelID.Equal(h.md.entryPointID) {
|
||||
if !tid.Equal(tunnelID) && !h.md.directTunnel {
|
||||
// client is a public entrypoint.
|
||||
if tunnelID.Equal(h.md.entryPointID) && !h.md.entryPointID.IsZero() {
|
||||
if tid.IsZero() {
|
||||
resp.Status = relay.StatusNetworkUnreachable
|
||||
resp.WriteTo(conn)
|
||||
err := fmt.Errorf("no route to host %s", host)
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if tid.IsPrivate() {
|
||||
resp.Status = relay.StatusHostUnreachable
|
||||
resp.WriteTo(conn)
|
||||
err := fmt.Errorf("access denied: tunnel %s is private for host %s", tunnelID, host)
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// direct routing
|
||||
if h.md.directTunnel {
|
||||
tid = tunnelID
|
||||
} else if !tid.Equal(tunnelID) {
|
||||
resp.Status = relay.StatusHostUnreachable
|
||||
resp.WriteTo(conn)
|
||||
err := fmt.Errorf("no route to host %s", host)
|
||||
@ -62,36 +78,35 @@ func (h *tunnelHandler) handleConnect(ctx context.Context, conn net.Conn, networ
|
||||
|
||||
log.Debugf("%s >> %s", conn.RemoteAddr(), cc.RemoteAddr())
|
||||
|
||||
rc := &tcpConn{
|
||||
Conn: conn,
|
||||
}
|
||||
// cache the header
|
||||
if _, err := resp.WriteTo(&rc.wbuf); err != nil {
|
||||
return err
|
||||
}
|
||||
conn = rc
|
||||
|
||||
var features []relay.Feature
|
||||
af := &relay.AddrFeature{} // source/visitor address
|
||||
af.ParseFrom(conn.RemoteAddr().String())
|
||||
features = append(features, af)
|
||||
|
||||
if host != "" {
|
||||
port, _ := strconv.Atoi(sp)
|
||||
// target host
|
||||
af = &relay.AddrFeature{
|
||||
AType: relay.AddrDomain,
|
||||
Host: host,
|
||||
Port: uint16(port),
|
||||
if h.md.noDelay {
|
||||
if _, err := resp.WriteTo(conn); err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
features = append(features, af)
|
||||
} else {
|
||||
rc := &tcpConn{
|
||||
Conn: conn,
|
||||
}
|
||||
// cache the header
|
||||
if _, err := resp.WriteTo(&rc.wbuf); err != nil {
|
||||
return err
|
||||
}
|
||||
conn = rc
|
||||
}
|
||||
|
||||
resp = relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
Features: features,
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
|
||||
af := &relay.AddrFeature{}
|
||||
af.ParseFrom(srcAddr)
|
||||
resp.Features = append(resp.Features, af) // src address
|
||||
|
||||
af = &relay.AddrFeature{}
|
||||
af.ParseFrom(dstAddr)
|
||||
resp.Features = append(resp.Features, af) // dst address
|
||||
|
||||
resp.WriteTo(cc)
|
||||
|
||||
t := time.Now()
|
||||
|
@ -118,7 +118,7 @@ func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
}
|
||||
|
||||
var user, pass string
|
||||
var address string
|
||||
var srcAddr, dstAddr string
|
||||
var networkID relay.NetworkID
|
||||
var tunnelID relay.TunnelID
|
||||
for _, f := range req.Features {
|
||||
@ -129,7 +129,12 @@ func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
}
|
||||
case relay.FeatureAddr:
|
||||
if feature, _ := f.(*relay.AddrFeature); feature != nil {
|
||||
address = net.JoinHostPort(feature.Host, strconv.Itoa(int(feature.Port)))
|
||||
v := net.JoinHostPort(feature.Host, strconv.Itoa(int(feature.Port)))
|
||||
if srcAddr != "" {
|
||||
dstAddr = v
|
||||
} else {
|
||||
srcAddr = v
|
||||
}
|
||||
}
|
||||
case relay.FeatureTunnel:
|
||||
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
|
||||
@ -170,9 +175,12 @@ func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
switch req.Cmd & relay.CmdMask {
|
||||
case relay.CmdConnect:
|
||||
defer conn.Close()
|
||||
return h.handleConnect(ctx, conn, network, address, tunnelID, log)
|
||||
|
||||
log.Debugf("connect: %s >> %s/%s", srcAddr, dstAddr, network)
|
||||
return h.handleConnect(ctx, conn, network, srcAddr, dstAddr, tunnelID, log)
|
||||
case relay.CmdBind:
|
||||
return h.handleBind(ctx, conn, network, address, tunnelID, log)
|
||||
log.Debugf("bind: %s >> %s/%s", srcAddr, dstAddr, network)
|
||||
return h.handleBind(ctx, conn, network, dstAddr, tunnelID, log)
|
||||
default:
|
||||
resp.Status = relay.StatusBadRequest
|
||||
resp.WriteTo(conn)
|
||||
|
@ -15,6 +15,7 @@ import (
|
||||
|
||||
type metadata struct {
|
||||
readTimeout time.Duration
|
||||
noDelay bool
|
||||
hash string
|
||||
directTunnel bool
|
||||
entryPointID relay.TunnelID
|
||||
@ -22,18 +23,13 @@ type metadata struct {
|
||||
}
|
||||
|
||||
func (h *tunnelHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
readTimeout = "readTimeout"
|
||||
entryPointID = "entrypoint.id"
|
||||
hash = "hash"
|
||||
)
|
||||
h.md.readTimeout = mdutil.GetDuration(md, "readTimeout")
|
||||
h.md.noDelay = mdutil.GetBool(md, "nodelay")
|
||||
|
||||
h.md.readTimeout = mdutil.GetDuration(md, readTimeout)
|
||||
|
||||
h.md.hash = mdutil.GetString(md, hash)
|
||||
h.md.hash = mdutil.GetString(md, "hash")
|
||||
|
||||
h.md.directTunnel = mdutil.GetBool(md, "tunnel.direct")
|
||||
h.md.entryPointID = parseTunnelID(mdutil.GetString(md, entryPointID))
|
||||
h.md.entryPointID = parseTunnelID(mdutil.GetString(md, "entrypoint.id"))
|
||||
|
||||
h.md.ingress = registry.IngressRegistry().Get(mdutil.GetString(md, "ingress"))
|
||||
if h.md.ingress == nil {
|
||||
|
@ -177,6 +177,11 @@ func parseTunnelID(s string) (tid relay.TunnelID) {
|
||||
}
|
||||
|
||||
func getTunnelConn(network string, pool *ConnectorPool, tid relay.TunnelID, retry int, log logger.Logger) (conn net.Conn, cid relay.ConnectorID, err error) {
|
||||
if tid.IsZero() {
|
||||
err = ErrTunnelID
|
||||
return
|
||||
}
|
||||
|
||||
if retry <= 0 {
|
||||
retry = 1
|
||||
}
|
||||
|
Reference in New Issue
Block a user