/
aliyun.go
451 lines (391 loc) · 11.2 KB
/
aliyun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package aliyun
import (
"compress/gzip"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"sort"
"strings"
"time"
"github.com/XieXianbin/msg-provider/utils/urllib"
"github.com/XieXianbin/msg-provider/utils/uuid"
)
var HttpDebugEnable bool = false
type SmsParam struct {
Action string
SignName string
TemplateCode string
RecNum string
ParamString string
Format string
Version string
AccessKeyId string
SignatureMethod string
Timestamp string
SignatureVersion string
SignatureNonce string
RegionId string
}
func (this *SmsParam) SetAction(action string) {
this.Action = action
}
func (this *SmsParam) GetAction() string {
return this.Action
}
func (this *SmsParam) SetSignName(signname string) {
this.SignName = signname
}
func (this *SmsParam) GetSignName() string {
return this.SignName
}
func (this *SmsParam) SetTemplateCode(templatecode string) {
this.TemplateCode = templatecode
}
func (this *SmsParam) GetTemplateCode() string {
return this.TemplateCode
}
func (this *SmsParam) SetRecNum(recnum string) {
this.RecNum = recnum
}
func (this *SmsParam) GetRecNum() string {
return this.RecNum
}
func (this *SmsParam) SetParamString(paramstring string) {
this.ParamString = paramstring
}
func (this *SmsParam) GetParamString() string {
return this.ParamString
}
func (this *SmsParam) SetFormat(format string) {
this.Format = format
}
func (this *SmsParam) GetFormat() string {
return this.Format
}
func (this *SmsParam) SetVersion(version string) {
this.Version = version
}
func (this *SmsParam) GetVersion() string {
return this.Version
}
func (this *SmsParam) SetAccessKeyId(accesskeyid string) {
this.AccessKeyId = accesskeyid
}
func (this *SmsParam) GetAccessKeyId() string {
return this.AccessKeyId
}
func (this *SmsParam) SetSignatureMethod(signaturemethod string) {
this.SignatureMethod = signaturemethod
}
func (this *SmsParam) GetSignatureMethod() string {
return this.SignatureMethod
}
func (this *SmsParam) SetTimestamp(timestamp string) {
this.Timestamp = timestamp
}
func (this *SmsParam) GetTimestamp() string {
return this.Timestamp
}
func (this *SmsParam) SetSignatureVersion(signatureversion string) {
this.SignatureVersion = signatureversion
}
func (this *SmsParam) GetSignatureVersion() string {
return this.SignatureVersion
}
func (this *SmsParam) SetSignatureNonce(signaturenonce string) {
this.SignatureNonce = signaturenonce
}
func (this *SmsParam) GetSignatureNonce() string {
return this.SignatureNonce
}
func (this *SmsParam) SetRegionId(regionid string) {
this.RegionId = regionid
}
func (this *SmsParam) GetRegionId() string {
return this.RegionId
}
// 短信服务器返回的错误信息
type ErrorMessage struct {
HttpCode int `json"-"`
Model *string `json:"Model,omitempty"`
RequestId *string `json:"RequestId,omitempty"`
Message *string `json:"Message,omitempty"`
Code *string `json:"Code,omitempty"`
}
func (e *ErrorMessage) GetHttpCode() int {
return e.HttpCode
}
func (e *ErrorMessage) SetHttpCode(code int) {
e.HttpCode = code
}
func (e *ErrorMessage) GetModel() string {
if e != nil && e.Model != nil {
return *e.Model
}
return ""
}
func (e *ErrorMessage) GetRequestId() string {
if e != nil && e.RequestId != nil {
return *e.RequestId
}
return ""
}
func (e *ErrorMessage) GetMessage() string {
if e != nil && e.Message != nil {
return *e.Message
}
return ""
}
func (e *ErrorMessage) GetCode() string {
if e != nil && e.Code != nil {
return *e.Code
}
return ""
}
func (e *ErrorMessage) Error() string {
body, err := json.Marshal(e)
if err != nil {
return ""
}
return string(body)
}
type SMSClient struct {
// SMS服务的地址,默认为(https://sms.aliyuncs.com)
EndPoint string
// 访问SMS服务的accessid,通过官方网站申请或通过管理员获取
AccessId string
// 访问SMS服务的accesskey,通过官方网站申请或通过管理员获取
AccessKey string
// 连接池中每个连接的Socket超时,单位为秒,可以为int或float。默认值为50
SocketTimeout int
// 其他参数
Param SmsParam
param map[string]string
}
// 设置短信服务器
func (c *SMSClient) SetEndPoint(end_point string) {
c.EndPoint = end_point
}
// 设置短信服务的accessid,通过官方网站申请或通过管理员获取
func (c *SMSClient) SetAccessId(accessid string) {
c.AccessId = accessid
}
// 设置短信服务的accesskey,通过官方网站申请或通过管理员获取
func (c *SMSClient) SetAccessKey(accesskey string) {
c.AccessKey = accesskey
}
// 设置短信服务的Socket超时,单位为秒,可以为int或float。默认值为50
func (c *SMSClient) SetSocketTimeout(sockettimeout int) {
if sockettimeout == 0 {
sockettimeout = 50
}
c.SocketTimeout = sockettimeout
}
// 发送给多个手机号, 最多100个
func (c *SMSClient) SendMulti(recnum []string, signname, templatecode, paramstring string) (e *ErrorMessage, err error) {
var body []byte
e = &ErrorMessage{}
if len(recnum) > 100 {
return nil, errors.New("number of recnum should be less than 100")
}
c.Param.SetSignName(signname)
c.Param.SetTemplateCode(templatecode)
c.Param.SetParamString(paramstring)
c.Param.SetRecNum(strings.Join(recnum, ","))
signature := signature_method(c.AccessKey, c.calc_string_to_sign())
req := urllib.Post(c.EndPoint)
for k, v := range c.param {
req.Param(k, v)
}
req.Param("Signature", signature)
resp, err := req.Response()
if err != nil {
return nil, err
}
if resp.Body == nil {
return nil, nil
}
defer resp.Body.Close()
if resp.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
body, err = ioutil.ReadAll(reader)
} else {
body, err = ioutil.ReadAll(resp.Body)
}
if err != nil {
return nil, err
}
err = json.Unmarshal(body, e)
e.SetHttpCode(resp.StatusCode)
if HttpDebugEnable {
fmt.Println("C-->S:", req.DumpRequestString())
fmt.Println("S-->C:", e.Error())
}
if err != nil {
return e, err
}
if e.GetCode() != "" {
return e, errors.New(e.GetCode())
}
return e, nil
}
// 发送给一个手机号
func (c *SMSClient) SendOne(recnum, signname, templatecode, paramstring string) (e *ErrorMessage, err error) {
var body []byte
e = &ErrorMessage{}
c.Param.SetSignName(signname)
c.Param.SetTemplateCode(templatecode)
c.Param.SetParamString(paramstring)
c.Param.SetRecNum(recnum)
signature := signature_method(c.AccessKey, c.calc_string_to_sign())
req := urllib.Post(c.EndPoint)
if HttpDebugEnable {
req.Debug(true)
}
for k, v := range c.param {
req.Param(k, v)
}
req.Param("Signature", signature)
resp, err := req.Response()
if err != nil {
return nil, err
}
if resp.Body == nil {
return nil, nil
}
defer resp.Body.Close()
if resp.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
body, err = ioutil.ReadAll(reader)
} else {
body, err = ioutil.ReadAll(resp.Body)
}
if err != nil {
return nil, err
}
err = json.Unmarshal(body, e)
e.SetHttpCode(resp.StatusCode)
if HttpDebugEnable {
fmt.Println("C-->S:", req.DumpRequestString())
fmt.Println("S-->C:", e.Error())
}
if err != nil {
return e, err
}
if e.GetCode() != "" {
return e, errors.New(e.GetCode())
}
return e, nil
}
func (c *SMSClient) calc_string_to_sign() string {
c.param = make(map[string]string)
c.param["Action"] = c.Param.GetAction()
c.param["SignName"] = c.Param.GetSignName()
c.param["TemplateCode"] = c.Param.GetTemplateCode()
c.param["RecNum"] = c.Param.GetRecNum()
c.param["ParamString"] = c.Param.GetParamString()
c.param["Format"] = c.Param.GetFormat()
c.param["Version"] = c.Param.GetVersion()
c.param["AccessKeyId"] = c.Param.GetAccessKeyId()
c.param["SignatureMethod"] = c.Param.GetSignatureMethod()
c.param["Timestamp"] = time.Now().UTC().Format(time.RFC3339)
// sync c.Param.Timestamp
c.Param.SetTimestamp(c.param["Timestamp"])
c.param["SignatureVersion"] = c.Param.GetSignatureVersion()
c.param["SignatureNonce"] = uuid.New()
// sync c.Param.SignatureNonce
c.Param.SetSignatureNonce(c.param["SignatureNonce"])
c.param["RegionId"] = c.Param.GetRegionId()
strslice := make([]string, len(c.param))
i := 0
for k, v := range c.param {
data := url.Values{}
data.Add(k, v)
strslice[i] = data.Encode()
strslice[i] = aliyun_sms_encode_over(strslice[i])
i++
}
sort.Strings(strslice)
return "POST&" + percent_encode("/") + "&" + percent_encode(strings.Join(strslice, "&"))
}
func signature_method(key, string_to_sign string) string {
// The signature method is supposed to be HmacSHA1
// A switch case is required if there is other methods available
mac := hmac.New(sha1.New, []byte(key+"&"))
mac.Write([]byte(string_to_sign))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
// 一般支持 URL 编码的库(比如 Java 中的 java.net.URLEncoder)都是按照“application/x-www-form-urlencoded”的MIME类型的规则进行编码的。
// 实现时可以直接使用这类方式进行编码,
// 把编码后的字符串中加号(+)替换成%20、星号(*)替换成%2A、%7E 替换回波浪号(~), 即可得到所需要的编码字符串
func percent_encode(s string) string {
s = url.QueryEscape(s)
s = strings.Replace(s, "+", "%20", -1)
s = strings.Replace(s, "*", "%2A", -1)
s = strings.Replace(s, "%7E", "~", -1)
return s
}
// 把编码后的字符串中加号(+)替换成%20、星号(*)替换成%2A、%7E 替换回波浪号(~), 即可得到所需要的编码字符串
func aliyun_sms_encode_over(s string) string {
s = strings.Replace(s, "+", "%20", -1)
s = strings.Replace(s, "*", "%2A", -1)
s = strings.Replace(s, "%7E", "~", -1)
return s
}
// 创建一个短信发送客户端
func New(accessid, accesskey string) (c *SMSClient) {
c = new(SMSClient)
if c.EndPoint == "" {
c.EndPoint = "https://sms.aliyuncs.com/"
}
c.AccessId = accessid
c.AccessKey = accesskey
c.Param.SetAction("SingleSendSms")
c.Param.SetSignName("your_signname")
c.Param.SetTemplateCode("your_templatecode")
c.Param.SetRecNum("your_recnum")
c.Param.SetParamString("your_paramstring")
c.Param.SetFormat("JSON")
c.Param.SetVersion("2016-09-27")
c.Param.SetAccessKeyId(accessid)
c.Param.SetSignatureMethod("HMAC-SHA1")
c.Param.SetTimestamp(time.Now().UTC().Format(time.RFC3339))
c.Param.SetSignatureVersion("1.0")
c.Param.SetSignatureNonce(uuid.New())
c.Param.SetRegionId("cn-hangzhou")
// set default setting for urllib
url_setting := urllib.HttpSettings{
false, // ShowDebug
"GiterLab", // UserAgent
50 * time.Second, // ConnectTimeout
50 * time.Second, // ReadWriteTimeout
nil, // TlsClientConfig
nil, // Proxy
nil, // Transport
false, // EnableCookie
true, // Gzip
true, // DumpBody
}
if c.SocketTimeout != 0 {
url_setting.ConnectTimeout = time.Duration(c.SocketTimeout) * time.Second
url_setting.ReadWriteTimeout = time.Duration(c.SocketTimeout) * time.Second
}
if HttpDebugEnable {
url_setting.ShowDebug = true
} else {
url_setting.ShowDebug = false
}
urllib.SetDefaultSetting(url_setting)
return c
}