🚸 优化 Agent 取 IP

This commit is contained in:
naiba
2021-06-22 14:04:07 +08:00
parent 355c625414
commit d59acfa824
4 changed files with 72 additions and 54 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
@@ -8,22 +9,53 @@ import (
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/genkiroid/cert"
"github.com/go-ping/ping"
"github.com/naiba/nezha/pkg/utils"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/naiba/nezha/pkg/utils"
)
func main() {
// icmp()
// tcpping()
httpWithSSLInfo()
// httpWithSSLInfo()
// sysinfo()
// cmdExec()
resolveIP("ipapi.co", true)
resolveIP("ipapi.co", false)
}
func resolveIP(addr string, ipv6 bool) {
url := strings.Split(addr, ":")
dnsServers := []string{"2606:4700:4700::1001", "2001:4860:4860::8844", "2400:3200::1", "2400:3200:baba::1"}
if !ipv6 {
dnsServers = []string{"1.0.0.1", "8.8.4.4", "223.5.5.5", "223.6.6.6"}
}
log.Println(net.LookupIP(url[0]))
for i := 0; i < len(dnsServers); i++ {
dnsServer := dnsServers[i]
if ipv6 {
dnsServer = "[" + dnsServer + "]"
}
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Second * 10,
}
return d.DialContext(ctx, "udp", dnsServer+":53")
},
}
log.Println(r.LookupIP(context.Background(), "ip", url[0]))
}
}
func tcpping() {