From b2725e0ef4a5d6023ec3ebd05e11ab59f3505085 Mon Sep 17 00:00:00 2001 From: yoneyan Date: Sat, 22 Apr 2023 22:50:17 +0900 Subject: [PATCH] [support] Support #178. --- pkg/api/api.go | 8 +- pkg/api/core/group/service/interface.go | 1 + pkg/api/core/tool/config/config.go | 3 +- .../core/tool/notification/notification.go | 5 +- pkg/api/core/tool/notification/slack.go | 4 +- pkg/api/core/tool/slack/block.go | 458 ++++++++++++++++++ pkg/api/core/tool/slack/slack.go | 115 +++++ pkg/api/core/tool/tool.go | 20 + pkg/api/store/group/service/ip/v0/ip.go | 8 +- pkg/api/store/group/service/v0/service.go | 24 +- pkg/api/store/user/v0/user.go | 2 +- pkg/backend/cmd/start.go | 4 + 12 files changed, 633 insertions(+), 19 deletions(-) create mode 100644 pkg/api/core/tool/slack/block.go create mode 100644 pkg/api/core/tool/tool.go diff --git a/pkg/api/api.go b/pkg/api/api.go index 1539ae87..275e95ea 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -68,15 +68,15 @@ func AdminRestAPI() { v1.POST("/logout", token.DeleteAdminUser) // - // Token + // BotToken // v1.POST("/token/generate", token.GenerateByAdmin) v1.POST("/token", token.AddByAdmin) - // Token Delete + // BotToken Delete v1.DELETE("/token", token.DeleteAllByAdmin) v1.DELETE("/token/:id", token.DeleteByAdmin) - // Token Update + // BotToken Update v1.PUT("/token/:id", token.UpdateByAdmin) v1.GET("/token", token.GetAllByAdmin) v1.GET("/token/:id", token.GetByAdmin) @@ -323,7 +323,7 @@ func UserRestAPI() { func cors(c *gin.Context) { - //c.Header("Access-Control-Allow-Headers", "Accept, Content-ID, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, Access-Control-Request-Headers, Access-Control-Request-Method, Connection, Host, Origin, User-Agent, Referer, Cache-Control, X-header") + //c.Header("Access-Control-Allow-Headers", "Accept, Content-ID, Content-Length, Accept-Encoding, X-CSRF-BotToken, Authorization, Access-Control-Request-Headers, Access-Control-Request-Method, Connection, Host, Origin, User-Agent, Referer, Cache-Control, X-header") c.Header("Access-Control-Allow-Origin", "*") c.Header("Access-Control-Allow-Methods", "*") c.Header("Access-Control-Allow-Headers", "*") diff --git a/pkg/api/core/group/service/interface.go b/pkg/api/core/group/service/interface.go index 5cc58cba..755743dc 100644 --- a/pkg/api/core/group/service/interface.go +++ b/pkg/api/core/group/service/interface.go @@ -12,6 +12,7 @@ const ( Open = 3 SearchNewNumber = 4 GIDAndAddAllow = 5 + ASN = 6 UpdateName = 100 UpdateDate = 102 UpdateRoute = 103 diff --git a/pkg/api/core/tool/config/config.go b/pkg/api/core/tool/config/config.go index 17269045..8771afab 100644 --- a/pkg/api/core/tool/config/config.go +++ b/pkg/api/core/tool/config/config.go @@ -100,7 +100,8 @@ type Radius struct { } type Slack struct { - Token string `json:"token"` + AppToken string `json:"app_token"` + BotToken string `json:"bot_token"` Channels SlackChannels `json:"channels"` } diff --git a/pkg/api/core/tool/notification/notification.go b/pkg/api/core/tool/notification/notification.go index 3c3e17b1..86c5179b 100644 --- a/pkg/api/core/tool/notification/notification.go +++ b/pkg/api/core/tool/notification/notification.go @@ -13,6 +13,7 @@ var Notification NotifyStruct func NewNotification() { // slack - slackToken := config.Conf.Slack.Token - Notification.Slack = slack.New(slackToken) + slackToken := config.Conf.Slack.BotToken + appToken := slack.OptionAppLevelToken(config.Conf.Slack.AppToken) + Notification.Slack = slack.New(slackToken, appToken, slack.OptionDebug(config.IsDebug)) } diff --git a/pkg/api/core/tool/notification/slack.go b/pkg/api/core/tool/notification/slack.go index be485c42..0494097c 100644 --- a/pkg/api/core/tool/notification/slack.go +++ b/pkg/api/core/tool/notification/slack.go @@ -1,6 +1,7 @@ package notification import ( + "fmt" "github.com/homenoc/dsbd-backend/pkg/api/core/tool/config" "github.com/slack-go/slack" "strings" @@ -45,11 +46,12 @@ func NoticeLog(color string, baseKeyValue []string) { slackAttachField = append(slackAttachField, slack.AttachmentField{Title: splitKeyValue[0], Value: keyValue[len(splitKeyValue[0])+1:]}) } - Notification.Slack.PostMessage(config.Conf.Slack.Channels.Log, slack.MsgOptionAttachments( + _, _, err := Notification.Slack.PostMessage(config.Conf.Slack.Channels.Log, slack.MsgOptionAttachments( slack.Attachment{ Color: color, Title: "System", Fields: slackAttachField, }, )) + fmt.Println(err) } diff --git a/pkg/api/core/tool/slack/block.go b/pkg/api/core/tool/slack/block.go new file mode 100644 index 00000000..c270d9bf --- /dev/null +++ b/pkg/api/core/tool/slack/block.go @@ -0,0 +1,458 @@ +package slack + +import ( + "fmt" + "github.com/homenoc/dsbd-backend/pkg/api/core" + "github.com/homenoc/dsbd-backend/pkg/api/core/group/service" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool/config" + "github.com/homenoc/dsbd-backend/pkg/api/core/user" + dbIP "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/ip/v0" + dbService "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/v0" + dbUser "github.com/homenoc/dsbd-backend/pkg/api/store/user/v0" + "github.com/slack-go/slack" + "gorm.io/gorm" + "net" + "strconv" +) + +func errorProcess(blocks []slack.Block, err1, err2 string) slack.MsgOption { + blocks = append(blocks, []slack.Block{ + slack.NewDividerBlock(), + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Error*\n" + + err1 + "\n" + + err2, + }, + }}...) + return slack.MsgOptionBlocks(blocks...) +} + +func invalidCommand() slack.MsgOption { + return slack.MsgOptionText(":scream: Invalid Command", false) +} + +func getHelpInfo() slack.MsgOption { + return slack.MsgOptionBlocks( + &slack.SectionBlock{ + Type: slack.MBTHeader, + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: ":question: HELP", + }, + }, + slack.NewDividerBlock(), + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*April 14, 2023*", + }, + }, + slack.NewDividerBlock(), + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Common*\n" + + "`/dsbd help` help page", + }, + }, + slack.NewDividerBlock(), + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Search*\n" + + "`/dsbd user {userID}` search UserID\n" + + //"`/dsbd group {groupID}` search Group\n" + + //"`/dsbd service {id or serviceID}` search Service\n" + + //"`/dsbd connection {id or connectionID}` search Connection\n" + + "`/dsbd asn {AS Number}` search AS Number\n" + + "`/dsbd addr {ipaddr}` search IP Address", + }, + }, + slack.NewDividerBlock(), + ) +} + +func getUserInfo(userId uint) slack.MsgOption { + blocks := []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTHeader, + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: ":bust_in_silhouette: Search User", + }, + }, + slack.NewDividerBlock(), + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "取得時間: *" + tool.GetNowStr() + "*", + }, + }, + } + userDetail := dbUser.Get(user.ID, &core.User{Model: gorm.Model{ID: userId}}) + if userDetail.Err != nil { + return errorProcess(blocks, "データ取得エラー", userDetail.Err.Error()) + } + + groupIDStr := "None" + if userDetail.User[0].GroupID != nil { + groupIDStr = strconv.Itoa(int(*userDetail.User[0].GroupID)) + } + blocks = append(blocks, []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Created by*\n" + tool.DateToStr(userDetail.User[0].CreatedAt), + }, + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Updated by*\n" + tool.DateToStr(userDetail.User[0].UpdatedAt), + }, + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*UserName:*\n" + userDetail.User[0].Name, + }, + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*UserName(En):*\n" + userDetail.User[0].NameEn, + }, + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*E-Mail*\n" + userDetail.User[0].Email, + }, + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*GroupID*\n" + groupIDStr, + }, + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: "Dashboard Link", + }, + Accessory: &slack.Accessory{ + ButtonElement: &slack.ButtonBlockElement{ + Type: "button", + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: "Go Page", + }, + URL: config.Conf.Controller.Admin.ReturnURL + "/dashboard/user/" + strconv.Itoa(int(userDetail.User[0].ID)), + }, + }, + }, + slack.NewDividerBlock(), + }...) + return slack.MsgOptionBlocks(blocks...) +} + +func getASNInfo(asn int) slack.MsgOption { + blocks := []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTHeader, + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: ":office: Search ASN [" + strconv.Itoa(asn) + "]", + }, + }, + slack.NewDividerBlock(), + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "取得時間: *" + tool.GetNowStr() + "*", + }, + }, + } + resultService := dbService.Get(service.ASN, &core.Service{ASN: tool.ToUintP(uint(asn))}) + if resultService.Err != nil { + return errorProcess(blocks, "データ取得エラー", resultService.Err.Error()) + } + + for _, detailSer := range resultService.Service { + serviceCode := strconv.Itoa(int(detailSer.GroupID)) + "-" + detailSer.ServiceType + fmt.Sprintf("%03d", detailSer.ServiceNumber) + connection_blocks := []*slack.TextBlockObject{} + ip_blocks := []*slack.TextBlockObject{} + for _, detailConnection := range detailSer.Connection { + connectionCode := serviceCode + "-" + detailConnection.ConnectionType + fmt.Sprintf("%03d", detailConnection.ConnectionNumber) + connection_blocks = append(connection_blocks, []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: connectionCode, + }, + }...) + } + for _, ip := range detailSer.IP { + ip_blocks = append(ip_blocks, []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: ip.IP, + }, + }...) + } + + blocks = append(blocks, []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "ServiceID: *" + serviceCode + "*", + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Created by*\n" + tool.DateToStr(detailSer.CreatedAt), + }, + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Updated by*\n" + tool.DateToStr(detailSer.UpdatedAt), + }, + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*ID*\n" + strconv.Itoa(int(detailSer.ID)), + }, + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*GroupID/Name*\n[" + strconv.Itoa(int(detailSer.GroupID)) + "] " + detailSer.Group.Org, + }, + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: " ", + }, + Accessory: &slack.Accessory{ + ButtonElement: &slack.ButtonBlockElement{ + Type: "button", + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: "Group Page", + }, + URL: config.Conf.Controller.Admin.ReturnURL + "/dashboard/group/" + strconv.Itoa(int(detailSer.GroupID)), + }, + }, + }, + }...) + + if len(ip_blocks) != 0 { + blocks = append(blocks, []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*IP一覧*", + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: ip_blocks, + }}...) + } + if len(connection_blocks) != 0 { + blocks = append(blocks, []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*接続情報一覧*", + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: connection_blocks, + }}...) + } + + blocks = append(blocks, []slack.Block{slack.NewDividerBlock()}...) + } + + return slack.MsgOptionBlocks(blocks...) +} + +func getAddrInfo(addr string) slack.MsgOption { + blocks := []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTHeader, + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: ":office: Search IP Address [" + addr + "]", + }, + }, + slack.NewDividerBlock(), + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "取得時間: *" + tool.GetNowStr() + "*", + }, + }, + } + resultIP := dbIP.GetAll() + if resultIP.Err != nil { + return errorProcess(blocks, "データ取得エラー", resultIP.Err.Error()) + } + + // check input value + inputIP := net.ParseIP(addr) + if inputIP == nil { + return errorProcess(blocks, "入力エラー", "IPアドレスを入力してください(例: 1.1.1.1)") + } + // search IP + var serviceID uint = 0 + for _, detailIP := range resultIP.IP { + _, ipNet, err := net.ParseCIDR(detailIP.IP) + if err != nil { + continue + } + if ipNet.Contains(inputIP) { + serviceID = detailIP.ServiceID + break + } + } + if serviceID == 0 { + return errorProcess(blocks, "Not Found...", "一致するアドレスがありませんでした。("+addr+")") + } + resultService := dbService.Get(service.ID, &core.Service{Model: gorm.Model{ID: serviceID}}) + if resultService.Err != nil { + return errorProcess(blocks, "データ取得エラー", resultService.Err.Error()) + } + oneService := resultService.Service[0] + serviceCode := strconv.Itoa(int(oneService.GroupID)) + "-" + oneService.ServiceType + fmt.Sprintf("%03d", oneService.ServiceNumber) + connection_blocks := []*slack.TextBlockObject{} + ip_blocks := []*slack.TextBlockObject{} + for _, oneConnection := range oneService.Connection { + connectionCode := serviceCode + "-" + oneConnection.ConnectionType + fmt.Sprintf("%03d", oneConnection.ConnectionNumber) + connection_blocks = append(connection_blocks, []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: connectionCode, + }, + }...) + } + for _, ip := range oneService.IP { + ip_blocks = append(ip_blocks, []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: ip.IP, + }, + }...) + } + + blocks = append(blocks, []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "ServiceID: *" + serviceCode + "*", + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Created by*\n" + tool.DateToStr(oneService.CreatedAt), + }, + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*Updated by*\n" + tool.DateToStr(oneService.UpdatedAt), + }, + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: []*slack.TextBlockObject{ + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*ID*\n" + strconv.Itoa(int(oneService.ID)), + }, + &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*GroupID/Name*\n[" + strconv.Itoa(int(oneService.GroupID)) + "] " + oneService.Group.Org, + }, + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: " ", + }, + Accessory: &slack.Accessory{ + ButtonElement: &slack.ButtonBlockElement{ + Type: "button", + Text: &slack.TextBlockObject{ + Type: "plain_text", + Text: "Group Page", + }, + URL: config.Conf.Controller.Admin.ReturnURL + "/dashboard/group/" + strconv.Itoa(int(oneService.GroupID)), + }, + }, + }, + }...) + + if len(ip_blocks) != 0 { + blocks = append(blocks, []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*IP一覧*", + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: ip_blocks, + }}...) + } + if len(connection_blocks) != 0 { + blocks = append(blocks, []slack.Block{ + &slack.SectionBlock{ + Type: slack.MBTSection, + Text: &slack.TextBlockObject{ + Type: "mrkdwn", + Text: "*接続情報一覧*", + }, + }, + &slack.SectionBlock{ + Type: slack.MBTSection, + Fields: connection_blocks, + }}...) + } + + blocks = append(blocks, []slack.Block{slack.NewDividerBlock()}...) + + return slack.MsgOptionBlocks(blocks...) +} diff --git a/pkg/api/core/tool/slack/slack.go b/pkg/api/core/tool/slack/slack.go index 6782968e..8d1fdaf9 100644 --- a/pkg/api/core/tool/slack/slack.go +++ b/pkg/api/core/tool/slack/slack.go @@ -1,5 +1,18 @@ package slack +import ( + "fmt" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool/config" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification" + "github.com/slack-go/slack" + "github.com/slack-go/slack/slackevents" + "github.com/slack-go/slack/socketmode" + "log" + "os" + "strconv" + "strings" +) + func NoticeSlackType(slackType uint) string { if slackType == 0 { return "追加" @@ -11,3 +24,105 @@ func NoticeSlackType(slackType uint) string { return "" } } + +func StartAppSlack() { + notifySlack := notification.Notification.Slack + socketMode := socketmode.New( + notifySlack, + socketmode.OptionDebug(config.IsDebug), + socketmode.OptionLog(log.New(os.Stdout, "sm: ", log.Lshortfile|log.LstdFlags)), + ) + authTest, authTestErr := notifySlack.AuthTest() + if authTestErr != nil { + fmt.Fprintf(os.Stderr, "SLACK_BOT_TOKEN is invalid: %v\n", authTestErr) + os.Exit(1) + } + selfUserId := authTest.UserID + + go func() { + for event := range socketMode.Events { + switch event.Type { + case socketmode.EventTypeEventsAPI: + socketMode.Ack(*event.Request) + + eventPayload, _ := event.Data.(slackevents.EventsAPIEvent) + switch eventPayload.Type { + case slackevents.CallbackEvent: + switch event := eventPayload.InnerEvent.Data.(type) { + case *slackevents.MessageEvent: + if event.User != selfUserId && strings.Contains(event.Text, "こんにちは") { + _, _, err := notifySlack.PostMessage( + event.Channel, + slack.MsgOptionText( + fmt.Sprintf(":wave: こんにちは <@%v> さん!", event.User), + false, + ), + ) + if err != nil { + log.Printf("Failed to reply: %v", err) + } + } + default: + socketMode.Debugf("Skipped: %v", event) + } + default: + socketMode.Debugf("unsupported Events API eventPayload received") + } + case socketmode.EventTypeSlashCommand: + cmd, ok := event.Data.(slack.SlashCommand) + if !ok { + continue + } + inputArray := strings.Split(cmd.Text, " ") + socketMode.Ack(*event.Request) + if cmd.Command != "/dsbd" || len(inputArray) < 1 { + socketMode.PostMessage(cmd.ChannelName, invalidCommand(), slack.MsgOptionReplaceOriginal(cmd.ResponseURL)) + continue + } + var msgOption slack.MsgOption = invalidCommand() + switch inputArray[0] { + case "echo": + msgOption = slack.MsgOptionText("hello world", false) + case "user": + if len(inputArray) < 2 { + break + } + userID := 0 + userID, _ = strconv.Atoi(inputArray[1]) + if userID == 0 { + break + } + + msgOption = getUserInfo(uint(userID)) + case "addr": + if len(inputArray) < 2 { + break + } + msgOption = getAddrInfo(inputArray[1]) + case "asn": + if len(inputArray) < 2 { + break + } + asn := 0 + asn, _ = strconv.Atoi(inputArray[1]) + if asn == 0 { + break + } + msgOption = getASNInfo(asn) + case "help": + msgOption = getHelpInfo() + } + + _, _, err := socketMode.PostMessage(cmd.ChannelName, msgOption, slack.MsgOptionReplaceOriginal(cmd.ResponseURL)) + if err != nil { + log.Println(err) + } + + default: + socketMode.Debugf("Skipped: %v", event.Type) + } + } + }() + + socketMode.Run() +} diff --git a/pkg/api/core/tool/tool.go b/pkg/api/core/tool/tool.go new file mode 100644 index 00000000..703e6db1 --- /dev/null +++ b/pkg/api/core/tool/tool.go @@ -0,0 +1,20 @@ +package tool + +import "time" + +func GetNowStr() string { + now := time.Now() + return now.Format("2006-01-02 15:04:05") +} + +func DateToStr(d time.Time) string { + return d.Format("2006-01-02 15:04:05") +} + +func ToIntP(d int) *int { + return &d +} + +func ToUintP(d uint) *uint { + return &d +} diff --git a/pkg/api/store/group/service/ip/v0/ip.go b/pkg/api/store/group/service/ip/v0/ip.go index fe038210..a66adeb8 100644 --- a/pkg/api/store/group/service/ip/v0/ip.go +++ b/pkg/api/store/group/service/ip/v0/ip.go @@ -80,11 +80,13 @@ func Get(base int, data *core.IP) ip.ResultDatabase { var ips []core.IP - if base == ip.ID { //ID + switch base { + // ID + case ip.ID: err = db.First(&ips, data.ID).Error - } else { + default: log.Println("base select error") - return ip.ResultDatabase{Err: fmt.Errorf("(%s)error: base select\n", time.Now())} + err = fmt.Errorf("(%s)error: base select\n", time.Now()) } return ip.ResultDatabase{IP: ips, Err: err} } diff --git a/pkg/api/store/group/service/v0/service.go b/pkg/api/store/group/service/v0/service.go index 0165a025..a4e2ff94 100644 --- a/pkg/api/store/group/service/v0/service.go +++ b/pkg/api/store/group/service/v0/service.go @@ -113,7 +113,9 @@ func Get(base int, data *core.Service) service.ResultDatabase { var serviceStruct []core.Service - if base == service.ID { //ID + switch base { + //ID + case service.ID: err = db.Preload("IP"). Preload("IP.Plan"). Preload("Connection"). @@ -123,23 +125,24 @@ func Get(base int, data *core.Service) service.ResultDatabase { Preload("JPNICTech"). Preload("Group"). First(&serviceStruct, data.ID).Error - } else if base == service.Org { //Mail + // Mail + case service.Org: err = db.Preload("IP"). Preload("Connection"). Preload("JPNICAdmin"). Preload("JPNICTech"). Where("org = ?", data.Org).Find(&serviceStruct).Error - } else if base == service.GID { + case service.GID: err = db.Preload("IP"). Preload("Connection"). Preload("JPNICAdmin"). Preload("JPNICTech"). Where("group_id = ?", data.GroupID).Find(&serviceStruct).Error - } else if base == service.GIDAndAddAllow { + case service.GIDAndAddAllow: err = db.Where("group_id = ? AND add_allow = ?", data.GroupID, true).Find(&serviceStruct).Error - } else if base == service.SearchNewNumber { + case service.SearchNewNumber: err = db.Where("group_id = ?", data.GroupID).Find(&serviceStruct).Error - } else if base == service.Open { + case service.Open: err = db.Where("group_id = ? AND open = ?", data.GroupID, true). Preload("IP", "open = ?", true). Preload("Connection", "open = ?", true). @@ -148,9 +151,16 @@ func Get(base int, data *core.Service) service.ResultDatabase { Preload("JPNICAdmin"). Preload("JPNICTech"). Find(&serviceStruct).Error - } else { + case service.ASN: + err = db.Where("asn = ? AND pass = ? AND enable = ?", data.ASN, true, true). + Preload("IP"). + Preload("Connection", "open = ?", true). + Preload("Group"). + Find(&serviceStruct).Error + default: log.Println("base select error") return service.ResultDatabase{Err: fmt.Errorf("(%s)error: base select\n", time.Now())} + } return service.ResultDatabase{Err: err, Service: serviceStruct} } diff --git a/pkg/api/store/user/v0/user.go b/pkg/api/store/user/v0/user.go index ef6c8668..3ff64cb3 100644 --- a/pkg/api/store/user/v0/user.go +++ b/pkg/api/store/user/v0/user.go @@ -133,7 +133,7 @@ func Get(base int, u *core.User) user.ResultDatabase { err = db.Where("group_id = ?", u.GroupID).Find(&userStruct).Error } else if base == user.Email { //Mail err = db.Where("email = ?", u.Email).Find(&userStruct).Error - } else if base == user.MailToken { //Token + } else if base == user.MailToken { //BotToken err = db.Where("mail_token = ?", u.MailToken).Find(&userStruct).Error } else if base == user.GIDAndLevel { //GroupID and Level err = db.Where("group_id = ? AND level = ?", u.GroupID, u.Level).Find(&userStruct).Error diff --git a/pkg/backend/cmd/start.go b/pkg/backend/cmd/start.go index 6bdbba3c..0b22acb7 100644 --- a/pkg/backend/cmd/start.go +++ b/pkg/backend/cmd/start.go @@ -4,6 +4,7 @@ import ( "github.com/homenoc/dsbd-backend/pkg/api" "github.com/homenoc/dsbd-backend/pkg/api/core/tool/config" "github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool/slack" "github.com/spf13/cobra" "github.com/stripe/stripe-go/v73" "log" @@ -33,6 +34,7 @@ var startUserCmd = &cobra.Command{ log.Fatalf("error config process |%v", err) } notification.NewNotification() + go slack.StartAppSlack() notification.NoticeLog("good", []string{ "Status: User側 API起動", }) @@ -59,7 +61,9 @@ var startAdminCmd = &cobra.Command{ if config.GetConfig(confPath) != nil { log.Fatalf("error config process |%v", err) } + notification.NewNotification() + go slack.StartAppSlack() notification.NoticeLog("good", []string{ "Status: Admin側 API起動", })