-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (77 loc) · 2.13 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"net"
"net/http"
"os"
"time"
)
type IfconfigInfo struct {
IP string `json:"ip"`
IPDecimal int `json:"ip_decimal"`
Country string `json:"country"`
CountryIso string `json:"country_iso"`
CountryEu bool `json:"country_eu"`
Asn string `json:"asn"`
AsnOrg string `json:"asn_org"`
Hostname string `json:"hostname"`
}
func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
hosts := []string{"hoto.moe", "test1.enhawiki.kr", "test1-cf.enhawiki.kr", "jp1.hotomoe.net", "jp1-cf.hotomoe.net", "us1.hotomoe.net", "us1-cf.hotomoe.net"}
names := []string{"Current (HOTOUSNET Cache)", "Korea", "Korea (Cloudflare)", "Japan", "Japan (Cloudflare)", "United States CA", "United States CA (Cloudflare)"}
info, err := getIfconfigInfo("")
if err == nil {
println("Client IP:", info.IP)
println("Client Country:", info.Country)
println("Client ASN:", info.Asn)
} else {
panic("Failed to get client IP")
}
println()
for index, host := range hosts {
println("Host:", host)
println("Name:", names[index])
ip := getIpAddress(host)
println("IP:", ip)
info, err := getIfconfigInfo(ip)
if err == nil {
println("ASN:", info.Asn)
}
latency := getLatency(host)
println("Latency:", latency, "ms")
println()
}
println("Press Enter to exit")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func getLatency(host string) int64 {
currentTime := time.Now().UnixMilli()
resp, err := http.Get("https://" + host)
if err != nil {
return -1
}
defer resp.Body.Close()
latency := time.Now().UnixMilli() - currentTime
return latency
}
func getIpAddress(dnsName string) string {
ips, err := net.DefaultResolver.LookupNetIP(context.Background(), "ip4", dnsName)
if err != nil {
return "unknown"
}
return ips[0].String()
}
func getIfconfigInfo(ip string) (IfconfigInfo, error) {
var info IfconfigInfo
resp, err := http.Get("https://ifconfig.co/json?ip=" + ip)
if err != nil {
return info, err
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&info)
return info, nil
}