forward non-HTTP traffic based on host

This commit is contained in:
ginuerzh
2023-01-30 21:09:21 +08:00
parent 3afa3b6860
commit 1c6bc9283e
11 changed files with 127 additions and 13 deletions

View File

@ -10,6 +10,7 @@ import (
"net"
"sync"
mdata "github.com/go-gost/core/metadata"
"github.com/go-gost/relay"
)
@ -121,6 +122,7 @@ type bindConn struct {
net.Conn
localAddr net.Addr
remoteAddr net.Addr
md mdata.Metadata
}
func (c *bindConn) LocalAddr() net.Addr {
@ -130,3 +132,8 @@ func (c *bindConn) LocalAddr() net.Addr {
func (c *bindConn) RemoteAddr() net.Addr {
return c.remoteAddr
}
// Metadata implements metadata.Metadatable interface.
func (c *bindConn) Metadata() mdata.Metadata {
return c.md
}

View File

@ -8,6 +8,7 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/relay"
"github.com/go-gost/x/internal/util/mux"
mdx "github.com/go-gost/x/metadata"
)
type tcpListener struct {
@ -44,11 +45,16 @@ func (p *tcpListener) getPeerConn(conn net.Conn) (net.Conn, error) {
return nil, err
}
var address string
var address, host string
for _, f := range resp.Features {
if f.Type() == relay.FeatureAddr {
if fa, ok := f.(*relay.AddrFeature); ok {
address = net.JoinHostPort(fa.Host, strconv.Itoa(int(fa.Port)))
v := net.JoinHostPort(fa.Host, strconv.Itoa(int(fa.Port)))
if address != "" {
host = v
} else {
address = v
}
}
}
}
@ -58,11 +64,15 @@ func (p *tcpListener) getPeerConn(conn net.Conn) (net.Conn, error) {
return nil, err
}
return &bindConn{
cn := &bindConn{
Conn: conn,
localAddr: p.addr,
remoteAddr: raddr,
}, nil
}
if host != "" {
cn.md = mdx.NewMetadata(map[string]any{"host": host})
}
return cn, nil
}
func (p *tcpListener) Addr() net.Addr {