Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
wip: more tunnel fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Oct 3, 2024
1 parent 91848b8 commit 08df1f0
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 114 deletions.
9 changes: 3 additions & 6 deletions cmd/sst/mosaic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/sst/ion/cmd/sst/mosaic/dev"
"github.com/sst/ion/cmd/sst/mosaic/multiplexer"
"github.com/sst/ion/cmd/sst/mosaic/socket"
"github.com/sst/ion/cmd/sst/mosaic/tunnel"
"github.com/sst/ion/cmd/sst/mosaic/watcher"
"github.com/sst/ion/internal/util"
"github.com/sst/ion/pkg/bus"
Expand Down Expand Up @@ -156,11 +155,6 @@ func CmdMosaic(c *cli.Cli) error {
return socket.Start(c.Context, p, server)
})

wg.Go(func() error {
defer c.Cancel()
return tunnel.Start(c.Context, p)
})

wg.Go(func() error {
evts := bus.Subscribe(&runtime.BuildInput{})
for {
Expand Down Expand Up @@ -246,6 +240,9 @@ func CmdMosaic(c *cli.Cli) error {
append([]string{"SST_CHILD=" + d.Name}, multiEnv...)...,
)
}
for range evt.Tunnels {
multi.AddProcess("tunnel", []string{currentExecutable, "tunnel", "--stage", p.App().Stage}, "⇄", "Tunnel", "", true, true, os.Environ()...)
}
break
}
}
Expand Down
3 changes: 0 additions & 3 deletions cmd/sst/mosaic/multiplexer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ func (p *process) start() error {
}

func (p *process) Kill() {
if p.cmd != nil && p.cmd.Process != nil {
util.TerminateProcess(p.cmd.Process.Pid)
}
p.vt.Close()
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/sst/mosaic/multiplexer/tcell-term/vt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/creack/pty"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
"github.com/sst/ion/internal/util"
)

type (
Expand Down Expand Up @@ -460,7 +461,7 @@ func (vt *VT) Close() {
vt.mu.Lock()
defer vt.mu.Unlock()
if vt.cmd != nil && vt.cmd.Process != nil {
vt.cmd.Process.Kill()
util.TerminateProcess(vt.cmd.Process.Pid)
vt.cmd.Wait()
}
vt.pty.Close()
Expand Down
72 changes: 0 additions & 72 deletions cmd/sst/mosaic/tunnel/tunnel.go

This file was deleted.

16 changes: 8 additions & 8 deletions cmd/sst/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os/exec"
"os/user"
"strings"
"time"

"github.com/sst/ion/cmd/sst/cli"
"github.com/sst/ion/cmd/sst/mosaic/ui"
Expand Down Expand Up @@ -42,7 +41,8 @@ var CmdTunnel = &cli.Command{
}
subnets := strings.Join(tun.Subnets, ",")
// run as root
tunnelCmd := exec.Command(
tunnelCmd := exec.CommandContext(
c.Context,
"sudo", "-n", "-E",
tunnel.BINARY_PATH, "tunnel", "start",
"--subnets", subnets,
Expand All @@ -53,16 +53,13 @@ var CmdTunnel = &cli.Command{
tunnelCmd.Stdout = os.Stdout
tunnelCmd.Stderr = os.Stderr
util.SetProcessGroupID(tunnelCmd)
util.SetProcessCancel(tunnelCmd)
slog.Info("starting tunnel", "cmd", tunnelCmd.Args)
err = tunnelCmd.Start()
time.Sleep(time.Second * 1)
fmt.Println("tunneling through", tun.IP, "for")
for _, subnet := range tun.Subnets {
fmt.Println("-", subnet)
}
<-c.Context.Done()
util.TerminateProcess(tunnelCmd.Process.Pid)
tunnelCmd.Wait()
err = tunnelCmd.Run()
return nil
},
Children: []*cli.Command{
Expand Down Expand Up @@ -169,7 +166,10 @@ var CmdTunnel = &cli.Command{
return tunnel.Start(c.Context, subnets...)
})
slog.Info("tunnel started")
wg.Wait()
err = wg.Wait()
if err != nil {
slog.Error("failed to start tunnel", "error", err)
}
return nil
},
},
Expand Down
6 changes: 6 additions & 0 deletions internal/util/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ func SetProcessGroupID(cmd *exec.Cmd) {
// without killing this process (i.e. this code here).
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}

func SetProcessCancel(cmd *exec.Cmd) {
cmd.Cancel = func() error {
return TerminateProcess(cmd.Process.Pid)
}
}
5 changes: 1 addition & 4 deletions pkg/runtime/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ func (r *Runtime) Run(ctx context.Context, input *runtime.RunInput) (runtime.Wor
input.WorkerID,
)
util.SetProcessGroupID(cmd)
cmd.Cancel = func() error {
return util.TerminateProcess(cmd.Process.Pid)
}

util.SetProcessCancel(cmd)

Check failure on line 109 in pkg/runtime/node/node.go

View workflow job for this annotation

GitHub Actions / build

undefined: util.SetProcessCancel

Check failure on line 109 in pkg/runtime/node/node.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: util.SetProcessCancel
cmd.Env = input.Env
cmd.Env = append(cmd.Env, "NODE_OPTIONS="+os.Getenv("NODE_OPTIONS"))
cmd.Env = append(cmd.Env, "VSCODE_INSPECTOR_OPTIONS="+os.Getenv("VSCODE_INSPECTOR_OPTIONS"))
Expand Down
24 changes: 9 additions & 15 deletions pkg/runtime/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func (r *PythonRuntime) Build(ctx context.Context, input *runtime.BuildInput) (*
slog.Info("Copied file %s to %s", file, destPath)
}


// Find the closest pyproject.toml
startingPath := filepath.Dir(file)
pyProjectFile, err := FindClosestPyProjectToml(startingPath)
Expand All @@ -142,7 +141,7 @@ func (r *PythonRuntime) Build(ctx context.Context, input *runtime.BuildInput) (*

// We need to check if a deployment instead of dev
rpcBuildEnabled := false
if !input.Dev && rpcBuildEnabled{
if !input.Dev && rpcBuildEnabled {
// Handle zip and containers differently
// TODO: walln - handle container flag (not sure how the RPC call is shaped from component?)

Expand Down Expand Up @@ -172,11 +171,8 @@ func (r *PythonRuntime) Build(ctx context.Context, input *runtime.BuildInput) (*
ctx,
"uv",
"sync")

util.SetProcessGroupID(uvSyncCmd)
uvSyncCmd.Cancel = func() error {
return util.TerminateProcess(uvSyncCmd.Process.Pid)
}
util.SetProcessCancel(uvSyncCmd)

Check failure on line 175 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / build

undefined: util.SetProcessCancel

Check failure on line 175 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: util.SetProcessCancel

// use the output pyproject.toml directory as the working directory
workDir := filepath.Dir(filepath.Join(targetDir, filepath.Base(pyProjectFile)))
Expand All @@ -197,9 +193,7 @@ func (r *PythonRuntime) Build(ctx context.Context, input *runtime.BuildInput) (*
filepath.Join(workDir, ".venv", "lib", "python3.*", "site-packages", "*"),
filepath.Join(targetDir))
util.SetProcessGroupID(sitePackagesCmd)
sitePackagesCmd.Cancel = func() error {
return util.TerminateProcess(sitePackagesCmd.Process.Pid)
}
util.SetProcessCancel(sitePackagesCmd)

Check failure on line 196 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / build

undefined: util.SetProcessCancel

Check failure on line 196 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: util.SetProcessCancel

slog.Info("starting build site packages", "env", sitePackagesCmd.Env, "args", sitePackagesCmd.Args)
sitePackagesCmd.Dir = workDir
Expand All @@ -215,8 +209,8 @@ func (r *PythonRuntime) Build(ctx context.Context, input *runtime.BuildInput) (*
"rm",
"-rf",
filepath.Join(workDir, ".venv"))

util.SetProcessGroupID(removeVirtualEnvCmd)
util.SetProcessCancel(removeVirtualEnvCmd)

Check failure on line 213 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / build

undefined: util.SetProcessCancel

Check failure on line 213 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: util.SetProcessCancel
removeVirtualEnvCmd.Cancel = func() error {
return util.TerminateProcess(removeVirtualEnvCmd.Process.Pid)
}
Expand Down Expand Up @@ -349,8 +343,8 @@ func (r *PythonRuntime) Run(ctx context.Context, input *runtime.RunInput) (runti
ctx,
"uv",
args...)

util.SetProcessGroupID(cmd)
util.SetProcessCancel(cmd)

Check failure on line 347 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / build

undefined: util.SetProcessCancel

Check failure on line 347 in pkg/runtime/python/python.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: util.SetProcessCancel
cmd.Cancel = func() error {
return util.TerminateProcess(cmd.Process.Pid)
}
Expand Down Expand Up @@ -489,13 +483,12 @@ func getPythonFiles(filePath string) ([]string, error) {
}

return pythonFiles, nil
}

}

func writeResourcesFile(resourcesFile string, links map[string]json.RawMessage) error {
jsonData, err := json.MarshalIndent(links, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal links to JSON: %v", err)
return fmt.Errorf("failed to marshal links to JSON: %v", err)
}

// determine the directory of the resources file
Expand All @@ -513,4 +506,5 @@ func writeResourcesFile(resourcesFile string, links map[string]json.RawMessage)
}

return nil
}
}

5 changes: 1 addition & 4 deletions pkg/tunnel/tunnel_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ func Start(ctx context.Context, routes ...string) error {
slog.Info("creating interface", "name", name, "os", runtime.GOOS)
socksCmd := exec.CommandContext(ctx, "tun2socks", "-device", name, "-proxy", "socks5://127.0.0.1:1080")
util.SetProcessGroupID(socksCmd)
socksCmd.Cancel = func() error {
util.TerminateProcess(socksCmd.Process.Pid)
return nil
}
util.SetProcessCancel(socksCmd)

Check failure on line 18 in pkg/tunnel/tunnel_darwin.go

View workflow job for this annotation

GitHub Actions / build

undefined: util.SetProcessCancel

Check failure on line 18 in pkg/tunnel/tunnel_darwin.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: util.SetProcessCancel
socksCmd.Start()
time.Sleep(time.Second * 1)
cmds := [][]string{
Expand Down
12 changes: 11 additions & 1 deletion pkg/tunnel/tunnel_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log/slog"
"os/exec"
"runtime"

"github.com/sst/ion/internal/util"
)

func Start(ctx context.Context, routes ...string) error {
Expand All @@ -27,7 +29,15 @@ func Start(ctx context.Context, routes ...string) error {
}
defer destroy()
socksCmd := exec.CommandContext(ctx, "tun2socks", "-device", name, "-proxy", "socks5://127.0.0.1:1080")
return socksCmd.Run()
util.SetProcessGroupID(socksCmd)
util.SetProcessCancel(socksCmd)
slog.Info("running tun2socks", "cmd", socksCmd.Args)
err = socksCmd.Run()
if err != nil {
slog.Error("failed to run tun2socks", "error", err)
return err
}
return nil
}

func destroy() error {
Expand Down

0 comments on commit 08df1f0

Please sign in to comment.