http_tax.go 7.4 KB

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