liscense-main.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //go:build windows
  2. // +build windows
  3. package comm
  4. import (
  5. "encoding/base64"
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "os"
  10. "path"
  11. "time"
  12. "github.com/nats-io/nats.go"
  13. )
  14. var msgbus *NatsBus
  15. // 开启liscense检测
  16. func InitLiscenseBus(bus *NatsBus) error {
  17. msgbus = bus
  18. // liscense = NewLiscenseTask()
  19. // return liscense.Run(nil)
  20. return nil
  21. }
  22. type LiscenseTask struct {
  23. Task
  24. time.Time
  25. dirty bool
  26. SK string
  27. activeObj *LiscenseActiveCode
  28. appPrivateKey []byte
  29. appKey string
  30. keyLabel []byte
  31. DataDir string
  32. }
  33. type TimeFlag struct {
  34. I string
  35. R int32
  36. LT time.Time
  37. }
  38. func NewLiscenseTask(sk string, privateKey string, label string, appKey string, dataDir string) *LiscenseTask {
  39. t := &LiscenseTask{SK: sk, appPrivateKey: []byte(privateKey), keyLabel: []byte(label), appKey: appKey, DataDir: dataDir}
  40. t.Task = NewBackgroundTask(t, nil)
  41. t.LoadLiscense()
  42. t.SubscribeActiveMsg()
  43. return t
  44. }
  45. type ActiveReq struct {
  46. ActiveCode string
  47. }
  48. type ActiveResp struct {
  49. Error error
  50. Succ bool
  51. AuthName string
  52. StartTime time.Time
  53. ExpireTime time.Time
  54. Duration int
  55. ErrorDesc string
  56. }
  57. // 加本地liscense
  58. func (t *LiscenseTask) LoadLiscense() error {
  59. datafile := path.Join(t.DataDir, "liscense")
  60. d, e := ioutil.ReadFile(datafile)
  61. if e != nil {
  62. return e
  63. }
  64. c, e := base64.StdEncoding.DecodeString(string(d))
  65. if e != nil {
  66. return e
  67. }
  68. p, e := decryptOAEP(t.appPrivateKey, c, t.keyLabel)
  69. if e != nil {
  70. return e
  71. }
  72. o := &LiscenseActiveCode{}
  73. e = json.Unmarshal(p, o)
  74. if e != nil {
  75. return e
  76. }
  77. t.activeObj = o
  78. return nil
  79. }
  80. func (t *LiscenseTask) Active(req *ActiveReq) *ActiveResp {
  81. //清除已有的激活信息
  82. infoPath := path.Join(t.DataDir, "authinfo")
  83. os.Remove(infoPath)
  84. //存储激活码
  85. datafile := path.Join(t.DataDir, "liscense")
  86. e := ioutil.WriteFile(datafile, []byte(req.ActiveCode), os.ModePerm)
  87. if e != nil {
  88. return &ActiveResp{Succ: false, Error: e}
  89. }
  90. e = t.LoadLiscense()
  91. if e != nil {
  92. os.Remove(datafile)
  93. return &ActiveResp{Succ: false, Error: fmt.Errorf("激活码无效")}
  94. }
  95. //验证当前系统时间
  96. if time.Now().Before(t.activeObj.CreateTime) {
  97. return &ActiveResp{Succ: false, Error: fmt.Errorf("系统时间无效,无法激活")}
  98. }
  99. //存储最新时间
  100. f := t.setTimeFlag()
  101. if f == nil {
  102. return &ActiveResp{Succ: false, Error: fmt.Errorf("系统时间无效")}
  103. }
  104. if t.IsLiscenseOk() {
  105. //保存激活码
  106. obj := t.activeObj
  107. ret := &ActiveResp{Succ: true, AuthName: obj.AuthName, Duration: int(obj.Duration), StartTime: obj.StartTime, ExpireTime: obj.ExpireTime}
  108. return ret
  109. }
  110. return &ActiveResp{Succ: false, Error: fmt.Errorf("激活码无效")}
  111. }
  112. func (t *LiscenseTask) UpdateAuthInfo(status string) {
  113. info := &AuthInfo{}
  114. info.AuthId = t.activeObj.AuthId
  115. info.AuthName = t.activeObj.AuthName
  116. info.Expired = t.activeObj.ExpireTime
  117. info.CreateTime = t.activeObj.CreateTime
  118. info.Duration = t.activeObj.Duration
  119. info.Status = status
  120. infoPath := path.Join(t.DataDir, "authinfo")
  121. d, e := json.Marshal(info)
  122. if e == nil {
  123. ioutil.WriteFile(infoPath, d, os.ModePerm)
  124. }
  125. }
  126. func (t *LiscenseTask) SubscribeActiveMsg() {
  127. if msgbus == nil {
  128. return
  129. }
  130. msgbus.QueueSubscribe(&SubOption{
  131. Sub: "liscense.active." + t.appKey,
  132. Obj: func() interface{} { return &ActiveReq{} },
  133. Call: func(obj interface{}, msg *nats.Msg) interface{} {
  134. req := obj.(*ActiveReq)
  135. resp := t.Active(req)
  136. if resp.Error != nil {
  137. resp.ErrorDesc = resp.Error.Error()
  138. } else {
  139. msgbus.PublishObj("liscense.active.succ", map[string]string{"appkey": t.appKey})
  140. }
  141. return resp
  142. },
  143. })
  144. }
  145. func (t *LiscenseTask) getTimeFlag() *TimeFlag {
  146. //存储最新时间
  147. tickfile := path.Join(t.DataDir, "tick")
  148. d, e := ioutil.ReadFile(tickfile)
  149. if e != nil {
  150. return nil
  151. }
  152. p, e := Decode(t.SK, string(d))
  153. if e != nil {
  154. return nil
  155. }
  156. f := &TimeFlag{}
  157. e = json.Unmarshal(p, f)
  158. if e != nil {
  159. return nil
  160. }
  161. if f.I != t.appKey {
  162. return nil
  163. }
  164. return f
  165. }
  166. func (t *LiscenseTask) setTimeFlag() *TimeFlag {
  167. //存储最新时间
  168. tickfile := path.Join(t.DataDir, "tick")
  169. f := &TimeFlag{I: t.appKey, R: radomInt(), LT: time.Now()}
  170. flag, e := json.Marshal(f)
  171. if e != nil {
  172. return nil
  173. }
  174. c, e := Encode(t.SK, flag)
  175. if e != nil {
  176. return nil
  177. }
  178. e = ioutil.WriteFile(tickfile, []byte(c), os.ModePerm)
  179. if e != nil {
  180. return nil
  181. }
  182. return f
  183. }
  184. func (t *LiscenseTask) UpdateTimeFlag() bool {
  185. f1 := t.getTimeFlag()
  186. if f1 == nil {
  187. t.dirty = true
  188. return false
  189. }
  190. if time.Now().Before(f1.LT) {
  191. t.dirty = true
  192. return false
  193. }
  194. t.dirty = false
  195. t.setTimeFlag()
  196. return true
  197. }
  198. func (t *LiscenseTask) IsLiscenseOk() bool {
  199. ready := true
  200. if t.activeObj == nil || t.activeObj.AppKey != t.appKey {
  201. ready = false
  202. }
  203. //校验系统时间
  204. if t.dirty {
  205. ready = false
  206. }
  207. if t.activeObj != nil {
  208. hash := GetDeviceHash()
  209. if t.activeObj.DeviceHashCode != hash {
  210. ready = false
  211. }
  212. }
  213. infoPath := path.Join(t.DataDir, "authinfo")
  214. if !ready {
  215. os.Remove(infoPath)
  216. return false
  217. }
  218. isOk := false
  219. s := AuthStatusNoActive
  220. if t.activeObj.Duration == -1 {
  221. isOk = true
  222. s = AuthStatusOK
  223. } else {
  224. n := time.Now()
  225. if n.Before(t.activeObj.ExpireTime) && n.After(t.activeObj.StartTime) {
  226. isOk = true
  227. s = AuthStatusOK
  228. } else {
  229. s = AuthStatusExpired
  230. }
  231. if !isOk {
  232. os.Remove(infoPath)
  233. }
  234. }
  235. t.UpdateAuthInfo(s)
  236. return isOk
  237. }
  238. func (t *LiscenseTask) Loop() bool {
  239. t.UpdateTimeFlag()
  240. time.Sleep(5 * time.Minute) //5分钟更新一下
  241. return false
  242. }
  243. func (t *LiscenseTask) OnAfterRun() error {
  244. return nil
  245. }
  246. func (t *LiscenseTask) OnBeforeRun() error {
  247. // if t.activeObj == nil {
  248. // return fmt.Errorf("应用未激活")
  249. // }
  250. return nil
  251. }