-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.go
86 lines (70 loc) · 2.68 KB
/
browser.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
// +build js,wasm
package main
import (
"net/url"
"syscall/js"
"github.com/cloudflare/cfssl/log"
)
func run(this js.Value, inputs []js.Value) interface{} {
caCommonName := js.Global().Get("document").Call("getElementById", "root_common_name").Get("value").String()
caCommonName = strOrDefault(caCommonName, "mtls.dev")
caBundle, err := generateCACert(caCommonName)
if err != nil {
log.Errorf("generateCACert(%s): %s", caCommonName, err)
return nil
}
serverCommonName := js.Global().Get("document").Call("getElementById", "server_common_name").Get("value").String()
serverHostsJoined := js.Global().Get("document").Call("getElementById", "server_hosts").Get("value").String()
serverExpiration := js.Global().Get("document").Call("getElementById", "server_duration").Get("value").String()
serverConf := CertConfig{
CommonName: strOrDefault(serverCommonName, "localhost"),
Hosts: strOrDefault(serverHostsJoined, "localhost,mtls.dev"),
Expiration: strOrDefault(serverExpiration, defaultConfig.ExpiryString),
CACert: caBundle.Cert,
CAKey: caBundle.Key,
}
serverBundle, err := generateServerCert(serverConf)
if err != nil {
log.Errorf("generateServerCert(%s, %s): %s", serverCommonName, serverHostsJoined, err)
return nil
}
clientCommonName := js.Global().Get("document").Call("getElementById", "client_common_name").Get("value").String()
clientExpiration := js.Global().Get("document").Call("getElementById", "client_duration").Get("value").String()
clientConf := CertConfig{
CommonName: strOrDefault(clientCommonName, "localhost"),
Expiration: strOrDefault(clientExpiration, defaultConfig.ExpiryString),
CACert: caBundle.Cert,
CAKey: caBundle.Key,
}
clientBundle, err := generateClientCert(clientConf)
downloadAll(caBundle, serverBundle, clientBundle)
return nil
}
func downloadAll(caBundle, serverBundle, clientBundle CertBundle) {
download("ca-key.pem", caBundle.Key)
download("ca.pem", caBundle.Cert)
download("server-key.pem", serverBundle.Key)
download("server.pem", serverBundle.Cert)
download("client-key.pem", clientBundle.Key)
download("client.pem", clientBundle.Cert)
}
func download(filename string, contents []byte) {
escaped := url.PathEscape(string(contents))
document := js.Global().Get("document")
a := document.Call("createElement", "a")
a.Set("href", "data:text/plain;charset=utf-8,"+escaped)
a.Set("download", filename)
a.Set("style", "display: none;")
document.Get("body").Call("appendChild", a)
a.Call("click")
document.Get("body").Call("removeChild", a)
}
func registerRunFunc() {
js.Global().Set("run", js.FuncOf(run))
}
func strOrDefault(str string, def string) string {
if str != "" {
return str
}
return def
}