Skip to content

Commit 5a2ef8e

Browse files
authored
fix: avoid messing with Tailscale firewall rules (#90)
1 parent 4941977 commit 5a2ef8e

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

wgengine/router/router_windows.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ import (
2626
"tailscale.com/types/logger"
2727
)
2828

29+
const firewallRulePrefix = "CoderConnect-"
30+
31+
var (
32+
firewallRuleIn = firewallRulePrefix + "In"
33+
firewallRuleProcess = firewallRulePrefix + "Process"
34+
)
35+
2936
type winRouter struct {
3037
logf func(fmt string, args ...any)
3138
netMon *netmon.Monitor // may be nil
@@ -237,28 +244,28 @@ func (ft *firewallTweaker) doAsyncSet() {
237244
// Must only be invoked from doAsyncSet.
238245
func (ft *firewallTweaker) doSet(local []string, killswitch bool, clear bool, procRule bool, allowedRoutes []netip.Prefix) error {
239246
if clear {
240-
ft.logf("clearing Tailscale-In firewall rules...")
247+
ft.logf("clearing %s firewall rules...", firewallRuleIn)
241248
// We ignore the error here, because netsh returns an error for
242249
// deleting something that doesn't match.
243250
// TODO(bradfitz): care? That'd involve querying it before/after to see
244251
// whether it was necessary/worked. But the output format is localized,
245252
// so can't rely on parsing English. Maybe need to use OLE, not netsh.exe?
246-
d, _ := ft.runFirewall("delete", "rule", "name=Tailscale-In", "dir=in")
247-
ft.logf("cleared Tailscale-In firewall rules in %v", d)
253+
d, _ := ft.runFirewall("delete", "rule", "name="+firewallRuleIn, "dir=in")
254+
ft.logf("cleared %s firewall rules in %v", firewallRuleIn, d)
248255
}
249256
if procRule {
250-
ft.logf("deleting any prior Tailscale-Process rule...")
251-
d, err := ft.runFirewall("delete", "rule", "name=Tailscale-Process", "dir=in") // best effort
257+
ft.logf("deleting any prior %s rule...", firewallRuleProcess)
258+
d, err := ft.runFirewall("delete", "rule", "name="+firewallRuleProcess, "dir=in") // best effort
252259
if err == nil {
253-
ft.logf("removed old Tailscale-Process rule in %v", d)
260+
ft.logf("removed old %s rule in %v", firewallRuleProcess, d)
254261
}
255262
var exe string
256263
exe, err = os.Executable()
257264
if err != nil {
258-
ft.logf("failed to find Executable for Tailscale-Process rule: %v", err)
265+
ft.logf("failed to find Executable for %s rule: %v", firewallRuleProcess, err)
259266
} else {
260-
ft.logf("adding Tailscale-Process rule to allow UDP for %q ...", exe)
261-
d, err = ft.runFirewall("add", "rule", "name=Tailscale-Process",
267+
ft.logf("adding %s rule to allow UDP for %q ...", firewallRuleProcess, exe)
268+
d, err = ft.runFirewall("add", "rule", "name="+firewallRuleProcess,
262269
"dir=in",
263270
"action=allow",
264271
"edge=yes",
@@ -268,24 +275,24 @@ func (ft *firewallTweaker) doSet(local []string, killswitch bool, clear bool, pr
268275
"enable=yes",
269276
)
270277
if err != nil {
271-
ft.logf("error adding Tailscale-Process rule: %v", err)
278+
ft.logf("error adding %s rule: %v", firewallRuleProcess, err)
272279
} else {
273280
ft.mu.Lock()
274281
ft.didProcRule = true
275282
ft.mu.Unlock()
276-
ft.logf("added Tailscale-Process rule in %v", d)
283+
ft.logf("added %s rule in %v", firewallRuleProcess, d)
277284
}
278285
}
279286
}
280287
for _, cidr := range local {
281-
ft.logf("adding Tailscale-In rule to allow %v ...", cidr)
288+
ft.logf("adding %s rule to allow %v ...", firewallRuleIn, cidr)
282289
var d time.Duration
283-
d, err := ft.runFirewall("add", "rule", "name=Tailscale-In", "dir=in", "action=allow", "localip="+cidr, "profile=private", "enable=yes")
290+
d, err := ft.runFirewall("add", "rule", "name="+firewallRuleIn, "dir=in", "action=allow", "localip="+cidr, "profile=private", "enable=yes")
284291
if err != nil {
285-
ft.logf("error adding Tailscale-In rule to allow %v: %v", cidr, err)
292+
ft.logf("error adding %s rule to allow %v: %v", firewallRuleIn, cidr, err)
286293
return err
287294
}
288-
ft.logf("added Tailscale-In rule to allow %v in %v", cidr, d)
295+
ft.logf("added %s rule to allow %v in %v", firewallRuleIn, cidr, d)
289296
}
290297

291298
if !killswitch {

0 commit comments

Comments
 (0)