Skip to content

Commit

Permalink
v0.0.8 新增IPV6子网掩码检测
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritLHLS committed Nov 7, 2024
1 parent 612a1a4 commit 66e3d32
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: |
git config --global user.name 'github-actions'
git config --global user.email '[email protected]'
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:
Expand Down
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

下载及安装
Expand Down
8 changes: 7 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...")
Expand Down
97 changes: 97 additions & 0 deletions ipv6/ipv6.go
Original file line number Diff line number Diff line change
@@ -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:")
}
2 changes: 1 addition & 1 deletion model/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model

const BasicsVersion = "v0.0.7"
const BasicsVersion = "v0.0.8"

var EnableLoger bool

Expand Down

0 comments on commit 66e3d32

Please sign in to comment.