diff --git a/add.go b/add.go index 10dd2fe..cea2c1e 100644 --- a/add.go +++ b/add.go @@ -1,17 +1,4 @@ package dsnet -import ( - "io/ioutil" - "encoding/json" - "fmt" -) - func Add(hostname string, owner string, description string, publicKey string) { - raw, err := ioutil.ReadFile(CONFIG_FILE) - check(err) - conf := DsnetConfig{} - err = json.Unmarshal(raw, &conf) - check(err) - - fmt.Printf("%+v\n", conf) } diff --git a/go.mod b/go.mod index 732242c..e26d3ed 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/naggie/dsnet go 1.13 -require golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200205215550-e35592f146e4 +require ( + golang.org/x/tools v0.0.0-20200302155637-b1e4e04173e0 // indirect + golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200205215550-e35592f146e4 +) diff --git a/go.sum b/go.sum index 53479e4..7bd60f5 100644 --- a/go.sum +++ b/go.sum @@ -11,14 +11,19 @@ github.com/mdlayher/netlink v1.1.0/go.mod h1:H4WCitaheIsdF9yOYu8CFmCgQthAPIWZmcK github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72 h1:+ELyKg6m8UBf0nPFSqD0mi7zUfwPyXo23HNjMnXPz7w= golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191003171128-d98b1b443823/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191007182048-72f939374954/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190411185658-b44545bcd369/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -29,7 +34,12 @@ golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20200302155637-b1e4e04173e0 h1:fgnkocwzO9swV2pnFKnfje0dMOdhcdolLbvr1gNEtlQ= +golang.org/x/tools v0.0.0-20200302155637-b1e4e04173e0/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.zx2c4.com/wireguard v0.0.20200121 h1:vcswa5Q6f+sylDfjqyrVNNrjsFUUbPsgAQTBCAg/Qf8= golang.zx2c4.com/wireguard v0.0.20200121/go.mod h1:P2HsVp8SKwZEufsnezXZA4GRX/T49/HlU7DGuelXsU4= diff --git a/init.go b/init.go index 95758b9..89c9514 100644 --- a/init.go +++ b/init.go @@ -1,12 +1,10 @@ package dsnet import ( - "encoding/json" "fmt" "math/rand" "net" "time" - "io/ioutil" //"github.com/mikioh/ipaddr" ) @@ -24,9 +22,7 @@ func Init() { ReportFile: DEFAULT_REPORT_FILE, } - _json, _ := json.MarshalIndent(conf, "", " ") - err := ioutil.WriteFile(CONFIG_FILE, _json, 0600) - check(err) + conf.MustSave() fmt.Printf("Config written to %s. Please edit.", CONFIG_FILE) } diff --git a/types.go b/types.go index 511ed70..3aeabd7 100644 --- a/types.go +++ b/types.go @@ -1,10 +1,13 @@ package dsnet import ( - "golang.zx2c4.com/wireguard/wgctrl/wgtypes" + "encoding/json" + "io/ioutil" "net" - "time" "strings" + "time" + + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) // see https://github.com/WireGuard/wgctrl-go/blob/master/wgtypes/types.go for definitions @@ -58,6 +61,21 @@ type DsnetConfig struct { ReportFile string `validate:"required"` } +func MustLoadDsnetConfig() *DsnetConfig { + raw, err := ioutil.ReadFile(CONFIG_FILE) + check(err) + conf := DsnetConfig{} + err = json.Unmarshal(raw, &conf) + check(err) + return &conf +} + +func (conf *DsnetConfig) MustSave() { + _json, _ := json.MarshalIndent(conf, "", " ") + err := ioutil.WriteFile(CONFIG_FILE, _json, 0600) + check(err) +} + type Dsnet struct { Name string PrivateKey wgtypes.Key diff --git a/util.go b/util.go index 5c70f51..19a137b 100644 --- a/util.go +++ b/util.go @@ -1,16 +1,16 @@ package dsnet import ( - "os" "bufio" - "strings" "fmt" + "os" + "strings" ) func check(e error) { - if e != nil { - panic(e) - } + if e != nil { + panic(e) + } } func MustPromptString(prompt string, required bool) string {