httphandler.go 5.1 KB

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