Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszWojciechO committed Oct 11, 2024
1 parent d5a8e34 commit a661057
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
11 changes: 7 additions & 4 deletions daemon/firewall/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/netip"
"os/exec"
"regexp"
"sort"
"strings"
"sync"
Expand All @@ -15,8 +16,9 @@ import (
)

const (
ipv4Table = "iptables"
ipv6Table = "ip6tables"
ipv4Table = "iptables"
ipv6Table = "ip6tables"
defaultComment = "nordvpn"
)

const (
Expand Down Expand Up @@ -119,9 +121,10 @@ func (ipt *IPTables) applyRule(rule firewall.Rule, add bool) error {
}

func generateFlushRules(rules string) []string {
re := regexp.MustCompile(fmt.Sprintf(`--comment\s+%s(?:\s|$)`, regexp.QuoteMeta(defaultComment)))
flushRules := []string{}
for _, rule := range strings.Split(rules, "\n") {
if strings.Contains(rule, "nordvpn") {
if re.MatchString(rule) {
newRule := strings.Replace(rule, "-A", "-D", 1)
flushRules = append(flushRules, newRule)
}
Expand Down Expand Up @@ -427,7 +430,7 @@ func generateIPTablesRule(
jump := " -j "

if comment == "" {
comment = "nordvpn"
comment = defaultComment
}

var acceptComment string
Expand Down
4 changes: 4 additions & 0 deletions daemon/firewall/iptables/iptables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,10 @@ func TestGenerateFlushRules(t *testing.T) {
"-A FORWARD -d 10.55.97.34/24 -o eth0 -m conntrack --ctstate RELATED,ESTABLISHED -m comment --comment \"comment b\" -j ACCEPT",
"-A FORWARD -i eth0 -m comment --comment \"comment A\" -j REJECT --reject-with icmp-port-unreachable",
"-A FORWARD -o eth1 -m comment --comment \"comment B\" -j REJECT --reject-with icmp-port-unreachable",
"-A FORWARD -i eth0 -m comment --comment nordvpn-meshnet -j REJECT --reject-with icmp-port-unreachable",
"-A FORWARD -o eth1 -m comment --comment meshnet-nordvpn -j REJECT --reject-with icmp-port-unreachable",
"-A FORWARD -i eth0 -m comment --comment nordvpn-meshnet-test -j REJECT --reject-with icmp-port-unreachable",
"-A FORWARD -o eth1 -m comment --comment \"nordvpn test\" -j REJECT --reject-with icmp-port-unreachable",
"-A OUTPUT -d 169.254.0.0/16 -p tcp -m tcp --dport 53 -m comment --comment nordvpn -j DROP",
"-A OUTPUT -d 169.254.0.0/16 -p udp -m udp --dport 53 -m comment --comment nordvpn -j DROP",
"-A OUTPUT -d 192.168.0.0/16 -p tcp -m tcp --dport 53 -m comment --comment nordvpn -j DROP",
Expand Down
19 changes: 14 additions & 5 deletions networker/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const (
allowIncomingRule = "-allow-rule-"
// a string to be prepended with peers public key and appended with peers ip address to form the internal rule name
// for blocking incoming connections into local networks
blockLanRule = "-block-lan-rule-"
blockLanRule = "-block-lan-rule-"
meshnetFirewallRuleComment = "nordvpn-meshnet"
)

// ConnectionStatus of a currently active connection
Expand Down Expand Up @@ -1080,6 +1081,9 @@ func (netw *Combined) UnsetFirewall() error {
netw.mu.Lock()
defer netw.mu.Unlock()

if netw.isKillSwitchSet {
return nil
}
return netw.fw.Flush()
}

Expand Down Expand Up @@ -1456,7 +1460,8 @@ func (netw *Combined) allowIncoming(publicKey string, address netip.Addr, lanAll
RemoteNetworks: []netip.Prefix{
netip.PrefixFrom(address, address.BitLen()),
},
Allow: true,
Allow: true,
Comment: meshnetFirewallRuleComment,
}
rules = append(rules, rule)

Expand All @@ -1480,7 +1485,8 @@ func (netw *Combined) allowIncoming(publicKey string, address netip.Addr, lanAll
RemoteNetworks: []netip.Prefix{
netip.PrefixFrom(address, address.BitLen()),
},
Allow: false,
Allow: false,
Comment: meshnetFirewallRuleComment,
}

rules = append(rules, rule)
Expand Down Expand Up @@ -1512,7 +1518,8 @@ func (netw *Combined) allowFileshare(publicKey string, address netip.Addr) error
RemoteNetworks: []netip.Prefix{
netip.PrefixFrom(address, address.BitLen()),
},
Allow: true,
Allow: true,
Comment: meshnetFirewallRuleComment,
}}

ruleIndex := slices.Index(netw.rules, ruleName)
Expand Down Expand Up @@ -1692,6 +1699,7 @@ func (netw *Combined) defaultMeshBlock(ip netip.Addr) error {
Direction: firewall.Inbound,
RemoteNetworks: []netip.Prefix{defaultMeshSubnet},
Allow: false,
Comment: meshnetFirewallRuleComment,
},
// Allow inbound traffic for the existing connections
// E. g. this device is making some calls to another
Expand All @@ -1708,7 +1716,8 @@ func (netw *Combined) defaultMeshBlock(ip netip.Addr) error {
firewall.Established,
},
},
Allow: true,
Allow: true,
Comment: meshnetFirewallRuleComment,
},
}); err != nil {
return err
Expand Down

0 comments on commit a661057

Please sign in to comment.