Explorar el Código

add check header

张延森 hace 3 años
padre
commit
b9a1f8862f
Se han modificado 5 ficheros con 95 adiciones y 13 borrados
  1. 16
    0
      help.go
  2. 25
    0
      help.html
  3. 7
    13
      main.go
  4. 3
    0
      sms.go
  5. 44
    0
      utils.go

+ 16
- 0
help.go Ver fichero

@@ -0,0 +1,16 @@
1
+package main
2
+
3
+import (
4
+	"io/ioutil"
5
+	"net/http"
6
+)
7
+
8
+func helpHandler(w http.ResponseWriter, r *http.Request) {
9
+	content, err := ioutil.ReadFile("./help.html")
10
+	if err != nil {
11
+		http.Error(w, err.Error(), http.StatusInternalServerError)
12
+	}
13
+
14
+	w.Header().Set("Content-Type", "text/html; charset=utf-8")
15
+	w.Write(content)
16
+}

+ 25
- 0
help.html Ver fichero

@@ -9,6 +9,31 @@
9 9
 <body>
10 10
   <h2>接口说明</h2>
11 11
   <h3>/sms</h3>
12
+  <p>
13
+    接口校验 说明: (Header)
14
+  </p>
15
+  <table cellpadding="10" cellspacing="10">
16
+    <tr>
17
+      <th>参数</th>
18
+      <th>类型</th>
19
+      <th>说明</th>
20
+    </tr>
21
+    <tr>
22
+      <td>x-appid</td>
23
+      <td>string</td>
24
+      <td>应用APPID</td>
25
+    </tr>
26
+    <tr>
27
+      <td>x-timestamp</td>
28
+      <td>int64</td>
29
+      <td>接口请求时间, UTC, 自 1970-1-1 以来的毫秒数 </td>
30
+    </tr>
31
+    <tr>
32
+      <td>x-sign</td>
33
+      <td>string</td>
34
+      <td>校验位,计算方式 = md5(appid + secret + timestamp)</td>
35
+    </tr>
36
+  </table>
12 37
   <p>
13 38
     Body 说明: (格式 json)
14 39
   </p>

+ 7
- 13
main.go Ver fichero

@@ -3,39 +3,33 @@ package main
3 3
 import (
4 4
 	"fmt"
5 5
 	"io"
6
-	"io/ioutil"
7 6
 	"log"
8 7
 	"net/http"
9 8
 )
10 9
 
11
-const _accessKeyId string = "LTAI5tGjnZY6k799BHxhmqcm"
12
-const _accessKeySecret string = "eU1DmULbgHe2dnIg3P93634PO2vEh5"
13 10
 const _addr string = ":8081"
14 11
 
15 12
 func smsHandler(w http.ResponseWriter, r *http.Request) {
13
+	if err := authHeader(r); err != nil {
14
+		http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
15
+		return
16
+	}
17
+
16 18
 	defer r.Body.Close()
17 19
 	body, err := io.ReadAll(r.Body)
18 20
 	if err != nil {
19 21
 		http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
22
+		return
20 23
 	}
21 24
 
22 25
 	if err := SendSMS(&body); err != nil {
23 26
 		http.Error(w, err.Error()+"\n访问 /help 查看使用帮助", http.StatusInternalServerError)
27
+		return
24 28
 	}
25 29
 
26 30
 	fmt.Fprintf(w, "success")
27 31
 }
28 32
 
29
-func helpHandler(w http.ResponseWriter, r *http.Request) {
30
-	content, err := ioutil.ReadFile("./help.html")
31
-	if err != nil {
32
-		http.Error(w, err.Error(), http.StatusInternalServerError)
33
-	}
34
-
35
-	w.Header().Set("Content-Type", "text/html; charset=utf-8")
36
-	w.Write(content)
37
-}
38
-
39 33
 func main() {
40 34
 	http.HandleFunc("/sms", smsHandler)
41 35
 	http.HandleFunc("/help", helpHandler)

+ 3
- 0
sms.go Ver fichero

@@ -8,6 +8,9 @@ import (
8 8
 	"github.com/alibabacloud-go/tea/tea"
9 9
 )
10 10
 
11
+const _accessKeyId string = "LTAI5tGjnZY6k799BHxhmqcm"
12
+const _accessKeySecret string = "eU1DmULbgHe2dnIg3P93634PO2vEh5"
13
+
11 14
 var client *dysmsapi20170525.Client
12 15
 
13 16
 func CreateClient(accessKeyId string, accessKeySecret string) (_result *dysmsapi20170525.Client, _err error) {

+ 44
- 0
utils.go Ver fichero

@@ -0,0 +1,44 @@
1
+package main
2
+
3
+import (
4
+	"crypto/md5"
5
+	"errors"
6
+	"fmt"
7
+	"net/http"
8
+	"strconv"
9
+	"time"
10
+)
11
+
12
+func md5Str(plain, salt string) string {
13
+	data := plain + salt
14
+	return fmt.Sprintf("%x", md5.Sum([]byte(data)))
15
+}
16
+
17
+func authHeader(r *http.Request) error {
18
+	appid := r.Header.Get("x-appid")
19
+	timestamp := r.Header.Get("x-timestamp")
20
+	sign := r.Header.Get("x-sign")
21
+
22
+	if appid == "" || timestamp == "" || sign == "" {
23
+		return errors.New("没有找到校验头信息")
24
+	}
25
+
26
+	millisec, err := strconv.ParseInt(timestamp, 10, 64)
27
+	if err != nil {
28
+		return errors.New("校验日期格式不正确")
29
+	}
30
+
31
+	tm := time.UnixMilli(millisec)
32
+	if time.Since(tm) > time.Minute*5 {
33
+		return errors.New("请求超时")
34
+	}
35
+
36
+	secret := appid + timestamp
37
+
38
+	checkStr := md5Str(appid+secret, timestamp)
39
+	if sign != checkStr {
40
+		return errors.New("非法请求")
41
+	}
42
+
43
+	return nil
44
+}