-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
71 lines (64 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gen2brain/beeep"
)
const checkInterval = time.Minute * 5
func main() {
var (
vpnIP = flag.String("ip", "", "the VPN public IP address, see https://ifconfig.co")
limit = flag.Duration("limit", time.Minute*30, "time limit before notifications start")
)
flag.Parse()
if *vpnIP == "" {
fmt.Println("missing -ip flag, you must specify VPN IP, see --help")
os.Exit(2)
}
for {
time.Sleep(checkInterval)
if err := checkIP(*limit, *vpnIP); err != nil {
log.Fatal(err)
}
}
}
var (
connectionTime time.Duration
lastIP string
)
func checkIP(timeLimit time.Duration, vpnIP string) error {
req, err := http.NewRequest("GET", "https://ifconfig.co", nil)
if err != nil {
return err
}
cl := &http.Client{
Timeout: time.Second * 10,
}
resp, err := cl.Do(req)
if err != nil {
return err
}
dat, err := ioutil.ReadAll(resp.Body)
if err != nil {
resp.Body.Close()
return err
}
resp.Body.Close()
lastIP = strings.TrimSpace(string(dat))
if lastIP != vpnIP {
return nil
}
connectionTime += checkInterval
if connectionTime >= timeLimit {
alert := fmt.Sprintf("You've been connected to the VPN for over %s", connectionTime)
log.Printf(alert)
beeep.Alert("VPN Warning", alert, "")
}
return nil
}