Skip to content

Commit 971edbb

Browse files
authored
Use preshared keys and make pingable (#15)
* make wireguard pingable * accept preshared key for a peer * forked to inventage * Revert "forked to inventage" This reverts commit d7cdee9.
1 parent ed90ef8 commit 971edbb

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

pkg/wgembed/iface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88

99
type WireGuardInterface interface {
1010
LoadConfig(config *ConfigFile) error
11-
AddPeer(publicKey string, addressCIDR []string) error
11+
AddPeer(publicKey string, presharedKey string, addressCIDR []string) error
1212
ListPeers() ([]wgtypes.Peer, error)
1313
RemovePeer(publicKey string) error
1414
PublicKey() (string, error)
1515
Close() error
16+
Ping() error
1617
}
1718

1819
// Options contains configuration options for the interface

pkg/wgembed/management.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,23 @@ import (
1212
// AddPeer adds a new peer to the interface.
1313
// The subnet sizes in addressCIDR should be /32 for IPv4 and /128 for IPv6,
1414
// as the whole subnet will be added to AllowedIPs for this device.
15-
func (wg *commonInterface) AddPeer(publicKey string, addressCIDR []string) error {
16-
key, err := wgtypes.ParseKey(publicKey)
15+
// The presharedKey is optinal and can be omitted with nil
16+
func (wg *commonInterface) AddPeer(publicKey string, presharedKey string, addressCIDR []string) error {
17+
wgPublicKey, err := wgtypes.ParseKey(publicKey)
1718
if err != nil {
1819
return errors.Wrapf(err, "bad public key %v", publicKey)
1920
}
2021

22+
var wgPresharedKey *wgtypes.Key
23+
if len(presharedKey) != 0 {
24+
psk, err := wgtypes.ParseKey(presharedKey)
25+
if err != nil {
26+
logrus.WithError(err).Warnf("ignoring bad pre-shared key: %v", presharedKey)
27+
} else {
28+
wgPresharedKey = &psk
29+
}
30+
}
31+
2132
parsedAddresses := make([]net.IPNet, 0, len(addressCIDR))
2233
for _, addr := range addressCIDR {
2334
_, allowedIPs, err := net.ParseCIDR(addr)
@@ -31,7 +42,8 @@ func (wg *commonInterface) AddPeer(publicKey string, addressCIDR []string) error
3142
config.ReplacePeers = false
3243
config.Peers = []wgtypes.PeerConfig{
3344
{
34-
PublicKey: key,
45+
PublicKey: wgPublicKey,
46+
PresharedKey: wgPresharedKey,
3547
AllowedIPs: parsedAddresses,
3648
ReplaceAllowedIPs: true,
3749
},
@@ -109,6 +121,13 @@ func (wg *commonInterface) Port() (int, error) {
109121
return device.ListenPort, nil
110122
}
111123

124+
func (wg *commonInterface) Ping() error {
125+
if _, err := wg.ListPeers(); err != nil {
126+
return errors.New("failed to ping wireguard")
127+
}
128+
return nil
129+
}
130+
112131
func (wg *commonInterface) configure(cb func(*wgtypes.Config) error) error {
113132
// TODO: concurrency
114133
// s.lock.Lock()

pkg/wgembed/noop.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (wg *NoOpWireguardInterface) LoadConfig(config *ConfigFile) error {
1313
return nil
1414
}
1515

16-
func (wg *NoOpWireguardInterface) AddPeer(publicKey string, addressCIDR []string) error {
16+
func (wg *NoOpWireguardInterface) AddPeer(publicKey string, presharedKey string, addressCIDR []string) error {
1717
return nil
1818
}
1919

@@ -32,3 +32,7 @@ func (wg *NoOpWireguardInterface) PublicKey() (string, error) {
3232
func (wg *NoOpWireguardInterface) Close() error {
3333
return nil
3434
}
35+
36+
func (wg *NoOpWireguardInterface) Ping() error {
37+
return nil
38+
}

0 commit comments

Comments
 (0)