|
@@ -22,6 +22,7 @@ import (
|
|
|
"sort"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
|
|
|
"github.com/go-xweb/xweb"
|
|
@@ -60,13 +61,31 @@ const (
|
|
|
LIMIT_LIST_COUNT = 100
|
|
|
)
|
|
|
|
|
|
-var APPID, SECRET string
|
|
|
+type User struct {
|
|
|
+ username string
|
|
|
+ appid string
|
|
|
+ ip_blimit bool
|
|
|
+ ip map[string]bool
|
|
|
+ secret string
|
|
|
+ ordernum int
|
|
|
+ status int
|
|
|
+}
|
|
|
+
|
|
|
+var UserLock = sync.Mutex{}
|
|
|
+var UserMap = map[string]*User{}
|
|
|
|
|
|
//
|
|
|
func (o *Oamanager) Index() error {
|
|
|
return o.Render("/pc/article.html")
|
|
|
}
|
|
|
|
|
|
+func ClearUserMap(w http.ResponseWriter, r *http.Request) {
|
|
|
+ UserLock.Lock()
|
|
|
+ UserMap = map[string]*User{}
|
|
|
+ log.Println("清空操作:", UserMap)
|
|
|
+ UserLock.Unlock()
|
|
|
+}
|
|
|
+
|
|
|
//
|
|
|
func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
defer util.Catch()
|
|
@@ -97,47 +116,60 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
rMsg = MSG_E3
|
|
|
} else {
|
|
|
apicount = redis.GetInt(REDISDB, "jyoacount-"+appid+oaday)
|
|
|
- log.Println("每天调用次数:", apicount)
|
|
|
if apicount >= LIMIT_COUNT { //调用接口上限判断
|
|
|
rCode = CODE_E4 //调用接口超过限制
|
|
|
rMsg = MSG_E4
|
|
|
} else {
|
|
|
- res, ok := mongodb.FindOneByField("user", &map[string]interface{}{
|
|
|
- "appid": appid,
|
|
|
- }, nil)
|
|
|
- if !ok {
|
|
|
- rCode = CODE_E5 //内部错误
|
|
|
- rMsg = MSG_E5
|
|
|
+ UserLock.Lock()
|
|
|
+ user := UserMap[appid]
|
|
|
+ if user == nil {
|
|
|
+ res, ok := mongodb.FindOneByField("user", &map[string]interface{}{
|
|
|
+ "appid": appid,
|
|
|
+ }, nil)
|
|
|
+ if ok && len(*res) > 0 {
|
|
|
+ user = &User{}
|
|
|
+ user.appid = appid
|
|
|
+ user.secret = util.ObjToString((*res)["secret"])
|
|
|
+ ip := util.ObjToString((*res)["userip"])
|
|
|
+ if ip == "*" {
|
|
|
+ user.ip_blimit = true
|
|
|
+ } else {
|
|
|
+ ips := strings.Split(ip, ",")
|
|
|
+ user.ip = map[string]bool{}
|
|
|
+ for _, k := range ips {
|
|
|
+ user.ip[k] = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ user.status = util.IntAll((*res)["status"])
|
|
|
+ user.ordernum = util.IntAll((*res)["ordernum"])
|
|
|
+ UserMap[appid] = user
|
|
|
+ }
|
|
|
+ if !ok {
|
|
|
+ rCode = CODE_E5 //内部错误
|
|
|
+ rMsg = MSG_E5
|
|
|
+ }
|
|
|
}
|
|
|
- if len(*res) > 0 && (*res)["status"] == 1 { //查看用户是否存在
|
|
|
+ UserLock.Unlock()
|
|
|
+ if user != nil && user.status == 1 { //查看用户是否存在
|
|
|
userip := GetAPIIP(r)
|
|
|
- log.Println(userip, "-ip--", (*res)["userip"])
|
|
|
var sign string
|
|
|
- if (*res)["userip"] == userip { //验证用户ip
|
|
|
- APPID = appid
|
|
|
- SECRET = util.ObjToString((*res)["secret"])
|
|
|
+ log.Println(userip, "-ip--", user.ip)
|
|
|
+ if user.ip_blimit || user.ip[userip] { //验证用户ip
|
|
|
+ //SECRET = util.ObjToString((*res)["secret"])
|
|
|
+ params := [][]string{
|
|
|
+ []string{"keyword", keyword},
|
|
|
+ []string{"appid", appid},
|
|
|
+ []string{"timestamp", strconv.Itoa(timestamp)}}
|
|
|
if r.FormValue("pagenum") == "" {
|
|
|
pagenum = 1
|
|
|
- sign = GET(action, [][]string{
|
|
|
- []string{"keyword", keyword},
|
|
|
- []string{"appid", appid},
|
|
|
- []string{"timestamp", strconv.Itoa(timestamp)},
|
|
|
- //[]string{"pagenum", strconv.Itoa(pagenum)},
|
|
|
- })
|
|
|
} else {
|
|
|
- sign = GET(action, [][]string{
|
|
|
- []string{"keyword", keyword},
|
|
|
- []string{"appid", appid},
|
|
|
- []string{"timestamp", strconv.Itoa(timestamp)},
|
|
|
- []string{"pagenum", strconv.Itoa(pagenum)},
|
|
|
- })
|
|
|
+ params = append(params, []string{"pagenum", strconv.Itoa(pagenum)})
|
|
|
}
|
|
|
- log.Println(signature, "签名串:", sign)
|
|
|
- ordernum := (*res)["ordernum"].(int)
|
|
|
+ sign = Signature(action, params, user.secret)
|
|
|
if sign == signature { //签名串验证
|
|
|
- username = util.ObjToString((*res)["username"])
|
|
|
+ username = user.username
|
|
|
if action == "getdata" {
|
|
|
- rData["data"] = GetData(pagenum, keyword, ordernum)
|
|
|
+ rData["data"] = GetData(pagenum, keyword, user.ordernum)
|
|
|
redis.Put(REDISDB, "jyoacount-"+appid+oaday, apicount+1, 24*60*60)
|
|
|
} else {
|
|
|
rCode = CODE_E1 //签名错误
|
|
@@ -162,7 +194,7 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
|
|
|
rCode = CODE_E1 //签名错误
|
|
|
rMsg = MSG_E1
|
|
|
}
|
|
|
- go APIlog(username, action, keyword, appid, rMsg, signature, pagenum, timestamp, rCode, apicount)
|
|
|
+ go tools.AddLog(r, username, rMsg, rCode, apicount)
|
|
|
if rCode == 0 {
|
|
|
w.Header().Set("Accept-Charset", "utf-8")
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
@@ -219,8 +251,7 @@ func GetAPIIP(req *http.Request) string {
|
|
|
return ip
|
|
|
}
|
|
|
|
|
|
-//GET请求
|
|
|
-func GET(action string, param [][]string) (signedStr string) {
|
|
|
+func Signature(action string, param [][]string, secret string) (signedStr string) {
|
|
|
ps := ¶mSorter{[]string{}, []string{}}
|
|
|
ps.Keys = append(ps.Keys, "action")
|
|
|
ps.Vals = append(ps.Vals, action)
|
|
@@ -234,7 +265,7 @@ func GET(action string, param [][]string) (signedStr string) {
|
|
|
reqStr := ps.String()
|
|
|
str := percentEncode(reqStr)
|
|
|
str = SP(str, "%3A", "%253A", -1)
|
|
|
- h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(SECRET+"&"))
|
|
|
+ h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(secret+"&"))
|
|
|
io.WriteString(h, str)
|
|
|
signedStr = base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
|
return
|