httphandler.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. . "commonapi"
  4. "crypto/sha1"
  5. "encoding/hex"
  6. "fmt"
  7. "io/ioutil"
  8. "math/rand"
  9. "net/http"
  10. simplejson "github.com/go-simplejson"
  11. )
  12. var handler = map[string]func(w http.ResponseWriter, r *http.Request, js *simplejson.Json, body []byte) int{} /*{
  13. QUERYTRANSID: commonFunc,
  14. QUERYBLOCKID: commonFunc,
  15. QUERYADDRESS: commonFunc,
  16. QUERYBLOCKINFO: commonFunc,
  17. QUERYBLOCKHEIGHT: commonFunc,
  18. }*/
  19. func init() {
  20. for k, _ := range Handler {
  21. handler[k] = commonFunc
  22. }
  23. }
  24. func commonHandler(w http.ResponseWriter, r *http.Request) {
  25. var ret = 0
  26. defer func() {
  27. if ret != 0 {
  28. errresp := fmt.Sprintf(`{"errcode":%v,"msg":"%v"}`, ret, GetErrMsg(ret, nil))
  29. w.Write([]byte(errresp))
  30. log.Error("send resp:%v", errresp)
  31. return
  32. }
  33. }()
  34. path := r.URL.Path
  35. defer r.Body.Close()
  36. body, err := ioutil.ReadAll(r.Body)
  37. if err != nil {
  38. ret = INNER_ERR
  39. log.Error("err:%s", err.Error())
  40. return
  41. }
  42. log.Debug("read body:%v", string(body))
  43. js, err := simplejson.NewJson(body)
  44. if err != nil {
  45. ret = FORMAT_ERR
  46. log.Error("err:%s", err.Error())
  47. return
  48. }
  49. timestamp := js.Get("timestamp").MustString()
  50. signature := js.Get("signature").MustString()
  51. var ok bool
  52. if ret, ok = checkPermission(timestamp, signature, []byte(path)); !ok {
  53. log.Error("check signature err")
  54. return
  55. }
  56. if _, ok := handler[path]; !ok {
  57. w.WriteHeader(404)
  58. ret = NO_AUTH
  59. log.Error("no path,invalid path:%v", path)
  60. return
  61. }
  62. log.Debug("path:%v", path)
  63. ret = handler[path](w, r, js, body)
  64. return
  65. }
  66. func commonFunc(w http.ResponseWriter, r *http.Request, js *simplejson.Json, body []byte) int {
  67. cointype := js.Get("cointype").MustString()
  68. taskH := NewtaskHandler()
  69. task := &taskInfo{
  70. cointype: cointype,
  71. jshandler: js,
  72. urlpath: r.URL.Path,
  73. resp: taskH,
  74. }
  75. chanindex := rand.Int31n(100) % int32(CPUNUM)
  76. taskChan[chanindex] <- task
  77. resp, err := taskH.waitProcessData()
  78. if err != nil {
  79. return TIME_OUT
  80. }
  81. w.Write(resp)
  82. log.Debug("resp write:%v", string(resp))
  83. return OK
  84. }
  85. func checkSignature(body []byte, timestamp, signature string) bool {
  86. sha1Contain := sha1.New()
  87. byteSha1 := append(body, append([]byte(timestamp), []byte("coinsapi^&#@(*33")...)...)
  88. sha1Contain.Write(byteSha1)
  89. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  90. log.Debug("caculate local signature:%v", localSig)
  91. return signature == localSig
  92. }
  93. func checkPermission(timestamp, signature string, body []byte) (int, bool) {
  94. // modify by hyb cancel checkPermission
  95. //if checkSignature(body, timestamp, signature) == false {
  96. // return NO_AUTH, false
  97. //}
  98. return OK, true
  99. }