2021-08-22 14:58:39 -07:00
|
|
|
package vpn
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2025-10-17 01:45:50 +02:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2024-10-14 16:44:05 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/netlink"
|
2021-08-22 14:58:39 -07:00
|
|
|
"github.com/qdm12/gluetun/internal/provider"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/provider/utils"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/wireguard"
|
2023-05-29 06:45:12 +00:00
|
|
|
"github.com/qdm12/gosettings"
|
2021-08-22 14:58:39 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// setupWireguard sets Wireguard up using the configurators and settings given.
|
|
|
|
|
// It returns a serverName for port forwarding (PIA) and an error if it fails.
|
2022-06-11 01:34:30 +00:00
|
|
|
func setupWireguard(ctx context.Context, netlinker NetLinker,
|
|
|
|
|
fw Firewall, providerConf provider.Provider,
|
2024-10-14 16:44:05 +00:00
|
|
|
settings settings.VPN, ipv6SupportLevel netlink.IPv6SupportLevel, logger wireguard.Logger) (
|
2025-10-17 01:45:50 +02:00
|
|
|
wireguarder *wireguard.Wireguard, connection models.Connection, err error,
|
2024-10-11 19:20:48 +00:00
|
|
|
) {
|
2024-10-14 16:44:05 +00:00
|
|
|
ipv6Internet := ipv6SupportLevel == netlink.IPv6Internet
|
|
|
|
|
connection, err = providerConf.GetConnection(settings.Provider.ServerSelection, ipv6Internet)
|
2021-08-22 14:58:39 -07:00
|
|
|
if err != nil {
|
2025-10-17 01:45:50 +02:00
|
|
|
return nil, models.Connection{}, fmt.Errorf("finding a VPN server: %w", err)
|
2021-08-22 14:58:39 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-14 16:44:05 +00:00
|
|
|
wireguardSettings := utils.BuildWireguardSettings(connection, settings.Wireguard, ipv6SupportLevel.IsSupported())
|
2021-08-22 14:58:39 -07:00
|
|
|
|
2021-09-23 14:42:28 +00:00
|
|
|
logger.Debug("Wireguard server public key: " + wireguardSettings.PublicKey)
|
2023-05-29 06:45:12 +00:00
|
|
|
logger.Debug("Wireguard client private key: " + gosettings.ObfuscateKey(wireguardSettings.PrivateKey))
|
|
|
|
|
logger.Debug("Wireguard pre-shared key: " + gosettings.ObfuscateKey(wireguardSettings.PreSharedKey))
|
2021-09-23 14:42:28 +00:00
|
|
|
|
2021-08-22 14:58:39 -07:00
|
|
|
wireguarder, err = wireguard.New(wireguardSettings, netlinker, logger)
|
|
|
|
|
if err != nil {
|
2025-10-17 01:45:50 +02:00
|
|
|
return nil, models.Connection{}, fmt.Errorf("creating Wireguard: %w", err)
|
2021-08-22 14:58:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = fw.SetVPNConnection(ctx, connection, settings.Wireguard.Interface)
|
|
|
|
|
if err != nil {
|
2025-10-17 01:45:50 +02:00
|
|
|
return nil, models.Connection{}, fmt.Errorf("setting firewall: %w", err)
|
2021-08-22 14:58:39 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-17 01:45:50 +02:00
|
|
|
return wireguarder, connection, nil
|
2021-08-22 14:58:39 -07:00
|
|
|
}
|