-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
138 lines (121 loc) · 3.11 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package client
import (
"crypto/sha1"
"hash"
"os"
"path"
)
type ActivationHandler interface {
IsLockVal() bool
ItemKey() string
GetValFn() (any, error)
CheckFn(data *LicenseInfo, v any) error
}
type LicenseCli interface {
GenerateActivationCode(opts ...GenerateOption) ([]byte, error)
ActivateLicense(licenseCode []byte) (*LicenseInfo, error)
VerifyLicense() bool
GetLicenseInfo() (*LicenseInfo, error)
}
type client struct {
subject string
isLockSubject bool
description string
rsaKey RSAKeyConfig
licenseFileSavePath string
licenseFileName string
activationHandlerMap map[string]ActivationHandler
activationEncryptFunc ActivationEncryptFunc
licenseDecryptFunc LicenseDecryptFunc
h hash.Hash
}
type ActivationEncryptFunc func(plainText []byte, publicKey []byte) ([]byte, error)
type LicenseDecryptFunc func(cipherByte []byte, privateKey []byte) ([]byte, error)
type Option func(*client)
func WithActivationEncryptFunc(fn ActivationEncryptFunc) Option {
return func(config *client) {
config.activationEncryptFunc = fn
}
}
func WithLicenseDecryptFunc(fn LicenseDecryptFunc) Option {
return func(config *client) {
config.licenseDecryptFunc = fn
}
}
func WithOAEPHash(h hash.Hash) Option {
return func(config *client) {
config.h = h
}
}
func WithAddActivationHandler(handler ActivationHandler) Option {
return func(config *client) {
config.activationHandlerMap[handler.ItemKey()] = handler
}
}
func WithActivationHandlerMap(handlerMap map[string]ActivationHandler) Option {
return func(config *client) {
config.activationHandlerMap = handlerMap
}
}
func WithIsLockSubject(isLock bool) Option {
return func(config *client) {
config.isLockSubject = isLock
}
}
func WithDescription(description string) Option {
return func(config *client) {
config.description = description
}
}
func WithLicenseFileSavePath(path string) Option {
return func(config *client) {
config.licenseFileSavePath = path
}
}
func WithLicenseFileName(name string) Option {
return func(config *client) {
config.licenseFileName = name
}
}
type RSAKeyConfig struct {
ActivationEncryptKey []byte
LicenseDecryptKey []byte
}
func NewLicenseCli(rsaKey RSAKeyConfig, subject string, opts ...Option) (LicenseCli, error) {
c := new(client)
c.subject = subject
c.rsaKey = rsaKey
c.activationHandlerMap = map[string]ActivationHandler{
SystemOS_ItemKey: NewSystemOSInfo(),
CPUInfo_ItemKey: NewCPUInfo(),
ProgramPath_ItemKey: NewProgramPath(),
}
for _, o := range opts {
o(c)
}
if c.licenseFileName == "" {
c.licenseFileName = "license.key"
}
if len(c.activationHandlerMap) == 0 {
return nil, ActivationHandlerErr
}
if c.h == nil {
c.h = sha1.New()
}
if c.activationEncryptFunc == nil {
c.activationEncryptFunc = c.encrypt
}
if c.licenseDecryptFunc == nil {
c.licenseDecryptFunc = c.decrypt
}
if c.licenseFileSavePath == "" {
c.licenseFileSavePath = c.licenseFileName
} else {
err := os.MkdirAll(c.licenseFileSavePath, os.ModeDir)
if err != nil {
return nil, err
}
c.licenseFileSavePath = path.Join(c.licenseFileSavePath, c.licenseFileName)
}
return c, nil
}