93 lines
2.1 KiB
Go
93 lines
2.1 KiB
Go
package license
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var key = []byte("914a3f206e294edc96b79818ad61d768")
|
|
|
|
type License struct {
|
|
ProductID string `json:"product_id"`
|
|
UniqueID string `json:"unique_id"`
|
|
Expiry string `json:"expiry"`
|
|
}
|
|
|
|
func decrypt(encryptedText string) (string, error) {
|
|
cipherText, err := base64.StdEncoding.DecodeString(encryptedText)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
cfb := cipher.NewCFBDecrypter(block, key[:aes.BlockSize])
|
|
plaintext := make([]byte, len(cipherText))
|
|
cfb.XORKeyStream(plaintext, cipherText)
|
|
return string(plaintext), nil
|
|
}
|
|
|
|
func getMachineID() (string, error) {
|
|
data, err := ioutil.ReadFile("/etc/machine-id")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(data)), nil
|
|
}
|
|
|
|
func getDiskUUIDs() (string, error) {
|
|
output, err := exec.Command("blkid").Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(output), nil
|
|
}
|
|
|
|
func GetUniqueID() (string, error) {
|
|
machineID, err := getMachineID()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
diskUUIDs, err := getDiskUUIDs()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
combinedID := machineID + diskUUIDs
|
|
hash := sha256.Sum256([]byte(combinedID))
|
|
return fmt.Sprintf("%x", hash), nil
|
|
}
|
|
|
|
func ValidateLicense(encryptedLicense string) (bool, error) {
|
|
layout := "2006-01-02"
|
|
now := time.Now()
|
|
decrypted, err := decrypt(encryptedLicense)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
var license License
|
|
err = json.Unmarshal([]byte(decrypted), &license)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if license.ProductID != "rtp-app" {
|
|
return false, err
|
|
}
|
|
date, err := time.Parse(layout, license.Expiry)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if date.Before(now) {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|