package utils import ( "fmt" "github.com/spf13/cast" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "math/rand" "slices" "strings" "wireguard-dashboard/client" ) type wireguard struct{} func Wireguard() wireguard { return wireguard{} } // GetSpecClient // @description: 获取指定客户端链接信息 // @receiver wireguard // @param pk // @return *wgtypes.Peer // @return error func (wireguard) GetSpecClient(pk string) (*wgtypes.Peer, error) { device, err := client.WireguardClient.Devices() if err != nil { return nil, err } for _, v := range device { for _, p := range v.Peers { if p.PublicKey.String() == pk { return &p, nil } } } return nil, nil } // GenerateClientIP // @description: 生成客户端IP // @receiver wireguard // @param serverIP // @param rule // @return string func (w wireguard) GenerateClientIP(serverIP, rule string, assignedIPS ...string) string { // 再次拆分,只取最后一段 suffix := w.GetIPSuffix(serverIP) prefix := w.GetIPPrefix(serverIP) // 如果是随机模式,则需要结尾数字小于等于 255,并且生成的数字不能是已分配当中任何一个 // 如果是自动模式,只需要是已经分配的最后一位自增 switch rule { case "RANDOM": suffix = w.random(assignedIPS...) case "AUTO": switch len(assignedIPS) { case 1: suffix = w.GetIPSuffix(assignedIPS[0]) case 2: suffix = w.GetIPSuffix(assignedIPS[1]) } suffix = cast.ToString(cast.ToInt64(suffix) + 1) } return fmt.Sprintf("%s.%s", prefix, suffix) } // random // @description: 随机模式 // @receiver w // @param assignedIPS // @return string func (w wireguard) random(assignedIPS ...string) string { randomNumber := rand.Int63n(256) var assuffixIP []int64 for _, v := range assignedIPS { assuffixIP = append(assuffixIP, cast.ToInt64(strings.Split(v, ".")[3])) } if slices.Contains(assuffixIP, randomNumber) { return w.random(assignedIPS...) } return cast.ToString(randomNumber) } // GetIPSuffix // @description: 获取IP后缀 [10.10.10.23] 这里只获取23 // @receiver w // @param ip // @return string func (w wireguard) GetIPSuffix(ip string) string { if strings.Contains(ip, "/") { ip = strings.Split(ip, "/")[0] } // 再次拆分,只取最后一段 return strings.Split(ip, ".")[3] } // GetIPPrefix // @description: 获取IP前缀[10.10.10.23] 这里只获取 10.10.10 // @receiver w // @param ip // @return string func (w wireguard) GetIPPrefix(ip string) string { if strings.Contains(ip, "/") { ip = strings.Split(ip, "/")[0] } return strings.Join(strings.Split(ip, ".")[:3], ".") }