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