feat: Do not quit when keyfile can not be opened, create key
- Refactor AddTestPeer Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
44961e91dc
commit
e5e4641264
@ -4,10 +4,8 @@ import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
@ -210,7 +208,7 @@ func setup(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return cli.Exit(err, 1)
|
||||
}
|
||||
slog.Debug(fmt.Sprintf("Private key: %s", privateKey.String()))
|
||||
slog.Debug(fmt.Sprintf("new public key: %s", privateKey.PublicKey().String()))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -223,24 +221,30 @@ func setup(ctx *cli.Context) error {
|
||||
privKeyFile, err := os.Open(PrivateKeyPath)
|
||||
defer privKeyFile.Close()
|
||||
if err != nil {
|
||||
return cli.Exit(err, 1)
|
||||
}
|
||||
privateKeyStr := make([]byte, 45)
|
||||
n, err := privKeyFile.Read(privateKeyStr)
|
||||
if err != nil {
|
||||
return cli.Exit(err, 1)
|
||||
}
|
||||
if n != 45 {
|
||||
slog.Warn("Private key length did not math the expected 45!")
|
||||
}
|
||||
slog.Debug(fmt.Sprintf("Read %d bytes from keyfile", n))
|
||||
slog.Error(err.Error())
|
||||
slog.Info("Could not open private key file")
|
||||
err := createPrivKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
privateKeyStr := make([]byte, 45)
|
||||
n, err := privKeyFile.Read(privateKeyStr)
|
||||
if err != nil {
|
||||
return cli.Exit(err, 1)
|
||||
}
|
||||
if n != 45 {
|
||||
slog.Warn("Private key length did not math the expected 45!")
|
||||
}
|
||||
slog.Debug(fmt.Sprintf("Read %d bytes from keyfile", n))
|
||||
|
||||
privateKey, err = wgtypes.ParseKey(string(privateKeyStr))
|
||||
slog.Debug("Keyfile opened for reading")
|
||||
if err != nil {
|
||||
return cli.Exit(err, 1)
|
||||
privateKey, err = wgtypes.ParseKey(string(privateKeyStr))
|
||||
slog.Debug("Keyfile opened for reading")
|
||||
if err != nil {
|
||||
return cli.Exit(err, 1)
|
||||
}
|
||||
slog.Debug("Private key parsed and is correct")
|
||||
}
|
||||
slog.Debug("Private key parsed and is correct")
|
||||
}
|
||||
|
||||
wg, err := hvpnnode3.InitWGLink(
|
||||
@ -275,7 +279,7 @@ func setup(ctx *cli.Context) error {
|
||||
|
||||
ipPool, err := hvpnnode3.NewPool(VPNIPCIDR)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("main.IPPool: %s", err))
|
||||
slog.Error(fmt.Sprintf("IPPool: %s", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.Debug(fmt.Sprintf("Init ip pool %s", VPNIPCIDR))
|
||||
@ -286,13 +290,13 @@ func setup(ctx *cli.Context) error {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Debug(fmt.Sprintf("main.testVip: IP Pool Test IP: %s", testVip.String()))
|
||||
slog.Debug(fmt.Sprintf("IP Pool Test IP: %s", testVip.String()))
|
||||
err = ipPool.Free(testVip)
|
||||
if err != nil {
|
||||
slog.Error("main.testVip: Could not free test Vip from IPPool!", err)
|
||||
os.Exit(1)
|
||||
slog.Error("Could not free test Vip from IPPool!", err)
|
||||
return cli.Exit(err.Error(), 1)
|
||||
}
|
||||
slog.Debug("main.testVip: Test IP Freed")
|
||||
slog.Debug("Test IP Freed")
|
||||
|
||||
IPPool = ipPool
|
||||
wgLink.IPPool = ipPool
|
||||
@ -328,48 +332,23 @@ func testWgPeerAdd(wgLink *hvpnnode3.WGLink) error {
|
||||
return err
|
||||
}
|
||||
publicKey := privateKey.PublicKey()
|
||||
|
||||
urlsafe := url.QueryEscape(publicKey.String())
|
||||
slog.Debug(urlsafe)
|
||||
|
||||
ip, err := wgLink.Allocate()
|
||||
_, err = wgLink.AddPeer(publicKey.String())
|
||||
if err != nil {
|
||||
slog.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
slog.Debug(fmt.Sprintf("Added test peer %v", publicKey.String()))
|
||||
|
||||
err = wgLink.DeletePeer(publicKey.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
peerConfig := wgtypes.PeerConfig{
|
||||
PublicKey: publicKey,
|
||||
AllowedIPs: []net.IPNet{
|
||||
{
|
||||
IP: ip,
|
||||
Mask: net.IPv4Mask(255, 255, 255, 255),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
wgConfig := wgtypes.Config{
|
||||
Peers: []wgtypes.PeerConfig{peerConfig},
|
||||
}
|
||||
|
||||
err = wgLink.ConfigureDevice(wgLink.Name, wgConfig)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slog.Debug(fmt.Sprintf("Added test peer %v", peerConfig.PublicKey))
|
||||
|
||||
wgConfig.ReplacePeers = true
|
||||
wgConfig.Peers = []wgtypes.PeerConfig{}
|
||||
|
||||
err = wgLink.ConfigureDevice(wgLink.Name, wgConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
peers, err := wgLink.GetAllPeers()
|
||||
if len(peers) != 0 {
|
||||
slog.Warn(fmt.Sprintf("Expected 0 peers, got %d", len(peers)))
|
||||
}
|
||||
slog.Debug("Removed test peer")
|
||||
wgLink.Free(ip)
|
||||
slog.Debug("Freed test peer ip")
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
13
link.go
13
link.go
@ -185,12 +185,13 @@ func (wg *WGLink) GetPeer(publickey string) (*wgtypes.Peer, error) {
|
||||
}
|
||||
|
||||
func (wg *WGLink) getPeer(pubkey wgtypes.Key) (*wgtypes.Peer, error) {
|
||||
dev, err := wg.Device(wg.Name)
|
||||
|
||||
peers, err := wg.GetAllPeers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, peer := range dev.Peers {
|
||||
for _, peer := range peers {
|
||||
if peer.PublicKey == pubkey {
|
||||
return &peer, nil
|
||||
}
|
||||
@ -198,6 +199,14 @@ func (wg *WGLink) getPeer(pubkey wgtypes.Key) (*wgtypes.Peer, error) {
|
||||
return nil, proto.PeerDoesNotExist
|
||||
}
|
||||
|
||||
func (wg *WGLink) GetAllPeers() ([]wgtypes.Peer, error) {
|
||||
dev, err := wg.Device(wg.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dev.Peers, nil
|
||||
}
|
||||
|
||||
func createARemovePeerCfg(publickey wgtypes.Key) wgtypes.Config {
|
||||
rmPeerCfg := wgtypes.PeerConfig{
|
||||
Remove: true,
|
||||
|
Loading…
Reference in New Issue
Block a user