47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package sni
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"github.com/go-gost/gost/v3/pkg/connector"
|
|
md "github.com/go-gost/gost/v3/pkg/metadata"
|
|
"github.com/go-gost/gost/v3/pkg/registry"
|
|
)
|
|
|
|
func init() {
|
|
registry.ConnectorRegistry().Register("sni", NewConnector)
|
|
}
|
|
|
|
type sniConnector struct {
|
|
md metadata
|
|
options connector.Options
|
|
}
|
|
|
|
func NewConnector(opts ...connector.Option) connector.Connector {
|
|
options := connector.Options{}
|
|
for _, opt := range opts {
|
|
opt(&options)
|
|
}
|
|
|
|
return &sniConnector{
|
|
options: options,
|
|
}
|
|
}
|
|
|
|
func (c *sniConnector) Init(md md.Metadata) (err error) {
|
|
return c.parseMetadata(md)
|
|
}
|
|
|
|
func (c *sniConnector) Connect(ctx context.Context, conn net.Conn, network, address string, opts ...connector.ConnectOption) (net.Conn, error) {
|
|
log := c.options.Logger.WithFields(map[string]any{
|
|
"remote": conn.RemoteAddr().String(),
|
|
"local": conn.LocalAddr().String(),
|
|
"network": network,
|
|
"address": address,
|
|
})
|
|
log.Infof("connect %s/%s", address, network)
|
|
|
|
return &sniClientConn{Conn: conn, host: c.md.host}, nil
|
|
}
|