523 lines
13 KiB
Go
523 lines
13 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"be.ems/src/framework/constants/uploadsubpath"
|
|
"be.ems/src/framework/i18n"
|
|
"be.ems/src/framework/utils/ctx"
|
|
"be.ems/src/framework/utils/file"
|
|
"be.ems/src/framework/utils/parse"
|
|
"be.ems/src/framework/utils/telnet"
|
|
"be.ems/src/framework/vo/result"
|
|
"be.ems/src/modules/network_data/model"
|
|
neDataService "be.ems/src/modules/network_data/service"
|
|
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.NewUDMAuthImpl,
|
|
neInfoService: neService.NewNeInfoImpl,
|
|
}
|
|
|
|
// UDM鉴权用户
|
|
//
|
|
// PATH /udm/auth
|
|
type UDMAuthController struct {
|
|
// UDM鉴权信息服务
|
|
udmAuthService neDataService.IUDMAuth
|
|
// 网元信息服务
|
|
neInfoService neService.INeInfo
|
|
}
|
|
|
|
// UDM鉴权用户重载数据
|
|
//
|
|
// POST /resetData/:neId
|
|
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
|
|
}
|
|
|
|
neId = ""
|
|
data := s.udmAuthService.ResetData(neId)
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户列表
|
|
//
|
|
// GET /list
|
|
func (s *UDMAuthController) List(c *gin.Context) {
|
|
querys := ctx.QueryMap(c)
|
|
querys["neId"] = ""
|
|
data := s.udmAuthService.SelectPage(querys)
|
|
c.JSON(200, result.Ok(data))
|
|
}
|
|
|
|
// UDM鉴权用户信息
|
|
//
|
|
// GET /:neId/:imsi
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
neId = ""
|
|
u := model.UDMAuth{
|
|
IMSI: imsi,
|
|
Amf: data["amf"],
|
|
Status: "1",
|
|
Ki: data["ki"],
|
|
AlgoIndex: data["algo"],
|
|
Opc: data["opc"],
|
|
NeId: neId,
|
|
}
|
|
|
|
// 查询imsi存在赋予id用于更新
|
|
list := s.udmAuthService.SelectList(u)
|
|
if len(list) > 0 {
|
|
item := list[0]
|
|
if item.ID != "" {
|
|
u.ID = item.ID
|
|
}
|
|
}
|
|
go s.udmAuthService.Insert(neId, u)
|
|
|
|
c.JSON(200, result.OkData(u))
|
|
}
|
|
|
|
// UDM鉴权用户新增
|
|
//
|
|
// POST /:neId
|
|
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.UDMAuth
|
|
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
|
|
}
|
|
|
|
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,ki=%s,amf=%s,algo=%s,opc=%s", body.IMSI, body.Ki, body.Amf, body.AlgoIndex, body.Opc)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
neId = ""
|
|
go s.udmAuthService.Insert(neId, body)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户批量新增
|
|
//
|
|
// POST /:neId/:num
|
|
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.UDMAuth
|
|
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
|
|
}
|
|
|
|
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,ki=%s,amf=%s,algo=%s,opc=%s", body.IMSI, num, body.Ki, body.Amf, body.AlgoIndex, body.Opc)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
neId = ""
|
|
go s.udmAuthService.LoadData(neId, body.IMSI, num)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户修改
|
|
//
|
|
// PUT /:neId
|
|
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.UDMAuth
|
|
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
|
|
}
|
|
|
|
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)
|
|
// 修改的参数名称
|
|
if body.Ki != "" {
|
|
cmd += fmt.Sprintf(",ki=%s", body.Ki)
|
|
}
|
|
if body.Amf != "" {
|
|
cmd += fmt.Sprintf(",amf=%s", body.Amf)
|
|
}
|
|
if body.AlgoIndex != "" {
|
|
cmd += fmt.Sprintf(",algo=%s", body.AlgoIndex)
|
|
}
|
|
if body.Opc != "" {
|
|
cmd += fmt.Sprintf(",opc=%s", body.Opc)
|
|
}
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
neId = ""
|
|
go s.udmAuthService.Insert(neId, body)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户删除
|
|
//
|
|
// DELETE /:neId/:imsi
|
|
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
|
|
}
|
|
|
|
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") {
|
|
neId = ""
|
|
go s.udmAuthService.Delete(neId, imsi)
|
|
}
|
|
resultData[imsi] = data
|
|
}
|
|
|
|
c.JSON(200, result.OkData(resultData))
|
|
}
|
|
|
|
// UDM鉴权用户批量删除
|
|
//
|
|
// DELETE /:neId/:imsi/:num
|
|
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
|
|
}
|
|
|
|
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") {
|
|
neId = ""
|
|
go s.udmAuthService.LoadData(neId, imsi, num)
|
|
}
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// UDM鉴权用户导出
|
|
//
|
|
// POST /export
|
|
func (s *UDMAuthController) Export(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
var body struct {
|
|
NeId string `json:"neId" binding:"required"`
|
|
Type string `json:"type" binding:"required"`
|
|
}
|
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
|
if err != nil {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
if !(body.Type == "csv" || body.Type == "txt") {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "ne.udm.errExportType")))
|
|
return
|
|
}
|
|
|
|
neId := ""
|
|
list := s.udmAuthService.SelectList(model.UDMAuth{NeId: neId})
|
|
// 文件名
|
|
fileName := fmt.Sprintf("udm_auth_user_export_%s_%d.%s", neId, time.Now().UnixMilli(), body.Type)
|
|
filePath := fmt.Sprintf("%s/%s", file.ParseUploadFileDir(uploadsubpath.EXPORT), fileName)
|
|
|
|
if body.Type == "csv" {
|
|
// 转换数据
|
|
data := [][]string{}
|
|
data = append(data, []string{"imsi", "ki", "algo", "amf", "opc"})
|
|
for _, v := range list {
|
|
data = append(data, []string{v.IMSI, v.Ki, v.AlgoIndex, v.Amf, v.Opc})
|
|
}
|
|
// 输出到文件
|
|
err := file.WriterFileCSV(data, filePath)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
}
|
|
|
|
if body.Type == "txt" {
|
|
// 转换数据
|
|
data := [][]string{}
|
|
for _, v := range list {
|
|
data = append(data, []string{v.IMSI, v.Ki, v.AlgoIndex, v.Amf, v.Opc})
|
|
}
|
|
// 输出到文件
|
|
err = file.WriterFileTXT(data, ",", filePath)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
}
|
|
|
|
c.FileAttachment(filePath, fileName)
|
|
}
|
|
|
|
// UDM鉴权用户导入
|
|
//
|
|
// POST /import
|
|
func (s *UDMAuthController) Import(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
var body struct {
|
|
NeId string `json:"neId" binding:"required"`
|
|
UploadPath string `json:"uploadPath" binding:"required"`
|
|
}
|
|
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()
|
|
|
|
// 发送MML
|
|
cmd := fmt.Sprintf("import authdat:path=%s", neFilePath)
|
|
data, err := telnet.ConvertToStr(telnetClient, cmd)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
// 命令ok时
|
|
if strings.Contains(data, "ok") {
|
|
if strings.HasSuffix(body.UploadPath, ".csv") {
|
|
data := file.ReadFileCSV(localFilePath)
|
|
neId := ""
|
|
go s.udmAuthService.InsertData(neId, "csv", data)
|
|
}
|
|
if strings.HasSuffix(body.UploadPath, ".txt") {
|
|
data := file.ReadFileTXT(",", localFilePath)
|
|
neId := ""
|
|
go s.udmAuthService.InsertData(neId, "txt", data)
|
|
}
|
|
}
|
|
c.JSON(200, result.OkMsg(data))
|
|
}
|