api.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package unicom
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "encoding/json"
  9. "net/http"
  10. "net/url"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "go-common/app/job/main/app-wall/model"
  16. "go-common/app/job/main/app-wall/model/unicom"
  17. "go-common/library/ecode"
  18. "go-common/library/log"
  19. httpx "go-common/library/net/http/blademaster"
  20. )
  21. const (
  22. // unicom
  23. _unicomUser = "000012"
  24. _unicomPass = "1pibH5e1BN4V"
  25. _unicomAppKey = "com.aop.app.bilibili"
  26. _unicomMethodQryFlowChange = "com.ssp.method.outqryflowchange"
  27. _unicomSecurity = "DVniSMVU6Z3cCIG3vbOn4Fqbof+QJ/6etD+lpa4M4clgj/Dv6XT0syTR8Xgu5nVzKuzro8eiTUzHy/QAzGjp+A=="
  28. // url
  29. _unicomFlowExchangeURL = "/openservlet"
  30. _unicomIPURL = "/web/statistics/subsystem_2/query_ip.php"
  31. )
  32. // FlowQry unicom phone qryflowchange
  33. func (d *Dao) FlowQry(c context.Context, phone int, requestNo int64, outorderid, orderid string, ts time.Time) (orderstatus, msg string, err error) {
  34. param := url.Values{}
  35. param.Set("appkey", _unicomAppKey)
  36. param.Set("apptx", strconv.FormatInt(requestNo, 10))
  37. param.Set("method", _unicomMethodQryFlowChange)
  38. param.Set("orderid", orderid)
  39. param.Set("outorderid", outorderid)
  40. param.Set("timestamp", ts.Format("2006-01-02 15:04:05"))
  41. param.Set("usernumber", strconv.Itoa(phone))
  42. urlVal := d.urlParams(param)
  43. urlVal = urlVal + "&" + d.sign(urlVal)
  44. var res struct {
  45. Code string `json:"respcode"`
  46. Orderstatus string `json:"orderstatus"`
  47. Failurtype string `json:"failurtype"`
  48. Msg string `json:"respdesc"`
  49. }
  50. if err = d.unicomHTTPGet(c, d.unicomFlowExchangeURL+"?"+urlVal, nil, &res); err != nil {
  51. log.Error("unicom flowQry url(%v) error(%v)", d.unicomFlowExchangeURL+"?"+urlVal, err)
  52. return
  53. }
  54. b, _ := json.Marshal(&res)
  55. log.Info("unicom flowQry url(%v) response(%s)", d.unicomFlowExchangeURL+"?"+urlVal, b)
  56. msg = res.Msg
  57. if res.Code != "0000" {
  58. err = ecode.String(res.Code)
  59. log.Error("unicom flowQry url(%v) code(%v) msg(%v)", d.unicomFlowExchangeURL+"?"+urlVal, res.Code, res.Msg)
  60. return
  61. }
  62. orderstatus = res.Orderstatus
  63. return
  64. }
  65. // UnicomIP unicom ip orders
  66. func (d *Dao) UnicomIP(c context.Context, now time.Time) (unicomIPs []*unicom.UnicomIP, err error) {
  67. params := url.Values{}
  68. params.Set("user", _unicomUser)
  69. tick := strconv.FormatInt(now.Unix(), 10)
  70. params.Set("tick", tick)
  71. mh := md5.Sum([]byte(_unicomUser + tick + _unicomPass))
  72. var (
  73. key string
  74. )
  75. if key = hex.EncodeToString(mh[:]); len(key) > 16 {
  76. key = key[:16]
  77. }
  78. params.Set("key", key)
  79. var res struct {
  80. Code int `json:"code"`
  81. LastUpdateTime int64 `json:"last_update_time"`
  82. Desc []struct {
  83. StartIP string `json:"start_ip"`
  84. Length string `json:"length"`
  85. } `json:"desc"`
  86. }
  87. if err = d.unicomHTTPPost(c, d.unicomIPURL, params, &res); err != nil {
  88. log.Error("unicom ip order url(%s) error(%v)", d.unicomIPURL+"?"+params.Encode(), err)
  89. return
  90. }
  91. if res.Code != 0 {
  92. err = ecode.Int(res.Code)
  93. log.Error("unicom ip order url(%s) res code (%d)", d.unicomIPURL+"?"+params.Encode(), res.Code)
  94. return
  95. }
  96. b, _ := json.Marshal(&res)
  97. log.Info("unicom ip url(%s) response(%s)", d.unicomIPURL+"?"+params.Encode(), b)
  98. for _, uip := range res.Desc {
  99. uiplen, _ := strconv.Atoi(uip.Length)
  100. if uiplen < 1 {
  101. log.Error("unicom ip length 0")
  102. continue
  103. }
  104. ipEndInt := model.InetAtoN(uip.StartIP) + uint32((uiplen - 1))
  105. ipEnd := model.InetNtoA(ipEndInt)
  106. unicomIP := &unicom.UnicomIP{}
  107. unicomIP.UnicomIPStrToint(uip.StartIP, ipEnd)
  108. unicomIPs = append(unicomIPs, unicomIP)
  109. }
  110. return
  111. }
  112. // unicomHTTPGet http get
  113. func (d *Dao) unicomHTTPGet(c context.Context, urlStr string, params url.Values, res interface{}) (err error) {
  114. return d.wallHTTP(c, d.uclient, http.MethodGet, urlStr, params, res)
  115. }
  116. // unicomHTTPPost http post
  117. func (d *Dao) unicomHTTPPost(c context.Context, urlStr string, params url.Values, res interface{}) (err error) {
  118. return d.wallHTTP(c, d.uclient, http.MethodPost, urlStr, params, res)
  119. }
  120. // wallHTTP http
  121. func (d *Dao) wallHTTP(c context.Context, client *httpx.Client, method, urlStr string, params url.Values, res interface{}) (err error) {
  122. var (
  123. req *http.Request
  124. )
  125. ru := urlStr
  126. if params != nil {
  127. ru = urlStr + "?" + params.Encode()
  128. }
  129. switch method {
  130. case http.MethodGet:
  131. req, err = http.NewRequest(http.MethodGet, ru, nil)
  132. default:
  133. req, err = http.NewRequest(http.MethodPost, urlStr, strings.NewReader(params.Encode()))
  134. }
  135. if err != nil {
  136. log.Error("unicom_http.NewRequest url(%s) error(%v)", urlStr+"?"+params.Encode(), err)
  137. return
  138. }
  139. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  140. req.Header.Set("X-BACKEND-BILI-REAL-IP", "")
  141. return client.Do(c, req, &res)
  142. }
  143. func (d *Dao) urlParams(v url.Values) string {
  144. if v == nil {
  145. return ""
  146. }
  147. var buf bytes.Buffer
  148. keys := make([]string, 0, len(v))
  149. for k := range v {
  150. keys = append(keys, k)
  151. }
  152. sort.Strings(keys)
  153. for _, k := range keys {
  154. vs := v[k]
  155. prefix := k + "="
  156. for _, v := range vs {
  157. if buf.Len() > 0 {
  158. buf.WriteByte('&')
  159. }
  160. buf.WriteString(prefix)
  161. buf.WriteString(v)
  162. }
  163. }
  164. return buf.String()
  165. }
  166. func (d *Dao) sign(params string) string {
  167. str := strings.Replace(params, "&", "$", -1)
  168. str2 := strings.Replace(str, "=", "$", -1)
  169. mh := md5.Sum([]byte(_unicomSecurity + "$" + str2 + "$" + _unicomSecurity))
  170. signparam := url.Values{}
  171. signparam.Set("sign", base64.StdEncoding.EncodeToString(mh[:]))
  172. return signparam.Encode()
  173. }