forked from user360/vuvuzela
/
main.go
67 lines (55 loc) · 1.36 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
package main
import (
"crypto/rand"
"encoding/json"
"flag"
"io/ioutil"
log "github.com/sirupsen/logrus"
. "github.com/davidlazar/vuvuzela"
. "github.com/davidlazar/vuvuzela/internal"
)
var doInit = flag.Bool("init", false, "create default config file")
var confPath = flag.String("conf", "confs/client.conf", "config file")
var pkiPath = flag.String("pki", "confs/pki.conf", "pki file")
type Conf struct {
MyName string
MyPublicKey *BoxKey
MyPrivateKey *BoxKey
}
func WriteDefaultConf(path string) {
myPublicKey, myPrivateKey, err := GenerateBoxKey(rand.Reader)
if err != nil {
log.Fatalf("GenerateBoxKey: %s", err)
}
conf := &Conf{
MyPublicKey: myPublicKey,
MyPrivateKey: myPrivateKey,
}
data, err := json.MarshalIndent(conf, "", " ")
if err != nil {
log.Fatalf("json encoding error: %s", err)
}
if err := ioutil.WriteFile(path, data, 0600); err != nil {
log.Fatalf("WriteFile: %s", err)
}
}
func main() {
flag.Parse()
if *doInit {
WriteDefaultConf(*confPath)
return
}
pki := ReadPKI(*pkiPath)
conf := new(Conf)
ReadJSONFile(*confPath, conf)
if conf.MyName == "" || conf.MyPublicKey == nil || conf.MyPrivateKey == nil {
log.Fatalf("missing required fields: %s", *confPath)
}
gc := &GuiClient{
pki: pki,
myName: conf.MyName,
myPublicKey: conf.MyPublicKey,
myPrivateKey: conf.MyPrivateKey,
}
gc.Run()
}