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{
|
conf := DsnetConfig{
|
||||||
PrivateKey: GenerateJSONPrivateKey()
|
PrivateKey: GenerateJSONPrivateKey(),
|
||||||
ListenPort: DEFAULT_LISTEN_PORT,
|
ListenPort: DEFAULT_LISTEN_PORT,
|
||||||
Network: getRandomNetwork(),
|
Network: getRandomNetwork(),
|
||||||
Peers: []PeerConfig{},
|
Peers: []PeerConfig{},
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user