switch to loop parsing

So the stop on error pattern can be used
This commit is contained in:
Julien Viard de Galbert 2021-07-06 10:50:42 +02:00
parent f8f7d0b0c7
commit 32b0b94f6c

View File

@ -47,6 +47,30 @@ const (
publicKeyKey key = "PublicKey" publicKeyKey key = "PublicKey"
) )
type dumpInterfaceIndex int
const (
dumpInterfacePrivateKeyIndex = iota
dumpInterfacePublicKeyIndex
dumpInterfaceListenPortIndex
dumpInterfaceFWMarkIndex
dumpInterfaceLen
)
type dumpPeerIndex int
const (
dumpPeerPublicKeyIndex = iota
dumpPeerPresharedKeyIndex
dumpPeerEndpointIndex
dumpPeerAllowedIPsIndex
dumpPeerLatestHandshakeIndex
dumpPeerTransferRXIndex
dumpPeerTransferTXIndex
dumpPeerPersistentKeepaliveIndex
dumpPeerLen
)
// Conf represents a WireGuard configuration file. // Conf represents a WireGuard configuration file.
type Conf struct { type Conf struct {
Interface *Interface Interface *Interface
@ -500,10 +524,10 @@ func ParseDump(buf []byte) *Conf {
c Conf c Conf
err error err error
iface *Interface iface *Interface
i int
peer *Peer peer *Peer
port uint64 port uint64
sec int64 sec int64
pka int
) )
// First line is Interface // First line is Interface
active = interfaceSection active = interfaceSection
@ -513,47 +537,70 @@ func ParseDump(buf []byte) *Conf {
switch active { switch active {
case interfaceSection: case interfaceSection:
if len(values) < 4 { if len(values) < dumpInterfaceLen {
break break
} }
iface = new(Interface) iface = new(Interface)
for i := range values {
iface.PrivateKey = []byte(values[0]) switch i {
port, err = strconv.ParseUint(values[2], 10, 32) case dumpInterfacePrivateKeyIndex:
if err == nil { iface.PrivateKey = []byte(values[i])
iface.ListenPort = uint32(port) case dumpInterfaceListenPortIndex:
port, err = strconv.ParseUint(values[i], 10, 32)
if err != nil {
continue
}
iface.ListenPort = uint32(port)
}
} }
c.Interface = iface c.Interface = iface
// Next lines are Peers // Next lines are Peers
active = peerSection active = peerSection
case peerSection: case peerSection:
if len(values) < 8 { if len(values) < dumpPeerLen {
break break
} }
peer = new(Peer) peer = new(Peer)
peer.PublicKey = []byte(values[0]) for i := range values {
if values[1] != dumpNone { switch i {
peer.PresharedKey = []byte(values[1]) case dumpPeerPublicKeyIndex:
} peer.PublicKey = []byte(values[i])
if values[2] != dumpNone { case dumpPeerPresharedKeyIndex:
peer.parseEndpoint(values[2]) if values[i] == dumpNone {
} continue
if values[3] != dumpNone { }
peer.parseAllowedIPs(values[3]) peer.PresharedKey = []byte(values[i])
} case dumpPeerEndpointIndex:
if values[4] != "0" { if values[i] == dumpNone {
sec, err = strconv.ParseInt(values[4], 10, 64) continue
if err == nil { }
peer.parseEndpoint(values[i])
case dumpPeerAllowedIPsIndex:
if values[i] == dumpNone {
continue
}
peer.parseAllowedIPs(values[i])
case dumpPeerLatestHandshakeIndex:
if values[i] == "0" {
// Use go zero value, not unix 0 timestamp.
peer.LatestHandshake = time.Time{}
continue
}
sec, err = strconv.ParseInt(values[i], 10, 64)
if err != nil {
continue
}
peer.LatestHandshake = time.Unix(sec, 0) peer.LatestHandshake = time.Unix(sec, 0)
} case dumpPeerPersistentKeepaliveIndex:
} if values[i] == dumpOff {
continue
if values[7] != dumpOff { }
i, err = strconv.Atoi(values[7]) pka, err = strconv.Atoi(values[i])
if err == nil { if err != nil {
peer.PersistentKeepalive = i continue
}
peer.PersistentKeepalive = pka
} }
} }
c.Peers = append(c.Peers, peer) c.Peers = append(c.Peers, peer)