@@ -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+
112131func (wg * commonInterface ) configure (cb func (* wgtypes.Config ) error ) error {
113132 // TODO: concurrency
114133 // s.lock.Lock()
0 commit comments