This repository has been archived by the owner on Aug 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
novnc_generate.go
144 lines (121 loc) · 2.79 KB
/
novnc_generate.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// +build novnc_generate
package main
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/shurcooL/vfsgen"
"github.com/spkg/zipfs"
)
const noVNCZip = "https://github.com/novnc/noVNC/archive/master.zip"
const vncScript = `
try {
function parseQuery(e){for(var o=e.split("&"),n={},t=0;t<o.length;t++){var d=o[t].split("="),p=decodeURIComponent(d[0]),r=decodeURIComponent(d[1]);if(void 0===n[p])n[p]=decodeURIComponent(r);else if("string"==typeof n[p]){var i=[n[p],decodeURIComponent(r)];n[p]=i}else n[p].push(decodeURIComponent(r))}return n};
fetch(parseQuery(window.location.search.replace(/^\?/, ""))["path"]).then(function(resp) {
return resp.text();
}).then(function (txt) {
if (txt.indexOf("not websocket") == -1) alert(txt);
});
} catch (ex) {
console.log(ex);
}
`
func main() {
resp, err := http.Get(noVNCZip)
if err != nil {
panic(err)
}
f, err := ioutil.TempFile("", "novnc*.zip")
if err != nil {
panic(err)
}
_, err = io.Copy(f, resp.Body)
if err != nil {
panic(err)
}
f.Close()
resp.Body.Close()
err = modifyZip(f.Name())
if err != nil {
panic(err)
}
zfs, err := zipfs.New(f.Name())
if err != nil {
panic(err)
}
err = vfsgen.Generate(zfs, vfsgen.Options{
Filename: "novnc_generated.go",
PackageName: "main",
VariableName: "noVNC",
VariableComment: "noVNC is the latest version of noVNC from GitHub as a http.FileSystem",
})
if err != nil {
panic(err)
}
}
// modifyZip adds the custom easy-novnc code into the noVNC zip file.
func modifyZip(zf string) error {
buf, err := ioutil.ReadFile(zf)
if err != nil {
return err
}
zr, err := zip.NewReader(bytes.NewReader(buf), int64(len(buf)))
if err != nil {
return err
}
f, err := os.Create(zf)
if err != nil {
return err
}
defer f.Close()
zw := zip.NewWriter(f)
defer zw.Close()
var found bool
for _, e := range zr.File {
var w io.Writer
rc, err := e.Open()
if err != nil {
return err
}
fbuf, err := ioutil.ReadAll(rc)
if err != nil {
return err
}
if filepath.Base(e.Name) == "vnc.html" {
found = true
fbuf = bytes.ReplaceAll(fbuf, []byte("</body>"), []byte(fmt.Sprintf("<script>%s</script></body>", vncScript)))
fi, err := os.Stat("novnc_generate.go")
if err != nil {
return err
}
w, err = zw.CreateHeader(&zip.FileHeader{
Name: e.Name,
Flags: e.Flags,
Method: e.Method,
Modified: fi.ModTime(),
Extra: e.Extra,
ExternalAttrs: e.ExternalAttrs,
})
} else {
w, err = zw.CreateHeader(&e.FileHeader)
}
if err != nil {
return err
}
_, err = io.Copy(w, bytes.NewReader(fbuf))
if err != nil {
return err
}
rc.Close()
}
if !found {
return errors.New("could not find vnc.html")
}
return nil
}