mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 19:56:06 +01:00
cleanup
This commit is contained in:
@@ -12,36 +12,6 @@ import (
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
func TestAmneziaWGConfigTemplates(t *testing.T) {
|
||||
tpl, err := newTemplateHandler()
|
||||
require.NoError(t, err)
|
||||
options := domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4", "I1": "<b 0x1234>"}
|
||||
iface := &domain.Interface{Identifier: "awg0", Backend: config.AmneziaWGBackendName, AmneziaWG: options, PrivateKey: "private"}
|
||||
reader, err := tpl.GetInterfaceConfig(iface, nil)
|
||||
require.NoError(t, err)
|
||||
contents, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "PrivateKey = private\nH1 = 100-200\nI1 = <b 0x1234>\nJc = 4\n")
|
||||
require.Contains(t, string(contents), "\nAddress = ")
|
||||
peer := &domain.Peer{AmneziaWG: options}
|
||||
peer.Interface.PrivateKey = "private"
|
||||
for _, style := range []string{domain.ConfigStyleWgQuick, domain.ConfigStyleRaw} {
|
||||
reader, err := tpl.GetPeerConfig(peer, style)
|
||||
require.NoError(t, err)
|
||||
contents, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "PrivateKey = private\nH1 = 100-200\nI1 = <b 0x1234>\nJc = 4\n")
|
||||
require.True(t, strings.Index(string(contents), "H1 =") < strings.Index(string(contents), "[Peer]"))
|
||||
}
|
||||
peer.AmneziaWG = nil
|
||||
reader, err = tpl.GetPeerConfig(peer, domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err = io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.NotContains(t, string(contents), "H1 =")
|
||||
require.Contains(t, string(contents), "PrivateKey = private\nAddress =")
|
||||
}
|
||||
|
||||
type amneziaConfigRepo struct {
|
||||
iface domain.Interface
|
||||
peer domain.Peer
|
||||
@@ -58,30 +28,40 @@ func (r *amneziaConfigRepo) GetInterfaceAndPeers(context.Context, domain.Interfa
|
||||
return &r.iface, []domain.Peer{r.peer}, nil
|
||||
}
|
||||
|
||||
func TestAmneziaWGDownloadsAndQRUseParentOptions(t *testing.T) {
|
||||
func TestAmneziaWGConfigExports(t *testing.T) {
|
||||
tpl, err := newTemplateHandler()
|
||||
require.NoError(t, err)
|
||||
repo := &amneziaConfigRepo{
|
||||
iface: domain.Interface{Identifier: "awg0", Backend: config.AmneziaWGBackendName, AmneziaWG: domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4"}},
|
||||
peer: domain.Peer{Identifier: "peer", InterfaceIdentifier: "awg0"},
|
||||
iface: domain.Interface{Identifier: "awg0", Backend: config.AmneziaWGBackendName, PrivateKey: "private",
|
||||
AmneziaWG: domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4", "I1": "<b 0x1234>"}},
|
||||
peer: domain.Peer{Identifier: "peer", InterfaceIdentifier: "awg0", Interface: domain.PeerInterfaceConfig{PrivateKey: "private"}},
|
||||
}
|
||||
manager := Manager{wg: repo, tplHandler: tpl}
|
||||
ctx := domain.SetUserInfo(context.Background(), domain.SystemAdminContextUserInfo())
|
||||
reader, err := manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "H1 = 100-200")
|
||||
reader, err = manager.GetPeerConfigQrCode(ctx, "peer", domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err = io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, contents)
|
||||
// Changing parent options affects the next download without editing peers.
|
||||
ctx := domain.SetUserInfo(t.Context(), domain.SystemAdminContextUserInfo())
|
||||
read := func(reader io.Reader, err error) string {
|
||||
t.Helper()
|
||||
require.NoError(t, err)
|
||||
data, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
return string(data)
|
||||
}
|
||||
const options = "PrivateKey = private\nH1 = 100-200\nI1 = <b 0x1234>\nJc = 4\n"
|
||||
server := read(manager.GetInterfaceConfig(ctx, "awg0"))
|
||||
require.Contains(t, server, options)
|
||||
require.Contains(t, server, "\nAddress = ")
|
||||
for _, style := range []string{domain.ConfigStyleWgQuick, domain.ConfigStyleRaw} {
|
||||
client := read(manager.GetPeerConfig(ctx, "peer", style))
|
||||
beforePeers, _, found := strings.Cut(client, "[Peer]")
|
||||
require.True(t, found)
|
||||
require.Contains(t, beforePeers, options)
|
||||
}
|
||||
firstQR := read(manager.GetPeerConfigQrCode(ctx, "peer", domain.ConfigStyleWgQuick))
|
||||
require.NotEmpty(t, firstQR)
|
||||
repo.iface.AmneziaWG["H1"] = "300-400"
|
||||
reader, err = manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err = io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "H1 = 300-400")
|
||||
require.Contains(t, read(manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick)), "H1 = 300-400")
|
||||
require.NotEqual(t, firstQR, read(manager.GetPeerConfigQrCode(ctx, "peer", domain.ConfigStyleWgQuick)))
|
||||
repo.iface.AmneziaWG = nil
|
||||
client := read(manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick))
|
||||
require.Contains(t, client, "PrivateKey = private\nAddress =")
|
||||
require.NotContains(t, client, "H1 =")
|
||||
}
|
||||
|
||||
@@ -195,8 +195,12 @@ func (m Manager) GetPeerConfig(ctx context.Context, id domain.PeerIdentifier, st
|
||||
if err := domain.ValidateUserAccessRights(ctx, peer.UserIdentifier); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.loadAmneziaWGOptions(ctx, peer); err != nil {
|
||||
return nil, err
|
||||
iface, err := m.wg.GetInterface(ctx, peer.InterfaceIdentifier)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load parent interface: %w", err)
|
||||
}
|
||||
if iface.Backend == config.AmneziaWGBackendName {
|
||||
peer.Interface.AmneziaWG = iface.AmneziaWG
|
||||
}
|
||||
|
||||
return m.tplHandler.GetPeerConfig(peer, style)
|
||||
@@ -204,19 +208,7 @@ func (m Manager) GetPeerConfig(ctx context.Context, id domain.PeerIdentifier, st
|
||||
|
||||
// GetPeerConfigQrCode returns a QR code image containing the configuration for the given peer.
|
||||
func (m Manager) GetPeerConfigQrCode(ctx context.Context, id domain.PeerIdentifier, style string) (io.Reader, error) {
|
||||
peer, err := m.wg.GetPeer(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch peer %s: %w", id, err)
|
||||
}
|
||||
|
||||
if err := domain.ValidateUserAccessRights(ctx, peer.UserIdentifier); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.loadAmneziaWGOptions(ctx, peer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfgData, err := m.tplHandler.GetPeerConfig(peer, style)
|
||||
cfgData, err := m.GetPeerConfig(ctx, id, style)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get peer config for %s: %w", id, err)
|
||||
}
|
||||
@@ -256,17 +248,6 @@ func (m Manager) GetPeerConfigQrCode(ctx context.Context, id domain.PeerIdentifi
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (m Manager) loadAmneziaWGOptions(ctx context.Context, peer *domain.Peer) error {
|
||||
iface, err := m.wg.GetInterface(ctx, peer.InterfaceIdentifier)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load parent interface: %w", err)
|
||||
}
|
||||
if iface.Backend == config.AmneziaWGBackendName {
|
||||
peer.Interface.AmneziaWG = iface.AmneziaWG
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PersistInterfaceConfig writes the configuration file for the given interface to the file system.
|
||||
func (m Manager) PersistInterfaceConfig(ctx context.Context, id domain.InterfaceIdentifier) error {
|
||||
iface, peers, err := m.wg.GetInterfaceAndPeers(ctx, id)
|
||||
|
||||
Reference in New Issue
Block a user