http_tax.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package lxhttp
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/base64"
  6. "encoding/json"
  7. "fmt"
  8. login_js "git.listensoft.net/tool/jspkit/common/login-js"
  9. Rsvmp "git.listensoft.net/tool/jspkit/common/rsvmp"
  10. "git.listensoft.net/tool/jspkit/logger"
  11. "git.listensoft.net/tool/jspkit/taxerr"
  12. "github.com/tidwall/gjson"
  13. "go.uber.org/zap"
  14. "golang.org/x/net/publicsuffix"
  15. "io"
  16. "io/ioutil"
  17. "net/http"
  18. "net/http/cookiejar"
  19. "net/url"
  20. "strings"
  21. "time"
  22. )
  23. // PostByteReader PostReader 不规则参数 直接传字符串
  24. func PostByteReader(client *http.Client, uri string, bytesData []byte, headers map[string]string, Cookies []*http.Cookie) ([]byte, error) {
  25. f := 0
  26. var ck []*http.Cookie
  27. beggin:
  28. if len(ck) != 0 {
  29. Cookies = append(Cookies, ck...)
  30. }
  31. request, err := http.NewRequest("POST", uri, bytes.NewReader(bytesData))
  32. if err != nil {
  33. return nil, err
  34. }
  35. if strings.Contains(uri, "mhzx/api/mh/mhsy/getUserInfo") {
  36. request, err = http.NewRequest("GET", uri, bytes.NewReader(bytesData))
  37. if err != nil {
  38. return nil, err
  39. }
  40. }
  41. request.Header.Add("Content-Type", "application/json")
  42. request.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
  43. for k, v := range headers {
  44. request.Header.Set(k, v)
  45. }
  46. // 瑞数加密
  47. for _, v := range Cookies {
  48. request.AddCookie(v)
  49. }
  50. resp, err := client.Do(request)
  51. if err != nil {
  52. return nil, err
  53. }
  54. defer resp.Body.Close()
  55. if !strings.Contains(uri, "api/login") {
  56. if resp.StatusCode == 412 && f < 3 && Rsvmp.NeedRsvmpAPi(uri) {
  57. ck, uri = Rsvmp.Rsvmp(uri)
  58. f++
  59. goto beggin
  60. }
  61. if resp.StatusCode != 200 {
  62. logger.Info("resp", zap.String("uri", uri), zap.Int("code", resp.StatusCode))
  63. body1, _ := io.ReadAll(resp.Body)
  64. return body1, taxerr.NewWebStuckTitle(true)
  65. }
  66. }
  67. body, err := io.ReadAll(resp.Body)
  68. if err != nil {
  69. return nil, err
  70. }
  71. if strings.Contains(resp.Header.Get(`Content-Encoding`), "gzip") {
  72. return GZIPDecode(body)
  73. }
  74. if strings.Contains(string(body), "目前暂不支持查看该申报表的申报明细") {
  75. return nil, taxerr.NewSystemV3("税局提示:目前暂不支持查看该申报表的申报明细", "请核实")
  76. }
  77. if strings.Contains(uri, "pdf") || strings.Contains(uri, "download") || strings.Contains(uri, "Pdf") || strings.Contains(uri, "exportSbmxxqcx") || strings.Contains(uri, `previewPdfData`) || strings.Contains(uri, "DescribeSbmxxqcx") {
  78. logger.Info("xx", zap.String("uri", uri), zap.Int("pdf字节changed", len(body)))
  79. } else {
  80. logger.Info("xx", zap.String("uri", uri), zap.Int("body", len(body)))
  81. }
  82. return body, nil
  83. }
  84. func NewHttpClientForNoRedirects(proxy, auth string) (client *http.Client) {
  85. // 禁止重定向
  86. client = &http.Client{
  87. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  88. // 返回错误以阻止重定向
  89. return http.ErrUseLastResponse
  90. },
  91. Timeout: 10 * time.Second,
  92. }
  93. tr := &http.Transport{
  94. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //忽略证书校验
  95. }
  96. if proxy != "" {
  97. u, _ := url.Parse(proxy)
  98. tr.Proxy = http.ProxyURL(u)
  99. }
  100. if auth != "" {
  101. tr.ProxyConnectHeader = http.Header{
  102. "Proxy-Authorization": []string{"Basic " + base64.StdEncoding.EncodeToString([]byte(auth))},
  103. }
  104. }
  105. client.Transport = tr
  106. options := cookiejar.Options{
  107. PublicSuffixList: publicsuffix.List,
  108. }
  109. curCookieJar, _ := cookiejar.New(&options)
  110. client.Jar = curCookieJar
  111. return
  112. }
  113. func AutoHttp(client *http.Client, uri string, Method string, bytesData []byte, headers map[string]string) (body []byte, err error) {
  114. var Cookies []*http.Cookie
  115. for range "..." {
  116. request, err := http.NewRequest(Method, uri, bytes.NewReader(bytesData))
  117. if err != nil {
  118. continue
  119. }
  120. for k, v := range headers {
  121. request.Header.Add(k, v)
  122. }
  123. // 瑞数加密
  124. for _, v := range Cookies {
  125. request.AddCookie(v)
  126. }
  127. resp, err := client.Do(request)
  128. if err != nil {
  129. continue
  130. }
  131. if resp.StatusCode == 412 {
  132. logger.Info("需要瑞数加密了:", zap.Int("code", resp.StatusCode))
  133. Cookies, _ = Rsvmp.Rsvmp(uri)
  134. logger.Info("加密完成")
  135. logger.Info("ck", zap.Any("ck", Cookies))
  136. continue
  137. }
  138. defer resp.Body.Close()
  139. body, err = io.ReadAll(resp.Body)
  140. if err != nil {
  141. continue
  142. }
  143. break
  144. }
  145. return
  146. }
  147. func Get_etax_clientId_redirectUri(C *http.Client, Api string, fuc func(url string) (ck []*http.Cookie, uri string)) map[string]interface{} {
  148. Location := ""
  149. f := 0
  150. begin:
  151. var ck []*http.Cookie
  152. ck, Api = fuc(Api)
  153. if len(ck) != 0 {
  154. u, _ := url.Parse(Api)
  155. C.Jar.SetCookies(u, ck)
  156. }
  157. // 创建一个新的 HTTP 请求
  158. req, err := http.NewRequest("GET", Api, nil)
  159. if err != nil {
  160. fmt.Println("Error creating request:", err)
  161. }
  162. req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
  163. req.Header.Set("Accept", "application/json, text/plain, */*")
  164. if strings.Contains(Api, "invoice-query/invoice-query") {
  165. req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
  166. req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
  167. req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
  168. //req.Header.Set("Referer", "https://etax.hebei.chinatax.gov.cn:8443/")
  169. //req.Header.Set("Host", "dppt.hebei.chinatax.gov.cn:8443")
  170. }
  171. nn := 0
  172. n:
  173. // 发送请求
  174. resp, err := C.Do(req)
  175. if err != nil {
  176. logger.Info("Error sending request:" + err.Error())
  177. if nn < 6 {
  178. nn++
  179. goto n
  180. }
  181. return nil
  182. }
  183. defer resp.Body.Close()
  184. body, _ := ioutil.ReadAll(resp.Body)
  185. fmt.Println(string(body))
  186. Location = resp.Header.Get(`Location`)
  187. fmt.Println("Location:", Location)
  188. if Location == "" {
  189. return nil
  190. }
  191. if f == 0 {
  192. Api = Location
  193. f++
  194. goto begin
  195. }
  196. res := login_js.Js("clientId", Location)
  197. return res.(map[string]interface{})
  198. }
  199. func VerifyLogin(c *http.Client, area, BaseIndex string, client_id, redirect_uri, new_key16 string, header map[string]string, fuc func(url string) (ck []*http.Cookie, uri string)) string {
  200. var jsonAny []byte
  201. var err error
  202. uri := fmt.Sprintf("https://tpass.%s.chinatax.gov.cn:8443/sys-api/v1.0/auth/user/verifyLogin", area)
  203. ck, _ := fuc(uri)
  204. if len(ck) > 0 {
  205. u, _ := url.Parse(uri)
  206. c.Jar.SetCookies(u, ck)
  207. }
  208. n := map[string]interface{}{
  209. "data": map[string]string{
  210. "client_id": client_id,
  211. "redirect_uri": redirect_uri,
  212. },
  213. "url": "/auth/user/verifyLogin",
  214. "method": "post",
  215. }
  216. param_datas := login_js.Js("signature", n, new_key16)
  217. for range ".........." {
  218. jsonAny, err = POSTJsonAny(c, uri, param_datas, header)
  219. if err != nil {
  220. continue
  221. } else {
  222. break
  223. }
  224. }
  225. /// {"code" : 1000,"msg" : "处理成功!","zipCode" : "0","encryptCode" : "2","datagram" : "610695f2eb65a0eb961cf80c3a69f9777b8282c41fbecf238500d3c670fb8d5298fdcd4929f62c6c601d1a4c07563073","signtype" : "HMacSHA256", "signature" : "8B0CF4EE45C7FF0398BDB0D3946D91960E26017448DC91BE928256DC417307E8","timestamp" : "20241004114508","requestId" : "f551b69773a75535"}
  226. var jsonAny2 map[string]interface{}
  227. json.Unmarshal([]byte(jsonAny), &jsonAny2)
  228. responseJson := login_js.Js("responseParse", jsonAny2, new_key16)
  229. //{"code":"","datagram":"{\"code\":\"3D384374310C458E8DFF9B979E3D8FFB\"}","encryptCode":"2","msg":"处理成功!","requestId":"86ba5db97beff424","signature":"DE3ECA4D94F8C293CDB1695E5648904CACCABCBF998AD2F6188DF2988F67F4D9","signtype":"HMacSHA256","timestamp":"20241004120553","zipCode":"0"}
  230. responseJson2 := responseJson.(map[string]interface{})
  231. return gjson.Get(responseJson2["datagram"].(string), "code").String()
  232. }