@@ -7,54 +7,45 @@ import (
7
7
"github.com/qdm12/gluetun/internal/models"
8
8
)
9
9
10
- func (c * Config ) SetVPNConnection (ctx context.Context ,
11
- connection models.Connection , vpnIntf string ,
12
- ) (err error ) {
13
- c .stateMutex .Lock ()
14
- defer c .stateMutex .Unlock ()
15
-
16
- if ! c .enabled {
17
- c .logger .Info ("firewall disabled, only updating internal VPN connection" )
18
- c .vpnConnection = connection
19
- return nil
20
- }
21
-
22
- c .logger .Info ("allowing VPN connection..." )
23
-
24
- if c .vpnConnection .Equal (connection ) {
25
- return nil
26
- }
27
-
28
- remove := true
29
- if c .vpnConnection .IP .IsValid () {
30
- for _ , defaultRoute := range c .defaultRoutes {
31
- if err := c .acceptOutputTrafficToVPN (ctx , defaultRoute .NetInterface , c .vpnConnection , remove ); err != nil {
32
- c .logger .Error ("cannot remove outdated VPN connection rule: " + err .Error ())
33
- }
34
- }
35
- }
36
- c .vpnConnection = models.Connection {}
37
-
38
- if c .vpnIntf != "" {
39
- if err = c .acceptOutputThroughInterface (ctx , c .vpnIntf , remove ); err != nil {
40
- c .logger .Error ("cannot remove outdated VPN interface rule: " + err .Error ())
41
- }
42
- }
43
- c .vpnIntf = ""
44
-
45
- remove = false
46
-
47
- for _ , defaultRoute := range c .defaultRoutes {
48
- if err := c .acceptOutputTrafficToVPN (ctx , defaultRoute .NetInterface , connection , remove ); err != nil {
49
- return fmt .Errorf ("allowing output traffic through VPN connection: %w" , err )
50
- }
51
- }
52
- c .vpnConnection = connection
53
-
54
- if err = c .acceptOutputThroughInterface (ctx , vpnIntf , remove ); err != nil {
55
- return fmt .Errorf ("accepting output traffic through interface %s: %w" , vpnIntf , err )
56
- }
57
- c .vpnIntf = vpnIntf
58
-
59
- return nil
10
+ func (c * Config ) SetVPNConnection (ctx context.Context , connection models.Connection , intf string ) (err error ) {
11
+ c .stateMutex .Lock ()
12
+ defer c .stateMutex .Unlock ()
13
+
14
+ if ! c .enabled {
15
+ c .vpnConnection = connection
16
+ c .vpnIntf = intf
17
+ return nil
18
+ }
19
+
20
+ // Remove previous VPN rules
21
+ if c .vpnConnection .IP .IsValid () {
22
+ const remove = true
23
+ interfacesSeen := make (map [string ]struct {}, len (c .defaultRoutes ))
24
+ for _ , defaultRoute := range c .defaultRoutes {
25
+ _ , seen := interfacesSeen [defaultRoute .NetInterface ]
26
+ if seen {
27
+ continue
28
+ }
29
+ interfacesSeen [defaultRoute .NetInterface ] = struct {}{}
30
+ err = c .acceptOutputTrafficToVPN (ctx , defaultRoute .NetInterface , c .vpnConnection , remove )
31
+ if err != nil {
32
+ return fmt .Errorf ("removing output traffic through VPN: %w" , err )
33
+ }
34
+ }
35
+ }
36
+
37
+ c .vpnConnection = connection
38
+ c .vpnIntf = intf
39
+
40
+ // Add new VPN rules
41
+ if err = c .allowVPNIP (ctx ); err != nil {
42
+ return err
43
+ }
44
+
45
+ // Re-apply user post-rules after VPN changes
46
+ if err = c .applyUserPostRules (ctx ); err != nil {
47
+ return fmt .Errorf ("re-applying user post-rules after VPN change: %w" , err )
48
+ }
49
+
50
+ return nil
60
51
}
0 commit comments