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