ip_checker/main.go

97 строки
1.7 KiB
Go
Исходник Обычный вид История

2017-06-14 19:05:47 +00:00
package main
import (
"fmt"
"github.com/lxn/walk"
"github.com/marcsauter/single"
"io/ioutil"
"log"
"net/http"
"time"
)
func get_ip() (string, error) {
resp, err := http.Get("https://4.ifcfg.me/ip")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
ip := fmt.Sprintf("%s", body)
return ip, err
}
2017-06-16 18:15:12 +00:00
func err_fatal(err error) {
if err != nil {
log.Fatal(err)
}
}
2017-06-14 19:05:47 +00:00
var old_ip string
func main() {
2017-06-14 19:18:31 +00:00
single_proc := single.New("IP Checker")
2017-06-14 19:05:47 +00:00
single_proc.Lock()
defer single_proc.Unlock()
mw, err := walk.NewMainWindow()
2017-06-16 18:15:12 +00:00
err_fatal(err)
2017-06-14 19:05:47 +00:00
icon, err := walk.NewIconFromResourceId(9)
2017-06-16 18:15:12 +00:00
err_fatal(err)
2017-06-14 19:05:47 +00:00
ni, err := walk.NewNotifyIcon()
2017-06-16 18:15:12 +00:00
err_fatal(err)
2017-06-14 19:05:47 +00:00
defer ni.Dispose()
2017-06-16 18:15:12 +00:00
err_fatal(ni.SetIcon(icon))
2017-06-14 19:05:47 +00:00
exitAction := walk.NewAction()
2017-06-16 18:15:12 +00:00
err_fatal(exitAction.SetText("E&xit"))
2017-06-14 19:05:47 +00:00
exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
2017-06-16 18:15:12 +00:00
err_fatal(ni.ContextMenu().Actions().Add(exitAction))
2017-06-14 19:05:47 +00:00
2017-06-16 18:15:12 +00:00
err_fatal(ni.SetVisible(true))
2017-06-14 19:05:47 +00:00
go func() {
first_loop := true
for first_loop {
ip, ip_err := get_ip()
if ip_err == nil {
first_loop = false
old_ip = ip
2017-06-16 18:15:12 +00:00
err_fatal(ni.ShowCustom(
2017-06-14 19:05:47 +00:00
"IP Checker",
2017-06-16 18:15:12 +00:00
" Current IP is "+old_ip))
2017-06-16 18:19:55 +00:00
err_fatal(ni.SetToolTip("Current IP is " + old_ip))
2017-06-14 19:05:47 +00:00
go func() {
for {
time.Sleep(time.Second * 60)
ip, ip_err := get_ip()
if ip_err == nil {
if old_ip != ip {
2017-06-16 18:15:12 +00:00
err_fatal(ni.ShowCustom(
2017-06-14 19:05:47 +00:00
"IP Checker",
2017-06-16 18:15:12 +00:00
" Old IP - "+old_ip+" New IP - "+ip))
2017-06-16 18:19:55 +00:00
err_fatal(ni.SetToolTip(" Old IP - " + old_ip + " New IP - " + ip))
2017-06-14 19:05:47 +00:00
old_ip = ip
}
}
}
}()
}
}
}()
mw.Run()
}