Skip to content

Commit

Permalink
feat(sms):add alibaba cloud dysmsapi
Browse files Browse the repository at this point in the history
  • Loading branch information
404tk committed Apr 10, 2023
1 parent 9bf95f8 commit 453185d
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
_oss "github.com/404tk/cloudtoolkit/pkg/providers/alibaba/oss"
_ram "github.com/404tk/cloudtoolkit/pkg/providers/alibaba/ram"
_rds "github.com/404tk/cloudtoolkit/pkg/providers/alibaba/rds"
_sms "github.com/404tk/cloudtoolkit/pkg/providers/alibaba/sms"
"github.com/404tk/cloudtoolkit/pkg/schema"
"github.com/404tk/cloudtoolkit/utils"
"github.com/404tk/cloudtoolkit/utils/cache"
"github.com/aliyun/alibaba-cloud-sdk-go/services/bssopenapi"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
Expand All @@ -27,6 +29,7 @@ type Provider struct {
OssClient *oss.Client
RamClient *ram.Client
RdsClient *rds.Client
SmsClient *dysmsapi.Client
resourceGroups []string
}

Expand Down Expand Up @@ -81,6 +84,7 @@ func New(options schema.Options) (*Provider, error) {
ossClient, err := oss.New("oss-"+region+".aliyuncs.com", accessKey, secretKey, oss.SecurityToken(token))
ramClient, err := ram.NewClientWithStsToken(region, accessKey, secretKey, token)
rdsClient, err := rds.NewClientWithStsToken(region, accessKey, secretKey, token)
smsclient, err := dysmsapi.NewClientWithStsToken(region, accessKey, secretKey, token)
/*
rmClient, err := resourcemanager.NewClientWithAccessKey(region, accessKey, secretKey)
if err != nil {
Expand All @@ -103,6 +107,7 @@ func New(options schema.Options) (*Provider, error) {
OssClient: ossClient,
RamClient: ramClient,
RdsClient: rdsClient,
SmsClient: smsclient,
resourceGroups: []string{""},
}, err
}
Expand Down Expand Up @@ -134,6 +139,9 @@ func (p *Provider) Resources(ctx context.Context) (*schema.Resources, error) {
rdsprovider := &_rds.RdsProvider{Client: p.RdsClient, ResourceGroups: p.resourceGroups}
list.Databases, err = rdsprovider.GetDatabases(ctx)

smsprovider := &_sms.SmsProvider{Client: p.SmsClient}
list.Sms, err = smsprovider.GetResource(ctx)

return list, err
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/alibaba/ram/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (d *RamProvider) GetRamUser(ctx context.Context) ([]*schema.User, error) {
select {
case <-ctx.Done():
return list, nil
default:
continue
}
}
if !response.IsTruncated {
Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/alibaba/rds/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (d *RdsProvider) GetDatabases(ctx context.Context) ([]*schema.Database, err
select {
case <-ctx.Done():
return list, nil
default:
continue
}
}
}
Expand Down
78 changes: 78 additions & 0 deletions pkg/providers/alibaba/sms/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package sms

import (
"context"
"log"

"github.com/404tk/cloudtoolkit/pkg/schema"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)

type SmsProvider struct {
Client *dysmsapi.Client
}

func (d *SmsProvider) GetResource(ctx context.Context) (schema.Sms, error) {
res := schema.Sms{}
select {
case <-ctx.Done():
return res, nil
default:
log.Println("[*] List SMS resource ...")
}
var err error
res.Signs, err = listSmsSign(d.Client)
if err != nil {
log.Println("[-] List SMS failed.")
return res, err
}
res.Templates, err = listSmsTemplate(d.Client)
res.DailySize, err = querySendStatistics(d.Client)

return res, err
}

var status = map[string]string{
"AUDIT_STATE_INIT": "审核中",
"AUDIT_STATE_PASS": "审核通过",
"AUDIT_STATE_NOT_PASS": "审核未通过",
"AUDIT_STATE_CANCEL": "取消审核",
}

func listSmsSign(client *dysmsapi.Client) ([]schema.SmsSign, error) {
signs := []schema.SmsSign{}
request := dysmsapi.CreateQuerySmsSignListRequest()
request.Scheme = "https"
response, err := client.QuerySmsSignList(request)
if err != nil {
return signs, err
}
for _, sign := range response.SmsSignList {
s, _ := status[sign.AuditStatus]
signs = append(signs, schema.SmsSign{
Name: sign.SignName,
Type: sign.BusinessType,
Status: s,
})
}
return signs, nil
}

func listSmsTemplate(client *dysmsapi.Client) ([]schema.SmsTemplate, error) {
temps := []schema.SmsTemplate{}
request := dysmsapi.CreateQuerySmsTemplateListRequest()
request.Scheme = "https"
response, err := client.QuerySmsTemplateList(request)
if err != nil {
return temps, err
}
for _, temp := range response.SmsTemplateList {
s, _ := status[temp.AuditStatus]
temps = append(temps, schema.SmsTemplate{
Name: temp.TemplateName,
Status: s,
Content: temp.TemplateContent,
})
}
return temps, nil
}
45 changes: 45 additions & 0 deletions pkg/providers/alibaba/sms/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package sms

import (
"fmt"
"log"
"time"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)

func querySendStatistics(client *dysmsapi.Client) (int64, error) {
date := time.Now().UTC().Format("20060102")
request := dysmsapi.CreateQuerySendStatisticsRequest()
request.Scheme = "https"
request.IsGlobe = requests.NewInteger(1)
request.StartDate = date
request.EndDate = date
request.PageIndex = requests.NewInteger(1)
request.PageSize = requests.NewInteger(10)
response, err := client.QuerySendStatistics(request)
if err != nil {
return 0, err
}
return response.Data.TotalSize, nil
}

func querySendDetails(client *dysmsapi.Client, phone string) {
request := dysmsapi.CreateQuerySendDetailsRequest()
request.Scheme = "https"
request.SendDate = time.Now().UTC().Format("20060102")
request.PageSize = requests.NewInteger(10)
request.CurrentPage = requests.NewInteger(1)
request.PhoneNumber = phone
response, err := client.QuerySendDetails(request)
if err != nil {
log.Println(err)
return
}
fmt.Printf("\n%-10s\t%-90s\n", "SendDate", "Content")
fmt.Printf("%-10s\t%-90s\n", "--------", "-------")
for _, detail := range response.SmsSendDetailDTOs.SmsSendDetailDTO {
fmt.Printf("%-10s\t%-90s\n\n", detail.SendDate, detail.Content)
}
}
2 changes: 2 additions & 0 deletions pkg/providers/aws/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (d *S3Provider) GetBuckets(ctx context.Context) ([]*schema.Storage, error)
select {
case <-ctx.Done():
return list, nil
default:
continue
}
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Resources struct {
Storages []*Storage
Users []*User
Databases []*Database
Sms Sms
}

type Host struct {
Expand Down Expand Up @@ -65,6 +66,24 @@ type Database struct {
Region string `table:"Region"`
}

type Sms struct {
Signs []SmsSign
Templates []SmsTemplate
DailySize int64
}

type SmsSign struct {
Name string `table:"Name"`
Type string `table:"Type"`
Status string `table:"Status"`
}

type SmsTemplate struct {
Name string `table:"Name"`
Status string `table:"Status"`
Content string `table:"Content"`
}

// ErrNoSuchKey means no such key exists in metadata.
type ErrNoSuchKey struct {
Name string
Expand Down
2 changes: 1 addition & 1 deletion runner/banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const banner = `
`

// Version is the current version of cloudtoolkit
const Version = `0.0.5`
const Version = `0.0.6`

// showBanner is used to show the banner to the user
func ShowBanner() {
Expand Down
5 changes: 5 additions & 0 deletions runner/payloads/cloudlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func (p CloudList) Run(ctx context.Context, config map[string]string) {
pprint(len(resources.Hosts), "Hosts", resources.Hosts)
pprint(len(resources.Storages), "Storages", resources.Storages)
pprint(len(resources.Users), "Users", resources.Users)
pprint(len(resources.Sms.Signs), "SMS Signs", resources.Sms.Signs)
pprint(len(resources.Sms.Templates), "SMS Templates", resources.Sms.Templates)
if resources.Sms.DailySize > 0 {
fmt.Printf("[*] The total number of SMS messages sent today is %v.\n", resources.Sms.DailySize)
}

log.Println("[+] Done.")
}
Expand Down

0 comments on commit 453185d

Please sign in to comment.