tsnet: add Dial method to allow dialing out to the tailnet.

Signed-off-by: Maisem Ali <maisem@tailscale.com>
pull/3631/head
Maisem Ali 3 years ago committed by Maisem Ali
parent 535b925d1b
commit 44937b59e7

@ -63,6 +63,15 @@ type Server struct {
mu sync.Mutex mu sync.Mutex
listeners map[listenKey]*listener listeners map[listenKey]*listener
dialer *tsdial.Dialer
}
// Dial connects to the address on the tailnet.
func (s *Server) Dial(ctx context.Context, network, address string) (net.Conn, error) {
if err := s.init(); err != nil {
return nil, err
}
return s.dialer.UserDial(ctx, network, address)
} }
func (s *Server) doInit() { func (s *Server) doInit() {
@ -71,6 +80,11 @@ func (s *Server) doInit() {
} }
} }
func (s *Server) init() error {
s.initOnce.Do(s.doInit)
return s.initErr
}
func (s *Server) start() error { func (s *Server) start() error {
if v, _ := strconv.ParseBool(os.Getenv("TAILSCALE_USE_WIP_CODE")); !v { if v, _ := strconv.ParseBool(os.Getenv("TAILSCALE_USE_WIP_CODE")); !v {
return errors.New("code disabled without environment variable TAILSCALE_USE_WIP_CODE set true") return errors.New("code disabled without environment variable TAILSCALE_USE_WIP_CODE set true")
@ -117,11 +131,11 @@ func (s *Server) start() error {
return err return err
} }
dialer := new(tsdial.Dialer) // mutated below (before used) s.dialer = new(tsdial.Dialer) // mutated below (before used)
eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{ eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{
ListenPort: 0, ListenPort: 0,
LinkMonitor: linkMon, LinkMonitor: linkMon,
Dialer: dialer, Dialer: s.dialer,
}) })
if err != nil { if err != nil {
return err return err
@ -132,7 +146,7 @@ func (s *Server) start() error {
return fmt.Errorf("%T is not a wgengine.InternalsGetter", eng) return fmt.Errorf("%T is not a wgengine.InternalsGetter", eng)
} }
ns, err := netstack.Create(logf, tunDev, eng, magicConn, dialer) ns, err := netstack.Create(logf, tunDev, eng, magicConn, s.dialer)
if err != nil { if err != nil {
return fmt.Errorf("netstack.Create: %w", err) return fmt.Errorf("netstack.Create: %w", err)
} }
@ -141,11 +155,11 @@ func (s *Server) start() error {
if err := ns.Start(); err != nil { if err := ns.Start(); err != nil {
return fmt.Errorf("failed to start netstack: %w", err) return fmt.Errorf("failed to start netstack: %w", err)
} }
dialer.UseNetstackForIP = func(ip netaddr.IP) bool { s.dialer.UseNetstackForIP = func(ip netaddr.IP) bool {
_, ok := eng.PeerForIP(ip) _, ok := eng.PeerForIP(ip)
return ok return ok
} }
dialer.NetstackDialTCP = func(ctx context.Context, dst netaddr.IPPort) (net.Conn, error) { s.dialer.NetstackDialTCP = func(ctx context.Context, dst netaddr.IPPort) (net.Conn, error) {
return ns.DialContextTCP(ctx, dst) return ns.DialContextTCP(ctx, dst)
} }
@ -156,7 +170,7 @@ func (s *Server) start() error {
} }
logid := "tslib-TODO" logid := "tslib-TODO"
lb, err := ipnlocal.NewLocalBackend(logf, logid, store, dialer, eng) lb, err := ipnlocal.NewLocalBackend(logf, logid, store, s.dialer, eng)
if err != nil { if err != nil {
return fmt.Errorf("NewLocalBackend: %v", err) return fmt.Errorf("NewLocalBackend: %v", err)
} }
@ -217,15 +231,15 @@ func (s *Server) forwardTCP(c net.Conn, port uint16) {
} }
} }
// Listen announces only on the Tailscale network.
func (s *Server) Listen(network, addr string) (net.Listener, error) { func (s *Server) Listen(network, addr string) (net.Listener, error) {
host, port, err := net.SplitHostPort(addr) host, port, err := net.SplitHostPort(addr)
if err != nil { if err != nil {
return nil, fmt.Errorf("tsnet: %w", err) return nil, fmt.Errorf("tsnet: %w", err)
} }
s.initOnce.Do(s.doInit) if err := s.init(); err != nil {
if s.initErr != nil { return nil, err
return nil, s.initErr
} }
key := listenKey{network, host, port} key := listenKey{network, host, port}

Loading…
Cancel
Save