add tunnel handler and connector
This commit is contained in:
95
connector/tunnel/bind.go
Normal file
95
connector/tunnel/bind.go
Normal file
@ -0,0 +1,95 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-gost/core/connector"
|
||||
"github.com/go-gost/relay"
|
||||
"github.com/go-gost/x/internal/util/mux"
|
||||
)
|
||||
|
||||
// Bind implements connector.Binder.
|
||||
func (c *tunnelConnector) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||
log := c.options.Logger
|
||||
|
||||
addr, cid, err := c.initTunnel(conn, network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Infof("create tunnel on %s/%s OK, tunnel=%s, connector=%s", addr, network, c.md.tunnelID.String(), cid)
|
||||
|
||||
session, err := mux.ServerSession(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &bindListener{
|
||||
network: network,
|
||||
addr: addr,
|
||||
session: session,
|
||||
logger: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *tunnelConnector) initTunnel(conn net.Conn, network, address string) (addr net.Addr, cid relay.ConnectorID, err error) {
|
||||
req := relay.Request{
|
||||
Version: relay.Version1,
|
||||
Cmd: relay.CmdBind,
|
||||
}
|
||||
|
||||
if network == "udp" {
|
||||
req.Cmd |= relay.FUDP
|
||||
}
|
||||
|
||||
if c.options.Auth != nil {
|
||||
pwd, _ := c.options.Auth.Password()
|
||||
req.Features = append(req.Features, &relay.UserAuthFeature{
|
||||
Username: c.options.Auth.Username(),
|
||||
Password: pwd,
|
||||
})
|
||||
}
|
||||
|
||||
af := &relay.AddrFeature{}
|
||||
af.ParseFrom(address)
|
||||
|
||||
req.Features = append(req.Features, af,
|
||||
&relay.TunnelFeature{
|
||||
ID: c.md.tunnelID.ID(),
|
||||
},
|
||||
)
|
||||
if _, err = req.WriteTo(conn); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// first reply, bind status
|
||||
resp := relay.Response{}
|
||||
if _, err = resp.ReadFrom(conn); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if resp.Status != relay.StatusOK {
|
||||
err = fmt.Errorf("%d: create tunnel %s failed", resp.Status, c.md.tunnelID.String())
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range resp.Features {
|
||||
switch f.Type() {
|
||||
case relay.FeatureAddr:
|
||||
if feature, _ := f.(*relay.AddrFeature); feature != nil {
|
||||
addr = &bindAddr{
|
||||
network: network,
|
||||
addr: net.JoinHostPort(feature.Host, strconv.Itoa(int(feature.Port))),
|
||||
}
|
||||
}
|
||||
case relay.FeatureTunnel:
|
||||
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
|
||||
cid = relay.NewConnectorID(feature.ID[:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user