From 660e4c3491cddb3be217e5dc1a732517d9404d85 Mon Sep 17 00:00:00 2001 From: Nikolay Lazarov Date: Wed, 30 Oct 2024 23:49:14 +0200 Subject: [PATCH 1/5] Replace calls to deprecated io/ioutil library with their supported alternatives --- gui/tunnels.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/gui/tunnels.go b/gui/tunnels.go index 20299f1..e996e7d 100755 --- a/gui/tunnels.go +++ b/gui/tunnels.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -380,12 +379,12 @@ func (t *Tunnels) Create() error { continue } - data, err := ioutil.ReadFile(fname) + data, err := os.ReadFile(fname) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(TunnelsPath, filepath.Base(fname)), data, 666) + err = os.WriteFile(filepath.Join(TunnelsPath, filepath.Base(fname)), data, 666) if err != nil { return err } @@ -606,7 +605,7 @@ func (t *Tunnels) Create() error { return err } - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return err } @@ -765,7 +764,7 @@ func (t *Tunnels) Create() error { } // write changes - if err := ioutil.WriteFile(path, []byte(data), 666); err != nil { + if err := os.WriteFile(path, []byte(data), 666); err != nil { return err } @@ -1180,12 +1179,12 @@ func parseZipArchive(target string) error { } defer fr.Close() - data, err := ioutil.ReadAll(fr) + data, err := io.ReadAll(fr) if err != nil { return err } - err = ioutil.WriteFile(filepath.Join(TunnelsPath, f.FileInfo().Name()), data, 666) + err = os.WriteFile(filepath.Join(TunnelsPath, f.FileInfo().Name()), data, 666) if err != nil { return err } From e4e2c3c7b589fcb6a939b724b69b05481a927ed8 Mon Sep 17 00:00:00 2001 From: Nikolay Lazarov Date: Wed, 30 Oct 2024 23:53:23 +0200 Subject: [PATCH 2/5] Cache icons instead of reading them from the filesystem every time --- gui/tunnels.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/gui/tunnels.go b/gui/tunnels.go index e996e7d..7cbbd77 100755 --- a/gui/tunnels.go +++ b/gui/tunnels.go @@ -66,12 +66,26 @@ type Tunnels struct { ticker *time.Ticker lastSelected string + + grayIcon *gtk.Image + greenIcon *gtk.Image } func (t *Tunnels) Create() error { t.icons = map[string]*gtk.Image{} t.ticker = time.NewTicker(1 * time.Second) + var err error + t.grayIcon, err = gtk.ImageNewFromFile(IconPath + "not_connected.png") + if err != nil { + return err + } + + t.greenIcon, err = gtk.ImageNewFromFile(IconPath + "connected.png") + if err != nil { + return err + } + tl, err := get.ListBox("tunnel_list") if err != nil { return err @@ -219,13 +233,8 @@ func (t *Tunnels) Create() error { // disconnect from given tunnel dc := func(d *wgtypes.Device) error { - gray, err := gtk.ImageNewFromFile(IconPath + "not_connected.png") - if err != nil { - return err - } - glib.IdleAdd(func() { - t.icons[d.Name].SetFromPixbuf(gray.GetPixbuf()) + t.icons[d.Name].SetFromPixbuf(t.grayIcon.GetPixbuf()) }) c := exec.Command("wg-quick", "down", d.Name) @@ -303,14 +312,9 @@ func (t *Tunnels) Create() error { header.SetSubtitle("Connected to " + strings.Join(activeNames, ", ")) }) - green, err := gtk.ImageNewFromFile(IconPath + "connected.png") - if err != nil { - return err - } - // set icon to connected for the tunnel's row glib.IdleAdd(func() { - t.icons[name].SetFromPixbuf(green.GetPixbuf()) + t.icons[name].SetFromPixbuf(t.greenIcon.GetPixbuf()) t.UpdateRow(row) indicator.SetIcon("wg_connected") }) @@ -1040,14 +1044,13 @@ func (t *Tunnels) ScanTunnels() error { var img *gtk.Image if dry.StringListContains(activeNames, name) { - green, err := gtk.ImageNewFromFile(IconPath + "connected.png") + green, err := gtk.ImageNewFromPixbuf(t.greenIcon.GetPixbuf()) if err != nil { return err } - img = green } else { - gray, err := gtk.ImageNewFromFile(IconPath + "not_connected.png") + gray, err := gtk.ImageNewFromPixbuf(t.grayIcon.GetPixbuf()) if err != nil { return err } From 4d429b4b7a1bc15071a2c445cc1e3ca0ae9c8425 Mon Sep 17 00:00:00 2001 From: Nikolay Lazarov Date: Wed, 30 Oct 2024 23:54:19 +0200 Subject: [PATCH 3/5] Remove unnecessary err check Just before this err check is another err check that always returns. --- gui/tunnels.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/gui/tunnels.go b/gui/tunnels.go index 7cbbd77..76aac25 100755 --- a/gui/tunnels.go +++ b/gui/tunnels.go @@ -302,10 +302,6 @@ func (t *Tunnels) Create() error { return errors.New(oerr) } - if err != nil { - return err - } - // update header label with tunnel names glib.IdleAdd(func() { activeNames := t.ActiveDeviceName() From b48fc7aa1b2cd9bba743c00cb40d5d471282c489 Mon Sep 17 00:00:00 2001 From: Nikolay Lazarov Date: Wed, 30 Oct 2024 23:57:47 +0200 Subject: [PATCH 4/5] Changes from `go generate` --- static/ab0x.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/ab0x.go b/static/ab0x.go index 265d186..b1c6764 100644 --- a/static/ab0x.go +++ b/static/ab0x.go @@ -1,5 +1,5 @@ -// Code generated by fileb0x at "2023-10-01 00:24:10.779914994 +0200 CEST m=+0.007497526" from config file "fileb0x.toml" DO NOT EDIT. -// modification hash(2708cdf7520cea1555d9794eb83c02ff.370050f9606640156d65adf676074775) +// Code generated by fileb0x at "2024-10-30 16:22:27.155230419 +0200 EET m=+0.001108700" from config file "fileb0x.toml" DO NOT EDIT. +// modification hash(aac4b8626e6a689714efe5f60e86752a.370050f9606640156d65adf676074775) package static From 0b7aca734e989d82e7da5947fe8ee2e2fc3beeac Mon Sep 17 00:00:00 2001 From: Nikolay Lazarov Date: Wed, 30 Oct 2024 23:56:13 +0200 Subject: [PATCH 5/5] Update gui with wireguard state on every tick By updating the gui on every tick, changes from `wg-quick` commands ran by the user outside of wireguird will be reflected in the wireguird interface. --- gui/tunnels.go | 54 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/gui/tunnels.go b/gui/tunnels.go index 76aac25..8906335 100755 --- a/gui/tunnels.go +++ b/gui/tunnels.go @@ -833,10 +833,36 @@ func (t *Tunnels) Create() error { for { <-t.ticker.C - if !window.HasToplevelFocus() { + // update tray icon + activeNames := t.ActiveDeviceName() + if len(activeNames) == 0 { + indicator.SetIcon("wireguard_off") + } else { + indicator.SetIcon("wg_connected") + } + + if !window.IsVisible() { continue } + glib.IdleAdd(func() { + // update list icons + for name := range t.icons { + if dry.StringListContains(activeNames, name) { + t.icons[name].SetFromPixbuf(t.greenIcon.GetPixbuf()) + } else { + t.icons[name].SetFromPixbuf(t.grayIcon.GetPixbuf()) + } + } + + // update header label with tunnel names + if len(activeNames) == 0 { + header.SetSubtitle("Not connected!") + } else { + header.SetSubtitle("Connected to " + strings.Join(activeNames, ", ")) + } + }) + row := tl.GetSelectedRow() if row == nil { t.UnknownLabels() @@ -849,7 +875,15 @@ func (t *Tunnels) Create() error { continue } - if !dry.StringListContains(t.ActiveDeviceName(), name) { + if !dry.StringListContains(activeNames, name) { + glib.IdleAdd(func() { + t.Interface.Status.SetText("Inactive") + t.Interface.PublicKey.SetText("unknown") + t.Interface.ListenPort.SetText("unknown") + t.ButtonChangeState.SetLabel("Activate") + t.Peer.LatestHandshake.SetText("unknown") + t.Peer.Transfer.SetText("unknown") + }) continue } @@ -859,16 +893,18 @@ func (t *Tunnels) Create() error { continue } - t.Interface.PublicKey.SetText(d.PublicKey.String()) - t.Interface.ListenPort.SetText(strconv.Itoa(d.ListenPort)) + glib.IdleAdd(func() { + t.Interface.Status.SetText("Active") + t.Interface.PublicKey.SetText(d.PublicKey.String()) + t.Interface.ListenPort.SetText(strconv.Itoa(d.ListenPort)) + t.ButtonChangeState.SetLabel("Deactivate") - for _, p := range d.Peers { - hs := humanize.Time(p.LastHandshakeTime) - glib.IdleAdd(func() { + for _, p := range d.Peers { + hs := humanize.Time(p.LastHandshakeTime) t.Peer.LatestHandshake.SetText(hs) t.Peer.Transfer.SetText(humanize.Bytes(uint64(p.ReceiveBytes)) + " received, " + humanize.Bytes(uint64(p.TransmitBytes)) + " sent") - }) - } + } + }) } }()