MCPcopy
hub / github.com/passteque/gluetun / setupOpenVPN

Function setupOpenVPN

internal/vpn/openvpn.go:15–52  ·  view source on GitHub ↗

setupOpenVPN sets OpenVPN up using the configurators and settings given. It returns a serverName for port forwarding (PIA) and an error if it fails.

(ctx context.Context, fw Firewall,
	openvpnConf OpenVPN, providerConf provider.Provider,
	settings settings.VPN, ipv6Supported bool, starter CmdStarter,
	logger openvpn.Logger)

Source from the content-addressed store, hash-verified

13// setupOpenVPN sets OpenVPN up using the configurators and settings given.
14// It returns a serverName for port forwarding (PIA) and an error if it fails.
15func setupOpenVPN(ctx context.Context, fw Firewall,
16 openvpnConf OpenVPN, providerConf provider.Provider,
17 settings settings.VPN, ipv6Supported bool, starter CmdStarter,
18 logger openvpn.Logger) (runner *openvpn.Runner, connection models.Connection, err error,
19) {
20 connection, err = providerConf.GetConnection(settings.Provider.ServerSelection, ipv6Supported)
21 if err != nil {
22 return nil, models.Connection{}, fmt.Errorf("finding a valid server connection: %w", err)
23 }
24
25 lines := providerConf.OpenVPNConfig(connection, settings.OpenVPN, ipv6Supported)
26
27 if err := openvpnConf.WriteConfig(lines); err != nil {
28 return nil, models.Connection{}, fmt.Errorf("writing configuration to file: %w", err)
29 }
30
31 if *settings.OpenVPN.User != "" {
32 err := openvpnConf.WriteAuthFile(*settings.OpenVPN.User, *settings.OpenVPN.Password)
33 if err != nil {
34 return nil, models.Connection{}, fmt.Errorf("writing auth to file: %w", err)
35 }
36 }
37
38 if *settings.OpenVPN.KeyPassphrase != "" {
39 err := openvpnConf.WriteAskPassFile(*settings.OpenVPN.KeyPassphrase)
40 if err != nil {
41 return nil, models.Connection{}, fmt.Errorf("writing askpass file: %w", err)
42 }
43 }
44
45 if err := fw.SetVPNConnection(ctx, connection, settings.OpenVPN.Interface); err != nil {
46 return nil, models.Connection{}, fmt.Errorf("allowing VPN connection through firewall: %w", err)
47 }
48
49 runner = openvpn.NewRunner(settings.OpenVPN, starter, logger)
50
51 return runner, connection, nil
52}

Callers 1

RunMethod · 0.85

Calls 8

NewRunnerFunction · 0.92
GetConnectionMethod · 0.65
ErrorfMethod · 0.65
OpenVPNConfigMethod · 0.65
WriteConfigMethod · 0.65
WriteAuthFileMethod · 0.65
WriteAskPassFileMethod · 0.65
SetVPNConnectionMethod · 0.65

Tested by

no test coverage detected