|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "strconv" |
| 12 | + "time" |
| 13 | + |
| 14 | + "go.coder.com/flog" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + flag.Usage = func() { |
| 19 | + fmt.Printf(`Usage: %v HOST [SSH ARGS...] |
| 20 | +
|
| 21 | +Start code-server over SSH. |
| 22 | +More info: https://github.com/codercom/sshcode |
| 23 | +`, os.Args[0]) |
| 24 | + } |
| 25 | + |
| 26 | + flag.Parse() |
| 27 | + host := flag.Arg(0) |
| 28 | + |
| 29 | + if host == "" { |
| 30 | + // if no host is specified output usage |
| 31 | + flag.Usage() |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + flog.Info("ensuring code-server is updated...") |
| 36 | + |
| 37 | + // downloads the latest code-server and allows it to be executed |
| 38 | + sshCmd := exec.Command("ssh", |
| 39 | + "-tt", |
| 40 | + host, |
| 41 | + `/bin/bash -c 'set -euxo pipefail || exit 1 |
| 42 | +mkdir -p ~/bin |
| 43 | +wget -q https://codesrv-ci.cdr.sh/latest-linux -O ~/bin/code-server |
| 44 | +chmod +x ~/bin/code-server |
| 45 | +'`, |
| 46 | + ) |
| 47 | + output, err := sshCmd.CombinedOutput() |
| 48 | + if err != nil { |
| 49 | + flog.Fatal("failed to update code-server: %v: %s", err, string(output)) |
| 50 | + } |
| 51 | + |
| 52 | + flog.Info("starting code-server...") |
| 53 | + localPort := scanAvailablePort() |
| 54 | + |
| 55 | + // starts code-server and forwards the remote port |
| 56 | + sshCmd = exec.Command("ssh", |
| 57 | + "-tt", |
| 58 | + "-L", |
| 59 | + localPort+":localhost:"+localPort, |
| 60 | + host, |
| 61 | + "~/bin/code-server --host 127.0.0.1 --allow-http --no-auth --port="+localPort, |
| 62 | + ) |
| 63 | + err = sshCmd.Start() |
| 64 | + if err != nil { |
| 65 | + flog.Fatal("failed to start code-server: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + var openCmd *exec.Cmd |
| 69 | + url := "http://127.0.0.1:" + localPort |
| 70 | + if commandExists("google-chrome") { |
| 71 | + openCmd = exec.Command("google-chrome", "--app="+url, "--disable-extensions", "--disable-plugins") |
| 72 | + } else if commandExists("firefox") { |
| 73 | + openCmd = exec.Command("firefox", "--url="+url, "-safe-mode") |
| 74 | + } |
| 75 | + |
| 76 | + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 77 | + defer cancel() |
| 78 | + for ctx.Err() == nil { |
| 79 | + // waits for code-server to be available before opening the browser |
| 80 | + r, _ := http.NewRequest("GET", url, nil) |
| 81 | + r = r.WithContext(ctx) |
| 82 | + resp, err := http.DefaultClient.Do(r) |
| 83 | + if err != nil { |
| 84 | + continue |
| 85 | + } |
| 86 | + resp.Body.Close() |
| 87 | + break |
| 88 | + } |
| 89 | + |
| 90 | + flog.Success("code-server is running. opening %s", url) |
| 91 | + err = openCmd.Start() |
| 92 | + if err != nil { |
| 93 | + flog.Fatal("failed to open browser: %v", err) |
| 94 | + } |
| 95 | + sshCmd.Wait() |
| 96 | +} |
| 97 | + |
| 98 | +// Checks if a command exists locally |
| 99 | +func commandExists(name string) bool { |
| 100 | + _, err := exec.LookPath(name) |
| 101 | + return err == nil |
| 102 | +} |
| 103 | + |
| 104 | +// Scans from 1024+ until an available port is found |
| 105 | +func scanAvailablePort() string { |
| 106 | + port := uint16(1024) |
| 107 | + for { |
| 108 | + l, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) |
| 109 | + if err != nil { |
| 110 | + // if we have an error the port is taken |
| 111 | + port++ |
| 112 | + continue |
| 113 | + } |
| 114 | + _ = l.Close() |
| 115 | + |
| 116 | + return strconv.Itoa(int(port)) |
| 117 | + } |
| 118 | +} |
0 commit comments