123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- package license
- import (
- "bufio"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "os/exec"
- "strings"
- "text/template"
- "time"
- )
- func GenerateLicense(pJ ProjectInfo, eI EnvironmentInfo, lI LicenseInfo) []byte {
- app := LicenseApplication{
- ProjectInfo: pJ,
- EnvironmentInfo: eI,
- LicenseInfo: lI,
- FeatureInfo: FeatureInfo{
- HA: true,
- Hint: true,
- FullTextIndex: true,
- FunctionIndex: true,
- BatchMode: true,
- PlSql: true,
- AddNode: false,
- AutoPartition: true,
- ResourceQuota: true,
- NetChannel: 0,
- DataAutoRebalance: true,
- BlackWhiteList: true,
- Mac: true,
- ColPrivilege: true,
- TransmitEncrypt: true,
- StoreEncrypt: true,
- BackupEncrypt: true,
- AuditMode: 0,
- GlobalBinlogNode: 0,
- Dblink: true,
- Json: true,
- Spatial: true,
- MaxParallel: 0,
- StreamLoad: true,
- },
- ServiceCapacity: ServiceCapacity{
- InstanceNum: 0,
- TableRows: 0,
- ConnectNum: 0,
- StoreCapacity: 0,
- MaxNodeNum: 0,
- ListenChannel: 0,
- },
- }
- fileName, _ := GenerateLicenseCofingFile(app)
- ExecLicenseProgram(fileName)
- license := ReadLicenseFile(fileName)
- return license
- }
- // 分布式
- func GenerateClusterTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = true
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- // 企业版
- func GenerateEnterpriseTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = false
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- // 标准版standard
- func GenerateStandardTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = false
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- // 敏捷版本
- func GenerateLiteTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #功能列表
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- auto_partition = true
- net_channel = 0
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- global_binlog_node = 0
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- // 个人版本
- func GeneratePersonTmpl() string {
- const tmpl = `
- #用户项目信息
- project_name = {{.ProjectInfo.ProjectName}}
- user_name = {{.ProjectInfo.UserName}}
- user_addr = {{.ProjectInfo.UserAddr}}
- serial_number = {{.ProjectInfo.SerialNumber}}
- #环境信息
- cpu_sn = {{.EnvironmentInfo.CpuSN}}
- baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- mac_addr = {{.EnvironmentInfo.MacAddr}}
- disk_id = {{.EnvironmentInfo.DiskID}}
- ip_addr = {{.EnvironmentInfo.IPAddr}}
- #License信息
- gen_date = {{.LicenseInfo.GenDate}}
- expire_date = {{.LicenseInfo.ExpireDate}}
- license_type = {{.LicenseInfo.LicenseType}}
- license_version = {{.LicenseInfo.LicenseVersion}}
- hard_type = {{.LicenseInfo.HardType}}
- #企业版
- ha = true
- hint = true
- full_text_index = true
- function_index = true
- batch_mode = true
- pl_sql = true
- #分布式版
- add_node = false
- auto_partition = true
- resource_quota = true
- net_channel = 0
- data_auto_rebalance = true
- #其他组件
- black_white_list = true
- mac = true
- col_privilege = true
- transmit_encrypt = true
- store_encrypt = true
- backup_encrypt = true
- audit_mode = 0
- global_binlog_node = 0
- dblink = true
- json = true
- spatial = true
- max_parallel = 0
- stream_load = true
- #服务能力
- instance_num = 0
- table_rows = 0
- connect_num = 0
- store_capacity = 0
- max_node_num = 0
- listen_channel = 0
- `
- return tmpl
- }
- func GenerateLicenseCofingFile(app LicenseApplication) (string, error) {
- var tmpl string
- switch app.LicenseInfo.LicenseType {
- case 4: //分布式
- tmpl = GenerateClusterTmpl()
- case 3: //企业版
- tmpl = GenerateEnterpriseTmpl()
- case 1: //个人版
- tmpl = GeneratePersonTmpl()
- case 2: //标准版
- tmpl = GenerateStandardTmpl()
- case 6: //敏捷版
- tmpl = GenerateLiteTmpl()
- }
- // const tmpl = `#用户项目信息
- // project_name = {{.ProjectInfo.ProjectName}}
- // user_name = {{.ProjectInfo.UserName}}
- // user_addr = {{.ProjectInfo.UserAddr}}
- // serial_number = {{.ProjectInfo.SerialNumber}}
- // #环境信息
- // cpu_sn = {{.EnvironmentInfo.CpuSN}}
- // baseboard_sn = {{.EnvironmentInfo.BaseboardSN}}
- // mac_addr = {{.EnvironmentInfo.MacAddr}}
- // disk_id = {{.EnvironmentInfo.DiskID}}
- // ip_addr = {{.EnvironmentInfo.IPAddr}}
- // #License信息
- // gen_date = {{.LicenseInfo.GenDate}}
- // expire_date = {{.LicenseInfo.ExpireDate}}
- // license_type = {{.LicenseInfo.LicenseType}}
- // license_version = {{.LicenseInfo.LicenseVersion}}
- // hard_type = {{.LicenseInfo.HardType}}
- // #功能信息
- // ha = {{.FeatureInfo.HA}}
- // hint = {{.FeatureInfo.Hint}}
- // full_text_index = {{.FeatureInfo.FullTextIndex}}
- // function_index = {{.FeatureInfo.FunctionIndex}}
- // batch_mode = {{.FeatureInfo.BatchMode}}
- // pl_sql = {{.FeatureInfo.PlSql}}
- // #分布式信息
- // add_node = {{.FeatureInfo.AddNode}}
- // auto_partition = {{.FeatureInfo.AutoPartition}}
- // resource_quota = {{.FeatureInfo.ResourceQuota}}
- // net_channel = {{.FeatureInfo.NetChannel}}
- // data_auto_rebalance = {{.FeatureInfo.DataAutoRebalance}}
- // #其他组件
- // black_white_list = {{.FeatureInfo.BlackWhiteList}}
- // mac = {{.FeatureInfo.Mac}}
- // col_privilege = {{.FeatureInfo.ColPrivilege}}
- // transmit_encrypt = {{.FeatureInfo.TransmitEncrypt}}
- // store_encrypt = {{.FeatureInfo.StoreEncrypt}}
- // backup_encrypt = {{.FeatureInfo.BackupEncrypt}}
- // audit_mode = {{.FeatureInfo.AuditMode}}
- // global_binlog_node = {{.FeatureInfo.GlobalBinlogNode}}
- // dblink = {{.FeatureInfo.Dblink}}
- // json = {{.FeatureInfo.Json}}
- // spatial = {{.FeatureInfo.Spatial}}
- // max_parallel = {{.FeatureInfo.MaxParallel}}
- // stream_load = {{.FeatureInfo.StreamLoad}}
- // #服务能力
- // instance_num = {{.ServiceCapacity.InstanceNum}}
- // table_rows = {{.ServiceCapacity.TableRows}}
- // connect_num = {{.ServiceCapacity.ConnectNum}}
- // store_capacity = {{.ServiceCapacity.StoreCapacity}}
- // max_node_num = {{.ServiceCapacity.MaxNodeNum}}
- // listen_channel = {{.ServiceCapacity.ListenChannel}}
- // `
- // 创建模板并解析
- t, err := template.New("config").Parse(tmpl)
- if err != nil {
- fmt.Println("Error creating template:", err)
- return "", err
- }
- fileName := fmt.Sprintf("lic_op_%s_%s.cfg", time.Now().Format("20060102150405"), app.UserName)
- // 创建文件
- file, err := os.Create(fileName)
- if err != nil {
- fmt.Println("Error creating file:", err)
- return "", err
- }
- // // 使用 bytes.Buffer 捕获模板渲染结果
- // var buf bytes.Buffer
- // // 将模板渲染到缓冲区中
- // err = t.Execute(&buf, app)
- // if err != nil {
- // log.Fatalf("Executing template failed: %v", err)
- // }
- // // 将渲染结果转换为字符串
- // renderedString := buf.String()
- // // 打印渲染的字符串(可选)
- // fmt.Println("renderedString", renderedString)
- // fmt.Println("这该死的app:", app)
- // 执行模板并写入文件
- err = t.Execute(file, app)
- if err != nil {
- fmt.Println("Error executing template:", err)
- }
- fileNameToCRLF := fmt.Sprintf("CRLF_" + fileName)
- //fmt.Println("这是什么玩意", fileNameToCRLF, fileName)
- err = convertLFtoCRLF(fileName, fileNameToCRLF)
- if err != nil {
- fmt.Printf("转换文件时出错: %v\n", err)
- } else {
- fmt.Println("文件转换成功!")
- }
- defer file.Close()
- //删除 fileNameToCRLF
- // err = os.Remove(fileName)
- // if err != nil {
- // fmt.Println("Error removing file:", err)
- // return "", err
- // } else {
- // fmt.Println("File successfully removed")
- // }
- return fileNameToCRLF, nil
- }
- func ExecLicenseProgram(fileName string) {
- cmd2 := exec.Command("pwd")
- // 获取标准输出和标准错误
- output2, err := cmd2.CombinedOutput()
- if err != nil {
- fmt.Printf("执行命令时出错: %v\n", err)
- return
- }
- // 打印输出
- fmt.Printf("PWD输出: %s\n", output2)
- // 定义要调用的可执行程序及其参数
- cmd := exec.Command("/home/gtong/license/lic_go/xugu_license/pkg/lic/xugu_linux_x86_64_20240920_license-test", "-gen_license", fileName)
- // 获取标准输出和标准错误
- output, err := cmd.CombinedOutput()
- if err != nil {
- fmt.Printf("执行命令时出错: %v\n", err)
- return
- }
- // 打印输出
- fmt.Printf("输出: %s\n", output)
- }
- func ReadLicenseFile(fileName string) []byte {
- // 打开文件
- localFile := "license.dat@" + fileName
- file, err := os.Open(localFile)
- if err != nil {
- fmt.Printf("无法打开文件: %v\n", err)
- return nil
- }
- defer file.Close()
- // 读取文件内容
- content, err := ioutil.ReadAll(file)
- if err != nil {
- fmt.Printf("无法读取文件: %v\n", err)
- return nil
- }
- return content
- }
- func convertLFtoCRLF(inputFile, outputFile string) error {
- // 打开输入文件
- inFile, err := os.Open(inputFile)
- if err != nil {
- return fmt.Errorf("无法打开输入文件: %v", err)
- }
- defer inFile.Close()
- // 创建输出文件
- outFile, err := os.Create(outputFile)
- if err != nil {
- return fmt.Errorf("无法创建输出文件: %v", err)
- }
- defer outFile.Close()
- reader := bufio.NewReader(inFile)
- writer := bufio.NewWriter(outFile)
- for {
- line, err := reader.ReadString('\n')
- if err != nil && err != io.EOF {
- return fmt.Errorf("读取文件时出错: %v", err)
- }
- if err == io.EOF {
- if len(line) > 0 {
- line = strings.TrimRight(line, "\n")
- writer.WriteString(line + "\r\n")
- }
- break
- }
- // 将 LF 转换为 CRLF
- line = strings.TrimRight(line, "\n")
- writer.WriteString(line + "\r\n")
- }
- writer.Flush()
- return nil
- }
|