From 66e3d32364caf87fd9bddb3d19f1d2bc69fe90ca Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Thu, 7 Nov 2024 12:35:28 +0000 Subject: [PATCH] =?UTF-8?q?v0.0.8=20=E6=96=B0=E5=A2=9EIPV6=E5=AD=90?= =?UTF-8?q?=E7=BD=91=E6=8E=A9=E7=A0=81=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yaml | 2 +- README.md | 5 -- cmd/main.go | 8 +++- ipv6/ipv6.go | 97 +++++++++++++++++++++++++++++++++++++++ model/model.go | 2 +- 5 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 ipv6/ipv6.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8271114..fd05332 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,7 +24,7 @@ jobs: run: | git config --global user.name 'github-actions' git config --global user.email 'github-actions@github.com' - TAG="v0.0.7-$(date +'%Y%m%d%H%M%S')" + TAG="v0.0.8-$(date +'%Y%m%d%H%M%S')" git tag $TAG git push origin $TAG env: diff --git a/README.md b/README.md index 6208a6c..da1b6b6 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,6 @@ Include: https://github.com/oneclickvirt/gostun - [x] 部分Windows10系统下打勾打叉编码错误显示,已判断是Win时使用Y/N显示而不是勾叉 - [x] 检测GPU相关信息,参考[ghw](https://github.com/jaypipes/ghw) -## TODO - -- [ ] 特化ASN和归属地查不出来的时候使用备用查询的API进行查询 -- [ ] 纯IPV6环境下使用cdn反代获取平台信息 - ## Usage 下载及安装 diff --git a/cmd/main.go b/cmd/main.go index 4d82a21..1874855 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,6 +8,7 @@ import ( "runtime" "strings" + "github.com/oneclickvirt/basics/ipv6" "github.com/oneclickvirt/basics/model" "github.com/oneclickvirt/basics/network" "github.com/oneclickvirt/basics/system" @@ -40,9 +41,14 @@ func main() { }() fmt.Println("项目地址:", "https://github.com/oneclickvirt/basics") ipInfo, _, _ := network.NetworkCheck("both", false, language) + ipv6Info, err := ipv6.GetIPv6Mask() res := system.CheckSystemInfo(language) fmt.Println("--------------------------------------------------") - fmt.Printf(strings.ReplaceAll(res+ipInfo, "\n\n", "\n")) + temp := strings.ReplaceAll(res+ipInfo, "\n\n", "\n") + if err == nil && res != "" { + temp += ipv6Info + } + fmt.Printf(temp) fmt.Println("--------------------------------------------------") if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { fmt.Println("Press Enter to exit...") diff --git a/ipv6/ipv6.go b/ipv6/ipv6.go new file mode 100644 index 0000000..7048e89 --- /dev/null +++ b/ipv6/ipv6.go @@ -0,0 +1,97 @@ +package ipv6 + +import ( + "fmt" + "net" + "os/exec" + "strings" +) + +// GetIPv6Mask 匹配获取公网 IPV6 的掩码信息 +func GetIPv6Mask() (string, error) { + interfaceName := getNetworkInterface() + if interfaceName == "" { + return "", fmt.Errorf("无法获取网络接口名称") + } + addrs, err := net.InterfaceAddrs() + if err != nil { + return "", err + } + for _, addr := range addrs { + if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipv6 := ipnet.IP.To16(); ipv6 != nil { + if !ipv6.IsLinkLocalUnicast() && !isIPv6LinkLocal(ipv6) && !isIPv6SiteLocal(ipv6) { + newIPv6 := generateNewIPv6(ipv6.String()) + addIPv6Address(interfaceName, newIPv6) + defer removeIPv6Address(interfaceName, newIPv6) + updatedAddrs, err := net.InterfaceAddrs() + if err != nil { + return "", err + } + if len(updatedAddrs) == len(addrs) { + _, bits := ipnet.Mask.Size() + return fmt.Sprintf("IPv6 子网掩码: /%d\n", bits), nil + } + for _, updatedAddr := range updatedAddrs { + if updatedIPnet, ok := updatedAddr.(*net.IPNet); ok { + if updatedIPv6 := updatedIPnet.IP.To16(); updatedIPv6 != nil { + if !isIPv6LinkLocal(updatedIPv6) && !isIPv6SiteLocal(updatedIPv6) && updatedIPv6.String() != ipv6.String() { + _, bits := updatedIPnet.Mask.Size() + return fmt.Sprintf("IPv6 子网掩码: /%d\n", bits), nil + } else if !isIPv6LinkLocal(updatedIPv6) && !isIPv6SiteLocal(updatedIPv6) && updatedIPv6.String() == ipv6.String() { + return "IPv6 子网掩码: /128", nil + } + } + } + } + } + } + } + } + return "", fmt.Errorf("无法获取公网 IPv6 地址") +} + +func getNetworkInterface() string { + ifaces, err := net.Interfaces() + if err != nil { + return "" + } + + for _, iface := range ifaces { + if strings.HasPrefix(iface.Name, "eth") || strings.HasPrefix(iface.Name, "en") { + return iface.Name + } + } + + return "" +} + +func generateNewIPv6(currentIPv6 string) string { + parts := strings.Split(currentIPv6, ":") + if len(parts) < 8 { + return "" + } + return fmt.Sprintf("%s:%s", strings.Join(parts[:7], ":"), "3") +} + +func addIPv6Address(interfaceName, ipv6Address string) { + _, err := exec.Command("ip", "addr", "add", ipv6Address+"/128", "dev", interfaceName).Output() + if err != nil { + return + } +} + +func removeIPv6Address(interfaceName, ipv6Address string) { + _, err := exec.Command("ip", "addr", "del", ipv6Address+"/128", "dev", interfaceName).Output() + if err != nil { + return + } +} + +func isIPv6LinkLocal(ip net.IP) bool { + return strings.HasPrefix(ip.String(), "fe80:") +} + +func isIPv6SiteLocal(ip net.IP) bool { + return strings.HasPrefix(ip.String(), "fec0:") +} \ No newline at end of file diff --git a/model/model.go b/model/model.go index c50b266..8c58b54 100644 --- a/model/model.go +++ b/model/model.go @@ -1,6 +1,6 @@ package model -const BasicsVersion = "v0.0.7" +const BasicsVersion = "v0.0.8" var EnableLoger bool