Skip to content

Commit

Permalink
cmd/awl-tray: add Proxy menu that allows to set current peer for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
pymq committed Jan 26, 2025
1 parent 65428d1 commit 63c416a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
5 changes: 1 addition & 4 deletions api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ func (h *Handler) UpdateProxySettings(c echo.Context) (err error) {
return c.JSON(http.StatusBadRequest, ErrorMessage("peer doesn't allow using as exit node"))
}

h.conf.Lock()
h.conf.SOCKS5.UsingPeerID = req.UsingPeerID
h.conf.Unlock()
h.conf.Save()
h.socks5.SetProxyPeerID(req.UsingPeerID)

return c.NoContent(http.StatusOK)
}
6 changes: 2 additions & 4 deletions cmd/awl-tray/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module awl-tray
go 1.23.0

require (
fyne.io/systray v1.10.1-0.20230403195833-7dc3c09283d6
fyne.io/systray v1.11.1-0.20240917165310-0f99d4d0758b
github.com/GrigoryKrasnochub/updaterini v0.1.0
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9
github.com/anywherelan/awl v0.0.0-00010101000000-000000000000
github.com/gen2brain/beeep v0.0.0-20230907135156-1a38885a97fc
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
github.com/godbus/dbus/v5 v5.1.1-0.20230522191255-76236955d466
github.com/ipfs/go-log/v2 v2.5.1
github.com/libp2p/go-libp2p v0.36.5
Expand All @@ -16,7 +16,6 @@ require (
)

replace (
fyne.io/systray => github.com/anywherelan/systray v0.0.0-20230509110713-9d662d95edcb
github.com/anywherelan/awl => ../../
github.com/ipfs/go-log/v2 => github.com/anywherelan/go-log/v2 v2.0.3-0.20221101180049-46e3967f6fe5
github.com/ncruces/zenity => github.com/pymq/zenity v0.0.0-20230509161854-c117c448544d
Expand Down Expand Up @@ -150,7 +149,6 @@ require (
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/urfave/cli/v2 v2.27.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
Expand Down
11 changes: 4 additions & 7 deletions cmd/awl-tray/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl
dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBrvjyP0v+ecvNYvCpyZgu5/xkfAUhi6wJj28eUfSU=
dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4=
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
fyne.io/systray v1.11.1-0.20240917165310-0f99d4d0758b h1:U8f3vqA1nLgLe25vPpSw4Hk5vjT37OyzfbeziBieNb8=
fyne.io/systray v1.11.1-0.20240917165310-0f99d4d0758b/go.mod h1:RVwqP9nYMo7h5zViCBHri2FgjXF7H2cub7MAq4NSoLs=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/GrigoryKrasnochub/updaterini v0.1.0 h1:csl3bqQIShuI8RnXzKprTgEhqi3I59XbfZn5+0LGYR8=
Expand All @@ -17,8 +19,6 @@ github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxk
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/anywherelan/go-log/v2 v2.0.3-0.20221101180049-46e3967f6fe5 h1:uQsw+HnQo6Ru5eFgUdEQYKMwkiYkoNZDQmY2ob1E58Y=
github.com/anywherelan/go-log/v2 v2.0.3-0.20221101180049-46e3967f6fe5/go.mod h1:r8UEDyeHO6bYVcP9R2/HnK2ZSZ5CJp89gubcHLKfRv0=
github.com/anywherelan/systray v0.0.0-20230509110713-9d662d95edcb h1:puTJIQvD41dONJuVUUoSU8wFfkipYBWFpxRhIOsBdV8=
github.com/anywherelan/systray v0.0.0-20230509110713-9d662d95edcb/go.mod h1:oM2AQqGJ1AMo4nNqZFYU8xYygSBZkW2hmdJ7n4yjedE=
github.com/anywherelan/ts-dns v0.0.0-20240721135326-6d6b7b811853 h1:RVKWGnppAfxgD2wphkq+OYDOqqI8zgbymBLl2pxYKzY=
github.com/anywherelan/ts-dns v0.0.0-20240721135326-6d6b7b811853/go.mod h1:ly7HpPle1G3D0jwrr12uolTGWKN3DPgxzBYNR086BLo=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down Expand Up @@ -84,8 +84,8 @@ github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7z
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/gen2brain/beeep v0.0.0-20230907135156-1a38885a97fc h1:NNgdMgPX3j33uEAoVVxNxillDPnxT0xbGv8uh4CKIAo=
github.com/gen2brain/beeep v0.0.0-20230907135156-1a38885a97fc/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 h1:ygs9POGDQpQGLJPlq4+0LBUmMBNox1N4JSpw+OETcvI=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
Expand Down Expand Up @@ -475,8 +475,6 @@ github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/tevino/abool v1.2.0 h1:heAkClL8H6w+mK5md9dzsuohKeXHUpY7Vw0ZCKW+huA=
github.com/tevino/abool v1.2.0/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
Expand Down Expand Up @@ -610,7 +608,6 @@ golang.org/x/sys v0.0.0-20190316082340-a2f829d7f35f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
60 changes: 59 additions & 1 deletion cmd/awl-tray/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/anywherelan/awl/config"
"github.com/anywherelan/awl/embeds"
"github.com/anywherelan/awl/entity"
"github.com/anywherelan/awl/update"
)

Expand All @@ -24,6 +25,7 @@ var (
peersCountMenu *systray.MenuItem
openBrowserMenu *systray.MenuItem
peersMenu *systray.MenuItem
proxyMenu *systray.MenuItem
startStopMenu *systray.MenuItem
restartMenu *systray.MenuItem
updateMenu *systray.MenuItem
Expand Down Expand Up @@ -78,7 +80,7 @@ func initTray() {
}()
systray.AddSeparator()

peersMenu = systray.AddMenuItem("Peers", "Peers")
peersMenu = systray.AddMenuItem("Peers", "")
go func() {
// On windows systray does not trigger clicked event on menus with submenus
for range peersMenu.ClickedCh {
Expand All @@ -92,9 +94,12 @@ func initTray() {
continue
}
refreshPeersSubmenus()
refreshProxySubmenus()
}
}()

proxyMenu = systray.AddMenuItem("Proxy", "")

startStopMenu = systray.AddMenuItem("", "")
go func() {
for range startStopMenu.ClickedCh {
Expand Down Expand Up @@ -141,17 +146,20 @@ func refreshMenusOnStartedServer() {
setPeersConnectedCounter(0)
openBrowserMenu.Enable()
peersMenu.Enable()
proxyMenu.Enable()
startStopMenu.SetTitle("Stop server")
restartMenu.Enable()

refreshPeersSubmenus()
refreshProxySubmenus()
}

func refreshMenusOnStoppedServer() {
statusMenu.SetTitle("Status: stopped")
setPeersConnectedCounter(0)
openBrowserMenu.Disable()
peersMenu.Disable()
proxyMenu.Disable()
startStopMenu.SetTitle("Start server")
restartMenu.Disable()
}
Expand Down Expand Up @@ -242,6 +250,56 @@ func refreshPeersSubmenus() {
}
}

var proxySubmenus []*systray.MenuItem
var previousProxies []entity.AvailableProxy
var previousProxyPeerID string

func refreshProxySubmenus() {
if app == nil || proxyMenu == nil {
return
}

app.Conf.RLock()
currentProxy := app.Conf.SOCKS5.UsingPeerID
app.Conf.RUnlock()

proxies := app.SOCKS5.ListAvailableProxies()

if slices.Equal(previousProxies, proxies) && previousProxyPeerID == currentProxy {
return
}

for _, submenu := range proxySubmenus {
submenu.Remove()
}
proxySubmenus = nil

previousProxies = proxies
previousProxyPeerID = currentProxy

for _, proxy := range proxies {
isChecked := proxy.PeerID == currentProxy
submenu := proxyMenu.AddSubMenuItemCheckbox(proxy.PeerName, "", isChecked)
proxySubmenus = append(proxySubmenus, submenu)

go func() {
for range submenu.ClickedCh {
app.SOCKS5.SetProxyPeerID(proxy.PeerID)

app.Conf.Lock()
for _, currentSubmenu := range proxySubmenus {
if currentSubmenu == submenu {
submenu.Check()
} else {
currentSubmenu.Uncheck()
}
}
app.Conf.Unlock()
}
}()
}
}

func onClickUpdateMenu() error {
updateMenu.SetTitle("Checking...")
updateMenu.Disable()
Expand Down
13 changes: 13 additions & 0 deletions service/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io"
"net"
"slices"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -80,9 +82,20 @@ func (s *SOCKS5) ListAvailableProxies() []entity.AvailableProxy {
}
s.conf.RUnlock()

slices.SortFunc(proxies, func(a, b entity.AvailableProxy) int {
return strings.Compare(a.PeerName, b.PeerName)
})

return proxies
}

func (s *SOCKS5) SetProxyPeerID(peerID string) {
s.conf.Lock()
s.conf.SOCKS5.UsingPeerID = peerID
s.conf.Unlock()
s.conf.Save()
}

func (s *SOCKS5) ProxyStreamHandler(stream network.Stream) {
defer func() {
_ = stream.Reset()
Expand Down

0 comments on commit 63c416a

Please sign in to comment.