-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
152 lines (130 loc) · 3.7 KB
/
util.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Package rewerse provides a simple interface to interact with the REWE API.
// Only Getters are implemented.
package rewerse
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/google/uuid"
"io"
"math/rand"
"net/http"
"strings"
)
const (
apiHost = "mobile-api.rewe.de"
clientHost = "mobile-clients-api.rewe.de"
)
var (
Client *http.Client
cert tls.Certificate
userAgent string
rdfa string
set = false
userAgents = []string{
"Phone/Samsung_SM-G975U", "Phone/Samsung_SM-N975U", "Phone/Samsung_SM-G973U", "Phone/OnePlus_HD1925",
"Phone/Xiaomi_M2007J3SY", "Phone/LG_LM-G820", "Phone/Google_Pixel_8_Pro", "Phone/Google_Pixel_7_Pro",
"Phone/Samsung_SM-S911B", "Phone/Samsung_SM-S918B", "Phone/OnePlus_AC2003", "Phone/Xiaomi_2201123G",
"Phone/Google_Pixel_8", "Phone/Google_Pixel_7a", "Phone/Samsung_SM-F946B", "Phone/Samsung_SM-S901B",
}
)
func BuildCustomRequest(host, path string) (req *http.Request, err error) {
if !set {
panic("certificates not set")
}
req, err = http.NewRequest(http.MethodGet, "https://"+host+"/api/"+path, nil)
if err != nil {
err = fmt.Errorf("error creating request: %v", err)
return
}
// Optional Headers
// just adding these to fit in :)
id, err := uuid.NewRandom()
if err != nil {
err = fmt.Errorf("error generating uuid: %v", err)
return
}
req.Header.Add("rdfa", rdfa)
req.Header.Add("correlation-id", id.String())
req.Header.Add("rd-service-types", "UNKNOWN")
req.Header.Add("x-rd-service-types", "UNKNOWN")
req.Header.Add("rd-is-lsfk", "false")
req.Header.Add("rd-customer-zip", "")
req.Header.Add("rd-postcode", "")
req.Header.Add("x-rd-customer-zip", "")
req.Header.Add("rd-market-id", "")
req.Header.Add("x-rd-market-id", "")
req.Header.Add("a-b-test-groups", "productlist-citrusad")
// todo: some requests have a ruleSet header, but for others it makes them go 404
// Strictly required headers
req.Header.Set("user-agent", fmt.Sprintf("REWE-Mobile-Client/3.18.5.33032 Android/14 %s", userAgent))
req.Header.Set("Host", host)
req.Header.Set("Connection", "Keep-Alive")
return
}
func DoRequest(req *http.Request, dest any) (err error) {
// Execute Request
resp, err := Client.Do(req)
if err != nil {
err = fmt.Errorf("error making request: %v", err)
return
}
defer CloseWithWrap(resp.Body, &err)
// Read the body
body, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("error reading response: %v", err)
return
}
//fmt.Println(string(body))
// Unmarshal the body into the destination
if err = json.Unmarshal(body, &dest); err != nil {
if strings.HasPrefix(string(body), "<!DOCTYPE html>") {
err = fmt.Errorf("error: response is html (cloudflared)")
return
}
err = fmt.Errorf("error unmarshalling response: %v", err)
return
}
return
}
func SetCertificate(clientCert, clientKey string) error {
var err error
cert, err = tls.LoadX509KeyPair(clientCert, clientKey)
if err != nil {
return fmt.Errorf("error loading certificates: %v", err)
}
set = true
// randomize a user-agent for this session
userAgent = userAgents[rand.Intn(len(userAgents))]
// rdfa is a static header
id, err := uuid.NewRandom()
if err != nil {
return fmt.Errorf("error generating uuid: %v", err)
}
rdfa = id.String()
Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
},
}
return nil
}
type CloseError struct {
OriginalError, CloseError error
}
func (c CloseError) Error() string {
return fmt.Sprintf("OriginalError=%v CloseError=%v", c.OriginalError, c.CloseError)
}
func CloseWithWrap(f io.Closer, e *error) {
err := f.Close()
if err != nil {
if *e != nil {
*e = CloseError{*e, err}
} else {
*e = err
}
}
}