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 }