/
encryption.go
61 lines (49 loc) · 1.23 KB
/
encryption.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (C) 2021 github.com/V4NSH4J
//
// This source code has been released under the GNU Affero General Public
// License v3.0. A copy of this license is available at
// https://www.gnu.org/licenses/agpl-3.0.en.html
package utilities
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"compress/zlib"
"github.com/andybalholm/brotli"
)
// Decoding brotli encrypted responses
func DecodeBr(data []byte) ([]byte, error) {
r := bytes.NewReader(data)
br := brotli.NewReader(r)
return ioutil.ReadAll(br)
}
// Function to handle all sorts of accepted-encryptions
func ReadBody(resp http.Response) ([]byte, error) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.Header.Get("Content-Encoding") == "gzip" {
gzipreader, err := zlib.NewReader(bytes.NewReader(body))
if err != nil {
return nil, err
}
gzipbody, err := ioutil.ReadAll(gzipreader)
if err != nil {
return nil, err
}
return gzipbody, nil
}
if resp.Header.Get("Content-Encoding") == "br" {
brreader := brotli.NewReader(bytes.NewReader(body))
brbody, err := ioutil.ReadAll(brreader)
if err != nil {
fmt.Println(string(brbody))
return nil, err
}
return brbody, nil
}
return body, nil
}