Skip to content

Commit

Permalink
report to stdout, untested
Browse files Browse the repository at this point in the history
  • Loading branch information
naggie committed Sep 9, 2022
1 parent ab80b06 commit ead319f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 47 deletions.
6 changes: 0 additions & 6 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://www.wireguard.com/netns/>.

"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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
39 changes: 3 additions & 36 deletions cmd/cli/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,15 @@ 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 {
peerTimeout := viper.GetDuration("peer_timeout")
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)
Expand All @@ -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]

Expand Down Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down Expand Up @@ -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/)
Expand Down

0 comments on commit ead319f

Please sign in to comment.