-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (60 loc) · 1.33 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
72
73
74
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/iqhater/myip/data"
v "github.com/iqhater/myip/view"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "'myip' is a tiny utility that shows the internal and external IP address.\nAlso 'myip' shows the adapter name where local IP is located.\nUsage: myip [no arguments are required].\n")
}
flag.Parse()
d := data.NewIPData()
d.GetInternalIP()
d.GetAdapterName()
v.PrintInternal(d)
sources := []string{
"https://api.myip.com/",
"https://api.miip.my",
"https://ip.seeip.org/geoip",
"https://api64.ipify.org?format=json",
"https://api.myip.la/en?json",
"https://ipapi.co/ip/",
"https://api.seeip.org/jsonip",
"https://myip.arens.online/json",
}
timeout, err := time.ParseDuration("10s")
if err != nil {
log.Println("Can't parse timeout duration: ", err)
}
done := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
for _, url := range sources {
url := url
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
done <- struct{}{}
return
default:
err := d.GetExternalIP(url, timeout)
if err == nil {
cancel()
return
}
// debug errors
// log.Println(err)
}
}
}(ctx)
}
<-done
defer close(done)
v.PrintExternal(d)
}