728 lines
21 KiB
Go
728 lines
21 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"be.ems/src/framework/constants/uploadsubpath"
|
|
"be.ems/src/framework/i18n"
|
|
"be.ems/src/framework/resp"
|
|
"be.ems/src/framework/telnet"
|
|
"be.ems/src/framework/utils/ctx"
|
|
"be.ems/src/framework/utils/file"
|
|
"be.ems/src/framework/utils/parse"
|
|
"be.ems/src/framework/vo/result"
|
|
"be.ems/src/modules/network_data/model"
|
|
neDataService "be.ems/src/modules/network_data/service"
|
|
neFetchlink "be.ems/src/modules/network_element/fetch_link"
|
|
neService "be.ems/src/modules/network_element/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
)
|
|
|
|
// 实例化控制层 UDMAuthController 结构体
|
|
var NewUDMAuth = &UDMAuthController{
|
|
udmAuthService: neDataService.NewUDMAuthUser,
|
|
neInfoService: neService.NewNeInfo,
|
|
}
|
|
|
|
// UDM鉴权用户
|
|
//
|
|
// PATH /udm/auth
|
|
type UDMAuthController struct {
|
|
udmAuthService *neDataService.UDMAuthUser // UDM鉴权信息服务
|
|
neInfoService *neService.NeInfo // 网元信息服务
|
|
}
|
|
|
|
// UDM鉴权用户重载数据
|
|
//
|
|
// PUT /resetData/:neId
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId path string true "NE ID" default(001)
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authentication User Reload Data
|
|
// @Description UDM Authentication User Reload Data
|
|
// @Router /neData/udm/auth/resetData/{neId} [put]
|
|
func (s *UDMAuthController) ResetData(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Param("neId")
|
|
if neId == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
data := s.udmAuthService.ResetData(neId)
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户列表
|
|
//
|
|
// GET /list
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId query string true "NE ID" default(001)
|
|
// @Param imsi query string false "IMSI"
|
|
// @Param pageNum query number true "pageNum" default(1)
|
|
// @Param pageSize query number true "pageSize" default(10)
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authentication User List
|
|
// @Description UDM Authentication User List
|
|
// @Router /neData/udm/auth/list [get]
|
|
func (s *UDMAuthController) List(c *gin.Context) {
|
|
querys := ctx.QueryMap(c)
|
|
data := s.udmAuthService.SelectPage(querys)
|
|
c.JSON(200, result.Ok(data))
|
|
}
|
|
|
|
// UDM鉴权用户信息
|
|
//
|
|
// GET /:neId/:imsi
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId path string true "NE ID" default(001)
|
|
// @Param value path string true "IMSI"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authentication User Information
|
|
// @Description UDM Authentication User Information
|
|
// @Router /neData/udm/auth/{neId}/{value} [get]
|
|
func (s *UDMAuthController) Info(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Param("neId")
|
|
imsi := c.Param("imsi")
|
|
if neId == "" || imsi == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient("UDM", neId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
// 发送MML
|
|
cmd := fmt.Sprintf("dsp authdat:imsi=%s", imsi)
|
|
data, err := telnet.ConvertToMap(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
c.JSON(200, result.ErrMsg("No Auth Data"))
|
|
return
|
|
}
|
|
|
|
// 解析返回的数据
|
|
u := s.udmAuthService.ParseInfo(imsi, neId, data)
|
|
s.udmAuthService.Insert(neId, u)
|
|
c.JSON(200, result.OkData(u))
|
|
}
|
|
|
|
// UDM鉴权用户新增
|
|
//
|
|
// POST /:neId
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId path string true "NE ID" default(001)
|
|
// @Param data body object true "Request Param"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authentication User Added
|
|
// @Description UDM Authentication User Added
|
|
// @Router /neData/udm/auth/{neId} [post]
|
|
func (s *UDMAuthController) Add(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Param("neId")
|
|
if neId == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
var body model.UDMAuthUser
|
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
|
if err != nil || body.IMSI == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient("UDM", neId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
// 发送MML
|
|
cmd := fmt.Sprintf("add authdat:imsi=%s,", body.IMSI)
|
|
cmd += s.udmAuthService.ParseCommandParams(body)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
s.udmAuthService.Insert(neId, body)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户批量新增
|
|
//
|
|
// POST /:neId/:num
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId path string true "NE ID" default(001)
|
|
// @Param value path number true "Number of releases, value includes start imsi"
|
|
// @Param data body object true "Request Param"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authentication User Batch Add
|
|
// @Description UDM Authentication User Batch Add
|
|
// @Router /neData/udm/auth/{neId}/{value} [post]
|
|
func (s *UDMAuthController) Adds(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Param("neId")
|
|
num := c.Param("num")
|
|
if neId == "" || num == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
var body model.UDMAuthUser
|
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
|
if err != nil || body.IMSI == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient("UDM", neId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
// 发送MML
|
|
cmd := fmt.Sprintf("baa authdat:start_imsi=%s,sub_num=%s,", body.IMSI, num)
|
|
cmd += s.udmAuthService.ParseCommandParams(body)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
s.udmAuthService.LoadData(neId, body.IMSI, num)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户修改
|
|
//
|
|
// PUT /:neId
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId path string true "NE ID" default(001)
|
|
// @Param data body object true "Request Param"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authenticated User Modification
|
|
// @Description UDM Authenticated User Modification
|
|
// @Router /neData/udm/auth/{neId} [put]
|
|
func (s *UDMAuthController) Edit(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Param("neId")
|
|
if neId == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
var body model.UDMAuthUser
|
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
|
if err != nil || body.IMSI == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient("UDM", neId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
// 发送MML
|
|
cmd := fmt.Sprintf("mod authdata:imsi=%s,", body.IMSI)
|
|
cmd += s.udmAuthService.ParseCommandParams(body)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
s.udmAuthService.Insert(neId, body)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户删除
|
|
//
|
|
// DELETE /:neId/:imsi
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId path string true "NE ID" default(001)
|
|
// @Param value path string true "IMSI, multiple separated by a , sign"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authenticated User Deletion
|
|
// @Description UDM Authenticated User Deletion
|
|
// @Router /neData/udm/auth/{neId}/{value} [delete]
|
|
func (s *UDMAuthController) Remove(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Param("neId")
|
|
imsi := c.Param("imsi")
|
|
if neId == "" || imsi == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 处理字符转id数组后去重
|
|
imsiArr := strings.Split(imsi, ",")
|
|
uniqueIDs := parse.RemoveDuplicates(imsiArr)
|
|
if len(uniqueIDs) <= 0 {
|
|
c.JSON(200, result.Err(nil))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient("UDM", neId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
resultData := map[string]string{}
|
|
for _, imsi := range uniqueIDs {
|
|
// 发送MML
|
|
cmd := fmt.Sprintf("del authdat:imsi=%s", imsi)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
resultData[imsi] = err.Error()
|
|
continue
|
|
}
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
s.udmAuthService.Delete(imsi, neId)
|
|
}
|
|
resultData[imsi] = data
|
|
}
|
|
|
|
c.JSON(200, result.OkData(resultData))
|
|
}
|
|
|
|
// UDM鉴权用户批量删除
|
|
//
|
|
// DELETE /:neId/:imsi/:num
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param neId path string true "NE ID" default(001)
|
|
// @Param imsi path string true "IMSI"
|
|
// @Param num path number true "Number of releases, value includes start imsi"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authentication User Batch Deletion
|
|
// @Description UDM Authentication User Batch Deletion
|
|
// @Router /neData/udm/auth/{neId}/{imsi}/{num} [delete]
|
|
func (s *UDMAuthController) Removes(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Param("neId")
|
|
imsi := c.Param("imsi")
|
|
num := c.Param("num")
|
|
if neId == "" || imsi == "" || num == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient("UDM", neId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
// 发送MML
|
|
cmd := fmt.Sprintf("bde authdat:start_imsi=%s,sub_num=%s", imsi, num)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
s.udmAuthService.LoadData(neId, imsi, num)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户导出
|
|
//
|
|
// POST /export
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param data body object true "Request Param"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authenticated User Export
|
|
// @Description UDM Authenticated User Export
|
|
// @Router /neData/udm/auth/export [post]
|
|
func (s *UDMAuthController) Export(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
// 查询结果,根据查询条件结果,单页最大值限制
|
|
querys := ctx.BodyJSONMap(c)
|
|
neId := querys["neId"].(string)
|
|
fileType := querys["type"].(string)
|
|
if neId == "" || fileType == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
if !(fileType == "csv" || fileType == "txt") {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "ne.udm.errImportUserSubFileFormat")))
|
|
return
|
|
}
|
|
|
|
querys["pageNum"] = 1
|
|
querys["pageSize"] = 10000
|
|
data := s.udmAuthService.SelectPage(querys)
|
|
if parse.Number(data["total"]) == 0 {
|
|
// 导出数据记录为空
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
|
return
|
|
}
|
|
rows := data["rows"].([]model.UDMAuthUser)
|
|
|
|
// rows := s.udmAuthService.SelectList(model.UDMAuthUser{NeId: neId})
|
|
if len(rows) <= 0 {
|
|
// 导出数据记录为空
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
|
return
|
|
}
|
|
|
|
// 文件名
|
|
fileName := fmt.Sprintf("udm_auth_user_export_%s_%d.%s", neId, time.Now().UnixMilli(), fileType)
|
|
filePath := filepath.Join(file.ParseUploadFileDir(uploadsubpath.EXPORT), fileName)
|
|
|
|
if fileType == "csv" {
|
|
// 转换数据 imsi,ki,algo,amf,opc,create_time
|
|
data := [][]string{}
|
|
data = append(data, []string{"imsi", "ki", "algo", "amf", "opc", "create_time"})
|
|
for _, v := range rows {
|
|
opc := v.Opc
|
|
if opc == "-" {
|
|
opc = ""
|
|
}
|
|
createTime := ""
|
|
if v.CreateTime == 0 {
|
|
createTime = time.Now().Format(time.RFC3339)
|
|
} else {
|
|
createTime = time.UnixMilli(v.CreateTime).Format(time.RFC3339)
|
|
}
|
|
data = append(data, []string{v.IMSI, v.Ki, v.AlgoIndex, v.Amf, opc, createTime})
|
|
}
|
|
// 输出到文件
|
|
err := file.WriterFileCSV(data, filePath)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
}
|
|
|
|
if fileType == "txt" {
|
|
// 转换数据
|
|
data := [][]string{}
|
|
for _, v := range rows {
|
|
opc := v.Opc
|
|
if opc == "-" {
|
|
opc = ""
|
|
}
|
|
createTime := ""
|
|
if v.CreateTime == 0 {
|
|
createTime = time.Now().Format(time.RFC3339)
|
|
} else {
|
|
createTime = time.UnixMilli(v.CreateTime).Format(time.RFC3339)
|
|
}
|
|
data = append(data, []string{v.IMSI, v.Ki, v.AlgoIndex, v.Amf, opc, createTime})
|
|
}
|
|
// 输出到文件
|
|
|
|
if err := file.WriterFileTXTLine(data, ",", filePath); err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
}
|
|
|
|
c.FileAttachment(filePath, fileName)
|
|
}
|
|
|
|
// UDM鉴权用户导入
|
|
//
|
|
// POST /import
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param data body object true "Request Param"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authenticated User Import
|
|
// @Description UDM Authenticated User Import
|
|
// @Router /neData/udm/auth/import [post]
|
|
func (s *UDMAuthController) Import(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
var body struct {
|
|
NeId string `json:"neId" binding:"required"` // 网元ID
|
|
UploadPath string `json:"uploadPath" binding:"required"` // 上传文件路径
|
|
TypeVal string `json:"typeVal" binding:"required,oneof=default k4"` // default: 默认导入方式, k4: k4类型导入方式
|
|
TypeData any `json:"typeData"` // k4类型的数据密钥
|
|
}
|
|
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 判断文件名
|
|
if !(strings.HasSuffix(body.UploadPath, ".csv") || strings.HasSuffix(body.UploadPath, ".txt")) {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "ne.udm.errImportUserAuthFileFormat")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", body.NeId)
|
|
if neInfo.NeId != body.NeId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
|
|
// 网元主机的SSH客户端
|
|
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer sshClient.Close()
|
|
// 网元主机的SSH客户端进行文件传输
|
|
sftpClient, err := sshClient.NewClientSFTP()
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer sftpClient.Close()
|
|
|
|
// 本地文件
|
|
localFilePath := file.ParseUploadFilePath(body.UploadPath)
|
|
neFilePath := fmt.Sprintf("/tmp/%s", filepath.Base(localFilePath))
|
|
// 复制到远程
|
|
if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil {
|
|
c.JSON(200, result.ErrMsg("error uploading file"))
|
|
return
|
|
}
|
|
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient(neInfo.NeType, neInfo.NeId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
// 结果信息
|
|
var resultMsg string
|
|
var resultErr error
|
|
|
|
// 默认的情况 发送MML
|
|
if body.TypeVal == "default" {
|
|
cmd := fmt.Sprintf("import authdat:path=%s", neFilePath)
|
|
resultMsg, resultErr = telnet.ConvertToStr(telnetClient, cmd)
|
|
}
|
|
|
|
// K4类型发特定请求
|
|
if body.TypeVal == "k4" {
|
|
resultMsg, resultErr = neFetchlink.UDMImportAuth(neInfo.IP, map[string]any{
|
|
"path": neFilePath, "k4": body.TypeData,
|
|
})
|
|
}
|
|
|
|
if resultErr != nil {
|
|
c.JSON(200, result.ErrMsg(resultErr.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(resultMsg, "ok") {
|
|
if strings.HasSuffix(body.UploadPath, ".csv") {
|
|
data := file.ReadFileCSV(localFilePath)
|
|
go s.udmAuthService.InsertData(neInfo.NeId, "csv", data)
|
|
}
|
|
if strings.HasSuffix(body.UploadPath, ".txt") {
|
|
data := file.ReadFileTXTLine(",", localFilePath)
|
|
go s.udmAuthService.InsertData(neInfo.NeId, "txt", data)
|
|
}
|
|
}
|
|
c.JSON(200, result.OkMsg(resultMsg))
|
|
}
|
|
|
|
// UDM鉴权用户导出解密数据
|
|
//
|
|
// GET /export-dec
|
|
//
|
|
// @Tags network_data/udm/auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param data body object true "Request Param"
|
|
// @Success 200 {object} object "Response Results"
|
|
// @Security TokenAuth
|
|
// @Summary UDM Authenticated User Export Decrypted Data
|
|
// @Description UDM Authenticated User Export Decrypted Data
|
|
// @Router /neData/udm/auth/export-dec [get]
|
|
func (s *UDMAuthController) ExportDec(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
neId := c.Query("neId")
|
|
if neId == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID("UDM", neId)
|
|
if neInfo.NeId != neId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
// 网元主机的Telnet客户端
|
|
telnetClient, err := s.neInfoService.NeRunTelnetClient("UDM", neId, 1)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer telnetClient.Close()
|
|
|
|
// 发送MML
|
|
// 导出解密的ki和opc 需要执行 dec authdat:imsi=all
|
|
// 生成文件 /user/local/etc/udm/authdata.txt
|
|
cmd := "dec authdat:imsi=all"
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
// 网元主机的SSH客户端
|
|
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
|
|
if err != nil {
|
|
c.JSON(200, resp.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer sshClient.Close()
|
|
// 网元主机的SSH客户端进行文件传输
|
|
sftpClient, err := sshClient.NewClientSFTP()
|
|
if err != nil {
|
|
c.JSON(200, resp.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
defer sftpClient.Close()
|
|
|
|
// 复制到本地
|
|
nePath := "/usr/local/etc/udm/authdata.txt"
|
|
localFilePath := filepath.Join("/tmp/omc/pull", filepath.Base(nePath))
|
|
if runtime.GOOS == "windows" {
|
|
localFilePath = fmt.Sprintf("C:%s", localFilePath)
|
|
}
|
|
if err = sftpClient.CopyFileRemoteToLocal(nePath, localFilePath); err != nil {
|
|
c.JSON(200, resp.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
sshClient.RunCMD(fmt.Sprintf("sudo rm -rf %s", nePath))
|
|
defer os.Remove(localFilePath)
|
|
c.FileAttachment(localFilePath, filepath.Base(nePath))
|
|
return
|
|
}
|
|
c.JSON(200, result.ErrMsg("unexpected result"))
|
|
}
|