read old report for later use
This commit is contained in:
parent
78b28be231
commit
3c8ba7e3bc
2
init.go
2
init.go
@ -19,7 +19,7 @@ func Init() {
|
||||
}
|
||||
|
||||
conf := DsnetConfig{
|
||||
PrivateKey: GenerateJSONPrivateKey()
|
||||
PrivateKey: GenerateJSONPrivateKey(),
|
||||
ListenPort: DEFAULT_LISTEN_PORT,
|
||||
Network: getRandomNetwork(),
|
||||
Peers: []PeerConfig{},
|
||||
|
@ -17,6 +17,7 @@ func Report() {
|
||||
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)
|
||||
}
|
||||
|
@ -4,8 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
@ -60,7 +62,7 @@ type DsnetReport struct {
|
||||
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)
|
||||
peerReports := make([]PeerReport, len(conf.Peers))
|
||||
|
||||
@ -114,6 +116,27 @@ func (report *DsnetReport) MustSave(filename string) {
|
||||
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 {
|
||||
// Used to update DNS
|
||||
Hostname string
|
||||
|
Loading…
Reference in New Issue
Block a user