Skip to content

Commit

Permalink
feat: support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri Devigne committed Jun 2, 2024
1 parent aee8624 commit 66d02e8
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 24 deletions.
Binary file added .DS_Store
Binary file not shown.
31 changes: 30 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,43 @@ name: Release
on:
push:
branches:
- main
- feat-support-windows

permissions:
contents: read

jobs:
generate-icons:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Compiled icons
run: |
export GOPATH=$(go env GOPATH)
go get github.com/cratonica/2goarray
go install github.com/cratonica/2goarray
bash generate-icons.sh
- uses: actions/upload-artifact@v4
with:
name: icons
path: icons/*.go

windows-build:
runs-on: windows-latest
needs: [ generate-icons ]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- uses: actions/download-artifact@v4
with:
name: icons
path: icons

- run: go build -o ipingtray.exe

- name: Set up NSIS
Expand All @@ -33,11 +57,16 @@ jobs:

macos-build:
runs-on: macos-13
needs: [ generate-icons ]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- uses: actions/download-artifact@v4
with:
name: icons
path: icons

- run: go build -o ipingtray
- run: go install github.com/machinebox/appify@latest
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
vendor
icons/*.go
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ https://support.apple.com/guide/mac-help/open-items-automatically-when-you-log-i

![./docs/screenshot.png](/docs/screenshot.png)

## Development

### Generate icons

If you wish to update the icons, you need to run the `generate-icons.sh` which gonna generate
golang []bytes array representing the icon.

To generate icons, you also need to install [`2goarray`](https://github.com/cratonica/2goarray)

```bash
go get github.com/cratonica/2goarray
go install github.com/cratonica/2goarray
```

> ⚠️ We use .ico file because it's the only supported format on windows, according to
> [@ZGGSONG](https://github.com/getlantern/systray/issues/154#issuecomment-1207607136)

## Contribution

Feel free to contribute !
9 changes: 9 additions & 0 deletions generate-icons.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#/bin/bash
set -x

for icon in icons/*.ico
do
icon_name="$(basename ${icon%.*})"
capitalized_name=$(echo "${icon_name:0:1}" | tr '[:lower:]' '[:upper:]')${icon_name:1}
$GOPATH/bin/2goarray "${capitalized_name}" icons < ${icon} > "icons/${icon_name}.go"
done
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ require (
)

require (
github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/tevino/abool v1.2.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
fyne.io/systray v1.10.0 h1:Yr1D9Lxeiw3+vSuZWPlaHC8BMjIHZXJKkek706AfYQk=
fyne.io/systray v1.10.0/go.mod h1:oM2AQqGJ1AMo4nNqZFYU8xYygSBZkW2hmdJ7n4yjedE=
github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa h1:Wg+722vs7a2zQH5lR9QWYsVbplKeffaQFIs5FTdfNNo=
github.com/cratonica/2goarray v0.0.0-20190331194516-514510793eaa/go.mod h1:6Arca19mRx58CA7OWEd7Wu1NpC1rd3uDnNs6s1pj/DI=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand All @@ -8,10 +10,14 @@ github.com/prometheus-community/pro-bing v0.4.0 h1:YMbv+i08gQz97OZZBwLyvmmQEEzyf
github.com/prometheus-community/pro-bing v0.4.0/go.mod h1:b7wRYZtCcPmt4Sz319BykUU241rWLe1VFXyiyWK/dH4=
github.com/tevino/abool v1.2.0 h1:heAkClL8H6w+mK5md9dzsuohKeXHUpY7Vw0ZCKW+huA=
github.com/tevino/abool v1.2.0/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
Binary file added icons/green.ico
Binary file not shown.
Binary file added icons/orange.ico
Binary file not shown.
Binary file added icons/red.ico
Binary file not shown.
Binary file added icons/white.ico
Binary file not shown.
24 changes: 24 additions & 0 deletions internal/ping/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ping

import (
probing "github.com/prometheus-community/pro-bing"
"net"
"runtime"
)

// Pinger represent object which perform ping
var Pinger *probing.Pinger

func Init(destination net.IP) {
var err error
Pinger, err = probing.NewPinger(destination.String())
if err != nil {
panic(err)
}

Pinger.SetLogger(nil)

if runtime.GOOS == "windows" {
Pinger.SetPrivileged(true)
}
}
53 changes: 31 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,68 @@ package main
import (
"fmt"
"log"
"net"
"runtime"
"strings"

"fyne.io/systray"
probing "github.com/prometheus-community/pro-bing"
)

var pinger *probing.Pinger
"ipingtray/icons"
"ipingtray/internal/ping"
)

func main() {
address := net.ParseIP("8.8.8.8")
ping.Init(address)

systray.Run(onReady, onExit)
}

func onReady() {
systray.SetTitle("Initializing...")
systray.SetIcon(icons.White)

mLatencyLabel := systray.AddMenuItem("Latency", "Current Latency")
mLatencyLabel.Disable()

systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit the app")

var err error
pinger, err = probing.NewPinger("8.8.8.8")
if err != nil {
panic(err)
}
pinger.SetLogger(nil)

// Update tray title
pinger.OnRecv = func(pkt *probing.Packet) {
ping.Pinger.OnRecv = func(pkt *probing.Packet) {
latency := pkt.Rtt.Milliseconds()
icon := icons.Red

switch {
case latency < 50:
systray.SetTitle(fmt.Sprintf("🟢 %d ms", latency))
icon = icons.Green
case latency < 75:
systray.SetTitle(fmt.Sprintf("🟠 %d ms", latency))
default:
systray.SetTitle(fmt.Sprintf("🔴 %d ms", latency))
icon = icons.Orange
}

mLatencyLabel.SetTitle(fmt.Sprintf("%s: %d ms", pinger.IPAddr().String(), latency))
updateTray(fmt.Sprintf("%d ms", latency), icon)
mLatencyLabel.SetTitle(fmt.Sprintf("%s: %d ms", ping.Pinger.IPAddr().String(), latency))
}

pinger.OnSendError = func(_ *probing.Packet, err error) {
systray.SetTitle("🔴 Network unavailable")
ping.Pinger.OnSendError = func(_ *probing.Packet, err error) {
updateTray("Network Unavailable", icons.Red)

if strings.Contains(err.Error(), "sendto") {
parts := strings.Split(err.Error(), ": ")
if len(parts) >= 2 {
mLatencyLabel.SetTitle(fmt.Sprintf("8.8.8.8: %s", parts[len(parts)-1]))
mLatencyLabel.SetTitle(fmt.Sprintf("%s: %s", ping.Pinger.Addr(), parts[len(parts)-1]))
return
}
}

mLatencyLabel.SetTitle(fmt.Sprintf("8.8.8.8: %s", err.Error()))
mLatencyLabel.SetTitle(fmt.Sprintf("%s: %s", ping.Pinger.Addr(), err.Error()))
}

// Running the ping
go func() {
if err := pinger.Run(); err != nil {
log.Println("Pinger has crashed")
if err := ping.Pinger.Run(); err != nil {
log.Printf("Pinger has crashed: %v\n", err)
systray.Quit()
}
}()
Expand All @@ -76,6 +76,15 @@ func onReady() {
}()
}

func updateTray(title string, icon []byte) {
systray.SetTitle(title)
systray.SetIcon(icon)

if runtime.GOOS == "windows" {
systray.SetTooltip(title)
}
}

func onExit() {
pinger.Stop()
ping.Pinger.Stop()
}

0 comments on commit 66d02e8

Please sign in to comment.