httphandler.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package main
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. "math/rand"
  8. "net/http"
  9. . "template"
  10. simplejson "github.com/go-simplejson"
  11. )
  12. //const (
  13. // CREATEADDR = "/coinproxy/createaddr"
  14. // TRANSINFO = "/coinproxy/query/transinfo"
  15. // COINBALANCE = "/coinproxy/coinbalance"
  16. // SENDCOINS = "/coinproxy/sendcoins"
  17. //)
  18. var handler = map[string]func(w http.ResponseWriter, r *http.Request, js *simplejson.Json, body []byte) int{
  19. CREATEADDR: commonFunc,
  20. TRANSINFO: commonFunc,
  21. COINBALANCE: commonFunc,
  22. SENDCOINS: commonFunc,
  23. CHAINHEIGHT: commonFunc,
  24. LISTUNSPENT: commonFunc,
  25. RAWTRANSFER: commonFunc,
  26. SUMCOINBALANCE: commonFunc,
  27. }
  28. func commonHandler(w http.ResponseWriter, r *http.Request) {
  29. var ret = 0
  30. defer func() {
  31. if ret != 0 {
  32. errresp := fmt.Sprintf(`{"errcode":%v,"msg":"%v"}`, ret, GetErrMsg(ret, nil))
  33. w.Write([]byte(errresp))
  34. log.Error("send resp:%v", errresp)
  35. return
  36. }
  37. }()
  38. path := r.URL.Path
  39. defer r.Body.Close()
  40. body, err := ioutil.ReadAll(r.Body)
  41. if err != nil {
  42. ret = INNER_ERR
  43. log.Error("err:%s", err.Error())
  44. return
  45. }
  46. log.Debug("read body:%v", string(body))
  47. js, err := simplejson.NewJson(body)
  48. if err != nil {
  49. ret = FORMAT_ERR
  50. log.Error("err:%s", err.Error())
  51. return
  52. }
  53. timestamp := js.Get("timestamp").MustString()
  54. signature := js.Get("signature").MustString()
  55. var ok bool
  56. if ret, ok = checkPermission(timestamp, signature, []byte(path)); !ok {
  57. log.Error("check signature err")
  58. return
  59. }
  60. if _, ok := handler[path]; !ok {
  61. w.WriteHeader(404)
  62. ret = NO_AUTH
  63. log.Error("no path,invalid path:%v", path)
  64. return
  65. }
  66. log.Debug("path:%v", path)
  67. ret = handler[path](w, r, js, body)
  68. return
  69. }
  70. func commonFunc(w http.ResponseWriter, r *http.Request, js *simplejson.Json, body []byte) int {
  71. cointype := js.Get("cointype").MustString()
  72. taskH := NewtaskHandler()
  73. task := &taskInfo{
  74. cointype: cointype,
  75. jshandler: js,
  76. urlpath: r.URL.Path,
  77. resp: taskH,
  78. }
  79. chanindex := rand.Int31n(100) % int32(CPUNUM)
  80. taskChan[chanindex] <- task
  81. resp, err := taskH.waitProcessData()
  82. if err != nil {
  83. return TIME_OUT
  84. }
  85. w.Write(resp)
  86. log.Debug("resp write:%v", string(resp))
  87. return OK
  88. }
  89. func checkSignature(body []byte, timestamp, signature string) bool {
  90. sha1Contain := sha1.New()
  91. byteSha1 := append(body, append([]byte(timestamp), []byte(Gconfig.CoinType.Sigkey)...)...)
  92. sha1Contain.Write(byteSha1)
  93. localSig := hex.EncodeToString(sha1Contain.Sum(nil))
  94. log.Debug("caculate local signature:%v", localSig)
  95. return signature == localSig
  96. }
  97. func checkPermission(timestamp, signature string, body []byte) (int, bool) {
  98. if string(body) != SENDCOINS {
  99. return OK, true
  100. }
  101. if checkSignature(body, timestamp, signature) == false {
  102. return NO_AUTH, false
  103. }
  104. return OK, true
  105. }
  106. ////获取入币交易信息
  107. //func transInfo(w http.ResponseWriter, r *http.Request) {
  108. // var ret = 0
  109. // defer func() {
  110. // if ret != 0 {
  111. // errresp := fmt.Sprintf(`{"errcode":%v,"msg":"%v"}`, ret, GetErrMsg(ret, nil))
  112. // w.Write([]byte(errresp))
  113. // return
  114. // }
  115. // }()
  116. // defer r.Body.Close()
  117. // body, err := ioutil.ReadAll(r.Body)
  118. // if err != nil {
  119. // ret = INNER_ERR
  120. // log.Error("err:%s", err.Error())
  121. // return
  122. // }
  123. // log.Debug("read body:%v", string(body))
  124. // js, err := simplejson.NewJson(body)
  125. // if err != nil {
  126. // ret = FORMAT_ERR
  127. // log.Error("err:%s", err.Error())
  128. // return
  129. // }
  130. // timestamp := js.Get("timestamp").MustString()
  131. // signature := js.Get("signature").MustString()
  132. // cointype := js.Get("cointype").MustString()
  133. // var ok bool
  134. // if ret, ok = checkPermission(timestamp, signature, "post", body); !ok {
  135. // //return
  136. // }
  137. // taskH := NewtaskHandler()
  138. // task := &taskInfo{
  139. // cointype: cointype,
  140. // jshandler: js,
  141. // urlpath: r.URL.Path,
  142. // resp: taskH,
  143. // }
  144. // chanindex := rand.Int31n(100) % int32(CPUNUM)
  145. // taskChan[chanindex] <- task
  146. // resp, err := taskH.waitProcessData()
  147. // if err != nil {
  148. // ret = TIME_OUT
  149. // return
  150. // }
  151. // w.Write(resp)
  152. // ret = OK
  153. // log.Debug("resp write:%v", string(resp))
  154. // return
  155. //}
  156. //func createAddr(w http.ResponseWriter, r *http.Request) {
  157. // var ret = 0
  158. // defer func() {
  159. // if ret != 0 {
  160. // errresp := fmt.Sprintf(`{"errcode":%v,"msg":"%v"}`, ret, GetErrMsg(ret, nil))
  161. // w.Write([]byte(errresp))
  162. // return
  163. // }
  164. // }()
  165. // defer r.Body.Close()
  166. // body, err := ioutil.ReadAll(r.Body)
  167. // if err != nil {
  168. // ret = INNER_ERR
  169. // log.Error("err:%s", err.Error())
  170. // return
  171. // }
  172. // js, err := simplejson.NewJson(body)
  173. // if err != nil {
  174. // ret = FORMAT_ERR
  175. // log.Error("err:%s", err.Error())
  176. // return
  177. // }
  178. // timestamp := js.Get("timestamp").MustString()
  179. // signature := js.Get("signature").MustString()
  180. // cointype := js.Get("cointype").MustString()
  181. // var ok bool
  182. // if ret, ok = checkPermission(timestamp, signature, "post", body); !ok {
  183. // //return
  184. // //TODO
  185. // }
  186. // taskH := NewtaskHandler()
  187. // task := &taskInfo{
  188. // cointype: cointype,
  189. // jshandler: js,
  190. // urlpath: r.URL.Path,
  191. // resp: taskH,
  192. // }
  193. // chanindex := rand.Int31n(100) % int32(CPUNUM)
  194. // taskChan[chanindex] <- task
  195. // resp, err := taskH.waitProcessData()
  196. // if err != nil {
  197. // ret = TIME_OUT
  198. // return
  199. // }
  200. // w.Write(resp)
  201. // ret = OK
  202. // return
  203. //}