license_generate.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. package license
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. "text/template"
  11. "time"
  12. )
  13. func GenerateLicense(pJ ProjectInfo, eI EnvironmentInfo, lI LicenseInfo) []byte {
  14. app := LicenseApplication{
  15. ProjectInfo: pJ,
  16. EnvironmentInfo: eI,
  17. LicenseInfo: lI,
  18. FeatureInfo: FeatureInfo{
  19. HA: true,
  20. Hint: true,
  21. FullTextIndex: true,
  22. FunctionIndex: true,
  23. BatchMode: true,
  24. PlSql: true,
  25. AddNode: false,
  26. AutoPartition: true,
  27. ResourceQuota: true,
  28. NetChannel: 0,
  29. DataAutoRebalance: true,
  30. BlackWhiteList: true,
  31. Mac: true,
  32. ColPrivilege: true,
  33. TransmitEncrypt: true,
  34. StoreEncrypt: true,
  35. BackupEncrypt: true,
  36. AuditMode: 0,
  37. GlobalBinlogNode: 0,
  38. Dblink: true,
  39. Json: true,
  40. Spatial: true,
  41. MaxParallel: 0,
  42. StreamLoad: true,
  43. },
  44. ServiceCapacity: ServiceCapacity{
  45. InstanceNum: 0,
  46. TableRows: 0,
  47. ConnectNum: 0,
  48. StoreCapacity: 0,
  49. MaxNodeNum: 0,
  50. ListenChannel: 0,
  51. },
  52. }
  53. fileName, _ := GenerateLicenseCofingFile(app)
  54. ExecLicenseProgram(fileName)
  55. license := ReadLicenseFile(fileName)
  56. return license
  57. }
  58. // 分布式
  59. func GenerateClusterTmpl() string {
  60. const tmpl = `
  61. #用户项目信息
  62. project_name = {{.ProjectInfo.ProjectName}}
  63. user_name = {{.ProjectInfo.UserName}}
  64. user_addr = {{.ProjectInfo.UserAddr}}
  65. serial_number = {{.ProjectInfo.SerialNumber}}
  66. #环境信息
  67. cpu_sn = {{.EnvironmentInfo.CpuSN}}
  68. baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
  69. mac_addr = {{.EnvironmentInfo.MacAddr}}
  70. disk_id = {{.EnvironmentInfo.DiskID}}
  71. ip_addr = {{.EnvironmentInfo.IPAddr}}
  72. #License信息
  73. gen_date = {{.LicenseInfo.GenDate}}
  74. expire_date = {{.LicenseInfo.ExpireDate}}
  75. license_type = {{.LicenseInfo.LicenseType}}
  76. license_version = {{.LicenseInfo.LicenseVersion}}
  77. hard_type = {{.LicenseInfo.HardType}}
  78. #企业版
  79. ha = true
  80. hint = true
  81. full_text_index = true
  82. function_index = true
  83. batch_mode = true
  84. pl_sql = true
  85. #分布式版
  86. add_node = true
  87. auto_partition = true
  88. resource_quota = true
  89. net_channel = 0
  90. data_auto_rebalance = true
  91. #其他组件
  92. black_white_list = true
  93. mac = true
  94. col_privilege = true
  95. transmit_encrypt = true
  96. store_encrypt = true
  97. backup_encrypt = true
  98. audit_mode = 0
  99. global_binlog_node = 0
  100. dblink = true
  101. json = true
  102. spatial = true
  103. max_parallel = 0
  104. stream_load = true
  105. #服务能力
  106. instance_num = 0
  107. table_rows = 0
  108. connect_num = 0
  109. store_capacity = 0
  110. max_node_num = 0
  111. listen_channel = 0
  112. `
  113. return tmpl
  114. }
  115. // 企业版
  116. func GenerateEnterpriseTmpl() string {
  117. const tmpl = `
  118. #用户项目信息
  119. project_name = {{.ProjectInfo.ProjectName}}
  120. user_name = {{.ProjectInfo.UserName}}
  121. user_addr = {{.ProjectInfo.UserAddr}}
  122. serial_number = {{.ProjectInfo.SerialNumber}}
  123. #环境信息
  124. cpu_sn = {{.EnvironmentInfo.CpuSN}}
  125. baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
  126. mac_addr = {{.EnvironmentInfo.MacAddr}}
  127. disk_id = {{.EnvironmentInfo.DiskID}}
  128. ip_addr = {{.EnvironmentInfo.IPAddr}}
  129. #License信息
  130. gen_date = {{.LicenseInfo.GenDate}}
  131. expire_date = {{.LicenseInfo.ExpireDate}}
  132. license_type = {{.LicenseInfo.LicenseType}}
  133. license_version = {{.LicenseInfo.LicenseVersion}}
  134. hard_type = {{.LicenseInfo.HardType}}
  135. #企业版
  136. ha = true
  137. hint = true
  138. full_text_index = true
  139. function_index = true
  140. batch_mode = true
  141. pl_sql = true
  142. #分布式版
  143. add_node = false
  144. auto_partition = true
  145. resource_quota = true
  146. net_channel = 0
  147. data_auto_rebalance = true
  148. #其他组件
  149. black_white_list = true
  150. mac = true
  151. col_privilege = true
  152. transmit_encrypt = true
  153. store_encrypt = true
  154. backup_encrypt = true
  155. audit_mode = 0
  156. global_binlog_node = 0
  157. dblink = true
  158. json = true
  159. spatial = true
  160. max_parallel = 0
  161. stream_load = true
  162. #服务能力
  163. instance_num = 0
  164. table_rows = 0
  165. connect_num = 0
  166. store_capacity = 0
  167. max_node_num = 0
  168. listen_channel = 0
  169. `
  170. return tmpl
  171. }
  172. // 标准版standard
  173. func GenerateStandardTmpl() string {
  174. const tmpl = `
  175. #用户项目信息
  176. project_name = {{.ProjectInfo.ProjectName}}
  177. user_name = {{.ProjectInfo.UserName}}
  178. user_addr = {{.ProjectInfo.UserAddr}}
  179. serial_number = {{.ProjectInfo.SerialNumber}}
  180. #环境信息
  181. cpu_sn = {{.EnvironmentInfo.CpuSN}}
  182. baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
  183. mac_addr = {{.EnvironmentInfo.MacAddr}}
  184. disk_id = {{.EnvironmentInfo.DiskID}}
  185. ip_addr = {{.EnvironmentInfo.IPAddr}}
  186. #License信息
  187. gen_date = {{.LicenseInfo.GenDate}}
  188. expire_date = {{.LicenseInfo.ExpireDate}}
  189. license_type = {{.LicenseInfo.LicenseType}}
  190. license_version = {{.LicenseInfo.LicenseVersion}}
  191. hard_type = {{.LicenseInfo.HardType}}
  192. #企业版
  193. ha = true
  194. hint = true
  195. full_text_index = true
  196. function_index = true
  197. batch_mode = true
  198. pl_sql = true
  199. #分布式版
  200. add_node = false
  201. auto_partition = true
  202. resource_quota = true
  203. net_channel = 0
  204. data_auto_rebalance = true
  205. #其他组件
  206. black_white_list = true
  207. mac = true
  208. col_privilege = true
  209. transmit_encrypt = true
  210. store_encrypt = true
  211. backup_encrypt = true
  212. audit_mode = 0
  213. global_binlog_node = 0
  214. dblink = true
  215. json = true
  216. spatial = true
  217. max_parallel = 0
  218. stream_load = true
  219. #服务能力
  220. instance_num = 0
  221. table_rows = 0
  222. connect_num = 0
  223. store_capacity = 0
  224. max_node_num = 0
  225. listen_channel = 0
  226. `
  227. return tmpl
  228. }
  229. // 敏捷版本
  230. func GenerateLiteTmpl() string {
  231. const tmpl = `
  232. #用户项目信息
  233. project_name = {{.ProjectInfo.ProjectName}}
  234. user_name = {{.ProjectInfo.UserName}}
  235. user_addr = {{.ProjectInfo.UserAddr}}
  236. serial_number = {{.ProjectInfo.SerialNumber}}
  237. #环境信息
  238. cpu_sn = {{.EnvironmentInfo.CpuSN}}
  239. baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
  240. mac_addr = {{.EnvironmentInfo.MacAddr}}
  241. disk_id = {{.EnvironmentInfo.DiskID}}
  242. ip_addr = {{.EnvironmentInfo.IPAddr}}
  243. #License信息
  244. gen_date = {{.LicenseInfo.GenDate}}
  245. expire_date = {{.LicenseInfo.ExpireDate}}
  246. license_type = {{.LicenseInfo.LicenseType}}
  247. license_version = {{.LicenseInfo.LicenseVersion}}
  248. hard_type = {{.LicenseInfo.HardType}}
  249. #功能列表
  250. ha = true
  251. hint = true
  252. full_text_index = true
  253. function_index = true
  254. batch_mode = true
  255. pl_sql = true
  256. auto_partition = true
  257. net_channel = 0
  258. col_privilege = true
  259. transmit_encrypt = true
  260. store_encrypt = true
  261. backup_encrypt = true
  262. global_binlog_node = 0
  263. json = true
  264. spatial = true
  265. max_parallel = 0
  266. stream_load = true
  267. instance_num = 0
  268. table_rows = 0
  269. connect_num = 0
  270. store_capacity = 0
  271. max_node_num = 0
  272. listen_channel = 0
  273. `
  274. return tmpl
  275. }
  276. // 个人版本
  277. func GeneratePersonTmpl() string {
  278. const tmpl = `
  279. #用户项目信息
  280. project_name = {{.ProjectInfo.ProjectName}}
  281. user_name = {{.ProjectInfo.UserName}}
  282. user_addr = {{.ProjectInfo.UserAddr}}
  283. serial_number = {{.ProjectInfo.SerialNumber}}
  284. #环境信息
  285. cpu_sn = {{.EnvironmentInfo.CpuSN}}
  286. baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
  287. mac_addr = {{.EnvironmentInfo.MacAddr}}
  288. disk_id = {{.EnvironmentInfo.DiskID}}
  289. ip_addr = {{.EnvironmentInfo.IPAddr}}
  290. #License信息
  291. gen_date = {{.LicenseInfo.GenDate}}
  292. expire_date = {{.LicenseInfo.ExpireDate}}
  293. license_type = {{.LicenseInfo.LicenseType}}
  294. license_version = {{.LicenseInfo.LicenseVersion}}
  295. hard_type = {{.LicenseInfo.HardType}}
  296. #企业版
  297. ha = true
  298. hint = true
  299. full_text_index = true
  300. function_index = true
  301. batch_mode = true
  302. pl_sql = true
  303. #分布式版
  304. add_node = false
  305. auto_partition = true
  306. resource_quota = true
  307. net_channel = 0
  308. data_auto_rebalance = true
  309. #其他组件
  310. black_white_list = true
  311. mac = true
  312. col_privilege = true
  313. transmit_encrypt = true
  314. store_encrypt = true
  315. backup_encrypt = true
  316. audit_mode = 0
  317. global_binlog_node = 0
  318. dblink = true
  319. json = true
  320. spatial = true
  321. max_parallel = 0
  322. stream_load = true
  323. #服务能力
  324. instance_num = 0
  325. table_rows = 0
  326. connect_num = 0
  327. store_capacity = 0
  328. max_node_num = 0
  329. listen_channel = 0
  330. `
  331. return tmpl
  332. }
  333. func GenerateLicenseCofingFile(app LicenseApplication) (string, error) {
  334. var tmpl string
  335. switch app.LicenseInfo.LicenseType {
  336. case 4: //分布式
  337. tmpl = GenerateClusterTmpl()
  338. case 3: //企业版
  339. tmpl = GenerateEnterpriseTmpl()
  340. case 1: //个人版
  341. tmpl = GeneratePersonTmpl()
  342. case 2: //标准版
  343. tmpl = GenerateStandardTmpl()
  344. case 6: //敏捷版
  345. tmpl = GenerateLiteTmpl()
  346. }
  347. // const tmpl = `#用户项目信息
  348. // project_name = {{.ProjectInfo.ProjectName}}
  349. // user_name = {{.ProjectInfo.UserName}}
  350. // user_addr = {{.ProjectInfo.UserAddr}}
  351. // serial_number = {{.ProjectInfo.SerialNumber}}
  352. // #环境信息
  353. // cpu_sn = {{.EnvironmentInfo.CpuSN}}
  354. // baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
  355. // mac_addr = {{.EnvironmentInfo.MacAddr}}
  356. // disk_id = {{.EnvironmentInfo.DiskID}}
  357. // ip_addr = {{.EnvironmentInfo.IPAddr}}
  358. // #License信息
  359. // gen_date = {{.LicenseInfo.GenDate}}
  360. // expire_date = {{.LicenseInfo.ExpireDate}}
  361. // license_type = {{.LicenseInfo.LicenseType}}
  362. // license_version = {{.LicenseInfo.LicenseVersion}}
  363. // hard_type = {{.LicenseInfo.HardType}}
  364. // #功能信息
  365. // ha = {{.FeatureInfo.HA}}
  366. // hint = {{.FeatureInfo.Hint}}
  367. // full_text_index = {{.FeatureInfo.FullTextIndex}}
  368. // function_index = {{.FeatureInfo.FunctionIndex}}
  369. // batch_mode = {{.FeatureInfo.BatchMode}}
  370. // pl_sql = {{.FeatureInfo.PlSql}}
  371. // #分布式信息
  372. // add_node = {{.FeatureInfo.AddNode}}
  373. // auto_partition = {{.FeatureInfo.AutoPartition}}
  374. // resource_quota = {{.FeatureInfo.ResourceQuota}}
  375. // net_channel = {{.FeatureInfo.NetChannel}}
  376. // data_auto_rebalance = {{.FeatureInfo.DataAutoRebalance}}
  377. // #其他组件
  378. // black_white_list = {{.FeatureInfo.BlackWhiteList}}
  379. // mac = {{.FeatureInfo.Mac}}
  380. // col_privilege = {{.FeatureInfo.ColPrivilege}}
  381. // transmit_encrypt = {{.FeatureInfo.TransmitEncrypt}}
  382. // store_encrypt = {{.FeatureInfo.StoreEncrypt}}
  383. // backup_encrypt = {{.FeatureInfo.BackupEncrypt}}
  384. // audit_mode = {{.FeatureInfo.AuditMode}}
  385. // global_binlog_node = {{.FeatureInfo.GlobalBinlogNode}}
  386. // dblink = {{.FeatureInfo.Dblink}}
  387. // json = {{.FeatureInfo.Json}}
  388. // spatial = {{.FeatureInfo.Spatial}}
  389. // max_parallel = {{.FeatureInfo.MaxParallel}}
  390. // stream_load = {{.FeatureInfo.StreamLoad}}
  391. // #服务能力
  392. // instance_num = {{.ServiceCapacity.InstanceNum}}
  393. // table_rows = {{.ServiceCapacity.TableRows}}
  394. // connect_num = {{.ServiceCapacity.ConnectNum}}
  395. // store_capacity = {{.ServiceCapacity.StoreCapacity}}
  396. // max_node_num = {{.ServiceCapacity.MaxNodeNum}}
  397. // listen_channel = {{.ServiceCapacity.ListenChannel}}
  398. // `
  399. // 创建模板并解析
  400. t, err := template.New("config").Parse(tmpl)
  401. if err != nil {
  402. fmt.Println("Error creating template:", err)
  403. return "", err
  404. }
  405. fileName := fmt.Sprintf("lic_op_%s_%s.cfg", time.Now().Format("20060102150405"), app.UserName)
  406. // 创建文件
  407. file, err := os.Create(fileName)
  408. if err != nil {
  409. fmt.Println("Error creating file:", err)
  410. return "", err
  411. }
  412. // // 使用 bytes.Buffer 捕获模板渲染结果
  413. // var buf bytes.Buffer
  414. // // 将模板渲染到缓冲区中
  415. // err = t.Execute(&buf, app)
  416. // if err != nil {
  417. // log.Fatalf("Executing template failed: %v", err)
  418. // }
  419. // // 将渲染结果转换为字符串
  420. // renderedString := buf.String()
  421. // // 打印渲染的字符串(可选)
  422. // fmt.Println("renderedString", renderedString)
  423. // fmt.Println("这该死的app:", app)
  424. // 执行模板并写入文件
  425. err = t.Execute(file, app)
  426. if err != nil {
  427. fmt.Println("Error executing template:", err)
  428. }
  429. fileNameToCRLF := fmt.Sprintf("CRLF_" + fileName)
  430. //fmt.Println("这是什么玩意", fileNameToCRLF, fileName)
  431. err = convertLFtoCRLF(fileName, fileNameToCRLF)
  432. if err != nil {
  433. fmt.Printf("转换文件时出错: %v\n", err)
  434. } else {
  435. fmt.Println("文件转换成功!")
  436. }
  437. defer file.Close()
  438. //删除 fileNameToCRLF
  439. // err = os.Remove(fileName)
  440. // if err != nil {
  441. // fmt.Println("Error removing file:", err)
  442. // return "", err
  443. // } else {
  444. // fmt.Println("File successfully removed")
  445. // }
  446. return fileNameToCRLF, nil
  447. }
  448. func ExecLicenseProgram(fileName string) {
  449. cmd2 := exec.Command("pwd")
  450. // 获取标准输出和标准错误
  451. output2, err := cmd2.CombinedOutput()
  452. if err != nil {
  453. fmt.Printf("执行命令时出错: %v\n", err)
  454. return
  455. }
  456. // 打印输出
  457. fmt.Printf("PWD输出: %s\n", output2)
  458. // 定义要调用的可执行程序及其参数
  459. cmd := exec.Command("/home/gtong/license/lic_go/xugu_license/pkg/lic/xugu_linux_x86_64_20240920_license-test", "-gen_license", fileName)
  460. // 获取标准输出和标准错误
  461. output, err := cmd.CombinedOutput()
  462. if err != nil {
  463. fmt.Printf("执行命令时出错: %v\n", err)
  464. return
  465. }
  466. // 打印输出
  467. fmt.Printf("输出: %s\n", output)
  468. }
  469. func ReadLicenseFile(fileName string) []byte {
  470. // 打开文件
  471. localFile := "license.dat@" + fileName
  472. file, err := os.Open(localFile)
  473. if err != nil {
  474. fmt.Printf("无法打开文件: %v\n", err)
  475. return nil
  476. }
  477. defer file.Close()
  478. // 读取文件内容
  479. content, err := ioutil.ReadAll(file)
  480. if err != nil {
  481. fmt.Printf("无法读取文件: %v\n", err)
  482. return nil
  483. }
  484. return content
  485. }
  486. func convertLFtoCRLF(inputFile, outputFile string) error {
  487. // 打开输入文件
  488. inFile, err := os.Open(inputFile)
  489. if err != nil {
  490. return fmt.Errorf("无法打开输入文件: %v", err)
  491. }
  492. defer inFile.Close()
  493. // 创建输出文件
  494. outFile, err := os.Create(outputFile)
  495. if err != nil {
  496. return fmt.Errorf("无法创建输出文件: %v", err)
  497. }
  498. defer outFile.Close()
  499. reader := bufio.NewReader(inFile)
  500. writer := bufio.NewWriter(outFile)
  501. for {
  502. line, err := reader.ReadString('\n')
  503. if err != nil && err != io.EOF {
  504. return fmt.Errorf("读取文件时出错: %v", err)
  505. }
  506. if err == io.EOF {
  507. if len(line) > 0 {
  508. line = strings.TrimRight(line, "\n")
  509. writer.WriteString(line + "\r\n")
  510. }
  511. break
  512. }
  513. // 将 LF 转换为 CRLF
  514. line = strings.TrimRight(line, "\n")
  515. writer.WriteString(line + "\r\n")
  516. }
  517. writer.Flush()
  518. return nil
  519. }