|
|
@@ -10,19 +10,12 @@ import (
|
|
|
"hello/connsql"
|
|
|
dt "hello/datastruct"
|
|
|
fwd "hello/forwardmode"
|
|
|
- "hello/mcryp"
|
|
|
"hello/model"
|
|
|
- "io"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
- "net/url"
|
|
|
- "os"
|
|
|
- "path/filepath"
|
|
|
|
|
|
// "sort"
|
|
|
"strconv"
|
|
|
- "strings"
|
|
|
- "sync"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
// "github.com/imroc/biu"
|
|
|
@@ -203,10 +196,6 @@ func GetSNList(c *gin.Context) {
|
|
|
cli, ok := basic.Dtumap_state.Get(v)
|
|
|
if ok {
|
|
|
node.Online = cli.Online
|
|
|
- // if cli.Online {
|
|
|
- // node.Relay3 = cli.PurifiDO3
|
|
|
- // node.Relay2 = cli.PurifiDO2
|
|
|
- // }
|
|
|
}
|
|
|
node.Sn = v
|
|
|
res.Array = append(res.Array, node)
|
|
|
@@ -369,80 +358,185 @@ func updateCompanyList() {
|
|
|
connsql.DTUManage_GetCompanyList(basic.Dtumap_sn)
|
|
|
}
|
|
|
|
|
|
-/*****************************通信机相关操作********************************************/
|
|
|
-func optDTUCtrl(c *gin.Context, cmd string, data string) bool {
|
|
|
- if sn, ok := c.GetQuery("sn"); !ok {
|
|
|
+func SetMeterAddress(c *gin.Context) {
|
|
|
+ value := dt.SetMeterAddressRes{}
|
|
|
+ cpid := c.Request.URL.Query().Get("sn")
|
|
|
+ if len(cpid) < 1 {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn null")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err := c.BindJSON(&value); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, err.Error())
|
|
|
+ } else {
|
|
|
+ state := fwd.API_SetMeterAddress(cpid, value)
|
|
|
+ AckHTML_OKWithData(c, state)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func SetMeterRatio(c *gin.Context) {
|
|
|
+ value := dt.SetMeterRatioRes{}
|
|
|
+ cpid := c.Request.URL.Query().Get("sn")
|
|
|
+ if len(cpid) < 1 {
|
|
|
AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn null")
|
|
|
- return false
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err := c.BindJSON(&value); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, err.Error())
|
|
|
} else {
|
|
|
- if len(sn) < 10 {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_SNLENGTH, "sn err")
|
|
|
- return false
|
|
|
+ if len(value.SerData) < 1 {
|
|
|
+ AckHTML_ErrJsonFmt(c, "json format err")
|
|
|
+ return
|
|
|
}
|
|
|
- res, ok := model.ModelDTUCtrl(sn, cmd, data)
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
+ state := fwd.API_SetMeterRatio(cpid, value)
|
|
|
+ AckHTML_OKWithData(c, state)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func GetElectricData(c *gin.Context) {
|
|
|
+ cpid := ""
|
|
|
+ mtype := ""
|
|
|
+ addr := 0
|
|
|
+ if val := c.Request.URL.Query().Get("sn"); len(val) < 1 {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn null")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ cpid = val
|
|
|
+ }
|
|
|
+ if val := c.Request.URL.Query().Get("type"); len(val) < 1 {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "type null")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ mtype = val
|
|
|
+ }
|
|
|
+ if val := c.Request.URL.Query().Get("addr"); len(val) < 1 {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "addr null")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ if n, err := strconv.Atoi(val); err != nil {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "addr error")
|
|
|
+ return
|
|
|
} else {
|
|
|
- AckHTML_OKWithData(c, res)
|
|
|
+ addr = n
|
|
|
}
|
|
|
- return ok
|
|
|
}
|
|
|
+ state := fwd.API_QueryElectricData(cpid, addr, mtype)
|
|
|
+ AckHTML_OKWithData(c, state)
|
|
|
}
|
|
|
|
|
|
-func optDTUCtrlWithSN(c *gin.Context, sn, cmd, data string) bool {
|
|
|
- if len(sn) < 10 {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_SNLENGTH, "sn err")
|
|
|
- return false
|
|
|
+func GetMeterRatio(c *gin.Context) {
|
|
|
+ cpid := ""
|
|
|
+ mtype := ""
|
|
|
+ addr := 0
|
|
|
+ if val, ok := c.GetQuery("sn"); !ok {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn null")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ cpid = val
|
|
|
}
|
|
|
- res, ok := model.ModelDTUCtrl(sn, cmd, data)
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
+ if val := c.Request.URL.Query().Get("type"); len(val) < 1 {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "type null")
|
|
|
+ return
|
|
|
} else {
|
|
|
- AckHTML_OKWithData(c, res)
|
|
|
+ mtype = val
|
|
|
+ }
|
|
|
+ if val, ok := c.GetQuery("addr"); !ok {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "addr null")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ n, er := strconv.Atoi(val)
|
|
|
+ if er != nil {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_HtmlParam, "addr error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ addr = n
|
|
|
}
|
|
|
- return ok
|
|
|
+ state := fwd.API_QueryMeterRatio(cpid, addr, mtype)
|
|
|
+ AckHTML_OKWithData(c, state)
|
|
|
}
|
|
|
|
|
|
-func DTUReRun(c *gin.Context) {
|
|
|
- optDTUCtrl(c, dt.CMD_RERUN, "")
|
|
|
+func DTUGetProjectList(c *gin.Context) {
|
|
|
+ obj, _ := connsql.GetAgentList("t_project")
|
|
|
+ AckHTML_OKWithData(c, obj)
|
|
|
}
|
|
|
|
|
|
-func DTUReboot(c *gin.Context) {
|
|
|
- if !cfg.ParamConf.Mode {
|
|
|
- AckHTML_OK(c)
|
|
|
+func DTUGetAgentList(c *gin.Context) {
|
|
|
+ obj, _ := connsql.GetAgentList("t_agent")
|
|
|
+ AckHTML_OKWithData(c, obj)
|
|
|
+}
|
|
|
+
|
|
|
+func DTUAddAgent(c *gin.Context) {
|
|
|
+ var info dt.DictIntStr
|
|
|
+ if err := c.BindJSON(&info); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, "json fmt err")
|
|
|
return
|
|
|
}
|
|
|
- optDTUCtrl(c, dt.CMD_REBOOT, "")
|
|
|
+ name := info.Value
|
|
|
+ if len([]rune(name)) < 2 || len([]rune(name)) > 50 {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_DTUErr, "name error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ connsql.AddAgentInfo(name)
|
|
|
+ AckHTML_OK(c)
|
|
|
}
|
|
|
|
|
|
-func GetDTUVersion(c *gin.Context) {
|
|
|
- optDTUCtrl(c, dt.CMD_GETVERSION, "")
|
|
|
+func DTUDeleteAgent(c *gin.Context) {
|
|
|
+ var info dt.DictIntStr
|
|
|
+ if err := c.BindJSON(&info); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, "json fmt err")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ connsql.DeleteAgentInfo(info.Key, info.Value)
|
|
|
+ AckHTML_OK(c)
|
|
|
}
|
|
|
|
|
|
-/*解析压缩格式的data数据
|
|
|
-出于降低流量开销目的,与通信机传输数据如果data长度大于512字节则会使用自定义压缩格式
|
|
|
-格式内容见文档,此处是对data按照自定义压缩格式解析
|
|
|
-返回:int状态码,string内容
|
|
|
-*/
|
|
|
-func paraseCompressData(str string) (int, string, error) {
|
|
|
- var info dt.CfgFileInfo
|
|
|
- //json格式检查
|
|
|
- if err := json.Unmarshal([]byte(str), &info); err != nil {
|
|
|
- //不符合压缩格式
|
|
|
- return dt.HTC_JSONFormat, "", errors.New("json format error")
|
|
|
+func DTUModifyAgent(c *gin.Context) {
|
|
|
+ var info dt.DictIntStr
|
|
|
+ if err := c.BindJSON(&info); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, "json fmt err")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err := connsql.UpdateAgentInfo(info.Key, info.Value); err != nil {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_EditCompany, "fail")
|
|
|
+ } else {
|
|
|
+ AckHTML_OK(c)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func DTUAddProject(c *gin.Context) {
|
|
|
+ var info dt.DictIntStr
|
|
|
+ if err := c.BindJSON(&info); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, "json fmt err")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ name := info.Value
|
|
|
+ if len([]rune(name)) < 2 || len([]rune(name)) > 50 {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_DTUErr, "name error")
|
|
|
+ return
|
|
|
}
|
|
|
- //数据检查
|
|
|
- if !info.Res {
|
|
|
- //通信机那边读取数据失败
|
|
|
- return dt.HTC_DTUErr, "", errors.New(info.CryData)
|
|
|
+ connsql.AddProjectInfo(name)
|
|
|
+ AckHTML_OK(c)
|
|
|
+}
|
|
|
+
|
|
|
+func DTUDeleteProject(c *gin.Context) {
|
|
|
+ var info dt.DictIntStr
|
|
|
+ if err := c.BindJSON(&info); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, "json fmt err")
|
|
|
+ return
|
|
|
}
|
|
|
- //解压
|
|
|
- if str, err := mcryp.ParaseRemoteFile(info); err != nil {
|
|
|
- //解压/校验失败
|
|
|
- return dt.HTC_UnCPR, "", err
|
|
|
+ connsql.DeleteProjectInfo(info.Key, info.Value)
|
|
|
+ AckHTML_OK(c)
|
|
|
+}
|
|
|
+
|
|
|
+func DTUModifyProject(c *gin.Context) {
|
|
|
+ var info dt.DictIntStr
|
|
|
+ if err := c.BindJSON(&info); err != nil {
|
|
|
+ AckHTML_ErrJsonFmt(c, "json fmt err")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err := connsql.UpdateProjectInfo(info.Key, info.Value); err != nil {
|
|
|
+ AckHTML_AbNormal(c, dt.HTC_EditCompany, "fail")
|
|
|
} else {
|
|
|
- //正确获取数据
|
|
|
- return dt.HTC_OK, str, nil
|
|
|
+ AckHTML_OK(c)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -479,180 +573,6 @@ func DTUGetConfig(c *gin.Context) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func Testdtu(c *gin.Context) {
|
|
|
- if sn := c.Request.URL.Query().Get("sn"); sn == "" {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn is null")
|
|
|
- } else {
|
|
|
- res, ok := model.ModelDTUCtrl(sn, dt.CMD_GETSYSCFG, "")
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- } else {
|
|
|
- if co, cdata, err := paraseCompressData(res); err != nil {
|
|
|
- AckHTML_AbNormal(c, co, err.Error())
|
|
|
- } else {
|
|
|
- AckHTML_OKWithData(c, cdata)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func GetDTUGetSyscfg(c *gin.Context) {
|
|
|
- if sn := c.Request.URL.Query().Get("sn"); sn == "" {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn is null")
|
|
|
- } else {
|
|
|
- res, ok := model.ModelDTUCtrl(sn, dt.CMD_GETSYSCFG, "")
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- } else {
|
|
|
- if co, cdata, err := paraseCompressData(res); err != nil {
|
|
|
- AckHTML_AbNormal(c, co, err.Error())
|
|
|
- } else {
|
|
|
- AckHTML_OKWithData(c, cdata)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//读取指定文件
|
|
|
-func GetAssigntxtFile(c *gin.Context) {
|
|
|
- if sn := c.Request.URL.Query().Get("sn"); sn == "" {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn is null")
|
|
|
- } else {
|
|
|
- path := c.Request.Header.Get("FilePath")
|
|
|
- if len(path) < 1 {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "miss path")
|
|
|
- return
|
|
|
- }
|
|
|
- //log.Println("GetAssigntxtFile path ", path)
|
|
|
- res, ok := model.ModelDTUCtrl(sn, dt.CMD_GETXTFILE, path)
|
|
|
- if !ok {
|
|
|
- //log.Println("GetAssigntxtFile Err ", res)
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- } else {
|
|
|
- //log.Println("GetAssigntxtFile OK ")
|
|
|
- if co, cdata, err := paraseCompressData(res); err != nil {
|
|
|
- AckHTML_AbNormal(c, co, err.Error())
|
|
|
- //log.Println("GetAssigntxtFile Err ", err.Error())
|
|
|
- } else {
|
|
|
- //log.Println("GetAssigntxtFile Sucess ")
|
|
|
- AckHTML_OKWithData(c, cdata)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//设置指定文件
|
|
|
-func SetAssigntxtFile(c *gin.Context) {
|
|
|
- var (
|
|
|
- te dt.RmtTxtFile
|
|
|
- dtudev dt.ConfigSystem
|
|
|
- )
|
|
|
- if err := c.BindJSON(&dtudev); err != nil {
|
|
|
- log.Println(err.Error())
|
|
|
- AckHTML_ErrJsonFmt(c, err.Error())
|
|
|
- } else {
|
|
|
- path := c.Request.Header.Get("FilePath")
|
|
|
- if len(path) < 1 {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "miss path")
|
|
|
- return
|
|
|
- }
|
|
|
- cfg := mcryp.PackRemoteFile(dtudev.Conf, false)
|
|
|
- te.Path = path
|
|
|
- te.Context = dt.JsonToString(cfg)
|
|
|
- if ok := optDTUCtrl(c, dt.CMD_SETXTFILE, dt.JsonToString(te)); ok {
|
|
|
- log.Println("SetAssigntxtFile ", "OK")
|
|
|
- //AckHTML_OK(c)
|
|
|
- } else {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "set to dtu fail")
|
|
|
- log.Println("SetAssigntxtFile ", "set to dtu fail")
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func GetDTUGetExtShell(c *gin.Context) {
|
|
|
- if sn := c.Request.URL.Query().Get("sn"); sn == "" {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "sn is null")
|
|
|
- } else {
|
|
|
- res, ok := model.ModelDTUCtrl(sn, dt.CMD_GETXTFILE, "/usr/ext.sh")
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- } else {
|
|
|
- if co, cdata, err := paraseCompressData(res); err != nil {
|
|
|
- AckHTML_AbNormal(c, co, err.Error())
|
|
|
- } else {
|
|
|
- AckHTML_OKWithData(c, cdata)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func getFileMD5(sn string, file string) (string, error) {
|
|
|
- res, ok := model.ModelDTUCtrl(sn, dt.CMD_GETFILEMD5, file)
|
|
|
- if !ok {
|
|
|
- return "", errors.New("get md5 error")
|
|
|
- } else {
|
|
|
- return res, nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func chkThresholdConfig(cfg *dt.ConfigThreshold) error {
|
|
|
- if len([]rune(cfg.Name)) > 24 {
|
|
|
- return errors.New("Range[0,12], Name is too longer : " + cfg.Name)
|
|
|
- }
|
|
|
- if cfg.Voc > 100 || cfg.Voc < 1 {
|
|
|
- return errors.New("Voc is Invalid, Range[1,100]")
|
|
|
- }
|
|
|
- if cfg.Pm > 100 || cfg.Pm < 1 {
|
|
|
- return errors.New("Pm is Invalid, Range[1,100]")
|
|
|
- }
|
|
|
-
|
|
|
- if cfg.Lamp > 100 || cfg.Lamp < 1 {
|
|
|
- return errors.New("Lamp is Invalid, Range[1,100]")
|
|
|
- }
|
|
|
- if cfg.Flow > 100000 || cfg.Flow < 1 {
|
|
|
- return errors.New("Flow is Invalid, Range[1,100000]")
|
|
|
- }
|
|
|
- if cfg.Ifan > 10 || cfg.Ifan < 0.0 {
|
|
|
- return errors.New("Ifan is Invalid, Range[0.0,10]")
|
|
|
- }
|
|
|
- if cfg.Ipur > 10 || cfg.Ipur < 0.0 {
|
|
|
- return errors.New("Ipur is Invalid, Range[0.0,10]")
|
|
|
- }
|
|
|
-
|
|
|
- if cfg.VocA > 10 || cfg.VocA < -10 {
|
|
|
- return errors.New("VocA is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.PmA > 10 || cfg.PmA < -10 {
|
|
|
- return errors.New("PmA is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.LampA > 10 || cfg.LampA < -10 {
|
|
|
- return errors.New("LampA is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.IfanA > 10 || cfg.IfanA < -10 {
|
|
|
- return errors.New("IfanA is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.IpurA > 10 || cfg.IpurA < -10 {
|
|
|
- return errors.New("IpurA is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
-
|
|
|
- if cfg.VocB > 10 || cfg.VocB < -10 {
|
|
|
- return errors.New("VocB is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.PmB > 10 || cfg.PmB < -10 {
|
|
|
- return errors.New("PmB is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.LampB > 10 || cfg.LampB < -10 {
|
|
|
- return errors.New("LampB is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.IfanB > 10 || cfg.IfanB < -10 {
|
|
|
- return errors.New("IfanB is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- if cfg.IpurB > 10 || cfg.IpurB < -10 {
|
|
|
- return errors.New("IpurB is Invalid, Range[-10,10]")
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
func checkCfgJsonItem(item dt.ConfigfsJSONItem) error {
|
|
|
if len(item.Name) > dt.LenName-1 {
|
|
|
return errors.New("Platform Name is too longer : " + item.Name)
|
|
|
@@ -685,19 +605,6 @@ func chkConfigfsJSON(cfg *dt.ConfigfsJSON) error {
|
|
|
if err := checkCfgJsonItem(cfg.Platform[i]); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- /*if len(cfg.Platform[i].Name) > dt.LenName-1 {
|
|
|
- return errors.New("Platform Name is too longer : " + cfg.Platform[i].Name)
|
|
|
- }
|
|
|
- if len(cfg.Platform[i].Pw) > dt.LenPW-1 {
|
|
|
- return errors.New("Platform PW is too longer : " + cfg.Platform[i].Pw)
|
|
|
- }
|
|
|
- if len(cfg.Platform[i].Mn) > dt.LenMN-1 {
|
|
|
- return errors.New("Platform MN is too longer : " + cfg.Platform[i].Mn)
|
|
|
- }
|
|
|
- pe := cfg.Platform[i].Secs
|
|
|
- if pe < 1 || pe > 60 {
|
|
|
- return errors.New("Platform Interval is Invalid : " + cfg.Platform[i].Name + " ( n > 60min)")
|
|
|
- }*/
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
@@ -713,12 +620,6 @@ func update_to_map(sn string, val dt.ConfigfsJSON) {
|
|
|
cli.Conf = val
|
|
|
basic.OnlineSN.Add(sn, cli)
|
|
|
}
|
|
|
- // if cli, ok := basic.Dtumap_state.Get(sn); ok {
|
|
|
- // log.Println("Dtumap_state:", cli.Conf)
|
|
|
- // }
|
|
|
- // if cli, ok := basic.OnlineSN.Get(sn); ok {
|
|
|
- // log.Println("OnlineSN:", cli.Conf)
|
|
|
- // }
|
|
|
}
|
|
|
|
|
|
func DTUSetConfig(c *gin.Context) {
|
|
|
@@ -767,339 +668,17 @@ func DTUAddItemConfig(c *gin.Context) {
|
|
|
AckHTML_OK(c)
|
|
|
//AckHTML_OKWithData(c, cfg)
|
|
|
}
|
|
|
-
|
|
|
- //cfg := mcryp.PackRemoteFile(dt.JsonToString(dtudev), false)
|
|
|
- /*if md5, err := getFileMD5(sn, dt.CMD_GETCFGCFG); err == nil {
|
|
|
- if cfg.Md5 == md5 {
|
|
|
- log.Println("tag: No Change")
|
|
|
- AckHTML_OK(c)
|
|
|
- return
|
|
|
- }
|
|
|
- }*/
|
|
|
- // optDTUCtrl(c, dt.CMD_ADDCFGCFG, dt.JsonToString(cfg))
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func GetDTUSetSyscfg(c *gin.Context) {
|
|
|
- var dtudev dt.ConfigSystem
|
|
|
- sn := c.Request.URL.Query().Get("sn")
|
|
|
- if err := c.BindJSON(&dtudev); err != nil {
|
|
|
- log.Println(err.Error())
|
|
|
- AckHTML_ErrJsonFmt(c, err.Error())
|
|
|
- } else {
|
|
|
- res := mcryp.PackRemoteFile(dtudev.Conf, false)
|
|
|
- if md5, err := getFileMD5(sn, dt.CMD_SETSYSCFG); err == nil {
|
|
|
- if res.Md5 == md5 {
|
|
|
- log.Println("tag: No Change")
|
|
|
- AckHTML_OK(c)
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
- if err := cfg.CheckSystemConf(dtudev.Conf); err != nil {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_Undefine, err.Error())
|
|
|
- return
|
|
|
- }
|
|
|
- //AckHTML_OK(c)
|
|
|
- if ok := optDTUCtrl(c, dt.CMD_SETSYSCFG, dt.JsonToString(res)); ok {
|
|
|
- //connsql.SaveConfigfsToDB(sn, dt.JsonToString(dtudev))
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func GetDTUSetExtShell(c *gin.Context) {
|
|
|
- var (
|
|
|
- te dt.RmtTxtFile
|
|
|
- dtudev dt.ConfigSystem
|
|
|
- )
|
|
|
- //sn := c.Request.URL.Query().Get("sn")
|
|
|
- if err := c.BindJSON(&dtudev); err != nil {
|
|
|
- log.Println(err.Error())
|
|
|
- AckHTML_ErrJsonFmt(c, err.Error())
|
|
|
- } else {
|
|
|
- /*if err := cfg.CheckSystemConf(dtudev.Conf); err != nil {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_Undefine, err.Error())
|
|
|
- return
|
|
|
- }*/
|
|
|
- cfg := mcryp.PackRemoteFile(dtudev.Conf, false)
|
|
|
- /*if md5, err := getFileMD5(sn, dt.CMD_SETSYSCFG); err == nil {
|
|
|
- if cfg.Md5 == md5 {
|
|
|
- log.Println("tag: No Change")
|
|
|
- AckHTML_OK(c)
|
|
|
- return
|
|
|
- }
|
|
|
- }*/
|
|
|
- te.Path = "/usr/ext.sh"
|
|
|
- te.Context = dt.JsonToString(cfg)
|
|
|
- if ok := optDTUCtrl(c, dt.CMD_SETXTFILE, dt.JsonToString(te)); ok {
|
|
|
- //connsql.SaveConfigfsToDB(sn, dt.JsonToString(dtudev))
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
-func GetDTUGetState(c *gin.Context) {
|
|
|
- optDTUCtrl(c, dt.CMD_GETSTATE, "")
|
|
|
-}*/
|
|
|
//以json格式获取运行状态
|
|
|
-func DTUGetStateJson(c *gin.Context) {
|
|
|
- var (
|
|
|
- reo dt.RunStateDTU
|
|
|
- // to dt.RunStateDTU
|
|
|
- )
|
|
|
+func GetDTUState(c *gin.Context) {
|
|
|
sn := c.Request.URL.Query().Get("sn")
|
|
|
- res, ok := model.ModelDTUCtrl(sn, dt.CMD_GETSTATE, "")
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- } else {
|
|
|
- if len(res) < 5 {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- return
|
|
|
- }
|
|
|
- d := mcryp.BASE64DecodeStr(res)
|
|
|
- if d == "" {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- return
|
|
|
- }
|
|
|
- err := json.Unmarshal([]byte(d), &reo)
|
|
|
- if err != nil {
|
|
|
- res = "json format error"
|
|
|
- AckHTML_ErrJsonFmt(c, "")
|
|
|
- } else {
|
|
|
- AckHTML_OKWithData(c, reo)
|
|
|
- }
|
|
|
- }
|
|
|
+ val := fwd.API_QueryDeviceStatus(sn)
|
|
|
+ AckHTML_OKWithData(c, val)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
-func GetDTUpgradeRctool(c *gin.Context) {
|
|
|
- optDTUCtrl(c, dt.CMD_UPRCTOOL, "")
|
|
|
-}
|
|
|
-func GetDTUpgradeHJ212(c *gin.Context) {
|
|
|
- optDTUCtrl(c, dt.CMD_UPHJ212, "")
|
|
|
-}
|
|
|
-
|
|
|
-func SetDTUpgradeRctool(c *gin.Context) {
|
|
|
- optDTUCtrl(c, dt.CMD_UPRCTOOL, "")
|
|
|
-}*/
|
|
|
-
|
|
|
-//生成DTU端HTTP下载信息包
|
|
|
-func makeHttpDTU_Down(n dt.RmtURLInfo) string {
|
|
|
- var (
|
|
|
- pack dt.RmtFilePack
|
|
|
- info dt.RmtFileInfo
|
|
|
- )
|
|
|
- info.Type = "http"
|
|
|
- if n.IsZip {
|
|
|
- info.Encode = "gzip"
|
|
|
- } else {
|
|
|
- info.Encode = ""
|
|
|
- }
|
|
|
- //info.Auth
|
|
|
- info.URL = "http://" + cfg.ParamConf.Htip + ":" + strconv.Itoa(cfg.ParamConf.HtPort) + "/file/download"
|
|
|
- info.HPath = encUrl(n)
|
|
|
- info.DIR = true
|
|
|
- pack.Info = dt.JsonToString(info)
|
|
|
- pack.MD5 = mcryp.MD5Str(pack.Info)
|
|
|
- str := dt.JsonToString(pack)
|
|
|
- _, bp := mcryp.EncypAESEncode([]byte(str), []byte(rmtDileKey))
|
|
|
- return bp
|
|
|
-}
|
|
|
-func makeFtpDTU_Down(n dt.FtpFileInfo) string {
|
|
|
- var (
|
|
|
- pack dt.RmtFilePack
|
|
|
- info dt.RmtFileInfo
|
|
|
- )
|
|
|
- info.Type = "ftp"
|
|
|
- if n.IsZip {
|
|
|
- info.Encode = "gzip"
|
|
|
- } else {
|
|
|
- info.Encode = ""
|
|
|
- }
|
|
|
- info.IP = cfg.ParamConf.FtpIP
|
|
|
- info.Port = strconv.Itoa(cfg.ParamConf.FtpPort)
|
|
|
- info.Usr = cfg.ParamConf.FtpUsr
|
|
|
- info.Pwd = cfg.ParamConf.FtpPwd
|
|
|
- info.Src = n.Src
|
|
|
- info.Dst = n.Dst
|
|
|
- info.DIR = n.DIR
|
|
|
- if info.DIR {
|
|
|
- info.Reboot = n.IsRbt
|
|
|
- } else {
|
|
|
- info.Reboot = false
|
|
|
- }
|
|
|
- pack.Info = dt.JsonToString(info)
|
|
|
- pack.MD5 = mcryp.MD5Str(pack.Info)
|
|
|
- str := dt.JsonToString(pack)
|
|
|
- _, bp := mcryp.EncypAESEncode([]byte(str), []byte(rmtDileKey))
|
|
|
- return bp
|
|
|
-}
|
|
|
-
|
|
|
-func SetDTUpgradeHJ212(c *gin.Context) {
|
|
|
- sn := c.Query("sn")
|
|
|
- key := c.Query("key")
|
|
|
- if sn == "" || key == "" {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "miss param")
|
|
|
- return
|
|
|
- }
|
|
|
- err, info := decUrl(key)
|
|
|
- if err != nil {
|
|
|
- log.Println(err)
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "key err")
|
|
|
- return
|
|
|
- }
|
|
|
- info.Sn = sn
|
|
|
- info.Time = dt.GetNowTime()
|
|
|
- data := makeHttpDTU_Down(info)
|
|
|
- res, ok := model.ModelDTUCtrl(sn, dt.CMD_FILEDOWNLOAD, data)
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- } else {
|
|
|
- AckHTML_OKWithData(c, res)
|
|
|
- }
|
|
|
- //optDTUCtrl(c, dt.CMD_FILEDOWNLOAD, CrypeToRmtFile("./app", "./app"))
|
|
|
-}
|
|
|
-
|
|
|
-func taskUpgradeBatch(c *gin.Context, info dt.UpgradeBatchInfo, flag string) {
|
|
|
- var (
|
|
|
- wg sync.WaitGroup
|
|
|
- mt sync.Mutex
|
|
|
- result = ""
|
|
|
- )
|
|
|
- model.FlagsCache.Add(flag, "")
|
|
|
- data := makeFtpDTU_Down(info.FtpInfo)
|
|
|
- for _, v := range info.DevList {
|
|
|
- wg.Add(1)
|
|
|
- go func(sn string) {
|
|
|
- str := ""
|
|
|
- if res, ok := model.ModelDTUCtrl(sn, dt.CMD_FILEDOWNLOAD, data); ok && res == "ok" {
|
|
|
- str = sn + "-ok"
|
|
|
- } else {
|
|
|
- str = sn + "-" + res
|
|
|
- }
|
|
|
- mt.Lock()
|
|
|
- result += str + ";"
|
|
|
- mt.Unlock()
|
|
|
- wg.Done()
|
|
|
- }(v)
|
|
|
- }
|
|
|
- wg.Wait()
|
|
|
- //log.Println("OptionUpgradeBatch:", result)
|
|
|
- if len(result) == 0 {
|
|
|
- result = "unknow error"
|
|
|
- //AckHTML_OKWithData(c, result)
|
|
|
- } // else {
|
|
|
- //AckHTML_AbNormal(c, dt.HTC_DTUErr, "null")
|
|
|
- //}
|
|
|
- model.FlagsCache.Add(flag, result)
|
|
|
-}
|
|
|
-
|
|
|
-func OptionUpgradeBatch(c *gin.Context) {
|
|
|
- var (
|
|
|
- //wg sync.WaitGroup
|
|
|
- //mt sync.Mutex
|
|
|
- info dt.UpgradeBatchInfo
|
|
|
- //result []string
|
|
|
- )
|
|
|
- flag, ok := c.GetQuery("flag")
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "miss param")
|
|
|
- return
|
|
|
- }
|
|
|
- if err := c.BindJSON(&info); err != nil {
|
|
|
- AckHTML_ErrJsonFmt(c, "")
|
|
|
- return
|
|
|
- }
|
|
|
- AckHTML_OK(c)
|
|
|
- go taskUpgradeBatch(c, info, flag)
|
|
|
- /*data := makeFtpDTU_Down(info.FtpInfo)
|
|
|
- for _, v := range info.DevList {
|
|
|
- wg.Add(1)
|
|
|
- go func(sn string) {
|
|
|
- str := ""
|
|
|
- if res, ok := model.ModelDTUCtrl(sn, dt.CMD_FILEDOWNLOAD, data); ok && res == "ok" {
|
|
|
- str = sn + " ok"
|
|
|
- } else {
|
|
|
- str = sn + " " + res
|
|
|
- }
|
|
|
- mt.Lock()
|
|
|
- result = append(result, str)
|
|
|
- mt.Unlock()
|
|
|
- wg.Done()
|
|
|
- }(v)
|
|
|
- }
|
|
|
- wg.Wait()
|
|
|
- //log.Println("OptionUpgradeBatch:", result)
|
|
|
- if len(result) > 0 {
|
|
|
- AckHTML_OKWithData(c, result)
|
|
|
- } else {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, "null")
|
|
|
- }*/
|
|
|
-}
|
|
|
-
|
|
|
-func GetUpBatchResult(c *gin.Context) {
|
|
|
- flag, ok := c.GetQuery("flag")
|
|
|
- if !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "miss param")
|
|
|
- return
|
|
|
- }
|
|
|
- if val, ok := model.FlagsCache.Get(flag); ok {
|
|
|
- //var obj = dt.OBJString{Obj: ""}
|
|
|
- if len(val) > 0 {
|
|
|
- model.FlagsCache.Remove(flag)
|
|
|
- }
|
|
|
- //obj.Obj = val
|
|
|
- AckHTML_OKWithData(c, val)
|
|
|
- } else {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, "")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func OptionUpgradeRmtf(c *gin.Context) {
|
|
|
- var info dt.FtpFileInfo
|
|
|
- sn, ok := c.GetQuery("sn")
|
|
|
- if !ok {
|
|
|
- //fmt.Println("OptionUpgradeRmtf : ", "miss param")
|
|
|
- AckHTML_AbNormal(c, dt.HTC_HtmlParam, "miss param")
|
|
|
- return
|
|
|
- }
|
|
|
- if err := c.BindJSON(&info); err != nil {
|
|
|
- //fmt.Println("json format error")
|
|
|
- AckHTML_ErrJsonFmt(c, "")
|
|
|
- return
|
|
|
- }
|
|
|
- data := makeFtpDTU_Down(info)
|
|
|
- if res, ok := model.ModelDTUCtrl(sn, dt.CMD_FILEDOWNLOAD, data); ok && res == "ok" {
|
|
|
- AckHTML_OKWithData(c, res)
|
|
|
- } else {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-var (
|
|
|
- rmtDileKey string
|
|
|
- keyEnUrl = "u98(-NHigdbpa-=0"
|
|
|
-)
|
|
|
-
|
|
|
-func SetRmtDileKey(n string) {
|
|
|
- rmtDileKey = n
|
|
|
-}
|
|
|
-func packRmtFileStr(src, dst string) string {
|
|
|
- var (
|
|
|
- pack dt.RmtFilePack
|
|
|
- info dt.RmtFileInfo
|
|
|
- )
|
|
|
- info.Port = strconv.Itoa(cfg.ParamConf.HtPort)
|
|
|
- info.IP = cfg.ParamConf.Htip
|
|
|
- info.Src = src
|
|
|
- info.Dst = dst
|
|
|
- str := dt.JsonToString(info)
|
|
|
- pack.Info = str
|
|
|
- pack.MD5 = mcryp.MD5Str(str)
|
|
|
- log.Println(pack)
|
|
|
- return dt.JsonToString(pack)
|
|
|
-}
|
|
|
-
|
|
|
/*
|
|
|
|
|
|
func packHttpRmtFile(name, dst string, zip bool) dt.RmtFileInfo {
|
|
|
@@ -1113,146 +692,12 @@ func packHttpRmtFile(name, dst string, zip bool) dt.RmtFileInfo {
|
|
|
return info
|
|
|
}*/
|
|
|
|
|
|
-//对远程更新文件信息加密
|
|
|
-func CrypeToRmtFile(src, dst string) string {
|
|
|
- str := packRmtFileStr(src, dst)
|
|
|
- //log.Println("CrypeToRmtFile : " + str)
|
|
|
- _, bp := mcryp.EncypAESEncode([]byte(str), []byte(rmtDileKey))
|
|
|
- return bp
|
|
|
-}
|
|
|
-
|
|
|
-var (
|
|
|
- uploadSave = "./file/down/"
|
|
|
- downloadSave = "./file/upload/"
|
|
|
-)
|
|
|
-
|
|
|
-func GetFileFromHtml(c *gin.Context) {
|
|
|
- fileDir := "." //c.Query("fileDir")
|
|
|
- fileName := c.Query("fileName")
|
|
|
- path := fileDir + "/" + fileName
|
|
|
- fmt.Println(path)
|
|
|
- is, _ := dt.PathExists(path)
|
|
|
- if !is {
|
|
|
- c.Redirect(http.StatusFound, "/404")
|
|
|
- return
|
|
|
- }
|
|
|
- c.Header("Content-Type", "application/octet-stream")
|
|
|
- c.Header("Content-Disposition", "attachment; filename="+fileName)
|
|
|
- c.Header("Content-Transfer-Encoding", "binary")
|
|
|
- c.File(path)
|
|
|
-}
|
|
|
-
|
|
|
-func StartFrpc(c *gin.Context) {
|
|
|
- if !cfg.ParamConf.Mode {
|
|
|
- AckHTML_OK(c)
|
|
|
- return
|
|
|
- }
|
|
|
- optDTUCtrl(c, dt.CMD_STARTFRP, "")
|
|
|
-}
|
|
|
-
|
|
|
-func SetFileFromHtml(c *gin.Context) {
|
|
|
- var ck = dt.RmtURLInfo{IsZip: false}
|
|
|
- //获取文件头
|
|
|
- file, err := c.FormFile("file")
|
|
|
- if err != nil {
|
|
|
- fmt.Println("ERROR: upload file failed. ", err)
|
|
|
- AckHTML_AbNormal(c, 90, "Request fail") //c.String(http.StatusBadRequest, "请求失败")
|
|
|
- return
|
|
|
- }
|
|
|
- //获取文件名
|
|
|
- fileName := uploadSave + filepath.Base(file.Filename) //"./file/" + file.Filename
|
|
|
- //保存文件到服务器本地
|
|
|
- //SaveUploadedFile(文件头,保存路径)
|
|
|
- if err := c.SaveUploadedFile(file, fileName); err != nil {
|
|
|
- AckHTML_AbNormal(c, 91, "Save fail") //c.String(http.StatusBadRequest, "保存失败 Error:%s", err.Error())
|
|
|
- return
|
|
|
- }
|
|
|
- //tarFile(fileName)
|
|
|
- //c.String(http.StatusOK, "上传文件成功")
|
|
|
- fmt.Println("fileName : " + fileName)
|
|
|
- new_name := fileName
|
|
|
- er, size := dt.GetFileSize(fileName)
|
|
|
- if er == nil && size > int64(1*1024*1024) {
|
|
|
- err, name := mcryp.NewTarGz(fileName)
|
|
|
- if err == nil {
|
|
|
- fmt.Println("new fileName : " + name)
|
|
|
- os.Remove(fileName)
|
|
|
- new_name = uploadSave + filepath.Base(name)
|
|
|
- ck.IsZip = true
|
|
|
- }
|
|
|
- }
|
|
|
- ck.Path = new_name
|
|
|
- //fmt.Println("ck : ", ck)
|
|
|
- re := encUrl(ck)
|
|
|
- AckHTML_OKWithMsg(c, re)
|
|
|
-}
|
|
|
-
|
|
|
-func SetPurifierState(c *gin.Context) {
|
|
|
- var (
|
|
|
- opt = ""
|
|
|
- onoff = ""
|
|
|
- )
|
|
|
- if !cfg.ParamConf.Mode {
|
|
|
- AckHTML_OK(c)
|
|
|
- return
|
|
|
- }
|
|
|
- sn := c.Request.URL.Query().Get("sn")
|
|
|
- st := c.Request.URL.Query().Get("state")
|
|
|
- mode := c.Request.URL.Query().Get("mode")
|
|
|
- if sn == "" || st == "" || mode == "" {
|
|
|
- ackHTML_ErrHtml(c, "sn state or mode is null")
|
|
|
- return
|
|
|
- }
|
|
|
- switch mode {
|
|
|
- case "0":
|
|
|
- opt = dt.CMD_PURIFIERSTATE
|
|
|
- break
|
|
|
- case "1":
|
|
|
- opt = dt.CMD_PURIFIERSTATEDO2
|
|
|
- break
|
|
|
- default:
|
|
|
- ackHTML_ErrHtml(c, "illegal mode")
|
|
|
- return
|
|
|
- }
|
|
|
- //fmt.Println(c.Request.URL.Query())
|
|
|
- if st == "on" {
|
|
|
- onoff = "on"
|
|
|
- } else {
|
|
|
- onoff = "off"
|
|
|
- }
|
|
|
- if res, ok := model.ModelDTUCtrl(sn, opt, onoff); !ok {
|
|
|
- AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
- } else {
|
|
|
- AckHTML_OK(c)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
/*
|
|
|
-var staticHandler http.Handler
|
|
|
-// 静态文件处理
|
|
|
-func StaticServer(w http.ResponseWriter, req *http.Request) {
|
|
|
- fmt.Println("path:" + req.URL.Path)
|
|
|
- staticHandler.ServeHTTP(w, req)
|
|
|
-}
|
|
|
-func init() {
|
|
|
- staticHandler = http.StripPrefix("/assets/", http.FileServer(http.Dir("radio")))
|
|
|
-}
|
|
|
-
|
|
|
-func CreateHTTPFileServer() { //已经有静态文件了
|
|
|
- http.HandleFunc("/assets/", StaticServer)
|
|
|
- err := http.ListenAndServe(":3000", nil)
|
|
|
- if err != nil {
|
|
|
- log.Fatal("ListenAndServe: ", err)
|
|
|
- }
|
|
|
-}*/
|
|
|
-
|
|
|
//获取通信机基本参数
|
|
|
func GetDTUBaseInfo(c *gin.Context) {
|
|
|
- sn := c.Request.URL.Query().Get("sn") //sn := c.Param("sn")
|
|
|
+ sn := c.Request.URL.Query().Get("sn")
|
|
|
if sn == "" {
|
|
|
- ackHTML_ErrHtml(c, "sn is null") //
|
|
|
- //c.JSON(http.StatusForbidden, dt.JsonToString(cerr))
|
|
|
- //c.String(http.StatusForbidden, "sn is null")
|
|
|
+ ackHTML_ErrHtml(c, "sn is null")
|
|
|
} else {
|
|
|
if res, ok := model.ModelDTUCtrl(sn, dt.CMD_GETDTUBASEINFO, ""); !ok {
|
|
|
AckHTML_AbNormal(c, dt.HTC_DTUErr, res)
|
|
|
@@ -1262,13 +707,9 @@ func GetDTUBaseInfo(c *gin.Context) {
|
|
|
loginfo dt.Loginfo
|
|
|
binfo dt.DTUBaseInfo
|
|
|
)
|
|
|
-
|
|
|
err := json.Unmarshal([]byte(res), &loginfo)
|
|
|
if err != nil {
|
|
|
AckHTML_AbNormal(c, dt.HTC_JSONFormat, err.Error())
|
|
|
- //c.JSON(http.StatusForbidden, dt.JsonToString(cerr))
|
|
|
- //log.Println("", err)
|
|
|
- //c.String(http.StatusForbidden, "json format error:", err.Error())
|
|
|
} else {
|
|
|
binfo.Version = loginfo.Version
|
|
|
binfo.Verplat = loginfo.Verplat
|
|
|
@@ -1283,259 +724,4 @@ func GetDTUBaseInfo(c *gin.Context) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-func upload(w http.ResponseWriter, req *http.Request) {
|
|
|
- maxsize := int64(10 * 1024 * 1024)
|
|
|
- contentType := req.Header.Get("content-type")
|
|
|
- sha256 := req.Header.Get("Content-SHA256") //req.PostFormValue("Content-SHA256")
|
|
|
- contentLen := req.ContentLength
|
|
|
- fmt.Printf("upload content-type:%s,content-length:%d,sha:%s\n", contentType, contentLen, sha256)
|
|
|
- if !strings.Contains(contentType, "multipart/form-data") {
|
|
|
- w.Write([]byte("content-type must be multipart/form-data"))
|
|
|
- return
|
|
|
- }
|
|
|
- if contentLen >= maxsize { // 10 MB
|
|
|
- w.Write([]byte("file to large,limit 10MB"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- err := req.ParseMultipartForm(maxsize)
|
|
|
- if err != nil {
|
|
|
- //http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
- w.Write([]byte("ParseMultipartForm error:" + err.Error()))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if len(req.MultipartForm.File) == 0 {
|
|
|
- w.Write([]byte("not have any file"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- for name, files := range req.MultipartForm.File {
|
|
|
- fmt.Printf("req.MultipartForm.File,name=%s", name)
|
|
|
-
|
|
|
- if len(files) != 1 {
|
|
|
- w.Write([]byte("too many files"))
|
|
|
- return
|
|
|
- }
|
|
|
- if name == "" {
|
|
|
- w.Write([]byte("is not FileData"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- for _, f := range files {
|
|
|
- handle, err := f.Open()
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte(fmt.Sprintf("unknown error,fileName=%s,fileSize=%d,err:%s", f.Filename, f.Size, err.Error())))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- path := "./" + f.Filename
|
|
|
- dst, _ := os.Create(path)
|
|
|
- io.Copy(dst, handle)
|
|
|
- dst.Close()
|
|
|
- fmt.Printf("successful uploaded,fileName=%s,fileSize=%.2f MB,savePath=%s \n", f.Filename, float64(contentLen)/1024/1024, path)
|
|
|
-
|
|
|
- err, cal := mcryp.GetFileSHA256Str(path)
|
|
|
- if err == nil {
|
|
|
- if cal != sha256 {
|
|
|
- log.Println("sha256 no match : " + sha256 + " " + cal)
|
|
|
- os.Remove(path)
|
|
|
- } else {
|
|
|
- log.Println("sha256 is match")
|
|
|
- }
|
|
|
- }
|
|
|
- w.Write([]byte("successful,url=" + url.QueryEscape(f.Filename)))
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Dealupload(w http.ResponseWriter, req *http.Request, c *gin.Context) {
|
|
|
- maxsize := int64(10 * 1024 * 1024)
|
|
|
- contentType := req.Header.Get("content-type")
|
|
|
- sha256 := req.Header.Get("Content-SHA256") //req.PostFormValue("Content-SHA256")
|
|
|
- contentLen := req.ContentLength
|
|
|
- fmt.Printf("upload content-type:%s,content-length:%d,sha:%s\n", contentType, contentLen, sha256)
|
|
|
- if !strings.Contains(contentType, "multipart/form-data") {
|
|
|
- w.Write([]byte("content-type must be multipart/form-data"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if contentLen >= maxsize { // 10 MB
|
|
|
- w.Write([]byte("file to large,limit 10MB"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- err := req.ParseMultipartForm(maxsize)
|
|
|
- if err != nil {
|
|
|
- //http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
- w.Write([]byte("ParseMultipartForm error:" + err.Error()))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if len(req.MultipartForm.File) == 0 {
|
|
|
- w.Write([]byte("not have any file"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- for name, files := range req.MultipartForm.File {
|
|
|
- fmt.Printf("req.MultipartForm.File,name=%s", name)
|
|
|
-
|
|
|
- if len(files) != 1 {
|
|
|
- w.Write([]byte("too many files"))
|
|
|
- return
|
|
|
- }
|
|
|
- if name == "" {
|
|
|
- w.Write([]byte("is not FileData"))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- for _, f := range files {
|
|
|
- handle, err := f.Open()
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte(fmt.Sprintf("unknown error,fileName=%s,fileSize=%d,err:%s", f.Filename, f.Size, err.Error())))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- path := "./" + f.Filename
|
|
|
- dst, _ := os.Create(path)
|
|
|
- io.Copy(dst, handle)
|
|
|
- dst.Close()
|
|
|
- fmt.Printf("successful uploaded,fileName=%s,fileSize=%.2f MB,savePath=%s \n", f.Filename, float64(contentLen)/1024/1024, path)
|
|
|
-
|
|
|
- err, cal := mcryp.GetFileSHA256Str(path)
|
|
|
- if err == nil {
|
|
|
- if cal != sha256 {
|
|
|
- log.Println("sha256 no match : " + sha256 + " " + cal)
|
|
|
- os.Remove(path)
|
|
|
- } else {
|
|
|
- log.Println("sha256 is match")
|
|
|
- }
|
|
|
- }
|
|
|
- w.Write([]byte("successful,url=" + url.QueryEscape(f.Filename)))
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func getContentType(fileName string) (extension, contentType string) {
|
|
|
- arr := strings.Split(fileName, ".")
|
|
|
- contentType = ""
|
|
|
- // see: https://tool.oschina.net/commons/
|
|
|
- if len(arr) >= 2 {
|
|
|
- extension = arr[len(arr)-1]
|
|
|
- switch extension {
|
|
|
- case "jpeg", "jpe", "jpg":
|
|
|
- contentType = "image/jpeg"
|
|
|
- case "png":
|
|
|
- contentType = "image/png"
|
|
|
- case "gif":
|
|
|
- contentType = "image/gif"
|
|
|
- case "mp4":
|
|
|
- contentType = "video/mpeg4"
|
|
|
- case "mp3":
|
|
|
- contentType = "audio/mp3"
|
|
|
- case "wav":
|
|
|
- contentType = "audio/wav"
|
|
|
- case "pdf":
|
|
|
- contentType = "application/pdf"
|
|
|
- case "doc", "":
|
|
|
- contentType = "application/msword"
|
|
|
- }
|
|
|
- }
|
|
|
- if contentType == "" {
|
|
|
- // .*( 二进制流,不知道下载文件类型)
|
|
|
- contentType = "application/octet-stream"
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-//加密URL信息
|
|
|
-func encUrl(n dt.RmtURLInfo) string {
|
|
|
- var t dt.RmtFilePack
|
|
|
- t.Info = dt.JsonToString(n)
|
|
|
- t.MD5 = mcryp.MD5Str(t.Info)
|
|
|
- r := dt.JsonToString(t)
|
|
|
- err, ret := mcryp.EncypAESEncode([]byte(r), []byte(keyEnUrl))
|
|
|
- if err != nil {
|
|
|
- return ""
|
|
|
- }
|
|
|
- return ret
|
|
|
-}
|
|
|
-
|
|
|
-//解密URL信息
|
|
|
-func decUrl(n string) (error, dt.RmtURLInfo) {
|
|
|
- var (
|
|
|
- t dt.RmtURLInfo
|
|
|
- c dt.RmtFilePack
|
|
|
- str = ""
|
|
|
- )
|
|
|
- if n == "" {
|
|
|
- return errors.New("null"), t
|
|
|
- }
|
|
|
- if err, ret := mcryp.DecypAESEncode(n, []byte(keyEnUrl)); err != nil {
|
|
|
- return errors.New("dec err"), t
|
|
|
- } else {
|
|
|
- str = string(ret)
|
|
|
- }
|
|
|
- if str == "" {
|
|
|
- return errors.New("null"), t
|
|
|
- }
|
|
|
- if err := json.Unmarshal([]byte(str), &c); err != nil {
|
|
|
- return errors.New("json err"), t
|
|
|
- }
|
|
|
- md5 := mcryp.MD5Str(c.Info)
|
|
|
- if md5 != c.MD5 {
|
|
|
- return errors.New("chech err"), t
|
|
|
- }
|
|
|
- if err := json.Unmarshal([]byte(c.Info), &t); err != nil {
|
|
|
- return errors.New("json err"), t
|
|
|
- }
|
|
|
- return nil, t
|
|
|
-}
|
|
|
-
|
|
|
-/*
|
|
|
-func download(w http.ResponseWriter, req *http.Request) {
|
|
|
- //if req.RequestURI == "/favicon.ico" {
|
|
|
- //return
|
|
|
- //}
|
|
|
- fmt.Println("download url : " + req.RequestURI)
|
|
|
- filename := filepath.Base(req.RequestURI) //req.RequestURI[1:]
|
|
|
- fmt.Println(filename)
|
|
|
- enEscapeUrl, err := url.QueryUnescape(filename)
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte(err.Error()))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- f, err := os.Open("./" + enEscapeUrl)
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte(err.Error()))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- info, err := f.Stat()
|
|
|
- if err != nil {
|
|
|
- w.Write([]byte(err.Error()))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- _, contentType := getContentType(filename)
|
|
|
- w.Header().Set("Content-Disposition", "attachment; filename="+filename)
|
|
|
- //w.Header().Set("Content-Type", http.DetectContentType(fileHeader))
|
|
|
- w.Header().Set("Content-Type", contentType)
|
|
|
- w.Header().Set("Content-Length", strconv.FormatInt(info.Size(), 10))
|
|
|
- err, sha := mcryp.GetFileSHA256Str(filename)
|
|
|
- w.Header().Set("Content-SHA256", sha)
|
|
|
- if err != nil {
|
|
|
-
|
|
|
- }
|
|
|
- f.Seek(0, 0)
|
|
|
- io.Copy(w, f)
|
|
|
}*/
|
|
|
-
|
|
|
-func CreateHTTPFileServer_2() {
|
|
|
- http.HandleFunc("/file/upload", upload)
|
|
|
- //http.HandleFunc("/", download)
|
|
|
- http.ListenAndServe(fmt.Sprintf(":%d", cfg.ParamConf.HtPort), nil)
|
|
|
-}
|