diff --git a/configtypes.go b/configtypes.go index 95b90dd..f78173a 100644 --- a/configtypes.go +++ b/configtypes.go @@ -42,9 +42,9 @@ type DsnetConfig struct { IP net.IP `validate:"required"` DNS net.IP `validate:"required"` // TODO Default subnets to route via VPN - ReportFile string `validate:"required"` - PrivateKey JSONKey `validate:"required,len=44"` - Peers []PeerConfig `validate:"dive"` + ReportFile string `validate:"required"` + PrivateKey JSONKey `validate:"required,len=44"` + Peers []PeerConfig `validate:"dive"` } func MustLoadDsnetConfig() *DsnetConfig { diff --git a/init.go b/init.go index 01d3df3..211859f 100644 --- a/init.go +++ b/init.go @@ -19,7 +19,7 @@ func Init() { } conf := DsnetConfig{ - PrivateKey: GenerateJSONPrivateKey() + PrivateKey: GenerateJSONPrivateKey(), ListenPort: DEFAULT_LISTEN_PORT, Network: getRandomNetwork(), Peers: []PeerConfig{}, diff --git a/report.go b/report.go index 9ca4327..0712aa7 100644 --- a/report.go +++ b/report.go @@ -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) } diff --git a/reporttypes.go b/reporttypes.go index f1ae33b..aa4c1c8 100644 --- a/reporttypes.go +++ b/reporttypes.go @@ -4,8 +4,10 @@ import ( "encoding/json" "io/ioutil" "net" + "os" "time" + "github.com/go-playground/validator/v10" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -46,13 +48,13 @@ func (s Status) MarshalJSON() ([]byte, error) { } type DsnetReport struct { - ExternalIP net.IP + ExternalIP net.IP InterfaceName string - ListenPort int + ListenPort int // domain to append to hostnames. Relies on separate DNS server for // resolution. Informational only. - Domain string - IP net.IP + Domain string + IP net.IP // IP network from which to allocate automatic sequential addresses // Network is chosen randomly when not specified Network JSONIPNet @@ -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)) @@ -84,27 +86,27 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig) DsnetReport { } peerReports[i] = PeerReport{ - Hostname: peer.Hostname, - Owner: peer.Owner, - Description: peer.Description, - IP: peer.IP, - Status: status, - Networks: peer.Networks, + Hostname: peer.Hostname, + Owner: peer.Owner, + Description: peer.Description, + IP: peer.IP, + Status: status, + Networks: peer.Networks, LastHandshakeTime: wgPeer.LastHandshakeTime, - ReceiveBytes: wgPeer.ReceiveBytes, - TransmitBytes: wgPeer.TransmitBytes, + ReceiveBytes: wgPeer.ReceiveBytes, + TransmitBytes: wgPeer.TransmitBytes, } } return DsnetReport{ - ExternalIP: conf.ExternalIP, + ExternalIP: conf.ExternalIP, InterfaceName: conf.InterfaceName, - ListenPort: conf.ListenPort, - Domain: conf.Domain, - IP: conf.IP, - Network: conf.Network, - DNS: conf.DNS, - Peers: peerReports, + ListenPort: conf.ListenPort, + Domain: conf.Domain, + IP: conf.IP, + Network: conf.Network, + DNS: conf.DNS, + Peers: peerReports, } } @@ -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