99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||
//go:build plus
|
||
|
||
package plus
|
||
|
||
import (
|
||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||
plusutils "github.com/TeaOSLab/EdgePlus/pkg/utils"
|
||
"github.com/iwind/TeaGo/lists"
|
||
"net"
|
||
"sync"
|
||
)
|
||
|
||
// 已经开通的组件
|
||
var plusLocker = sync.Mutex{}
|
||
var plusComponents = []string{}
|
||
|
||
var PlusEdition = ""
|
||
|
||
// UpdateComponents 修改开通的组件
|
||
func UpdateComponents(edition string, components []string) {
|
||
plusLocker.Lock()
|
||
PlusEdition = edition
|
||
plusComponents = components
|
||
plusLocker.Unlock()
|
||
}
|
||
|
||
// AllowComponent 检查是否允许使用某个组件
|
||
func AllowComponent(component ComponentCode) bool {
|
||
if !teaconst.IsPlus {
|
||
return false
|
||
}
|
||
|
||
// 允许不指定组件,表示只要是plus用户就可以
|
||
if len(component) == 0 {
|
||
return true
|
||
}
|
||
|
||
plusLocker.Lock()
|
||
defer plusLocker.Unlock()
|
||
|
||
if len(plusComponents) == 0 {
|
||
return false
|
||
}
|
||
|
||
for _, c := range plusComponents {
|
||
if c == "*" || c == component {
|
||
// 如果不是最新版本的授权,则忽略一些权限
|
||
if c == "*" && (len(PlusEdition) == 0 || plusutils.CompareEdition(PlusEdition, plusutils.EditionPro) < 0) && component == ComponentCodeAntiDDoS {
|
||
continue
|
||
}
|
||
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// ValidateMac 校验MAC地址
|
||
func ValidateMac(mac string) bool {
|
||
if len(mac) == 0 || mac == "*" {
|
||
return true
|
||
}
|
||
|
||
for _, addr := range FindAllMacAddresses() {
|
||
if addr == mac {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// FindAllMacAddresses 获取MAC地址
|
||
func FindAllMacAddresses() []string {
|
||
var macAddresses = []string{}
|
||
interfaces, _ := net.Interfaces()
|
||
for _, i := range interfaces {
|
||
var addr = i.HardwareAddr.String()
|
||
if len(addr) == 0 {
|
||
continue
|
||
}
|
||
if lists.ContainsString(macAddresses, addr) {
|
||
continue
|
||
}
|
||
if i.Flags&net.FlagLoopback == 1 {
|
||
continue
|
||
}
|
||
if i.Flags&net.FlagUp == 0 {
|
||
continue
|
||
}
|
||
ipAddrs, _ := i.Addrs()
|
||
if len(ipAddrs) == 0 {
|
||
continue
|
||
}
|
||
macAddresses = append(macAddresses, addr)
|
||
}
|
||
return macAddresses
|
||
}
|