switch to loop parsing
So the stop on error pattern can be used
This commit is contained in:
parent
f8f7d0b0c7
commit
32b0b94f6c
@ -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])
|
||||||
|
case dumpInterfaceListenPortIndex:
|
||||||
|
port, err = strconv.ParseUint(values[i], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
iface.ListenPort = uint32(port)
|
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])
|
||||||
|
case dumpPeerPresharedKeyIndex:
|
||||||
|
if values[i] == dumpNone {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if values[2] != dumpNone {
|
peer.PresharedKey = []byte(values[i])
|
||||||
peer.parseEndpoint(values[2])
|
case dumpPeerEndpointIndex:
|
||||||
|
if values[i] == dumpNone {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if values[3] != dumpNone {
|
peer.parseEndpoint(values[i])
|
||||||
peer.parseAllowedIPs(values[3])
|
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
|
||||||
}
|
}
|
||||||
if values[4] != "0" {
|
|
||||||
sec, err = strconv.ParseInt(values[4], 10, 64)
|
|
||||||
if err == nil {
|
|
||||||
peer.LatestHandshake = time.Unix(sec, 0)
|
peer.LatestHandshake = time.Unix(sec, 0)
|
||||||
|
case dumpPeerPersistentKeepaliveIndex:
|
||||||
|
if values[i] == dumpOff {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
pka, err = strconv.Atoi(values[i])
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
peer.PersistentKeepalive = pka
|
||||||
if values[7] != dumpOff {
|
|
||||||
i, err = strconv.Atoi(values[7])
|
|
||||||
if err == nil {
|
|
||||||
peer.PersistentKeepalive = i
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.Peers = append(c.Peers, peer)
|
c.Peers = append(c.Peers, peer)
|
||||||
|
Loading…
Reference in New Issue
Block a user