add vhost for port forwarding

This commit is contained in:
ginuerzh
2022-11-11 22:23:36 +08:00
parent 81b6efc9b8
commit 1ff2bab1f0
12 changed files with 195 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"net"
"time"
@ -11,6 +12,7 @@ import (
"github.com/go-gost/core/handler"
md "github.com/go-gost/core/metadata"
xchain "github.com/go-gost/x/chain"
"github.com/go-gost/x/handler/forward/internal/forward"
netpkg "github.com/go-gost/x/internal/net"
"github.com/go-gost/x/registry"
)
@ -84,18 +86,29 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
return nil
}
target := h.hop.Select(ctx)
network := "tcp"
if _, ok := conn.(net.PacketConn); ok {
network = "udp"
}
var rw io.ReadWriter
var host string
if h.md.sniffing {
if network == "tcp" {
rw, host, _ = forward.SniffHost(ctx, conn)
}
}
var target *chain.Node
if h.hop != nil {
target = h.hop.Select(ctx, chain.HostSelectOption(host))
}
if target == nil {
err := errors.New("target not available")
log.Error(err)
return err
}
network := "tcp"
if _, ok := conn.(net.PacketConn); ok {
network = "udp"
}
log = log.WithFields(map[string]any{
"dst": fmt.Sprintf("%s/%s", target.Addr, network),
})
@ -119,7 +132,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
t := time.Now()
log.Debugf("%s <-> %s", conn.RemoteAddr(), target.Addr)
netpkg.Transport(conn, cc)
netpkg.Transport(rw, cc)
log.WithFields(map[string]any{
"duration": time.Since(t),
}).Debugf("%s >-< %s", conn.RemoteAddr(), target.Addr)