read old report for later use

This commit is contained in:
Callan Bryant 2020-03-05 23:56:24 +00:00
parent 78b28be231
commit 3c8ba7e3bc
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
4 changed files with 49 additions and 25 deletions

View File

@ -19,7 +19,7 @@ func Init() {
} }
conf := DsnetConfig{ conf := DsnetConfig{
PrivateKey: GenerateJSONPrivateKey() PrivateKey: GenerateJSONPrivateKey(),
ListenPort: DEFAULT_LISTEN_PORT, ListenPort: DEFAULT_LISTEN_PORT,
Network: getRandomNetwork(), Network: getRandomNetwork(),
Peers: []PeerConfig{}, Peers: []PeerConfig{},

View File

@ -17,6 +17,7 @@ func Report() {
ExitFail("Could not retrieve device '%s' (%v)", conf.InterfaceName, err) ExitFail("Could not retrieve device '%s' (%v)", conf.InterfaceName, err)
} }
report := GenerateReport(dev, conf) oldReport := MustLoadDsnetReport()
report := GenerateReport(dev, conf, oldReport)
report.MustSave(conf.ReportFile) report.MustSave(conf.ReportFile)
} }

View File

@ -4,8 +4,10 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net" "net"
"os"
"time" "time"
"github.com/go-playground/validator/v10"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
@ -60,7 +62,7 @@ type DsnetReport struct {
Peers []PeerReport Peers []PeerReport
} }
func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig) DsnetReport { func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport {
wgPeerIndex := make(map[wgtypes.Key]wgtypes.Peer) wgPeerIndex := make(map[wgtypes.Key]wgtypes.Peer)
peerReports := make([]PeerReport, len(conf.Peers)) peerReports := make([]PeerReport, len(conf.Peers))
@ -114,6 +116,27 @@ func (report *DsnetReport) MustSave(filename string) {
check(err) check(err)
} }
func MustLoadDsnetReport() *DsnetReport {
raw, err := ioutil.ReadFile(CONFIG_FILE)
if os.IsNotExist(err) {
return nil
} else if os.IsPermission(err) {
ExitFail("%s cannot be accessed. Check read permissions.", CONFIG_FILE)
} else {
check(err)
}
report := DsnetReport{}
err = json.Unmarshal(raw, &report)
check(err)
err = validator.New().Struct(report)
check(err)
return &report
}
type PeerReport struct { type PeerReport struct {
// Used to update DNS // Used to update DNS
Hostname string Hostname string