代码拉取完成,页面将自动刷新
package tunnel
import (
"context"
"fmt"
"net"
"time"
"gitee.com/hao_shen/gost-core/connector"
md "gitee.com/hao_shen/gost-core/metadata"
ctxvalue "gitee.com/hao_shen/gost-x/ctx"
"gitee.com/hao_shen/gost-x/registry"
"github.com/go-gost/relay"
)
func init() {
registry.ConnectorRegistry().Register("router", NewConnector)
}
type routerConnector struct {
md metadata
options connector.Options
}
func NewConnector(opts ...connector.Option) connector.Connector {
options := connector.Options{}
for _, opt := range opts {
opt(&options)
}
return &routerConnector{
options: options,
}
}
func (c *routerConnector) Init(md md.Metadata) (err error) {
return c.parseMetadata(md)
}
func (c *routerConnector) 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,
"sid": string(ctxvalue.SidFromContext(ctx)),
})
log.Debugf("connect %s/%s", address, network)
if c.md.connectTimeout > 0 {
conn.SetDeadline(time.Now().Add(c.md.connectTimeout))
defer conn.SetDeadline(time.Time{})
}
req := relay.Request{
Version: relay.Version1,
Cmd: relay.CmdAssociate,
}
if c.options.Auth != nil {
pwd, _ := c.options.Auth.Password()
req.Features = append(req.Features, &relay.UserAuthFeature{
Username: c.options.Auth.Username(),
Password: pwd,
})
}
switch network {
case "udp":
req.Features = append(req.Features, &relay.NetworkFeature{
Network: relay.NetworkUDP,
})
case "ip":
req.Features = append(req.Features, &relay.NetworkFeature{
Network: relay.NetworkIP,
})
}
srcAddr := conn.LocalAddr().String()
if v := ctxvalue.ClientAddrFromContext(ctx); v != "" {
srcAddr = string(v)
}
af := &relay.AddrFeature{}
af.ParseFrom(srcAddr)
req.Features = append(req.Features, af) // src address
af = &relay.AddrFeature{}
af.ParseFrom(address)
req.Features = append(req.Features, af) // dst address
if !c.md.routerID.IsZero() {
req.Features = append(req.Features, &relay.TunnelFeature{
ID: c.md.routerID,
})
}
if _, err := req.WriteTo(conn); err != nil {
return nil, err
}
// drain the response
if err := readResponse(conn); err != nil {
return nil, err
}
switch network {
case "udp", "ip":
conn = &packetConn{
Conn: conn,
}
default:
err := fmt.Errorf("network %s is unsupported", network)
log.Error(err)
return nil, err
}
log.Debugf("connect to router %s@%s OK", address, c.md.routerID)
return conn, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。