@@ -8,17 +8,20 @@ import (
88 pm "github.com/cyber-shuttle/linkspan/internal/process"
99)
1010
11- // DevTunnelCreate creates a tunnel on the service (no ports registered yet)
12- // and registers it with GlobalDevTunnelManager.
13- func DevTunnelCreate (tunnelName string , expiration string , authToken string ) (DevTunnelInfo , error ) {
11+ // DevTunnelCreate creates a tunnel, starts hosting the relay, and forwards the
12+ // given serverPort so the client can communicate with linkspan immediately.
13+ // Additional ports (e.g. SSH) can be added later via DevTunnelForward.
14+ func DevTunnelCreate (tunnelName string , expiration string , authToken string , serverPort int ) (DevTunnelConnection , error ) {
1415 if err := InitSDK (authToken ); err != nil {
15- return DevTunnelInfo {}, fmt .Errorf ("devtunnel create: init SDK: %w" , err )
16+ return DevTunnelConnection {}, fmt .Errorf ("devtunnel create: init SDK: %w" , err )
1617 }
1718
1819 ctx := context .Background ()
20+
21+ // 1. Create the tunnel on the service.
1922 sdkTunnel , err := SDKCreateTunnel (ctx , tunnelName )
2023 if err != nil {
21- return DevTunnelInfo {}, fmt .Errorf ("devtunnel create %q: %w" , tunnelName , err )
24+ return DevTunnelConnection {}, fmt .Errorf ("devtunnel create %q: %w" , tunnelName , err )
2225 }
2326
2427 info := & DevTunnelInfo {
@@ -32,52 +35,43 @@ func DevTunnelCreate(tunnelName string, expiration string, authToken string) (De
3235 log .Printf ("devtunnel create: warning — failed to register %q in manager: %v" , tunnelName , err )
3336 }
3437
35- log .Printf ("devtunnel create: tunnel %q ready (id=%s)" , tunnelName , sdkTunnel .TunnelID )
36- return * info , nil
37- }
38-
39- // DevTunnelHost starts hosting the tunnel relay connection.
40- // No ports are forwarded yet — use DevTunnelForward to add port forwarding.
41- func DevTunnelHost (tunnelName string , authToken string ) (string , DevTunnelConnection , error ) {
42- if err := InitSDK (authToken ); err != nil {
43- return "" , DevTunnelConnection {}, fmt .Errorf ("devtunnel host: init SDK: %w" , err )
44- }
45-
46- devTunInfo , err := GlobalDevTunnelManager .Find (tunnelName )
47- if err != nil {
48- return "" , DevTunnelConnection {}, fmt .Errorf ("devtunnel host: tunnel %q not registered: %w" , tunnelName , err )
38+ // 2. Register the server port so it is forwarded through the tunnel.
39+ if serverPort > 0 {
40+ if err := SDKAddPort (ctx , tunnelName , serverPort ); err != nil {
41+ return DevTunnelConnection {}, fmt .Errorf ("devtunnel create: add server port %d to %q: %w" , serverPort , tunnelName , err )
42+ }
43+ info .Ports = append (info .Ports , serverPort )
4944 }
5045
51- ctx := context .Background ()
52-
46+ // 3. Obtain host token and start the relay with the server port forwarded.
5347 hostToken , err := SDKGetHostToken (ctx , tunnelName )
5448 if err != nil {
55- return "" , DevTunnelConnection {}, fmt .Errorf ("devtunnel host : get host token for %q: %w" , tunnelName , err )
49+ return DevTunnelConnection {}, fmt .Errorf ("devtunnel create : get host token for %q: %w" , tunnelName , err )
5650 }
5751
58- // Host without ports — all port forwarding is done later via DevTunnelForward.
59- cmdID , connectionURL , err := CLIHostTunnel (devTunInfo .TunnelID , hostToken , nil )
52+ cmdID , connectionURL , err := CLIHostTunnel (info .TunnelID , hostToken , info .Ports )
6053 if err != nil {
61- return "" , DevTunnelConnection {}, fmt .Errorf ("devtunnel host : start CLI for %q: %w" , tunnelName , err )
54+ return DevTunnelConnection {}, fmt .Errorf ("devtunnel create : start host for %q: %w" , tunnelName , err )
6255 }
6356
64- // Track the host process command ID so DevTunnelForward can restart it.
65- devTunInfo .HostCmdID = cmdID
66- devTunInfo .HostToken = hostToken
57+ info .HostCmdID = cmdID
58+ info .HostToken = hostToken
6759
6860 conn := DevTunnelConnection {
6961 ConnectionURL : connectionURL ,
70- DevTunnelInfo : devTunInfo ,
62+ DevTunnelInfo : info ,
7163 }
7264
65+ // 4. Get a connect token for the client side.
7366 connectToken , tokenErr := SDKGetConnectToken (ctx , tunnelName )
7467 if tokenErr != nil {
75- log .Printf ("devtunnel host : warning — could not obtain connect token for %q: %v" , tunnelName , tokenErr )
68+ log .Printf ("devtunnel create : warning — could not obtain connect token for %q: %v" , tunnelName , tokenErr )
7669 } else {
7770 conn .Token = connectToken
7871 }
7972
80- return cmdID , conn , nil
73+ log .Printf ("devtunnel create: tunnel %q ready (id=%s, url=%s, port=%d)" , tunnelName , sdkTunnel .TunnelID , connectionURL , serverPort )
74+ return conn , nil
8175}
8276
8377// DevTunnelForward adds port forwarding to an existing hosted tunnel.
@@ -116,7 +110,6 @@ func DevTunnelForward(tunnelName string, port int, authToken string) error {
116110
117111 hostToken := devTunInfo .HostToken
118112 if hostToken == "" {
119- // Re-fetch if not cached (shouldn't happen in normal flow).
120113 hostToken , err = SDKGetHostToken (ctx , tunnelName )
121114 if err != nil {
122115 return fmt .Errorf ("devtunnel forward: get host token for %q: %w" , tunnelName , err )
0 commit comments