From 127bc54a3dcf9ff8f8d880f60ae46a9d745a5c51 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 17 Jun 2024 15:17:32 +0800 Subject: [PATCH] =?UTF-8?q?del:=20=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/constants/cachekey/cachekey.go | 24 -- lib/core/{utils => }/ctx/ctx.go | 0 lib/core/datasource/datasource.go | 49 --- lib/core/datasource/repo.go | 126 -------- lib/core/mml_client/mml_client.go | 87 ------ lib/core/mml_client/send.go | 105 ------- lib/core/utils/date/date.go | 70 ----- lib/core/utils/parse/parse.go | 139 --------- lib/core/utils/regular/regular.go | 54 ---- lib/core/utils/scan/scan.go | 31 -- lib/core/vo/login_user.go | 2 - lib/core/vo/router.go | 17 -- lib/core/vo/router_meta.go | 17 -- lib/core/vo/treeselect.go | 36 --- lib/midware/arrow_ip_addr.go | 78 ----- lib/midware/authorize.go | 229 -------------- lib/midware/cors.go | 67 ----- lib/wsinfo/client.go | 45 --- lib/wsinfo/process_data.go | 382 ------------------------ 19 files changed, 1558 deletions(-) delete mode 100644 lib/core/constants/cachekey/cachekey.go rename lib/core/{utils => }/ctx/ctx.go (100%) delete mode 100644 lib/core/datasource/datasource.go delete mode 100644 lib/core/datasource/repo.go delete mode 100644 lib/core/mml_client/mml_client.go delete mode 100644 lib/core/mml_client/send.go delete mode 100644 lib/core/utils/date/date.go delete mode 100644 lib/core/utils/parse/parse.go delete mode 100644 lib/core/utils/regular/regular.go delete mode 100644 lib/core/utils/scan/scan.go delete mode 100644 lib/core/vo/router.go delete mode 100644 lib/core/vo/router_meta.go delete mode 100644 lib/core/vo/treeselect.go delete mode 100644 lib/midware/arrow_ip_addr.go delete mode 100644 lib/midware/authorize.go delete mode 100644 lib/midware/cors.go delete mode 100644 lib/wsinfo/client.go delete mode 100644 lib/wsinfo/process_data.go diff --git a/lib/core/constants/cachekey/cachekey.go b/lib/core/constants/cachekey/cachekey.go deleted file mode 100644 index 0e69445f..00000000 --- a/lib/core/constants/cachekey/cachekey.go +++ /dev/null @@ -1,24 +0,0 @@ -package cachekey - -// 缓存的key常量 - -// 登录用户 -const LOGIN_TOKEN_KEY = "login_tokens:" - -// 验证码 -const CAPTCHA_CODE_KEY = "captcha_codes:" - -// 参数管理 -const SYS_CONFIG_KEY = "sys_config:" - -// 字典管理 -const SYS_DICT_KEY = "sys_dict:" - -// 防重提交 -const REPEAT_SUBMIT_KEY = "repeat_submit:" - -// 限流 -const RATE_LIMIT_KEY = "rate_limit:" - -// 登录账户密码错误次数 -const PWD_ERR_CNT_KEY = "pwd_err_cnt:" diff --git a/lib/core/utils/ctx/ctx.go b/lib/core/ctx/ctx.go similarity index 100% rename from lib/core/utils/ctx/ctx.go rename to lib/core/ctx/ctx.go diff --git a/lib/core/datasource/datasource.go b/lib/core/datasource/datasource.go deleted file mode 100644 index 1ef16af8..00000000 --- a/lib/core/datasource/datasource.go +++ /dev/null @@ -1,49 +0,0 @@ -package datasource - -import ( - "database/sql" - "regexp" - - "be.ems/lib/dborm" - "xorm.io/xorm" -) - -// 获取默认数据源 -func DefaultDB() *xorm.Engine { - return dborm.DbClient.XEngine -} - -// RawDB 原生查询语句 -func RawDB(source string, sql string, parameters []any) ([]map[string]any, error) { - // 数据源 - db := DefaultDB() - - // 使用正则表达式替换连续的空白字符为单个空格 - fmtSql := regexp.MustCompile(`\s+`).ReplaceAllString(sql, " ") - - // log.Infof("sql=> %v", fmtSql) - // log.Infof("parameters=> %v", parameters) - - // 查询结果 - var rows []map[string]any - err := db.SQL(fmtSql, parameters...).Find(&rows) - if err != nil { - return nil, err - } - return rows, nil -} - -// ExecDB 原生执行语句 -func ExecDB(source string, sql string, parameters []any) (sql.Result, error) { - // 数据源 - db := DefaultDB() - - // 使用正则表达式替换连续的空白字符为单个空格 - fmtSql := regexp.MustCompile(`\s+`).ReplaceAllString(sql, " ") - // 执行结果 - res, err := db.Exec(append([]any{fmtSql}, parameters...)...) - if err != nil { - return nil, err - } - return res, err -} diff --git a/lib/core/datasource/repo.go b/lib/core/datasource/repo.go deleted file mode 100644 index 7b81a4e1..00000000 --- a/lib/core/datasource/repo.go +++ /dev/null @@ -1,126 +0,0 @@ -package datasource - -import ( - "fmt" - "reflect" - "strconv" - "strings" -) - -// PageNumSize 分页页码记录数 -func PageNumSize(pageNum, pageSize any) (int, int) { - // 记录起始索引 - pageNumStr := fmt.Sprintf("%v", pageNum) - num := 1 - if v, err := strconv.Atoi(pageNumStr); err == nil && v > 0 { - num = v - } - - // 显示记录数 - pageSizeStr := fmt.Sprintf("%v", pageSize) - size := 10 - if v, err := strconv.Atoi(pageSizeStr); err == nil && v > 0 { - size = v - } - return num - 1, size -} - -// SetFieldValue 判断结构体内是否存在指定字段并设置值 -func SetFieldValue(obj any, fieldName string, value any) { - // 获取结构体的反射值 - userValue := reflect.ValueOf(obj) - - // 获取字段的反射值 - fieldValue := userValue.Elem().FieldByName(fieldName) - - // 检查字段是否存在 - if fieldValue.IsValid() && fieldValue.CanSet() { - // 获取字段的类型 - fieldType := fieldValue.Type() - - // 转换传入的值类型为字段类型 - switch fieldType.Kind() { - case reflect.String: - if value == nil { - fieldValue.SetString("") - } else { - fieldValue.SetString(fmt.Sprintf("%v", value)) - } - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - intValue, err := strconv.ParseInt(fmt.Sprintf("%v", value), 10, 64) - if err != nil { - intValue = 0 - } - fieldValue.SetInt(intValue) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - uintValue, err := strconv.ParseUint(fmt.Sprintf("%v", value), 10, 64) - if err != nil { - uintValue = 0 - } - fieldValue.SetUint(uintValue) - case reflect.Float32, reflect.Float64: - floatValue, err := strconv.ParseFloat(fmt.Sprintf("%v", value), 64) - if err != nil { - floatValue = 0 - } - fieldValue.SetFloat(floatValue) - default: - // 设置字段的值 - fieldValue.Set(reflect.ValueOf(value).Convert(fieldValue.Type())) - } - } -} - -// ConvertIdsSlice 将 []string 转换为 []any -func ConvertIdsSlice(ids []string) []any { - // 将 []string 转换为 []any - arr := make([]any, len(ids)) - for i, v := range ids { - arr[i] = v - } - return arr -} - -// 查询-参数值的占位符 -func KeyPlaceholderByQuery(sum int) string { - placeholders := make([]string, sum) - for i := 0; i < sum; i++ { - placeholders[i] = "?" - } - return strings.Join(placeholders, ",") -} - -// 插入-参数映射键值占位符 keys, placeholder, values -func KeyPlaceholderValueByInsert(params map[string]any) ([]string, string, []any) { - // 参数映射的键 - keys := make([]string, len(params)) - // 参数映射的值 - values := make([]any, len(params)) - sum := 0 - for k, v := range params { - keys[sum] = k - values[sum] = v - sum++ - } - // 参数值的占位符 - placeholders := make([]string, sum) - for i := 0; i < sum; i++ { - placeholders[i] = "?" - } - return keys, strings.Join(placeholders, ","), values -} - -// 更新-参数映射键值占位符 keys, values -func KeyValueByUpdate(params map[string]any) ([]string, []any) { - // 参数映射的键 - keys := make([]string, len(params)) - // 参数映射的值 - values := make([]any, len(params)) - sum := 0 - for k, v := range params { - keys[sum] = k + "=?" - values[sum] = v - sum++ - } - return keys, values -} diff --git a/lib/core/mml_client/mml_client.go b/lib/core/mml_client/mml_client.go deleted file mode 100644 index a0040244..00000000 --- a/lib/core/mml_client/mml_client.go +++ /dev/null @@ -1,87 +0,0 @@ -package mmlclient - -import ( - "bufio" - "fmt" - "io" - "net" - "time" - - "be.ems/lib/core/conf" -) - -// 定义MMLClient结构体 -type MMLClient struct { - awaitTime time.Duration // 等待时间 - conn net.Conn - reader *bufio.Reader - size int // 包含字符 -} - -// 封装NewMMLClient函数,用于创建MMLClient实例 -// 网元UDM的IP地址 "198.51.100.1" -func NewMMLClient(ip string) (*MMLClient, error) { - // 创建TCP连接 - portMML := conf.Get("mml.port").(int) - hostMML := fmt.Sprintf("%s:%d", ip, portMML) - conn, err := net.Dial("tcp", hostMML) - if err != nil { - return nil, err - } - - // 进行登录 - usernameMML := conf.Get("mml.user").(string) - passwordMML := conf.Get("mml.password").(string) - fmt.Fprintln(conn, usernameMML) - fmt.Fprintln(conn, passwordMML) - - // 发送后等待 - awaitTime := time.Duration(300) * time.Millisecond - time.Sleep(awaitTime) - - // 读取内容 - buf := make([]byte, 1024*1024*1) - n, err := conn.Read(buf) - if err != nil { - return nil, err - } - - // 创建MMLClient实例 - client := &MMLClient{ - conn: conn, - reader: bufio.NewReader(conn), - awaitTime: awaitTime, - size: n, - } - - return client, nil -} - -// 封装Send函数,用于向TCP连接发送数据 -func (c *MMLClient) Send(msg string) error { - _, err := fmt.Fprintln(c.conn, msg) - if err != nil { - return err - } - time.Sleep(c.awaitTime) - return nil -} - -// 封装Receive函数,用于从TCP连接中接收数据 -func (c *MMLClient) Receive() (string, error) { - buf := make([]byte, 1024*1024*1) - n, err := c.reader.Read(buf) - if err != nil { - if err == io.EOF { - return "", fmt.Errorf("server closed the connection") - } - return "", err - } - c.size += n - return string(buf[0:n]), nil -} - -// 封装Close函数,用于关闭TCP连接 -func (c *MMLClient) Close() error { - return c.conn.Close() -} diff --git a/lib/core/mml_client/send.go b/lib/core/mml_client/send.go deleted file mode 100644 index 943772c4..00000000 --- a/lib/core/mml_client/send.go +++ /dev/null @@ -1,105 +0,0 @@ -package mmlclient - -import ( - "fmt" - "strings" -) - -// 发送MML原始消息 -// ip 网元IP地址 -// msg 指令 -func MMLSendMsg(ip, msg string) (string, error) { - // 创建MMLClient实例 - client, err := NewMMLClient(ip) - if err != nil { - return "", fmt.Errorf("failed to create MMLClient instance: %v", err) - } - defer client.Close() - - // 发送数据 - err = client.Send(msg) - if err != nil { - return "", fmt.Errorf("sending data failed: %v", err) - } - - // 接收数据 - data, err := client.Receive() - if err != nil { - return "", fmt.Errorf("failed to receive data: %v", err) - } - - return data, nil -} - -// 发送MML -// ip 网元IP地址 -// msg 指令 -func MMLSendMsgToString(ip, msg string) (string, error) { - // 发送获取数据 - str, err := MMLSendMsg(ip, msg) - if err != nil { - return "", err - } - - str = strings.ToLower(str) - - // 截断 - index := strings.Index(str, "\n") - if index != -1 { - str = str[:index] - } - - // 命令成功 - if strings.Contains(str, "ok") || strings.Contains(str, "success") { - return str, nil - } - - return "", fmt.Errorf(str) -} - -// 发送MML -// ip 网元IP地址 -// msg 指令 -func MMLSendMsgToMap(ip, msg string) (map[string]string, error) { - // 发送获取数据 - str, err := MMLSendMsg(ip, msg) - if err != nil { - return nil, err - } - - // 无数据 - if strings.HasPrefix(str, "No Auth Data") { - return nil, fmt.Errorf("no auth data") - } - - // 初始化一个map用于存储拆分后的键值对 - m := make(map[string]string) - - var items []string - if strings.Contains(str, "\r\n") { - // 按照分隔符"\r\n"进行拆分 - items = strings.Split(str, "\r\n") - } else if strings.Contains(str, "\n") { - // 按照分隔符"\n"进行拆分 - items = strings.Split(str, "\n") - } - - // 遍历拆分后的结果 - for _, item := range items { - var pair []string - - if strings.Contains(item, "=") { - // 按照分隔符"="进行拆分键值对 - pair = strings.SplitN(item, "=", 2) - } else if strings.Contains(item, ":") { - // 按照分隔符":"进行拆分键值对 - pair = strings.SplitN(item, ":", 2) - } - - if len(pair) == 2 { - // 将键值对存入map中 - m[pair[0]] = pair[1] - } - } - return m, err -} diff --git a/lib/core/utils/date/date.go b/lib/core/utils/date/date.go deleted file mode 100644 index d6235ea4..00000000 --- a/lib/core/utils/date/date.go +++ /dev/null @@ -1,70 +0,0 @@ -package date - -import ( - "fmt" - "time" - - "be.ems/lib/log" -) - -const ( - // 年 列如:2022 - YYYY = "2006" - // 年-月 列如:2022-12 - YYYY_MM = "2006-01" - // 年-月-日 列如:2022-12-30 - YYYY_MM_DD = "2006-01-02" - // 年月日时分秒 列如:20221230010159 - YYYYMMDDHHMMSS = "20060102150405" - // 年-月-日 时:分:秒 列如:2022-12-30 01:01:59 - YYYY_MM_DD_HH_MM_SS = "2006-01-02 15:04:05" -) - -// 格式时间字符串 -// -// dateStr 时间字符串 -// -// formatStr 时间格式 默认YYYY-MM-DD HH:mm:ss -func ParseStrToDate(dateStr, formatStr string) time.Time { - t, err := time.Parse(formatStr, dateStr) - if err != nil { - log.Infof("utils ParseStrToDate err %v", err) - return time.Time{} - } - return t -} - -// 格式时间 -// -// date 可转的Date对象 -// -// formatStr 时间格式 默认YYYY-MM-DD HH:mm:ss -func ParseDateToStr(date any, formatStr string) string { - t, ok := date.(time.Time) - if !ok { - switch v := date.(type) { - case int64: - if v == 0 { - return "" - } - t = time.UnixMilli(v) - case string: - parsedTime, err := time.Parse(formatStr, v) - if err != nil { - fmt.Printf("utils ParseDateToStr err %v \n", err) - return "" - } - t = parsedTime - default: - return "" - } - } - return t.Format(formatStr) -} - -// 格式时间成日期路径 -// -// 年/月 列如:2022/12 -func ParseDatePath(date time.Time) string { - return date.Format("2006/01") -} diff --git a/lib/core/utils/parse/parse.go b/lib/core/utils/parse/parse.go deleted file mode 100644 index ed2fd5dd..00000000 --- a/lib/core/utils/parse/parse.go +++ /dev/null @@ -1,139 +0,0 @@ -package parse - -import ( - "fmt" - "reflect" - "regexp" - "strconv" - "strings" - "time" - - "github.com/robfig/cron/v3" -) - -// Number 解析数值型 -func Number(str any) int64 { - switch str := str.(type) { - case string: - if str == "" { - return 0 - } - num, err := strconv.ParseInt(str, 10, 64) - if err != nil { - return 0 - } - return num - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: - return reflect.ValueOf(str).Int() - case float32, float64: - return int64(reflect.ValueOf(str).Float()) - default: - return 0 - } -} - -// Boolean 解析布尔型 -func Boolean(str any) bool { - switch str := str.(type) { - case string: - if str == "" || str == "false" || str == "0" { - return false - } - // 尝试将字符串解析为数字 - if num, err := strconv.ParseFloat(str, 64); err == nil { - return num != 0 - } - return true - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: - num := reflect.ValueOf(str).Int() - return num != 0 - case float32, float64: - num := reflect.ValueOf(str).Float() - return num != 0 - default: - return false - } -} - -// FirstUpper 首字母转大写 -// -// 字符串 abc_123!@# 结果 Abc_123 -func FirstUpper(str string) string { - if len(str) == 0 { - return str - } - reg := regexp.MustCompile(`[^_\w]+`) - str = reg.ReplaceAllString(str, "") - return strings.ToUpper(str[:1]) + str[1:] -} - -// Bit 比特位为单位 -func Bit(bit float64) string { - var GB, MB, KB string - - if bit > float64(1<<30) { - GB = fmt.Sprintf("%0.2f", bit/(1<<30)) - } - - if bit > float64(1<<20) && bit < (1<<30) { - MB = fmt.Sprintf("%.2f", bit/(1<<20)) - } - - if bit > float64(1<<10) && bit < (1<<20) { - KB = fmt.Sprintf("%.2f", bit/(1<<10)) - } - - if GB != "" { - return GB + "GB" - } else if MB != "" { - return MB + "MB" - } else if KB != "" { - return KB + "KB" - } else { - return fmt.Sprintf("%vB", bit) - } -} - -// CronExpression 解析 Cron 表达式,返回下一次执行的时间戳(毫秒) -// -// 【*/5 * * * * ?】 6个参数 -func CronExpression(expression string) int64 { - specParser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor) - schedule, err := specParser.Parse(expression) - if err != nil { - fmt.Println(err) - return 0 - } - return schedule.Next(time.Now()).UnixMilli() -} - -// SafeContent 内容值进行安全掩码 -func SafeContent(value string) string { - if len(value) < 3 { - return strings.Repeat("*", len(value)) - } else if len(value) < 6 { - return string(value[0]) + strings.Repeat("*", len(value)-1) - } else if len(value) < 10 { - return string(value[0]) + strings.Repeat("*", len(value)-2) + string(value[len(value)-1]) - } else if len(value) < 15 { - return value[:2] + strings.Repeat("*", len(value)-4) + value[len(value)-2:] - } else { - return value[:3] + strings.Repeat("*", len(value)-6) + value[len(value)-3:] - } -} - -// RemoveDuplicates 数组内字符串去重 -func RemoveDuplicates(ids []string) []string { - uniqueIDs := make(map[string]bool) - uniqueIDSlice := make([]string, 0) - - for _, id := range ids { - _, ok := uniqueIDs[id] - if !ok && id != "" { - uniqueIDs[id] = true - uniqueIDSlice = append(uniqueIDSlice, id) - } - } - - return uniqueIDSlice -} diff --git a/lib/core/utils/regular/regular.go b/lib/core/utils/regular/regular.go deleted file mode 100644 index e4b9cc66..00000000 --- a/lib/core/utils/regular/regular.go +++ /dev/null @@ -1,54 +0,0 @@ -package regular - -import ( - "regexp" -) - -// Replace 正则替换 -func Replace(originStr, pattern, repStr string) string { - regex := regexp.MustCompile(pattern) - return regex.ReplaceAllString(originStr, repStr) -} - -// 判断是否为有效用户名格式 -// -// 用户名不能以数字开头,可包含大写小写字母,数字,且不少于5位 -func ValidUsername(username string) bool { - if username == "" { - return false - } - pattern := `^[a-zA-Z][a-z0-9A-Z]{5,}` - match, err := regexp.MatchString(pattern, username) - if err != nil { - return false - } - return match -} - -// 判断是否为有效手机号格式,1开头的11位手机号 -func ValidMobile(mobile string) bool { - if mobile == "" { - return false - } - pattern := `^1[3|4|5|6|7|8|9][0-9]\d{8}$` - match, err := regexp.MatchString(pattern, mobile) - if err != nil { - return false - } - return match -} - -// 判断是否为http(s)://开头 -// -// link 网络链接 -func ValidHttp(link string) bool { - if link == "" { - return false - } - pattern := `^http(s)?:\/\/+` - match, err := regexp.MatchString(pattern, link) - if err != nil { - return false - } - return match -} diff --git a/lib/core/utils/scan/scan.go b/lib/core/utils/scan/scan.go deleted file mode 100644 index 6313763a..00000000 --- a/lib/core/utils/scan/scan.go +++ /dev/null @@ -1,31 +0,0 @@ -package scan - -import ( - "net" - "strconv" -) - -func ScanPort(port int) bool { - ln, err := net.Listen("tcp", ":"+strconv.Itoa(port)) - if err != nil { - return true - } - defer ln.Close() - return false -} - -func ScanUDPPort(port int) bool { - ln, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: port}) - if err != nil { - return true - } - defer ln.Close() - return false -} - -func ScanPortWithProto(port int, proto string) bool { - if proto == "udp" { - return ScanUDPPort(port) - } - return ScanPort(port) -} diff --git a/lib/core/vo/login_user.go b/lib/core/vo/login_user.go index 1d4c9776..0b4899f4 100644 --- a/lib/core/vo/login_user.go +++ b/lib/core/vo/login_user.go @@ -23,6 +23,4 @@ type LoginUser struct { // User 用户信息 User dborm.User `json:"user"` - - Session dborm.Session `json:"-"` } diff --git a/lib/core/vo/router.go b/lib/core/vo/router.go deleted file mode 100644 index a3cc4083..00000000 --- a/lib/core/vo/router.go +++ /dev/null @@ -1,17 +0,0 @@ -package vo - -// Router 路由信息对象 -type Router struct { - // 路由名字 英文首字母大写 - Name string `json:"name"` - // 路由地址 - Path string `json:"path"` - // 其他元素 - Meta RouterMeta `json:"meta"` - // 组件地址 - Component string `json:"component"` - // 重定向地址 - Redirect string `json:"redirect"` - // 子路由 - Children []Router `json:"children,omitempty"` -} diff --git a/lib/core/vo/router_meta.go b/lib/core/vo/router_meta.go deleted file mode 100644 index b3447e05..00000000 --- a/lib/core/vo/router_meta.go +++ /dev/null @@ -1,17 +0,0 @@ -package vo - -// RouterMeta 路由元信息对象 -type RouterMeta struct { - // 设置该菜单在侧边栏和面包屑中展示的名字 - Title string `json:"title"` - // 设置该菜单的图标 - Icon string `json:"icon"` - // 设置为true,则不会被 缓存 - Cache bool `json:"cache"` - // 内链地址(http(s)://开头), 打开目标位置 '_blank' | '_self' | '' - Target string `json:"target"` - // 在菜单中隐藏子节点 - HideChildInMenu bool `json:"hideChildInMenu"` - // 在菜单中隐藏自己和子节点 - HideInMenu bool `json:"hideInMenu"` -} diff --git a/lib/core/vo/treeselect.go b/lib/core/vo/treeselect.go deleted file mode 100644 index 26776268..00000000 --- a/lib/core/vo/treeselect.go +++ /dev/null @@ -1,36 +0,0 @@ -package vo - -// import sysmenu "be.ems/features/sys_menu" - -// TreeSelect 树结构实体类 -type TreeSelect struct { - // ID 节点ID - ID string `json:"id"` - - // Label 节点名称 - Label string `json:"label"` - - // Title 节点名称旧版本layui - Title string `json:"title"` - - // Children 子节点 - Children []TreeSelect `json:"children"` -} - -// // SysMenuTreeSelect 使用给定的 SysMenu 对象解析为 TreeSelect 对象 -// func SysMenuTreeSelect(sysMenu sysmenu.SysMenu) TreeSelect { -// t := TreeSelect{} -// t.ID = sysMenu.MenuID -// t.Label = sysMenu.MenuName - -// if len(sysMenu.Children) > 0 { -// for _, menu := range sysMenu.Children { -// child := SysMenuTreeSelect(menu) -// t.Children = append(t.Children, child) -// } -// } else { -// t.Children = []TreeSelect{} -// } - -// return t -// } diff --git a/lib/midware/arrow_ip_addr.go b/lib/midware/arrow_ip_addr.go deleted file mode 100644 index c9c7a32c..00000000 --- a/lib/midware/arrow_ip_addr.go +++ /dev/null @@ -1,78 +0,0 @@ -package midware - -import ( - "encoding/json" - "net/http" - "strings" - "time" - - "be.ems/lib/dborm" - "be.ems/lib/services" -) - -// 已禁用 -// 登录策略限制登录时间和访问ip范围 -func ArrowIPAddr(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ipAddr := strings.Split(r.RemoteAddr, ":")[0] - - // 读取配置信息 登录策略设置 - result, err := dborm.XormGetConfig("Security", "loginSecurity") - if err != nil { - next.ServeHTTP(w, r) - return - } - data := make(map[string]any) - err = json.Unmarshal([]byte(result["value_json"].(string)), &data) - if err != nil { - next.ServeHTTP(w, r) - return - } - - // 开关 - switchStr := data["switch"].(string) - if switchStr == "0" { - next.ServeHTTP(w, r) - return - } - - ipRange := data["ipRange"].(string) - logintimeRange := data["logintime_range"].(string) - - // 检查ip - ips := strings.Split(ipRange, "/") - hasIP := false - for _, ip := range ips { - if ipAddr == ip { - hasIP = true - } - } - if !hasIP { - services.ResponseErrorWithJson(w, 502, "网关登录策略-IP限制: "+ipAddr) - return - } - - // 检查开放时间 - logintimeRangeArr := strings.Split(logintimeRange, " - ") - - // 加载中国时区 - loc, _ := time.LoadLocation("Asia/Shanghai") - // 获取当前时间 - currentTime := time.Now().In(loc) - - // 获取当前日期 - currentDate := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location()) - ymd := currentDate.Format("2006-01-02") - - // 定义开始时间和结束时间 - startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", ymd+" "+logintimeRangeArr[0], loc) - endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", ymd+" "+logintimeRangeArr[1], loc) - - // 判断当前时间是否在范围内 - if currentTime.After(startTime) && currentTime.Before(endTime) { - next.ServeHTTP(w, r) - } else { - services.ResponseErrorWithJson(w, 502, "网关登录策略-不在开放时间范围内") - } - }) -} diff --git a/lib/midware/authorize.go b/lib/midware/authorize.go deleted file mode 100644 index b56e9c39..00000000 --- a/lib/midware/authorize.go +++ /dev/null @@ -1,229 +0,0 @@ -package midware - -import ( - "context" - "fmt" - "net/http" - - "be.ems/lib/core/cache" - "be.ems/lib/core/utils/ctx" - "be.ems/lib/core/vo" - "be.ems/lib/core/vo/result" - "be.ems/lib/dborm" - commonConstants "be.ems/src/framework/constants/common" - tokenUtils "be.ems/src/framework/utils/token" -) - -// 已禁用由Gin部分接管 -// Authorize 用户身份授权认证校验 -// -// 只需含有其中角色 "hasRoles": {"xxx"}, -// -// 只需含有其中权限 "hasPerms": {"xxx"}, -// -// 同时匹配其中角色 "matchRoles": {"xxx"}, -// -// 同时匹配其中权限 "matchPerms": {"xxx"}, -func Authorize(options map[string][]string) func(http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // 获取请求头标识信息 - tokenStr := ctx.Authorization(r) - // 获取请求头标识信息-旧头 - accessToken := r.Header.Get("AccessToken") - if tokenStr == "" && accessToken != "" { - // 验证令牌 == 这里直接查数据库session - if !dborm.XormExistValidToken(accessToken, 0) { - ctx.JSON(w, 401, result.CodeMsg(401, "Invalid identity authorization valid error")) - return - } - se, err := dborm.XormUpdateSessionShakeTime(accessToken) - if err != nil { - ctx.JSON(w, 401, result.CodeMsg(401, "Invalid identity authorization shake error")) - return - } - - // 获取缓存的用户信息 - data, ok := cache.GetLocalTTL(se.AccountId) - if data == nil || !ok { - ctx.JSON(w, 401, result.CodeMsg(401, "Invalid identity authorization info error")) - return - } - loginUser := data.(vo.LoginUser) - - // 登录用户角色权限校验 - if options != nil { - var roles []string - for _, item := range loginUser.User.Roles { - roles = append(roles, item.RoleKey) - } - perms := loginUser.Permissions - verifyOk := verifyRolePermission(roles, perms, options) - if !verifyOk { - msg := fmt.Sprintf("Unauthorized access %s %s", r.Method, r.RequestURI) - ctx.JSON(w, 403, result.CodeMsg(403, msg)) - return - } - } - - // 在请求的 Context 中存储数据 - rContext := r.Context() - rContext = context.WithValue(rContext, ctx.ContextKey(commonConstants.CTX_LOGIN_USER), loginUser) - // 继续处理请求 - next.ServeHTTP(w, r.WithContext(rContext)) - return - } - - // 获取请求头标识信息 - if tokenStr == "" { - ctx.JSON(w, 401, result.CodeMsg(401, "Invalid identity authorization token error")) - return - } - - // 验证令牌 - claims, err := tokenUtils.Verify(tokenStr) - if err != nil { - ctx.JSON(w, 401, result.CodeMsg(401, "Invalid identity authorization valid error")) - return - } - - // 获取缓存的用户信息 - loginUser := tokenUtils.LoginUser(claims) - if loginUser.UserID == "" { - ctx.JSON(w, 401, result.CodeMsg(401, "Invalid identity authorization shake error")) - return - } - - // 检查刷新有效期后存入上下文 - tokenUtils.RefreshIn(&loginUser) - - // 登录用户角色权限校验 - if options != nil { - var roles []string - for _, item := range loginUser.User.Roles { - roles = append(roles, item.RoleKey) - } - perms := loginUser.Permissions - verifyOk := verifyRolePermission(roles, perms, options) - if !verifyOk { - msg := fmt.Sprintf("Unauthorized access %s %s", r.Method, r.RequestURI) - ctx.JSON(w, 403, result.CodeMsg(403, msg)) - return - } - } - - // 在请求的 Context 中存储数据 - rContext := r.Context() - rContext = context.WithValue(rContext, ctx.ContextKey(commonConstants.CTX_LOGIN_USER), loginUser) - // 继续处理请求 - next.ServeHTTP(w, r.WithContext(rContext)) - }) - } -} - -// verifyRolePermission 校验角色权限是否满足 -// -// roles 角色字符数组 -// -// perms 权限字符数组 -// -// options 参数 -func verifyRolePermission(roles, perms []string, options map[string][]string) bool { - // 直接放行 管理员角色或任意权限 - if contains(roles, "admin") || contains(perms, "*:*:*") { - return true - } - opts := make([]bool, 4) - - // 只需含有其中角色 - hasRole := false - if arr, ok := options["hasRoles"]; ok && len(arr) > 0 { - hasRole = some(roles, arr) - opts[0] = true - } - - // 只需含有其中权限 - hasPerms := false - if arr, ok := options["hasPerms"]; ok && len(arr) > 0 { - hasPerms = some(perms, arr) - opts[1] = true - } - - // 同时匹配其中角色 - matchRoles := false - if arr, ok := options["matchRoles"]; ok && len(arr) > 0 { - matchRoles = every(roles, arr) - opts[2] = true - } - - // 同时匹配其中权限 - matchPerms := false - if arr, ok := options["matchPerms"]; ok && len(arr) > 0 { - matchPerms = every(perms, arr) - opts[3] = true - } - - // 同时判断 含有其中 - if opts[0] && opts[1] { - return hasRole || hasPerms - } - // 同时判断 匹配其中 - if opts[2] && opts[3] { - return matchRoles && matchPerms - } - // 同时判断 含有其中且匹配其中 - if opts[0] && opts[3] { - return hasRole && matchPerms - } - if opts[1] && opts[2] { - return hasPerms && matchRoles - } - - return hasRole || hasPerms || matchRoles || matchPerms -} - -// contains 检查字符串数组中是否包含指定的字符串 -func contains(arr []string, target string) bool { - for _, str := range arr { - if str == target { - return true - } - } - return false -} - -// some 检查字符串数组中含有其中一项 -func some(origin []string, target []string) bool { - has := false - for _, t := range target { - for _, o := range origin { - if t == o { - has = true - break - } - } - if has { - break - } - } - return has -} - -// every 检查字符串数组中同时包含所有项 -func every(origin []string, target []string) bool { - match := true - for _, t := range target { - found := false - for _, o := range origin { - if t == o { - found = true - break - } - } - if !found { - match = false - break - } - } - return match -} diff --git a/lib/midware/cors.go b/lib/midware/cors.go deleted file mode 100644 index a9243694..00000000 --- a/lib/midware/cors.go +++ /dev/null @@ -1,67 +0,0 @@ -package midware - -import ( - "net/http" - "strings" -) - -// 已禁用由Gin部分接管 -// Cors 跨域 -func Cors(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // 设置Vary头部 - w.Header().Set("Vary", "Origin") - w.Header().Set("Keep-Alive", "timeout=5") - - requestOrigin := r.Header.Get("Origin") - if requestOrigin == "" { - next.ServeHTTP(w, r) - return - } - - w.Header().Set("Access-Control-Allow-Origin", "*") - - w.Header().Set("Access-Control-Allow-Credentials", "true") - - // OPTIONS - if r.Method == "OPTIONS" { - requestMethod := r.Header.Get("Access-Control-Request-Method") - if requestMethod == "" { - next.ServeHTTP(w, r) - return - } - - // 响应最大时间值 - w.Header().Set("Access-Control-Max-Age", "31536000") - - // 允许方法 - allowMethods := []string{ - "OPTIONS", - "HEAD", - "GET", - "POST", - "PUT", - "DELETE", - "PATCH", - } - w.Header().Set("Access-Control-Allow-Methods", strings.Join(allowMethods, ",")) - - // 允许请求头 - allowHeaders := []string{ - "Accesstoken", - "Content-Type", - "operationtype", - } - w.Header().Set("Access-Control-Allow-Headers", strings.Join(allowHeaders, ",")) - - w.WriteHeader(204) - return - } - - // 暴露请求头 - exposeHeaders := []string{"X-RepeatSubmit-Rest", "AccessToken"} - w.Header().Set("Access-Control-Expose-Headers", strings.Join(exposeHeaders, ",")) - - next.ServeHTTP(w, r) - }) -} diff --git a/lib/wsinfo/client.go b/lib/wsinfo/client.go deleted file mode 100644 index 36201cdd..00000000 --- a/lib/wsinfo/client.go +++ /dev/null @@ -1,45 +0,0 @@ -package wsinfo - -import ( - "github.com/gorilla/websocket" -) - -type Client struct { - ID string - Socket *websocket.Conn - Msg chan []byte -} - -func NewWsClient(ID string, socket *websocket.Conn) *Client { - return &Client{ - ID: ID, - Socket: socket, - Msg: make(chan []byte, 100), - } -} - -func (c *Client) Read() { - defer func() { - close(c.Msg) - }() - for { - _, message, err := c.Socket.ReadMessage() - if err != nil { - return - } - ProcessData(c, message) - } -} - -func (c *Client) Write() { - defer func() { - c.Socket.Close() - }() - for { - message, ok := <-c.Msg - if !ok { - return - } - _ = c.Socket.WriteMessage(websocket.TextMessage, message) - } -} diff --git a/lib/wsinfo/process_data.go b/lib/wsinfo/process_data.go deleted file mode 100644 index e19aa62f..00000000 --- a/lib/wsinfo/process_data.go +++ /dev/null @@ -1,382 +0,0 @@ -package wsinfo - -import ( - "encoding/json" - "fmt" - "sort" - "strings" - "sync" - "time" - - "be.ems/lib/log" - "github.com/shirou/gopsutil/v3/host" - "github.com/shirou/gopsutil/v3/net" - "github.com/shirou/gopsutil/v3/process" -) - -type WsInput struct { - Type string `json:"type"` - DownloadProgress - PsProcessConfig - SSHSessionConfig - NetConfig -} - -type DownloadProgress struct { - Keys []string `json:"keys"` -} - -type PsProcessConfig struct { - Pid int32 `json:"pid"` - Name string `json:"name"` - Username string `json:"username"` -} - -type SSHSessionConfig struct { - LoginUser string `json:"loginUser"` - LoginIP string `json:"loginIP"` -} - -type NetConfig struct { - Port uint32 `json:"port"` - ProcessName string `json:"processName"` - ProcessID int32 `json:"processID"` -} - -type PsProcessData struct { - PID int32 `json:"PID"` - Name string `json:"name"` - PPID int32 `json:"PPID"` - Username string `json:"username"` - Status string `json:"status"` - StartTime string `json:"startTime"` - NumThreads int32 `json:"numThreads"` - NumConnections int `json:"numConnections"` - CpuPercent string `json:"cpuPercent"` - - DiskRead string `json:"diskRead"` - DiskWrite string `json:"diskWrite"` - CmdLine string `json:"cmdLine"` - - Rss string `json:"rss"` - VMS string `json:"vms"` - HWM string `json:"hwm"` - Data string `json:"data"` - Stack string `json:"stack"` - Locked string `json:"locked"` - Swap string `json:"swap"` - - CpuValue float64 `json:"cpuValue"` - RssValue uint64 `json:"rssValue"` - - Envs []string `json:"envs"` - - OpenFiles []process.OpenFilesStat `json:"openFiles"` - Connects []processConnect `json:"connects"` -} - -type processConnect struct { - Type string `json:"type"` - Status string `json:"status"` - Laddr net.Addr `json:"localaddr"` - Raddr net.Addr `json:"remoteaddr"` - PID int32 `json:"PID"` - Name string `json:"name"` -} - -type ProcessConnects []processConnect - -func (p ProcessConnects) Len() int { - return len(p) -} - -func (p ProcessConnects) Less(i, j int) bool { - return p[i].PID < p[j].PID -} - -func (p ProcessConnects) Swap(i, j int) { - p[i], p[j] = p[j], p[i] -} - -type sshSession struct { - Username string `json:"username"` - PID int32 `json:"PID"` - Terminal string `json:"terminal"` - Host string `json:"host"` - LoginTime string `json:"loginTime"` -} - -func ProcessData(c *Client, inputMsg []byte) { - wsInput := &WsInput{} - err := json.Unmarshal(inputMsg, wsInput) - if err != nil { - log.Errorf("unmarshal wsInput error,err %s", err.Error()) - return - } - switch wsInput.Type { - case "ps": - res, err := getProcessData(wsInput.PsProcessConfig) - if err != nil { - return - } - c.Msg <- res - case "ssh": - res, err := getSSHSessions(wsInput.SSHSessionConfig) - if err != nil { - return - } - c.Msg <- res - case "net": - res, err := getNetConnections(wsInput.NetConfig) - if err != nil { - return - } - c.Msg <- res - } - -} - -type Process struct { - Total uint64 `json:"total"` - Written uint64 `json:"written"` - Percent float64 `json:"percent"` - Name string `json:"name"` -} - -const ( - b = uint64(1) - kb = 1024 * b - mb = 1024 * kb - gb = 1024 * mb -) - -func formatBytes(bytes uint64) string { - switch { - case bytes < kb: - return fmt.Sprintf("%dB", bytes) - case bytes < mb: - return fmt.Sprintf("%.2fKB", float64(bytes)/float64(kb)) - case bytes < gb: - return fmt.Sprintf("%.2fMB", float64(bytes)/float64(mb)) - default: - return fmt.Sprintf("%.2fGB", float64(bytes)/float64(gb)) - } -} - -func getProcessData(processConfig PsProcessConfig) (res []byte, err error) { - var processes []*process.Process - processes, err = process.Processes() - if err != nil { - return - } - - var ( - result []PsProcessData - resultMutex sync.Mutex - wg sync.WaitGroup - numWorkers = 4 - ) - - handleData := func(proc *process.Process) { - procData := PsProcessData{ - PID: proc.Pid, - } - if processConfig.Pid > 0 && processConfig.Pid != proc.Pid { - return - } - if procName, err := proc.Name(); err == nil { - procData.Name = procName - } else { - procData.Name = "" - } - if processConfig.Name != "" && !strings.Contains(procData.Name, processConfig.Name) { - return - } - if username, err := proc.Username(); err == nil { - procData.Username = username - } - if processConfig.Username != "" && !strings.Contains(procData.Username, processConfig.Username) { - return - } - procData.PPID, _ = proc.Ppid() - statusArray, _ := proc.Status() - if len(statusArray) > 0 { - procData.Status = strings.Join(statusArray, ",") - } - createTime, procErr := proc.CreateTime() - if procErr == nil { - t := time.Unix(createTime/1000, 0) - procData.StartTime = t.Format("2006-1-2 15:04:05") - } - procData.NumThreads, _ = proc.NumThreads() - connections, procErr := proc.Connections() - if procErr == nil { - procData.NumConnections = len(connections) - for _, conn := range connections { - if conn.Laddr.IP != "" || conn.Raddr.IP != "" { - procData.Connects = append(procData.Connects, processConnect{ - Status: conn.Status, - Laddr: conn.Laddr, - Raddr: conn.Raddr, - }) - } - } - } - procData.CpuValue, _ = proc.CPUPercent() - procData.CpuPercent = fmt.Sprintf("%.2f", procData.CpuValue) + "%" - menInfo, procErr := proc.MemoryInfo() - if procErr == nil { - procData.Rss = formatBytes(menInfo.RSS) - procData.RssValue = menInfo.RSS - procData.Data = formatBytes(menInfo.Data) - procData.VMS = formatBytes(menInfo.VMS) - procData.HWM = formatBytes(menInfo.HWM) - procData.Stack = formatBytes(menInfo.Stack) - procData.Locked = formatBytes(menInfo.Locked) - procData.Swap = formatBytes(menInfo.Swap) - } else { - procData.Rss = "--" - procData.Data = "--" - procData.VMS = "--" - procData.HWM = "--" - procData.Stack = "--" - procData.Locked = "--" - procData.Swap = "--" - - procData.RssValue = 0 - } - ioStat, procErr := proc.IOCounters() - if procErr == nil { - procData.DiskWrite = formatBytes(ioStat.WriteBytes) - procData.DiskRead = formatBytes(ioStat.ReadBytes) - } else { - procData.DiskWrite = "--" - procData.DiskRead = "--" - } - procData.CmdLine, _ = proc.Cmdline() - procData.OpenFiles, _ = proc.OpenFiles() - procData.Envs, _ = proc.Environ() - - resultMutex.Lock() - result = append(result, procData) - resultMutex.Unlock() - } - - chunkSize := (len(processes) + numWorkers - 1) / numWorkers - for i := 0; i < numWorkers; i++ { - wg.Add(1) - start := i * chunkSize - end := (i + 1) * chunkSize - if end > len(processes) { - end = len(processes) - } - - go func(start, end int) { - defer wg.Done() - for j := start; j < end; j++ { - handleData(processes[j]) - } - }(start, end) - } - - wg.Wait() - - sort.Slice(result, func(i, j int) bool { - return result[i].PID < result[j].PID - }) - res, err = json.Marshal(result) - return -} - -func getSSHSessions(config SSHSessionConfig) (res []byte, err error) { - var ( - result []sshSession - users []host.UserStat - processes []*process.Process - ) - processes, err = process.Processes() - if err != nil { - return - } - users, err = host.Users() - if err != nil { - return - } - for _, proc := range processes { - name, _ := proc.Name() - if name != "sshd" || proc.Pid == 0 { - continue - } - connections, _ := proc.Connections() - for _, conn := range connections { - for _, user := range users { - if user.Host == "" { - continue - } - if conn.Raddr.IP == user.Host { - if config.LoginUser != "" && !strings.Contains(user.User, config.LoginUser) { - continue - } - if config.LoginIP != "" && !strings.Contains(user.Host, config.LoginIP) { - continue - } - if terminal, err := proc.Cmdline(); err == nil { - if strings.Contains(terminal, user.Terminal) { - session := sshSession{ - Username: user.User, - Host: user.Host, - Terminal: user.Terminal, - PID: proc.Pid, - } - t := time.Unix(int64(user.Started), 0) - session.LoginTime = t.Format("2006-1-2 15:04:05") - result = append(result, session) - } - } - } - } - } - } - res, err = json.Marshal(result) - return -} - -var netTypes = [...]string{"tcp", "udp"} - -func getNetConnections(config NetConfig) (res []byte, err error) { - var ( - result []processConnect - proc *process.Process - ) - for _, netType := range netTypes { - connections, _ := net.Connections(netType) - if err == nil { - for _, conn := range connections { - if config.ProcessID > 0 && config.ProcessID != conn.Pid { - continue - } - proc, err = process.NewProcess(conn.Pid) - if err == nil { - name, _ := proc.Name() - if name != "" && config.ProcessName != "" && !strings.Contains(name, config.ProcessName) { - continue - } - if config.Port > 0 && config.Port != conn.Laddr.Port && config.Port != conn.Raddr.Port { - continue - } - result = append(result, processConnect{ - Type: netType, - Status: conn.Status, - Laddr: conn.Laddr, - Raddr: conn.Raddr, - PID: conn.Pid, - Name: name, - }) - } - - } - } - } - res, err = json.Marshal(result) - return -}