mirror of
https://github.com/oneclickvirt/basics.git
synced 2025-10-06 17:17:15 +08:00
v0.0.8 新增IPV6子网掩码检测
This commit is contained in:
2
.github/workflows/ci.yaml
vendored
2
.github/workflows/ci.yaml
vendored
@@ -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:
|
||||
|
@@ -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
|
||||
|
||||
下载及安装
|
||||
|
@@ -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...")
|
||||
|
97
ipv6/ipv6.go
Normal file
97
ipv6/ipv6.go
Normal file
@@ -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:")
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
package model
|
||||
|
||||
const BasicsVersion = "v0.0.7"
|
||||
const BasicsVersion = "v0.0.8"
|
||||
|
||||
var EnableLoger bool
|
||||
|
||||
|
Reference in New Issue
Block a user