-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/ui: move Mullvad nodes to their own list on their own page (#…
…111) * internal/ui: move self check into `peerName()` * internal/ui: begin implementing `MullvadPage` * internal/ui: refactor page name calculation into the pages themselves * internal/ui: remove Mullvad nodes from main peer list * internal/ui: more simplification of updates * internal/ui: fix `nil` check for `a.statusPage` * internal/ui: separate self-page from peer pages * internal/ui: add Mullvad page to list of pages * internal/ui: implement Mullvad exit node list * internal/ui: indicate that a Mullvad node is active * internal/xcmp: remove * internal/ui: add flags for Mullvad nodes * internal/ui: don't bother uppercasing an already uppercase string * cmd/trayscale: update PGO file * meta: add v0.12.0 to metainfo
- Loading branch information
1 parent
a41f4c6
commit 30dbd08
Showing
12 changed files
with
310 additions
and
81 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package ui | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
_ "embed" | ||
"fmt" | ||
"log/slog" | ||
"slices" | ||
|
||
"deedles.dev/trayscale/internal/tsutil" | ||
"github.com/diamondburned/gotk4-adwaita/pkg/adw" | ||
"github.com/diamondburned/gotk4/pkg/gtk/v4" | ||
"tailscale.com/ipn/ipnstate" | ||
"tailscale.com/tailcfg" | ||
) | ||
|
||
//go:embed mullvadpage.ui | ||
var mullvadPageXML string | ||
|
||
type MullvadPage struct { | ||
*adw.StatusPage `gtk:"Page"` | ||
|
||
ExitNodesGroup *adw.PreferencesGroup | ||
|
||
name string | ||
|
||
exitNodeRows rowManager[*ipnstate.PeerStatus] | ||
} | ||
|
||
func NewMullvadPage() *MullvadPage { | ||
var page MullvadPage | ||
fillFromBuilder(&page, mullvadPageXML) | ||
return &page | ||
} | ||
|
||
func (page *MullvadPage) Root() gtk.Widgetter { | ||
return page.StatusPage | ||
} | ||
|
||
func (page *MullvadPage) ID() string { | ||
return "mullvad" | ||
} | ||
|
||
func (page *MullvadPage) Name() string { | ||
return page.name | ||
} | ||
|
||
func (page *MullvadPage) Init(a *App, peer *ipnstate.PeerStatus, status tsutil.Status) { | ||
page.name = "Mullvad Exit Nodes" | ||
|
||
page.exitNodeRows.Parent = page.ExitNodesGroup | ||
page.exitNodeRows.New = func(peer *ipnstate.PeerStatus) row[*ipnstate.PeerStatus] { | ||
row := exitNodeRow{ | ||
peer: peer, | ||
|
||
w: adw.NewActionRow(), | ||
r: gtk.NewSwitch(), | ||
} | ||
|
||
row.w.AddSuffix(row.r) | ||
row.w.SetTitle(mullvadExitNodeName(peer)) | ||
|
||
row.r.SetMarginTop(12) | ||
row.r.SetMarginBottom(12) | ||
row.r.ConnectStateSet(func(s bool) bool { | ||
if s == row.r.State() { | ||
return false | ||
} | ||
|
||
if s { | ||
err := a.TS.AdvertiseExitNode(context.TODO(), false) | ||
if err != nil { | ||
slog.Error("disable exit node advertisement", "err", err) | ||
// Continue anyways. | ||
} | ||
} | ||
|
||
var node *ipnstate.PeerStatus | ||
if s { | ||
node = row.peer | ||
} | ||
err := a.TS.ExitNode(context.TODO(), node) | ||
if err != nil { | ||
slog.Error("set exit node", "err", err) | ||
row.r.SetActive(!s) | ||
return true | ||
} | ||
a.poller.Poll() <- struct{}{} | ||
return true | ||
}) | ||
|
||
return &row | ||
} | ||
} | ||
|
||
func (page *MullvadPage) Update(a *App, peer *ipnstate.PeerStatus, status tsutil.Status) { | ||
page.name = "Mullvad Exit Nodes" | ||
|
||
var exitNodeID tailcfg.StableNodeID | ||
if status.Status.ExitNodeStatus != nil { | ||
exitNodeID = status.Status.ExitNodeStatus.ID | ||
} | ||
|
||
nodes := make([]*ipnstate.PeerStatus, 0, len(status.Status.Peer)) | ||
for _, peer := range status.Status.Peer { | ||
if tsutil.IsMullvad(peer) { | ||
nodes = append(nodes, peer) | ||
if peer.ID == exitNodeID { | ||
page.name = fmt.Sprintf("Mullvad Exit Nodes [%v]", mullvadExitNodeName(peer)) | ||
} | ||
} | ||
} | ||
slices.SortFunc(nodes, func(p1 *ipnstate.PeerStatus, p2 *ipnstate.PeerStatus) int { | ||
return cmp.Compare(p1.DNSName, p2.DNSName) | ||
}) | ||
|
||
page.exitNodeRows.Update(nodes) | ||
} | ||
|
||
type exitNodeRow struct { | ||
peer *ipnstate.PeerStatus | ||
|
||
w *adw.ActionRow | ||
r *gtk.Switch | ||
} | ||
|
||
func (row *exitNodeRow) Update(peer *ipnstate.PeerStatus) { | ||
row.peer = peer | ||
|
||
row.w.SetTitle(mullvadExitNodeName(peer)) | ||
|
||
row.r.SetState(peer.ExitNode) | ||
row.r.SetActive(peer.ExitNode) | ||
} | ||
|
||
func (row *exitNodeRow) Widget() gtk.Widgetter { | ||
return row.w | ||
} | ||
|
||
func mullvadExitNodeName(peer *ipnstate.PeerStatus) string { | ||
return fmt.Sprintf("%v %v, %v", countryCodeToFlag(peer.Location.CountryCode), peer.Location.Country, peer.Location.City) | ||
} | ||
|
||
func countryCodeToFlag(code string) string { | ||
var raw [2]rune | ||
for i, c := range code { | ||
raw[i] = 127397 + c | ||
} | ||
|
||
return string(raw[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<!-- Created with Cambalache 0.17.2 --> | ||
<interface> | ||
<requires lib="gtk" version="4.12"/> | ||
<requires lib="libadwaita" version="1.0"/> | ||
<object class="AdwStatusPage" id="Page"> | ||
<property name="title">Mullvad Exit Nodes</property> | ||
<child> | ||
<object class="AdwClamp"> | ||
<child> | ||
<object class="GtkBox"> | ||
<property name="orientation">vertical</property> | ||
<property name="spacing">12</property> | ||
<child> | ||
<object class="AdwPreferencesGroup" id="ExitNodesGroup"/> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</interface> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.