diff --git a/CONFIG.md b/CONFIG.md index 4d762d0..700b849 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -68,12 +68,6 @@ route the whole internet through the server peer, add `0.0.0.0/0` to the list before adding peers. For more advanced options and theory, see . - "ReportFile": "/var/lib/dsnetreport.json", - -This is the location of the report file generated with `dsnet report`. It is -suggested that this command is run via a cron job; the report can be safely -consumed by a web service or DNS integration script, for instance. - The report contains no sensitive information. At one site I use it together with [hugo](https://gohugo.io/) [shortcodes](https://gohugo.io/templates/shortcode-templates/) to generate a diff --git a/README.md b/README.md index ef02f10..4ebc268 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ It works on AMD64 based linux and also ARMv5. init Create /etc/dsnetconfig.json containing default configuration + new keys without loading. Edit to taste. regenerate Regenerate keys and config for peer remove Remove a peer by hostname provided as argument + sync - report Generate a JSON status report to the location configured in /etc/dsnetconfig.json. + report Generate a JSON status report to stdout sync Update wireguard configuration from /etc/dsnetconfig.json after validating up Create the interface, run pre/post up, sync version Print version @@ -152,8 +152,8 @@ See [CONFIG.md](CONFIG.md) for an explanation of each field. # Report file overview -An example report file, generated by `dsnet report` to -`/var/lib/dsnetreport.json` by default: +An example report file, generated by `dsnet report`. Suggested location: +`/var/lib/dsnetreport.json`: { "ExternalIP": "198.51.100.2", diff --git a/cmd/cli/report.go b/cmd/cli/report.go index f6cad60..31b8d58 100644 --- a/cmd/cli/report.go +++ b/cmd/cli/report.go @@ -82,9 +82,8 @@ func GenerateReport() { ExitFail("Could not retrieve device '%s' (%v)", conf.InterfaceName, err) } - oldReport := MustLoadDsnetReport() report := GetReport(dev, conf, oldReport) - report.MustSave() + report.Print() } func GetReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport { @@ -92,7 +91,6 @@ func GetReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) D peerExpiry := viper.GetDuration("peer_expiry") wgPeerIndex := make(map[wgtypes.Key]wgtypes.Peer) peerReports := make([]PeerReport, 0) - oldPeerReportIndex := make(map[string]PeerReport) peersOnline := 0 linkDev, err := netlink.LinkByName(conf.InterfaceName) @@ -104,12 +102,6 @@ func GetReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) D wgPeerIndex[peer.PublicKey] = peer } - if oldReport != nil { - for _, report := range oldReport.Peers { - oldPeerReportIndex[report.Hostname] = report - } - } - for _, peer := range conf.Peers { wgPeer, known := wgPeerIndex[peer.PublicKey.Key] @@ -174,34 +166,9 @@ func GetReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) D } } -func (report *DsnetReport) MustSave() { - reportFilePath := viper.GetString("report_file") - +func (report *DsnetReport) Print() { _json, _ := json.MarshalIndent(report, "", " ") _json = append(_json, '\n') - err := ioutil.WriteFile(reportFilePath, _json, 0644) - check(err) -} - -func MustLoadDsnetReport() *DsnetReport { - reportFilePath := viper.GetString("report_file_path") - raw, err := ioutil.ReadFile(reportFilePath) - - if os.IsNotExist(err) { - return nil - } else if os.IsPermission(err) { - ExitFail("%s cannot be accessed. Check read permissions.", reportFilePath) - } else { - check(err) - } - - report := DsnetReport{} - err = json.Unmarshal(raw, &report) - check(err) - - err = validator.New().Struct(report) - check(err) - - return &report + fmt.Print(_json) } diff --git a/cmd/root.go b/cmd/root.go index 528d2ec..7e7f45e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -94,7 +94,7 @@ var ( reportCmd = &cobra.Command{ Use: "report", - Short: fmt.Sprintf("Generate a JSON status report to the location configured in %s.", viper.GetString("config_file")), + Short: "Generate a JSON status report to stdout", Run: func(cmd *cobra.Command, args []string) { cli.GenerateReport() }, @@ -145,7 +145,6 @@ func init() { viper.SetDefault("config_file", "/etc/dsnetconfig.json") viper.SetDefault("fallback_wg_bing", "wireguard-go") viper.SetDefault("listen_port", 51820) - viper.SetDefault("report_file", "/var/lib/dsnetreport.json") viper.SetDefault("interface_name", "dsnet") // if last handshake (different from keepalive, see https://www.wireguard.com/protocol/)