ソースを参照

feat:新增签名直接获取数据

xuzhiheng 3 年 前
コミット
37188be9dc
4 ファイル変更112 行追加31 行削除
  1. 2 2
      api_test/config.json
  2. 64 23
      api_test/main.go
  3. 2 1
      jyservice/src/main.go
  4. 44 5
      jyservice/src/usermanager/getdata.go

+ 2 - 2
api_test/config.json

@@ -1,6 +1,6 @@
 {
 {
-    "appid": "jyOh1XQgUJBQ5bTUlKCyZ1",
-    "key": "56IrWR74",
+    "appid": "jyPRZXQgoJCANdSUBBAiNr",
+    "key": "B00Wi4Cg",
     "day": "-1",
     "day": "-1",
     "apiurl": "https://api.jianyu360.com"
     "apiurl": "https://api.jianyu360.com"
 }
 }

+ 64 - 23
api_test/main.go

@@ -7,23 +7,29 @@ import (
 	"fmt"
 	"fmt"
 	"io/ioutil"
 	"io/ioutil"
 	"log"
 	"log"
+	"net"
 	"net/http"
 	"net/http"
+	"net/url"
 	"qfw/util"
 	"qfw/util"
 	"strings"
 	"strings"
 	"time"
 	"time"
 )
 )
 
 
 var (
 var (
-	config map[string]string
-	//	appid  = "jyNjdXQgUDAwdaTklMPz5i"
-	//	key    = "404M0v2j"
-	//	apiurl = "http://127.0.0.1:8801"
-	//	appid  = "jyMi1XQgMABQNcSkBMIhBq"
-	//	key    = "6PzV0CUa"
-	//	apiurl = "https://testapi.jianyu360.com"
+	config                  map[string]string
 	appid, key, apiurl, day string
 	appid, key, apiurl, day string
+	httpClient              *http.Client
+	MaxIdleCons             int = 100
+	MaxIdleConsPerHost      int = 100
+	IdleConnTimeout         int = 2048
+	ConnectTimeOut          int = 30
+	KeepAlive               int = 30
 )
 )
 
 
+func init() {
+	httpClient = createHttpClient()
+}
+
 func main() {
 func main() {
 	util.ReadConfig(&config)
 	util.ReadConfig(&config)
 	appid = config["appid"]
 	appid = config["appid"]
@@ -33,6 +39,23 @@ func main() {
 	getData()
 	getData()
 }
 }
 
 
+func createHttpClient() *http.Client {
+	client := &http.Client{
+		Transport: &http.Transport{
+			Proxy: http.ProxyFromEnvironment,
+			DialContext: (&net.Dialer{
+				Timeout:   time.Duration(ConnectTimeOut) * time.Second, //TCP连接超时30s
+				KeepAlive: time.Duration(KeepAlive) * time.Second,      //TCP keepalive保活检测定时30s
+			}).DialContext,
+			MaxIdleConns:          MaxIdleCons,
+			MaxIdleConnsPerHost:   MaxIdleConsPerHost,
+			IdleConnTimeout:       time.Duration(IdleConnTimeout) * time.Second, //闲置连接超时2048s
+			ResponseHeaderTimeout: time.Second * 60,
+		},
+	}
+	return client
+}
+
 func getToken() (token string) {
 func getToken() (token string) {
 	tm := fmt.Sprintf("%d", time.Now().Unix())
 	tm := fmt.Sprintf("%d", time.Now().Unix())
 	res := post(apiurl+"/user/access_token", map[string]string{
 	res := post(apiurl+"/user/access_token", map[string]string{
@@ -40,7 +63,7 @@ func getToken() (token string) {
 		"timestamp": tm,
 		"timestamp": tm,
 		"signature": MD5(appid + tm + key),
 		"signature": MD5(appid + tm + key),
 		//"key":       "6PzV0CUa",
 		//"key":       "6PzV0CUa",
-	})
+	}, nil)
 	log.Println(tm, MD5(appid+tm+key), res)
 	log.Println(tm, MD5(appid+tm+key), res)
 	if res != nil && res["access_token"] != "" {
 	if res != nil && res["access_token"] != "" {
 		token, _ = res["access_token"].(string)
 		token, _ = res["access_token"].(string)
@@ -49,15 +72,21 @@ func getToken() (token string) {
 }
 }
 
 
 func getData() {
 func getData() {
-	token := getToken()
+	// token := getToken()
+	tm := fmt.Sprint(time.Now().Unix())
 	data := post(apiurl+"/data/getalldata", map[string]string{
 	data := post(apiurl+"/data/getalldata", map[string]string{
-		"access_token": token,
-		"day":          day,
-		"next":         "92",
+		// "access_token": token,
+		"day":   day,
+		"next":  "0",
+		"appid": appid,
+	}, map[string]string{
+		"timestamp": tm,
+		"signature": MD5(appid + tm + key),
 	})
 	})
 	//s, _ := json.Marshal(data["data"])
 	//s, _ := json.Marshal(data["data"])
 	//delete(data, "data")
 	//delete(data, "data")
-	log.Println(token, data)
+	log.Println("tm", appid, tm)
+	log.Println(MD5(appid+tm+key), data)
 }
 }
 
 
 func MD5(str string) string {
 func MD5(str string) string {
@@ -66,19 +95,31 @@ func MD5(str string) string {
 	return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
 	return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
 }
 }
 
 
-func post(url string, form map[string]string) (data map[string]interface{}) {
-	str := ""
+func post(urls string, form, header map[string]string) (data map[string]interface{}) {
+	formValues := make(url.Values)
 	for k, v := range form {
 	for k, v := range form {
-		str += "&" + k + "=" + v
+		formValues[k] = []string{v}
+	}
+	log.Println(formValues)
+	header["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
+	request, err := http.NewRequest("POST", urls, strings.NewReader(formValues.Encode()))
+	if err != nil {
+		return
+	}
+	for k, v := range header {
+		request.Header.Add(k, v)
+	}
+	response, err := httpClient.Do(request) //前面预处理一些参数,状态,Do执行发送;处理返回结果;Do:发送请求,
+	if err != nil {
+		return
 	}
 	}
-	log.Println(str)
-	res, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str))
+	defer response.Body.Close()
+	replay, err := ioutil.ReadAll(response.Body)
 	if err != nil {
 	if err != nil {
-		log.Println("post err:", err.Error())
-	} else if res.Body != nil {
-		defer res.Body.Close()
-		bs, _ := ioutil.ReadAll(res.Body)
-		json.Unmarshal(bs, &data)
+		log.Println("read reply error:", err)
+		return
 	}
 	}
+	log.Println(string(replay))
+	json.Unmarshal(replay, &data)
 	return
 	return
 }
 }

+ 2 - 1
jyservice/src/main.go

@@ -16,10 +16,11 @@ func Server() {
 //-----windows
 //-----windows
 import (
 import (
 	"config"
 	"config"
-	"github.com/fvbock/endless"
 	"usermanager"
 	"usermanager"
 	"utils"
 	"utils"
 
 
+	"github.com/fvbock/endless"
+
 	log "github.com/sirupsen/logrus"
 	log "github.com/sirupsen/logrus"
 )
 )
 
 

+ 44 - 5
jyservice/src/usermanager/getdata.go

@@ -7,6 +7,11 @@ import (
 	. "config"
 	. "config"
 	"fmt"
 	"fmt"
 
 
+	"crypto/md5"
+	"encoding/hex"
+	"math"
+	"strconv"
+
 	log "github.com/sirupsen/logrus"
 	log "github.com/sirupsen/logrus"
 	"gopkg.in/mgo.v2/bson"
 	"gopkg.in/mgo.v2/bson"
 
 
@@ -39,7 +44,11 @@ var (
 func GetData(w http.ResponseWriter, r *http.Request) {
 func GetData(w http.ResponseWriter, r *http.Request) {
 	defer util.Catch()
 	defer util.Catch()
 	access_token := r.FormValue("access_token")
 	access_token := r.FormValue("access_token")
-	_, _, d := CheckUserInfo(access_token, 0, 0, 0)
+	//新增签名方式  2022年3月3日徐志恒
+	appid := r.FormValue("appid")
+	signature := r.Header.Get("signature")
+	timestamp := r.Header.Get("timestamp")
+	_, _, d := CheckUserInfo(access_token, 0, 0, 0, signature, timestamp, appid)
 	WriteJSON(w, &d)
 	WriteJSON(w, &d)
 }
 }
 
 
@@ -49,16 +58,20 @@ func GetAllData(w http.ResponseWriter, r *http.Request) {
 	access_token := r.FormValue("access_token")
 	access_token := r.FormValue("access_token")
 	day := util.IntAll(r.FormValue("day"))
 	day := util.IntAll(r.FormValue("day"))
 	next := util.IntAll(r.FormValue("next"))
 	next := util.IntAll(r.FormValue("next"))
+	//新增签名方式  2022年3月3日徐志恒
+	appid := r.FormValue("appid")
+	signature := r.Header.Get("signature")
+	timestamp := r.Header.Get("timestamp")
 	if day > 0 || day < -60 {
 	if day > 0 || day < -60 {
 		d := JSON{"code": CODE_E2, "msg": MSG_E2}
 		d := JSON{"code": CODE_E2, "msg": MSG_E2}
 		WriteJSON(w, &d)
 		WriteJSON(w, &d)
 	} else {
 	} else {
-		_, _, d := CheckUserInfo(access_token, day, next, 1)
+		_, _, d := CheckUserInfo(access_token, day, next, 1, signature, timestamp, appid)
 		WriteJSON(w, &d)
 		WriteJSON(w, &d)
 	}
 	}
 }
 }
 
 
-func CheckUserInfo(access_token string, day, next, all int) (bcheck bool, appid string, d JSON) {
+func CheckUserInfo(access_token string, day, next, all int, signature, timestamp, appids string) (bcheck bool, appid string, d JSON) {
 	defer util.Catch()
 	defer util.Catch()
 	d = JSON{}
 	d = JSON{}
 	//第一层判断token是否失效或格式不对
 	//第一层判断token是否失效或格式不对
@@ -101,6 +114,34 @@ func CheckUserInfo(access_token string, day, next, all int) (bcheck bool, appid
 				}
 				}
 			}
 			}
 		}
 		}
+	} else if signature != "" {
+		userType = 2
+		_timestamp, _ := strconv.ParseInt(timestamp, 10, 64)
+		TimestampExpire := time.Now().Unix() - _timestamp
+		if math.Abs(float64(TimestampExpire)) > 600 {
+			d["code"] = CODE_E6
+			d["msg"] = MSG_E6
+		} else {
+			appid = appids
+			data, ok := Mgo.FindOneByField("user", &map[string]interface{}{"appid": appid}, `{"plan":1,"key":1,"fields":1}`)
+			if !ok && data == nil && *data == nil {
+				return
+			}
+			h := md5.New()
+			h.Write([]byte(appid + timestamp + util.ObjToString((*data)["key"])))
+			s1 := strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
+			log.Debug("signature:", signature, s1)
+			if s1 == signature {
+				bcheck = true
+				plan, _ := (*data)["plan"].(map[string]interface{})
+				planname = util.ObjToString(plan["name"])
+				fs, _ := (*data)["fields"].(string)
+				appfields = strings.Replace(fs, ",", "___", -1)
+			} else {
+				d["code"] = CODE_E7
+				d["msg"] = MSG_E7
+			}
+		}
 	}
 	}
 	if !bcheck && len(d) == 0 {
 	if !bcheck && len(d) == 0 {
 		d["code"] = CODE_E1
 		d["code"] = CODE_E1
@@ -227,7 +268,6 @@ func CheckUserInfo(access_token string, day, next, all int) (bcheck bool, appid
 							} else {
 							} else {
 								d["data"] = infos
 								d["data"] = infos
 							}
 							}
-							//log.Println(d["data"])
 							d["size"] = len(infos)
 							d["size"] = len(infos)
 							d["new_size"] = new_size
 							d["new_size"] = new_size
 							d["available_times"] = GETDATA_LIMIT_TIMES - limittoday - 1
 							d["available_times"] = GETDATA_LIMIT_TIMES - limittoday - 1
@@ -416,7 +456,6 @@ func GetProjectId(id string) string {
 
 
 		}
 		}
 		projectId = strings.Join(projectIdArr, ",")
 		projectId = strings.Join(projectIdArr, ",")
-		log.Println("projectId", projectId)
 	}
 	}
 	return projectId
 	return projectId
 }
 }