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

@ -42,9 +42,9 @@ type DsnetConfig struct {
IP net.IP `validate:"required"` IP net.IP `validate:"required"`
DNS net.IP `validate:"required"` DNS net.IP `validate:"required"`
// TODO Default subnets to route via VPN // TODO Default subnets to route via VPN
ReportFile string `validate:"required"` ReportFile string `validate:"required"`
PrivateKey JSONKey `validate:"required,len=44"` PrivateKey JSONKey `validate:"required,len=44"`
Peers []PeerConfig `validate:"dive"` Peers []PeerConfig `validate:"dive"`
} }
func MustLoadDsnetConfig() *DsnetConfig { func MustLoadDsnetConfig() *DsnetConfig {

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"
) )
@ -46,13 +48,13 @@ func (s Status) MarshalJSON() ([]byte, error) {
} }
type DsnetReport struct { type DsnetReport struct {
ExternalIP net.IP ExternalIP net.IP
InterfaceName string InterfaceName string
ListenPort int ListenPort int
// domain to append to hostnames. Relies on separate DNS server for // domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only. // resolution. Informational only.
Domain string Domain string
IP net.IP IP net.IP
// IP network from which to allocate automatic sequential addresses // IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified // Network is chosen randomly when not specified
Network JSONIPNet Network JSONIPNet
@ -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))
@ -84,27 +86,27 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig) DsnetReport {
} }
peerReports[i] = PeerReport{ peerReports[i] = PeerReport{
Hostname: peer.Hostname, Hostname: peer.Hostname,
Owner: peer.Owner, Owner: peer.Owner,
Description: peer.Description, Description: peer.Description,
IP: peer.IP, IP: peer.IP,
Status: status, Status: status,
Networks: peer.Networks, Networks: peer.Networks,
LastHandshakeTime: wgPeer.LastHandshakeTime, LastHandshakeTime: wgPeer.LastHandshakeTime,
ReceiveBytes: wgPeer.ReceiveBytes, ReceiveBytes: wgPeer.ReceiveBytes,
TransmitBytes: wgPeer.TransmitBytes, TransmitBytes: wgPeer.TransmitBytes,
} }
} }
return DsnetReport{ return DsnetReport{
ExternalIP: conf.ExternalIP, ExternalIP: conf.ExternalIP,
InterfaceName: conf.InterfaceName, InterfaceName: conf.InterfaceName,
ListenPort: conf.ListenPort, ListenPort: conf.ListenPort,
Domain: conf.Domain, Domain: conf.Domain,
IP: conf.IP, IP: conf.IP,
Network: conf.Network, Network: conf.Network,
DNS: conf.DNS, DNS: conf.DNS,
Peers: peerReports, Peers: peerReports,
} }
} }
@ -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