Skip to content
File

Blob: tun/client/forwarder.go

go119 lines
1package client
2 
3import (
4 "context"
5 "net"
6 
7 "go.miragespace.co/specter/spec/protocol"
8 "go.miragespace.co/specter/spec/rpc"
9 "go.miragespace.co/specter/spec/tun"
10 
11 "github.com/zhangyunhao116/skipmap"
12 "go.uber.org/atomic"
13 "go.uber.org/zap"
14)
15 
16type forwarder struct {
17 logger *zap.Logger
18 rootDomain *atomic.String
19 routes *skipmap.StringMap[*route]
20}
21 
22type route struct {
23 Tunnel
24 proxy *httpProxy
25}
26 
27func newForwarder(logger *zap.Logger) *forwarder {
28 return &forwarder{
29 logger: logger,
30 rootDomain: atomic.NewString(""),
31 routes: skipmap.NewString[*route](),
32 }
33}
34 
35func (f *forwarder) newRoute(hostname string, tunnel Tunnel) *route {
36 r := &route{Tunnel: tunnel, proxy: f.newHTTPProxy(hostname, tunnel)}
37 go r.proxy.forwarder.Serve(r.proxy.acceptor)
38 return r
39}
40 
41// setRoutes publishes complete entries. Callers serialize configuration updates;
42// readers only need the skipmap lookup and never create a proxy.
43func (f *forwarder) setRoutes(tunnels []Tunnel) {
44 next := make(map[string]Tunnel, len(tunnels))
45 for _, tunnel := range tunnels {
46 next[tunnel.Hostname] = tunnel
47 }
48 for hostname, tunnel := range next {
49 old, loaded := f.routes.Load(hostname)
50 if loaded {
51 previous := old.Tunnel
52 // Validation reparses URLs; compare the configured fields instead.
53 previous.parsed = tunnel.parsed
54 if previous == tunnel {
55 continue
56 }
57 }
58 f.routes.Store(hostname, f.newRoute(hostname, tunnel))
59 if loaded {
60 old.proxy.close()
61 }
62 }
63 f.routes.Range(func(hostname string, old *route) bool {
64 if _, ok := next[hostname]; !ok {
65 f.routes.Delete(hostname)
66 old.proxy.close()
67 }
68 return true
69 })
70}
71 
72func receiveLink(logger *zap.Logger, delegation net.Conn) (*protocol.Link, bool) {
73 link := &protocol.Link{}
74 if err := rpc.BoundedReceive(delegation, link, 1024); err != nil {
75 logger.Error("Receiving link information from gateway", zap.Error(err))
76 delegation.Close()
77 return nil, false
78 }
79 return link, true
80}
81 
82func (f *forwarder) handleIncomingDelegation(ctx context.Context, link *protocol.Link, delegation net.Conn) error {
83 hostname := link.GetHostname()
84 r, ok := f.routes.Load(hostname)
85 if !ok {
86 f.logger.Error("Unknown hostname in connection", zap.String("hostname", hostname))
87 delegation.Close()
88 return tun.ErrDestinationNotFound
89 }
90 return f.handleLink(ctx, link, delegation, r)
91}
92 
93func (f *forwarder) handleLink(ctx context.Context, link *protocol.Link, delegation net.Conn, r *route) error {
94 f.logger.Info("Incoming connection from gateway",
95 zap.String("protocol", link.GetAlpn().String()),
96 zap.String("hostname", link.GetHostname()),
97 zap.String("remote", link.GetRemote()))
98 
99 switch link.GetAlpn() {
100 case protocol.Link_HTTP:
101 r.proxy.acceptor.Handle(delegation)
102 case protocol.Link_TCP:
103 f.forwardStream(ctx, link.GetHostname(), delegation, r.Tunnel)
104 default:
105 f.logger.Error("Unknown alpn for forwarding", zap.String("alpn", link.GetAlpn().String()))
106 delegation.Close()
107 return tun.ErrDestinationNotFound
108 }
109 return nil
110}
111 
112func (f *forwarder) closeAll() {
113 f.routes.Range(func(hostname string, r *route) bool {
114 f.routes.Delete(hostname)
115 r.proxy.close()
116 return true
117 })
118}