httphandler.go 5.2 KB

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