Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions core/cmd/mistgate-clientd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func main() {
}

func shouldConnectAtStart(configured settings.Settings) bool {
return configured.LastServer != "" && (configured.Autoconnect || configured.Restore && configured.LastOn)
// "bypass" needs no server, and without a subscription there is no last one
return (configured.LastServer != "" || configured.Mode == "bypass") && (configured.Autoconnect || configured.Restore && configured.LastOn)
}

// shouldResumeAtStart: the tunnel was up when the service stopped without being told to disconnect (last_up stays set
Expand All @@ -103,7 +104,7 @@ func shouldConnectAtStart(configured settings.Settings) bool {
// nothing to hold and the two settings decide as before. A connect that never came up leaves the network open, before
// a restart and after it.
func shouldResumeAtStart(configured settings.Settings) bool {
return configured.LastServer != "" && configured.LastUp && configured.KillSwitch != "off"
return configured.LastServer != "" && configured.LastUp && configured.KillSwitchNow() != "off"
}

// savedServerGone: the server the service would connect to is no longer in any subscription (removed, or the list
Expand Down Expand Up @@ -304,7 +305,7 @@ func run(ctx context.Context, useFake bool, dataDir, verify string, powerEvents
if geo.ValidRegion(configured.RouteRegion) {
region = configured.RouteRegion
}
return geoStore.CheckName(configured.RouteMode, region, name, false)
return geoStore.CheckName(configured.RouteMode, region, name, false, false)
}
} else {
real := engine.NewReal(store.Profiles, settingsStore.Get, geoStore, verify)
Expand Down Expand Up @@ -413,7 +414,7 @@ func run(ctx context.Context, useFake bool, dataDir, verify string, powerEvents
})
diagnostics := diag.NewRunner(diag.Config{Profiles: store.Profiles, Tunnel: func() bool {
status, err := manager.Status(ctx)
return err == nil && status.State == "connected"
return err == nil && status.State == "connected" && status.Route != "bypass" // a bypass session has no tunnel to check
}, Connected: func() string {
// the status carries the profile's name (its id when it has no name), not the id
if status, err := manager.Status(ctx); err == nil && status.State == "connected" {
Expand Down
3 changes: 3 additions & 0 deletions core/cmd/mistgate-clientd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func TestShouldConnectAtStart(t *testing.T) {
{"restore but was off", settings.Settings{LastServer: "a", Restore: true}, false},
{"was on but restore is off", settings.Settings{LastServer: "a", LastOn: true}, false},
{"no last server", settings.Settings{Autoconnect: true, Restore: true, LastOn: true}, false},
{"bypass needs no last server", settings.Settings{Mode: "bypass", Autoconnect: true}, true},
{"bypass, nothing set", settings.Settings{Mode: "bypass"}, false},
} {
if got := shouldConnectAtStart(c.s); got != c.want {
t.Errorf("%s: got %v, want %v", c.name, got, c.want)
Expand All @@ -41,6 +43,7 @@ func TestShouldResumeAtStart(t *testing.T) {
{"was on, kill switch while", settings.Settings{LastServer: "a", LastUp: true, KillSwitch: "while"}, true},
{"was on, kill switch always", settings.Settings{LastServer: "a", LastUp: true, KillSwitch: "always"}, true},
{"was on, kill switch off", settings.Settings{LastServer: "a", LastUp: true, KillSwitch: "off"}, false},
{"bypass holds nothing", settings.Settings{LastServer: "a", LastUp: true, KillSwitch: "always", Mode: "bypass"}, false},
{"asked for, never up", settings.Settings{LastServer: "a", LastOn: true, KillSwitch: "while"}, false},
{"was off (the person disconnected), kill switch while", settings.Settings{LastServer: "a", KillSwitch: "while"}, false},
{"was off, kill switch always", settings.Settings{LastServer: "a", KillSwitch: "always"}, false},
Expand Down
2 changes: 1 addition & 1 deletion core/internal/auto/asn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestASNNet(t *testing.T) {
if again.ASNNet("s1") != "" {
t.Fatal("an unknown network left the old note")
}
again.Flush() // the change above started a save of its own: it must be over before the directory goes
again.Flush() // the change above started a save of its own: it must be over before the directory goes
var none *Memory // a nil memory remembers nothing
none.SetASNNet("s1", "net-a")
if none.ASNNet("s1") != "" {
Expand Down
2 changes: 1 addition & 1 deletion core/internal/engine/real_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *Real) Routes(configured settings.Settings) geo.Routes {
return r.geo.Routes(configured.RouteMode, configured.RouteRegion, "")
}
func (r *Real) CheckSite(configured settings.Settings, name string) geo.Check {
return r.geo.CheckName(configured.RouteMode, "", name, false)
return r.geo.CheckName(configured.RouteMode, "", name, false, false)
}
func (*Real) RefreshAutoRegion(onChange func()) {
if onChange != nil {
Expand Down
15 changes: 8 additions & 7 deletions core/internal/engine/real_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ func (r *Real) directResolver(name, region string, rules *geo.SiteRules) netip.A
// CheckSite is routes.check: where the forwarder would send a site now, by the code the flows use.
func (r *Real) CheckSite(configured settings.Settings, name string) geo.Check {
region := r.routeRegion(configured)
return r.geo.CheckName(configured.RouteMode, region, name, r.directResolver(name, region, r.geo.ActiveSiteRules()).IsValid())
// no flow gets a bypass strategy yet, so the answer says "never" until the bridge does it
return r.geo.CheckName(configured.RouteMode, region, name, r.directResolver(name, region, r.geo.ActiveSiteRules()).IsValid(), false)
}

func (r *Real) Routes(configured settings.Settings) geo.Routes {
Expand Down Expand Up @@ -846,11 +847,11 @@ func (r *Real) Start(ctx context.Context, target Target, report func(Step)) (inf
}
dns := tunnelDNS(customDNS, selected.PanelDNS, defaultTunnelDNS)
configured = r.settings()
if err := r.setFirewall(run, configured.KillSwitch == "off", adapter.LUID(), dns, configured.LAN); err != nil {
if err := r.setFirewall(run, configured.KillSwitchNow() == "off", adapter.LUID(), dns, configured.LAN); err != nil {
if runCtx.Err() != nil {
return Info{}, runCtx.Err()
}
if !consoleFirewallUnavailable(err) || configured.KillSwitch == "always" {
if !consoleFirewallUnavailable(err) || configured.KillSwitchNow() == "always" {
return Info{}, failure("tunnel_failed", "enable firewall rules")
}
}
Expand Down Expand Up @@ -889,11 +890,11 @@ func (r *Real) Start(ctx context.Context, target Target, report func(Step)) (inf
}
dns := awgDNS
configured = r.settings()
if err := r.setFirewall(run, configured.KillSwitch == "off", adapter.LUID(), dns, configured.LAN); err != nil {
if err := r.setFirewall(run, configured.KillSwitchNow() == "off", adapter.LUID(), dns, configured.LAN); err != nil {
if runCtx.Err() != nil {
return Info{}, runCtx.Err()
}
if !consoleFirewallUnavailable(err) || configured.KillSwitch == "always" {
if !consoleFirewallUnavailable(err) || configured.KillSwitchNow() == "always" {
return Info{}, failure("tunnel_failed", "enable firewall rules")
}
}
Expand Down Expand Up @@ -1209,7 +1210,7 @@ func (r *Real) startRun(ctx context.Context, hold bool) (context.Context, *realR
if hold {
settings := r.settings()
err = r.setFirewall(run, false, 0, nil, settings.LAN)
if consoleFirewallUnavailable(err) && settings.KillSwitch != "always" {
if consoleFirewallUnavailable(err) && settings.KillSwitchNow() != "always" {
err = nil
}
}
Expand Down Expand Up @@ -1305,7 +1306,7 @@ func (r *Real) Reapply() {
luid = adapter.LUID()
}
dns := append([]netip.Addr(nil), run.dns...)
doNotRestrict := adapter != nil && configured.KillSwitch == "off"
doNotRestrict := adapter != nil && configured.KillSwitchNow() == "off"
if adapter == nil && run.hold {
doNotRestrict = false
}
Expand Down
25 changes: 21 additions & 4 deletions core/internal/geo/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const (
RuleDirect
RuleVPN
RuleBlock
// The name is in the bypass selection and no other rule spoke. Not a route yet: callers that do not know it keep
// deciding as with NoRule.
RuleBypass
)

// ByName is the name half of every routing decision (flows and DNS questions use it alike). The panel's ad list blocks
Expand All @@ -39,6 +42,9 @@ func (r *SiteRules) ByName(mode, name string) RuleVerdict {
if mode == "selected" && r.VPN.Len() != 0 && r.VPN.Match(name) {
return RuleVPN
}
if r.Bypass.Len() != 0 && r.Bypass.Match(name) {
return RuleBypass
}
return NoRule
}

Expand Down Expand Up @@ -117,12 +123,23 @@ type Check struct {
By string `json:"by"` // mode, site, collection, region or default
Rule string `json:"rule"` // the site entry or collection id that matched; empty otherwise
DNS string `json:"dns"` // where the question about the name goes: direct or vpn (a blocked one is answered here)
// always: in the bypass selection, a strategy at once; auto: a strategy after a plain attempt failed; never: the
// route is not direct, the name is an exception (the direct selection), or the bypass is not in force.
Bypass string `json:"bypass"`
}

// CheckName decides for a name now by the same code as the flows; resolver says that a direct resolver is known.
func (s *Store) CheckName(mode, region, name string, resolver bool) Check {
route, by := DecideName(name, mode, region, s.ActiveSiteRules())
check := Check{Route: route, By: by, DNS: NameVPN}
// CheckName decides for a name now by the same code as the flows; resolver says that a direct resolver is known,
// bypass that the bypass is in force for direct flows.
func (s *Store) CheckName(mode, region, name string, resolver, bypass bool) Check {
rules := s.ActiveSiteRules()
route, by := DecideName(name, mode, region, rules)
check := Check{Route: route, By: by, DNS: NameVPN, Bypass: "never"}
if bypass && route == NameDirect && rules != nil && !rules.Direct.Match(name) {
check.Bypass = "auto"
if rules.Bypass.Match(name) {
check.Bypass = "always"
}
}
if by == byRules {
check.By, check.Rule = s.matching(route, name)
}
Expand Down
135 changes: 117 additions & 18 deletions core/internal/geo/names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ func TestCheckNameSaysWhyAndWhere(t *testing.T) {
resolver bool
want Check
}{
{"all", "ru", "ads.example", true, Check{"vpn", "mode", "", "vpn"}},
{"smart", "ru", "sub.ads.example", true, Check{"block", "site", "ads.example", "vpn"}},
{"smart", "ru", "a.tracker.example", true, Check{"block", "collection", "geosite:adlist", "vpn"}},
{"smart", "ru", "both.example", false, Check{"block", "site", "both.example", "vpn"}},
{"smart", "ru", "direct.example", true, Check{"direct", "site", "direct.example", "direct"}},
{"smart", "ru", "direct.example", false, Check{"direct", "site", "direct.example", "vpn"}},
{"smart", "ru", "yandex.ru", true, Check{"direct", "region", "", "direct"}},
{"smart", "ru", "yandex.ru", false, Check{"direct", "region", "", "vpn"}},
{"smart", "ru", "example.com", true, Check{"vpn", "default", "", "vpn"}},
{"selected", "", "vpn.example", true, Check{"vpn", "site", "vpn.example", "vpn"}},
{"selected", "ru", "example.com", true, Check{"direct", "default", "", "direct"}},
{"selected", "", "example.com", false, Check{"direct", "default", "", "vpn"}},
{"all", "ru", "ads.example", true, Check{"vpn", "mode", "", "vpn", "never"}},
{"smart", "ru", "sub.ads.example", true, Check{"block", "site", "ads.example", "vpn", "never"}},
{"smart", "ru", "a.tracker.example", true, Check{"block", "collection", "geosite:adlist", "vpn", "never"}},
{"smart", "ru", "both.example", false, Check{"block", "site", "both.example", "vpn", "never"}},
{"smart", "ru", "direct.example", true, Check{"direct", "site", "direct.example", "direct", "never"}},
{"smart", "ru", "direct.example", false, Check{"direct", "site", "direct.example", "vpn", "never"}},
{"smart", "ru", "yandex.ru", true, Check{"direct", "region", "", "direct", "never"}},
{"smart", "ru", "yandex.ru", false, Check{"direct", "region", "", "vpn", "never"}},
{"smart", "ru", "example.com", true, Check{"vpn", "default", "", "vpn", "never"}},
{"selected", "", "vpn.example", true, Check{"vpn", "site", "vpn.example", "vpn", "never"}},
{"selected", "ru", "example.com", true, Check{"direct", "default", "", "direct", "never"}},
{"selected", "", "example.com", false, Check{"direct", "default", "", "vpn", "never"}},
}
for _, test := range tests {
if got := store.CheckName(test.mode, test.region, test.name, test.resolver); got != test.want {
if got := store.CheckName(test.mode, test.region, test.name, test.resolver, false); got != test.want {
t.Errorf("CheckName(%s, %q, %q, %t) = %+v; want %+v", test.mode, test.region, test.name, test.resolver, got, test.want)
}
}
Expand All @@ -124,7 +124,7 @@ func TestCheckNameSaysWhyAndWhere(t *testing.T) {
if _, _, err := other.SetRules(Rules{Direct: Selection{Collections: []string{"geosite:local"}}}); err != nil {
t.Fatal(err)
}
if got := other.CheckName("smart", "", "a.local.example", false); got != (Check{"direct", "collection", "geosite:local", "vpn"}) {
if got := other.CheckName("smart", "", "a.local.example", false, false); got != (Check{"direct", "collection", "geosite:local", "vpn", "never"}) {
t.Errorf("direct collection: %+v", got)
}
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestPanelAdListBlocksInEveryModeAndIsLetGo(t *testing.T) {
if err := os.WriteFile(filepath.Join(store.dir, "site-category-ads-all.list"), []byte("+.adnet.example\n"), 0600); err != nil {
t.Fatal(err)
}
if got := store.CheckName("all", "", "x.adnet.example", false); got.Route != NameVPN {
if got := store.CheckName("all", "", "x.adnet.example", false, false); got.Route != NameVPN {
t.Fatalf("blocked before the preset asked for it: %+v", got)
}
store.SetPanelSites(nil, true)
Expand All @@ -200,17 +200,17 @@ func TestPanelAdListBlocksInEveryModeAndIsLetGo(t *testing.T) {
if rules.ByName("all", "adnet.example") != RuleBlock || rules.ByName("all", "direct.example") != NoRule || !rules.HasBlock {
t.Error("ByName or HasBlock ignore the ad list")
}
if got, want := store.CheckName("all", "", "a.adnet.example", true), (Check{"block", "collection", "geosite:category-ads-all", "vpn"}); got != want {
if got, want := store.CheckName("all", "", "a.adnet.example", true, false), (Check{"block", "collection", "geosite:category-ads-all", "vpn", "never"}); got != want {
t.Errorf("CheckName = %+v; want %+v", got, want)
}
if got := store.CheckName("smart", "", "a.tracker.example", false); got.Rule != "geosite:adlist" {
if got := store.CheckName("smart", "", "a.tracker.example", false, false); got.Rule != "geosite:adlist" {
t.Errorf("the user's own list is no longer named: %+v", got)
}

// the preset stops asking: the list goes, and the names with it
store.SetPanelSites(nil, false)
rules = store.ActiveSiteRules()
if rules.Ads != nil || rules.ByName("all", "adnet.example") != NoRule || store.CheckName("all", "", "adnet.example", false).Route != NameVPN {
if rules.Ads != nil || rules.ByName("all", "adnet.example") != NoRule || store.CheckName("all", "", "adnet.example", false, false).Route != NameVPN {
t.Error("the ad list stayed after the preset dropped it")
}
if _, ok := store.siteSet("geosite:category-ads-all"); ok {
Expand Down Expand Up @@ -294,3 +294,102 @@ func TestNormalizeSiteDomain(t *testing.T) {
}
}
}

func TestBypassRules(t *testing.T) {
dir := t.TempDir()
store, err := Open(dir)
if err != nil {
t.Fatal(err)
}
// a file written before the bypass selection existed
if got := store.GetRules().Bypass; got.Sites == nil || len(got.Sites)+len(got.Collections)+len(got.Apps) != 0 {
t.Fatalf("no file: %#v", got)
}
for _, bad := range []Rules{
{Bypass: Selection{Apps: []string{`c:\a\a.exe`}}},
{Bypass: Selection{Collections: []string{"service:"}}},
{Bypass: Selection{Collections: []string{"service:Video"}}},
{Bypass: Selection{Collections: []string{"service:a b"}}},
} {
if _, _, err := store.SetRules(bad); err != ErrBadRules {
t.Errorf("SetRules(%+v) = %v", bad, err)
}
}
rules := Rules{
Direct: Selection{Sites: []string{"plain.example", "both.example"}},
VPN: Selection{Sites: []string{"vpn.example"}},
Block: Selection{Sites: []string{"ads.example"}},
Bypass: Selection{Sites: []string{"slow.example", "both.example", "vpn.example", "ads.example"}, Collections: []string{"service:video", "service:gone"}},
}
if _, changed, err := store.SetRules(rules); err != nil || !changed {
t.Fatal(changed, err)
}
// no catalogue yet: a service has no names
if got := store.ActiveSiteRules().ByName("selected", "video.example"); got != NoRule {
t.Fatalf("no catalogue: %v", got)
}
store.SetServices(func(id string) []string {
if id == "video" {
return []string{"video.example", "Bad Name", "cdn.video.example"}
}
return nil
})
active := store.ActiveSiteRules()
if !active.HasBypass {
t.Fatal("HasBypass is false")
}
for _, test := range []struct {
mode, name string
want RuleVerdict
}{
{"all", "slow.example", NoRule},
{"smart", "slow.example", RuleBypass},
{"selected", "a.slow.example", RuleBypass},
{"selected", "a.video.example", RuleBypass}, // a service of the catalogue
{"smart", "both.example", RuleDirect}, // an exception wins
{"selected", "vpn.example", RuleVPN},
{"smart", "ads.example", RuleBlock},
{"selected", "other.example", NoRule},
} {
if got := active.ByName(test.mode, test.name); got != test.want {
t.Errorf("ByName(%s, %s) = %v; want %v", test.mode, test.name, got, test.want)
}
}
// today's callers read a bypass name as one without a rule
if route, by := DecideName("slow.example", "smart", "", active); route != NameVPN || by != byDefault {
t.Errorf("DecideName = %s, %s", route, by)
}
for _, test := range []struct {
mode, name string
bypass bool
want string
}{
{"selected", "slow.example", true, "always"},
{"selected", "video.example", true, "always"},
{"selected", "other.example", true, "auto"},
{"selected", "other.example", false, "never"},
{"selected", "vpn.example", true, "never"}, // through the VPN
{"smart", "both.example", true, "never"}, // direct, but an exception
{"selected", "both.example", true, "never"}, // an exception in every mode
{"smart", "ads.example", true, "never"},
{"all", "slow.example", true, "never"},
} {
if got := store.CheckName(test.mode, "", test.name, false, test.bypass).Bypass; got != test.want {
t.Errorf("CheckName(%s, %s, %t).Bypass = %s; want %s", test.mode, test.name, test.bypass, got, test.want)
}
}
routes := store.Routes("selected", "none", "")
var seen int
for _, c := range routes.Collections {
if c.ID == "service:video" && c.Available && c.Entries == 2 || c.ID == "service:gone" && !c.Available {
seen++
}
}
if seen != 2 {
t.Errorf("collections: %+v", routes.Collections)
}
again, err := Open(dir) // a restart
if err != nil || !sameRules(again.GetRules(), store.GetRules()) {
t.Fatalf("after a restart: %+v, %v", again.GetRules(), err)
}
}
Loading
Loading