@ -1,98 +1,64 @@
package libs
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
)
// System Info 定义系统信息结构体
type System Info struct {
// UserOsInfo 定义系统信息结构体
type UserOsInfo struct {
MAC string ` json:"mac" `
CPUUsage float64 ` json:"cpu_usage" `
OS string ` json:"os" `
Arch string ` json:"arch" `
MemoryTotal string ` json:"memory_total" `
AppName string ` json:"app_name" `
Hostname string ` json:"hostname" `
TopType string ` json:"toptype" `
UseType string ` json:"usetype" `
SourceType string ` json:"sourcetype" `
}
// GetSystemInfo 获取系统信息
// GetSystemInfo 获取系统综合信息
func GetSystemInfo ( ) map [ string ] string {
info := make ( map [ string ] string )
// 获取本机IP地址
ip , err := getIPAddress ( )
if err == nil {
info [ "ip" ] = ip
} else {
info [ "ip" ] = ""
}
func GenerateSystemInfo ( ) UserOsInfo {
info := UserOsInfo { }
// 获取MAC地址
mac , err := getMACAddress ( )
if err == nil {
info [ "mac" ] = mac
} else {
info [ "mac" ] = ""
}
// 获取CPU信息
cpuPercent , err := cpu . Percent ( time . Second , false )
if err == nil {
info [ "cpu" ] = strconv . FormatFloat ( cpuPercent [ 0 ] , 'f' , 2 , 64 ) + "%"
} else {
info [ "cpu" ] = ""
}
// 获取内存信息
memInfo , err := mem . VirtualMemory ( )
if err == nil {
info [ "memory_total" ] = byteCountSI ( memInfo . Total )
info [ "memory_used" ] = byteCountSI ( memInfo . Used )
info [ "memory_free" ] = byteCountSI ( memInfo . Free )
info . MAC = mac
} else {
info [ "memory_total" ] = ""
info [ "memory_used" ] = ""
info [ "memory_free" ] = ""
info . MAC = ""
}
// 获取主机名
hostname , err := os . Hostname ( )
if err == nil {
info [ "hostname" ] = hostname
info . Hostname = hostname
} else {
info [ "hostname" ] = ""
info . Hostname = ""
}
// 获取操作系统和架构信息
info [ "os" ] = runtime . GOOS
info [ "arch" ] = runtime . GOARCH
info . OS = runtime . GOOS
info . Arch = runtime . GOARCH
// 获取系统运行时间
uptime , err := host . Uptime ( )
if err == nil {
info [ "uptime_seconds" ] = strconv . FormatUint ( uptime , 10 )
} else {
info [ "uptime_seconds" ] = ""
info . AppName = "godoos"
info . TopType = os . Getenv ( "GODOTOPTYPE" )
if info . TopType == "" {
info . TopType = "web"
}
info . UseType = "person"
info . SourceType = "open"
return info
}
// g etIPAddress 获取本机IP地址
func g etIPAddress( ) ( string , error ) {
// G etIPAddress 获取本机IP地址
func G etIPAddress( ) ( string , error ) {
addrs , err := net . InterfaceAddrs ( )
if err != nil {
return "" , err
@ -118,128 +84,40 @@ func getMACAddress() (string, error) {
if err != nil {
return "" , err
}
var macs string
// 记录所有接口的信息
for _ , iface := range ifaces {
if iface . Flags & net . FlagUp != 0 && ( strings . HasPrefix ( iface . Name , "en" ) || strings . HasPrefix ( iface . Name , "eth" ) ) {
return iface . HardwareAddr . String ( ) , nil
if iface . HardwareAddr != nil {
macs += iface . HardwareAddr . String ( )
}
}
return "" , fmt . Errorf ( "no active Ethernet interface found" )
//返回md5加密数据
return Md5Encrypt ( macs ) , nil
}
// byteCountSI 格式化字节大小为可读字符串
func byteCountSI ( b uint64 ) string {
const unit = 1000
if b < unit {
return fmt . Sprintf ( "%d B" , b )
}
div , exp := int64 ( unit ) , 0
for n := b / unit ; n >= unit ; n /= unit {
div *= unit
exp ++
}
return fmt . Sprintf ( "%.1f %cB" , float64 ( b ) / float64 ( div ) , "kMGTPE" [ exp ] )
}
// GenerateBase64Info 生成基于mac、cpu、os、arch信息的Base64编码字符串
func GenerateBase64Info ( ) ( string , error ) {
// 获取必要的系统信息
cpuPercent , err := cpu . Percent ( time . Second , false )
if err != nil {
return "" , err
}
memInfo , err := mem . VirtualMemory ( )
if err != nil {
return "" , err
}
// 获取MAC地址
mac , err := getMACAddress ( )
if err == nil {
return "" , err
func Md5Encrypt ( s string ) string {
hasher := md5 . New ( )
hasher . Write ( [ ] byte ( s ) )
return hex . EncodeToString ( hasher . Sum ( nil ) )
}
// GetSystemInfo 生成基于mac、os、arch信息的Base64编码字符串
func GetSystemInfo ( ) ( string , error ) {
// 构造系统信息对象
systemInfo := SystemInfo {
MAC : mac ,
CPUUsage : cpuPercent [ 0 ] ,
OS : runtime . GOOS ,
Arch : runtime . GOARCH ,
MemoryTotal : byteCountSI ( memInfo . Total ) ,
lineseInfo , ok := GetConfig ( "osInfo" )
if ! ok {
return "" , fmt . Errorf ( "未找到osInfo配置" )
}
systemInfo , ok := lineseInfo . ( UserOsInfo )
if ! ok {
return "" , fmt . Errorf ( "osInfo配置错误" )
}
// 将系统信息序列化为JSON字符串
jsonBytes , err := json . Marshal ( systemInfo )
if err != nil {
return "" , err
}
// 对JSON字符串进行Base64编码
encodedInfo := base64 . StdEncoding . EncodeToString ( jsonBytes )
return encodedInfo , nil
}
// DecryptWithAES 使用AES解密数据
func DecryptWithAES ( ciphertext [ ] byte , key [ ] byte ) ( [ ] byte , error ) {
block , err := aes . NewCipher ( key )
if err != nil {
return nil , err
}
if len ( ciphertext ) < aes . BlockSize {
return nil , fmt . Errorf ( "ciphertext too short" )
}
iv := ciphertext [ : aes . BlockSize ]
ciphertext = ciphertext [ aes . BlockSize : ]
stream := cipher . NewCFBDecrypter ( block , iv )
stream . XORKeyStream ( ciphertext , ciphertext )
return ciphertext , nil
}
// VerifyAndDecrypt 验证时间戳并解密数据
func VerifyAndDecrypt ( encodedInfo string , startTime time . Time , endTime time . Time , flag string ) ( * SystemInfo , error ) {
// Base64解码
ciphertext , err := base64 . StdEncoding . DecodeString ( encodedInfo )
if err != nil {
return nil , err
}
// 生成AES密钥
aesKey , err := GenerateAESKey ( startTime , endTime , flag )
if err != nil {
return nil , err
}
// 解密数据
decryptedBytes , err := DecryptWithAES ( ciphertext , aesKey )
if err != nil {
return nil , err
}
// 反序列化JSON为SystemInfo
var systemInfo SystemInfo
if err := json . Unmarshal ( decryptedBytes , & systemInfo ) ; err != nil {
return nil , err
}
// 获取当前系统MAC地址
currentMAC , err := getMACAddress ( )
if err != nil {
return nil , fmt . Errorf ( "failed to get current MAC address: %w" , err )
}
// 检查解密后的MAC地址是否与当前系统MAC地址一致
if systemInfo . MAC != currentMAC {
return nil , fmt . Errorf ( "decrypted MAC address does not match the current system's MAC address" )
}
if systemInfo . OS != runtime . GOOS {
return nil , fmt . Errorf ( "decrypted GOOS does not match the current system's runtime.GOOS" )
}
// 验证时间有效性
currentTime := time . Now ( )
if currentTime . Before ( startTime ) || currentTime . After ( endTime ) {
return nil , fmt . Errorf ( "the provided time window is invalid" )
}
return & systemInfo , nil
}