trial.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //go:build ee
  2. // +build ee
  3. package pro
  4. import (
  5. "crypto/rand"
  6. "encoding/json"
  7. "errors"
  8. "time"
  9. "github.com/gravitl/netmaker/database"
  10. "github.com/gravitl/netmaker/logger"
  11. "github.com/gravitl/netmaker/logic"
  12. "github.com/gravitl/netmaker/models"
  13. "github.com/gravitl/netmaker/netclient/ncutils"
  14. "github.com/gravitl/netmaker/servercfg"
  15. "golang.org/x/crypto/nacl/box"
  16. )
  17. type TrialInfo struct {
  18. PrivKey []byte `json:"priv_key"`
  19. PubKey []byte `json:"pub_key"`
  20. Secret []byte `json:"secret"`
  21. }
  22. func addTrialLicenseHook() {
  23. logic.HookManagerCh <- models.HookDetails{
  24. Hook: TrialLicenseHook,
  25. Interval: time.Hour,
  26. }
  27. }
  28. type TrialDates struct {
  29. TrialStartedAt time.Time `json:"trial_started_at"`
  30. TrialEndsAt time.Time `json:"trial_ends_at"`
  31. }
  32. const trial_table_name = "trial"
  33. const trial_data_key = "trialdata"
  34. // stores trial end date
  35. func initTrial() error {
  36. telData, err := logic.FetchTelemetryData()
  37. if err != nil {
  38. return err
  39. }
  40. if telData.Hosts > 0 || telData.Networks > 0 || telData.Users > 0 {
  41. return nil // database is already populated, so skip creating trial
  42. }
  43. database.CreateTable(trial_table_name)
  44. records, err := database.FetchRecords(trial_table_name)
  45. if err != nil && !database.IsEmptyRecord(err) {
  46. return err
  47. }
  48. if len(records) > 0 {
  49. return nil
  50. }
  51. // setup encryption keys
  52. trafficPubKey, trafficPrivKey, err := box.GenerateKey(rand.Reader) // generate traffic keys
  53. if err != nil {
  54. return err
  55. }
  56. tPriv, err := ncutils.ConvertKeyToBytes(trafficPrivKey)
  57. if err != nil {
  58. return err
  59. }
  60. tPub, err := ncutils.ConvertKeyToBytes(trafficPubKey)
  61. if err != nil {
  62. return err
  63. }
  64. trialDates := TrialDates{
  65. TrialStartedAt: time.Now(),
  66. TrialEndsAt: time.Now().Add(time.Hour * 24 * 14),
  67. }
  68. t := TrialInfo{
  69. PrivKey: tPriv,
  70. PubKey: tPub,
  71. }
  72. tel, err := logic.FetchTelemetryRecord()
  73. if err != nil {
  74. return err
  75. }
  76. trialDatesData, err := json.Marshal(trialDates)
  77. if err != nil {
  78. return err
  79. }
  80. telePubKey, err := ncutils.ConvertBytesToKey(tel.TrafficKeyPub)
  81. if err != nil {
  82. return err
  83. }
  84. trialDatesSecret, err := ncutils.BoxEncrypt(trialDatesData, telePubKey, trafficPrivKey)
  85. if err != nil {
  86. return err
  87. }
  88. t.Secret = trialDatesSecret
  89. trialData, err := json.Marshal(t)
  90. if err != nil {
  91. return err
  92. }
  93. err = database.Insert(trial_data_key, string(trialData), trial_table_name)
  94. if err != nil {
  95. return err
  96. }
  97. return nil
  98. }
  99. // TrialLicenseHook - hook func to check if pro trial has ended
  100. func TrialLicenseHook() error {
  101. endDate, err := getTrialEndDate()
  102. if err != nil {
  103. logger.FatalLog0("failed to trial end date", err.Error())
  104. }
  105. if time.Now().After(endDate) {
  106. logger.Log(0, "***IMPORTANT: Your Trial Has Ended, to continue using pro version, please visit https://app.netmaker.io/ and create on-prem tenant to obtain a license***\nIf you wish to downgrade to community version, please run this command `/root/nm-quick.sh -d`")
  107. err = errors.New("your trial has ended")
  108. servercfg.ErrLicenseValidation = err
  109. return err
  110. }
  111. return nil
  112. }
  113. // get trial date
  114. func getTrialEndDate() (time.Time, error) {
  115. record, err := database.FetchRecord(trial_table_name, trial_data_key)
  116. if err != nil {
  117. return time.Time{}, err
  118. }
  119. var trialInfo TrialInfo
  120. err = json.Unmarshal([]byte(record), &trialInfo)
  121. if err != nil {
  122. return time.Time{}, err
  123. }
  124. tel, err := logic.FetchTelemetryRecord()
  125. if err != nil {
  126. return time.Time{}, err
  127. }
  128. telePrivKey, err := ncutils.ConvertBytesToKey(tel.TrafficKeyPriv)
  129. if err != nil {
  130. return time.Time{}, err
  131. }
  132. trialPubKey, err := ncutils.ConvertBytesToKey(trialInfo.PubKey)
  133. if err != nil {
  134. return time.Time{}, err
  135. }
  136. // decrypt secret
  137. secretDecrypt, err := ncutils.BoxDecrypt(trialInfo.Secret, trialPubKey, telePrivKey)
  138. if err != nil {
  139. return time.Time{}, err
  140. }
  141. trialDates := TrialDates{}
  142. err = json.Unmarshal(secretDecrypt, &trialDates)
  143. if err != nil {
  144. return time.Time{}, err
  145. }
  146. if trialDates.TrialEndsAt.IsZero() {
  147. return time.Time{}, errors.New("invalid date")
  148. }
  149. return trialDates.TrialEndsAt, nil
  150. }