package ctl import ( "Yunlot/RunCode/context" service "Yunlot/RunCode/service" "Yunlot/logs" "Yunlot/models/Product" context2 "context" "fmt" context1 "github.com/beego/beego/v2/server/web/context" "io/ioutil" "net/http" "os" "strconv" "strings" "time" ) var Go_Main string var Go_MainDATA = "\n// ---DATA---\nvar Data_hex = false\nvar Topic = \"/topic/snxxx1\"\nvar T_string = \"A456\"\nvar R_string = \"495051\"\n" func init() { // 获取 执行文件 content, err := os.ReadFile("./RunCode/config/main.txt") if err != nil { panic(any(err)) } //fmt.Println(string(content)) Go_Main = string(content) } // LanguagesController Get /languages func LanguagesController(writer http.ResponseWriter, request *http.Request) { ctx := &context.Context{Writer: writer, Req: request} if !ctx.IsGet() { ctx.NotAllow() return } var languages []string for lang := range service.DockerRunner.Runners { languages = append(languages, lang) } _ = ctx.JSON(http.StatusOK, languages) return } // 根据不同语言生成代码 run 运行/编译 func CreateCode(run bool, lang_int int, code string) string { switch lang_int { case 1: //Goland if run { code = "package main\n" + code + Go_Main } else { code = "package main\n" + code + Go_MainDATA + strings.Replace(Go_Main, "func main()", "func mainxx()", -1) } break case 2: // C zongjian := "\n*/\nimport \"C\"\n\n/////-------\nimport (\n\t\"encoding/hex\"\n\t\"fmt\"\n\t\"strings\"\n\t\"unsafe\"\n)\n\n/*\n设备->平台\n*/\nfunc T(t string, b []byte) string {\n\tvar r_b *C.char\n\tC.T(C.CString(t), C.CString(string(b)), &r_b)\n\tr_b_ := C.GoString(r_b)\n\tC.free(unsafe.Pointer(r_b)) // Free the memory allocated in C code\n\treturn r_b_\n}\n\n/*\n平台->设备\n*/\nfunc R(sn string, b string) (string, []byte) {\n\tvar r_t,r_b *C.char\n\tC.R(C.CString(sn), C.CString(b), &r_t, &r_b)\n\tr_t_ := C.GoString(r_t)\n\tr_b_ := C.GoString(r_b)\n\tC.free(unsafe.Pointer(r_t)) // Free the memory allocated in C code\n\tC.free(unsafe.Pointer(r_b)) // Free the memory allocated in C code\n\n\treturn r_t_, []byte(r_b_)\n}\n" if run { code_list := strings.Split(code, "// ---DATA---") if len(code_list) != 2 { logs.Println("len(code_list) != 2,没找到 【// ---DATA---】 ") return code } code = "package main\n/*\n" + code_list[0] + zongjian + code_list[1] + Go_Main } else { code = "package main\n/*\n" + code + zongjian + Go_MainDATA + strings.Replace(Go_Main, "func main()", "func mainxx()", -1) } break } return code } // RunController Post /run?lang={lang} func RunController(writer http.ResponseWriter, request *http.Request) { ctx := &context.Context{Writer: writer, Req: request} if !ctx.IsPost() { ctx.NotAllow() return } lang_int, err := strconv.Atoi(ctx.Get("lang", "")) if err != nil { ctx.Bad("lang 参数不能为空!") return } lang := Product.ProductLangFind(lang_int) if lang == "" { ctx.Bad("lang 参数不能为空!") return } else { if !service.DockerRunner.RunnerExists(lang) { ctx.Bad(lang + "语言编译异常!") return } body, _ := ioutil.ReadAll(request.Body) if len(body) == 0 { ctx.Bad("Body 内容为空!") return } // 生成代码 code := CreateCode(true, lang_int, string(body)) cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout)) defer cancelFn() content, err := service.DockerRunner.Exec(cancelCtx, lang, code) if err == nil { content_list := strings.Split(string(content), "|-=&") fmt.Println("content_list:", content_list) if len(content_list) != 4 { ctx.Error("解析错误!!!" + string(content)) return } ctx.RunOK(lang, "success", content_list[1], content_list[3], content_list[2]) return } if err == service.TimeoutError { ctx.Timeout("运行超时!") return } ctx.Error(string(content)) } } // RunController Post /run?lang={lang} func BuildController(Output *context1.BeegoOutput, writer http.ResponseWriter, request *http.Request) { ctx := &context.Context{Writer: writer, Req: request} if !ctx.IsPost() { ctx.NotAllow() return } lang_int, err := strconv.Atoi(ctx.Get("lang", "")) if err != nil { ctx.Bad("lang 参数不能为空!") return } lang := Product.ProductLangFind(lang_int) if lang == "" { ctx.Bad("lang 参数不能为空!") return } else { if !service.DockerRunner.RunnerExists(lang) { ctx.Bad(lang + "语言编译异常!") return } body, _ := ioutil.ReadAll(request.Body) if len(body) == 0 { ctx.Bad("Body 内容为空!") return } // 生成代码 code := CreateCode(false, lang_int, string(body)) //code := "package main\n" + string(body) + strings.Replace(Go_Main, "func main()", "func mainxx()", -1) cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout)) defer cancelFn() name_so := time.Now().UnixMilli() //fmt.Println("code:",code) content, err := service.DockerRunner.Build(cancelCtx, name_so, lang, code) fmt.Println("content:", content) if len(content) > 0 { ctx.Error(string(content)) return } if err == nil { ctx.RunOK(lang, fmt.Sprintf("%d", name_so), "", "", "") return } // 编译超时 //if err == service.TimeoutError { // ctx.Timeout("运行超时!") // return //} ctx.Error("E:" + string(content)) } } func BuildGenerate(ProductProtr *Product.ProductProt) { lang := Product.ProductLangFind(ProductProtr.T_lang) if lang == "" { ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 找不到编译语言 \n" ProductProtr.T_State = 3 ProductProtr.Update("T_State", "T_log") return } if !service.DockerRunner.RunnerExists(lang) { ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译语言异常 \n" ProductProtr.T_State = 3 ProductProtr.Update("T_State", "T_log") return } body := ProductProtr.T_text if len(body) == 0 { ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 内容为空 \n" ProductProtr.T_State = 3 ProductProtr.Update("T_State", "T_log") return } // 生成代码 code := CreateCode(false, ProductProtr.T_lang, body) cancelCtx, cancelFn := context2.WithTimeout(context2.Background(), time.Second*time.Duration(service.DockerRunner.Timeout)) defer cancelFn() name_so := time.Now().UnixMilli() //fmt.Println("code:",code) content, err := service.DockerRunner.Build(cancelCtx, name_so, lang, code) fmt.Println("content:", content) if len(content) > 0 { ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译异常:" + string(content) + " \n|-&" ProductProtr.T_State = 3 ProductProtr.Update("T_State", "T_log") return } if err == nil { ProductProtr.T_analysis = fmt.Sprintf("%d", name_so) ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译成功! \n|-&" ProductProtr.T_State = 1 ProductProtr.Update("T_analysis", "T_State", "T_log") return } // 编译超时 //if err == service.TimeoutError { // ctx.Timeout("运行超时!") // return //} ProductProtr.T_log += time.Now().Format("2006-01-02 15:04:05") + " 编译异常:" + string(content) + " \n" ProductProtr.T_State = 3 ProductProtr.Update("T_State", "T_log") return }