-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (86 loc) · 1.83 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
package main
import (
"embed"
"flag"
"io/fs"
"log"
"net/http"
"os/exec"
"runtime"
"github.com/linuxexam/webrun/util"
)
//go:embed ui
var UI embed.FS
const dev = false
func main() {
// parse args
var listen = flag.String("listen", ":8080", "listen port")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
if runtime.GOOS == "windows" {
args = append(args, "cmd")
} else {
args = append(args, "sh")
}
}
path, err := exec.LookPath(args[0])
if err != nil {
log.Fatalf("command %s doesn't exist!", args[0])
}
args[0] = path
// web UI
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// TODO: login
clientID := util.GenerateUUID()
cookie := &http.Cookie{
Name: "client_id",
Value: clientID,
Path: "/",
MaxAge: 3600,
HttpOnly: true,
Secure: false,
}
http.SetCookie(w, cookie)
NewClient(clientID, args[0], args[1:]...)
if dev {
http.FileServer(http.Dir("ui")).ServeHTTP(w, r)
} else {
sub, err := fs.Sub(UI, "ui")
if err != nil {
panic(err)
}
http.FileServer(http.FS(sub)).ServeHTTP(w, r)
}
})
// websocket handler for admin
// e.g. winsize change event
http.HandleFunc("/ws-admin", func(w http.ResponseWriter, r *http.Request) {
idCookie, err := r.Cookie("client_id")
if err != nil {
http.NotFound(w, r)
return
}
client := FindClient(idCookie.Value)
if client == nil {
http.NotFound(w, r)
return
}
client.ServeAdmin(w, r)
})
// websocket handler for terminal proto
http.HandleFunc("/ws-term", func(w http.ResponseWriter, r *http.Request) {
idCookie, err := r.Cookie("client_id")
if err != nil {
http.NotFound(w, r)
return
}
client := FindClient(idCookie.Value)
if client == nil {
http.NotFound(w, r)
return
}
client.ServeTerm(w, r)
})
log.Fatal(http.ListenAndServe(*listen, nil))
}