move to zerolog

This commit is contained in:
Mikaël Cluseau
2024-01-20 16:41:54 +01:00
parent 5ab8b74041
commit 6bf1d1ccf2
18 changed files with 205 additions and 170 deletions

20
vpn.go
View File

@ -1,11 +1,11 @@
package main
import (
"log"
"net"
"os"
"path/filepath"
"github.com/rs/zerolog/log"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
@ -13,11 +13,18 @@ import (
)
func setupVPN(vpn config.VPNDef, localGenDir string) {
log.Printf("setting up VPN %s", vpn.Name)
log := log.With().Str("vpn", vpn.Name).Logger()
log.Info().Msg("VPN: setting up")
vpnDir := filepath.Join(localGenDir, vpn.Name)
os.MkdirAll(vpnDir, 0750)
logMsg := log.Info()
if vpn.ListenPort != nil {
logMsg.Int("ListenPort", *vpn.ListenPort)
}
// public/private key
keyFile := filepath.Join(vpnDir, "key")
keyBytes, err := os.ReadFile(keyFile)
@ -39,7 +46,7 @@ func setupVPN(vpn config.VPNDef, localGenDir string) {
fatalf("bad VPN key: %v", err)
}
log.Printf("VPN %s public key is %s", vpn.Name, key.PublicKey().String())
logMsg.Stringer("PublicKey", key.PublicKey())
// pre-shared key
pskeyFile := filepath.Join(vpnDir, "pskey")
@ -62,7 +69,10 @@ func setupVPN(vpn config.VPNDef, localGenDir string) {
fatalf("bad VPN pre-shared key: %v", err)
}
log.Printf("VPN %s pre-shared key is %s", vpn.Name, key.String())
{
keyStr := key.String()
logMsg.Str("PresharedKey", keyStr[0:4]+"..."+keyStr[len(keyStr)-4:])
}
// setup interface
cfg := wgtypes.Config{
@ -110,12 +120,14 @@ func setupVPN(vpn config.VPNDef, localGenDir string) {
}
defer wg.Close()
log.Info().Strs("ips", vpn.IPs).Msg("VPN: creating interface")
run("ip", "link", "add", vpn.Name, "type", "wireguard")
for _, ip := range vpn.IPs {
run("ip", "addr", "add", ip, "dev", vpn.Name)
}
logMsg.Msg("VPN: configuring interface")
err = wg.ConfigureDevice(vpn.Name, cfg)
if err != nil {
fatalf("failed to setup VPN %s: %v", vpn.Name, err)