Commit a20b9855 authored by wangmingming's avatar wangmingming

gzip解压缩问题

parent af2d25ef
package tls package tls
import ( import (
"bytes"
"compress/flate"
"compress/gzip"
"crypto/rand" "crypto/rand"
"encoding/binary"
"errors" "errors"
"github.com/andybalholm/brotli"
http "github.com/bogdanfinn/fhttp" http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client" tls_client "github.com/bogdanfinn/tls-client"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"io" "io/ioutil"
"math/big" "math/big"
"net/url" "net/url"
"strings" "strings"
...@@ -90,9 +95,12 @@ func (c *Client) GetSessionCookies() map[string]string { ...@@ -90,9 +95,12 @@ func (c *Client) GetSessionCookies() map[string]string {
func (c *Client) GetText() string { func (c *Client) GetText() string {
defer c.Response.Body.Close() defer c.Response.Body.Close()
http.DecompressBody(c.Response) //http.DecompressBody(c.Response)
rb, _ := io.ReadAll(c.Response.Body) //rb, _ := io.ReadAll(c.Response.Body)
return string(rb) content, _ := ioutil.ReadAll(c.Response.Body)
encoding := c.Response.Header.Get("Content-Encoding")
DecompressBody(&content, encoding)
return string(content)
} }
func (c *Client) GetRespUrl() string { func (c *Client) GetRespUrl() string {
...@@ -149,3 +157,64 @@ func NewClient(forWard ForwardItem, ios bool) (Client, error) { ...@@ -149,3 +157,64 @@ func NewClient(forWard ForwardItem, ios bool) (Client, error) {
} }
return Client{Client: client, Item: forWard, Jar: jar, TlsVersion: tlsVersion.GetClientHelloStr()}, nil return Client{Client: client, Item: forWard, Jar: jar, TlsVersion: tlsVersion.GetClientHelloStr()}, nil
} }
// 解码Body数据
func DecompressBody(content *[]byte, encoding string) {
if encoding != "" {
if strings.ToLower(encoding) == "gzip" {
decodeGZip(content)
} else if strings.ToLower(encoding) == "deflate" {
decodeDeflate(content)
} else if strings.ToLower(encoding) == "br" {
decodeBrotli(content)
}
}
}
// 解码GZip编码
func decodeGZip(content *[]byte) error {
if content == nil {
return nil
}
b := new(bytes.Buffer)
binary.Write(b, binary.LittleEndian, content)
r, err := gzip.NewReader(b)
if err != nil {
return err
}
defer r.Close()
*content, err = ioutil.ReadAll(r)
if err != nil {
return err
}
return nil
}
// 解码deflate编码
func decodeDeflate(content *[]byte) error {
var err error
if content == nil {
return err
}
r := flate.NewReader(bytes.NewReader(*content))
defer r.Close()
*content, err = ioutil.ReadAll(r)
if err != nil {
return err
}
return nil
}
// 解码br编码
func decodeBrotli(content *[]byte) error {
var err error
if content == nil {
return err
}
r := brotli.NewReader(bytes.NewReader(*content))
*content, err = ioutil.ReadAll(r)
if err != nil {
return err
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment