/
jar.go
177 lines (151 loc) · 4.22 KB
/
jar.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gokhttp_cookiejar
import (
"fmt"
crypto_utils "github.com/BRUHItsABunny/crypto-utils"
"github.com/BRUHItsABunny/crypto-utils/padding"
gokhttp_constants "github.com/BRUHItsABunny/gOkHttp/constants"
"github.com/vmihailenco/msgpack/v5"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path/filepath"
"sync"
)
type JarOption struct {
Jar *CookieJarWrapper
}
func NewJarOption(jar *CookieJarWrapper) *JarOption {
return &JarOption{Jar: jar}
}
func (o *JarOption) Execute(client *http.Client) error {
client.Jar = o.Jar
return nil
}
type CookieJarWrapper struct {
sync.RWMutex
jarKeys map[*url.URL]struct{}
jar *cookiejar.Jar
f *os.File
encryptionKey string
}
func NewCookieJar(fileLocation, encryptionKey string, psl cookiejar.PublicSuffixList) (*CookieJarWrapper, error) {
var f *os.File
if len(fileLocation) > 0 {
dirName := filepath.Dir(fileLocation)
err := os.MkdirAll(dirName, 0600)
if err != nil {
return nil, fmt.Errorf("os.MkdirAll: %w", err)
}
f, err = os.OpenFile(fileLocation, os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("os.OpenFile: %w", err)
}
}
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: psl})
if err != nil {
return nil, fmt.Errorf("cookiejar.New: %w", err)
}
return &CookieJarWrapper{
RWMutex: sync.RWMutex{},
jarKeys: map[*url.URL]struct{}{},
jar: jar,
f: f,
encryptionKey: encryptionKey,
}, nil
}
// Interface functions, required for *http.Client integration
func (j *CookieJarWrapper) SetCookies(u *url.URL, cookies []*http.Cookie) {
j.Lock()
j.jarKeys[u] = struct{}{}
j.Unlock()
j.jar.SetCookies(u, cookies)
}
func (j *CookieJarWrapper) Cookies(u *url.URL) []*http.Cookie {
return j.jar.Cookies(u)
}
// Utility functions
/*
Persistence
Some design choices that were made in comparison to the previous implementation
1. I don't have a use case for a cookie file that may get used by multiple processes at the same time
2. Encryption algo is not exposed for modification to keep configuration simple
3. JSON is horrible to read write data in that essentially only gets read by a machine (if it needs to be humanreadable JSON makes sense, this is not the case here)
*/
func (j *CookieJarWrapper) Save() error {
return j.save()
}
func (j *CookieJarWrapper) save() error {
if j.f == nil {
return gokhttp_constants.ErrCookieJarNotPersistent
}
entries := map[string][]*http.Cookie{}
for u, _ := range j.jarKeys {
entries[u.String()] = j.Cookies(u)
}
data, err := msgpack.Marshal(&entries)
if err != nil {
return fmt.Errorf("msgpack.Marshal: %w", err)
}
// Reset file
err = j.f.Truncate(0)
if err != nil {
return fmt.Errorf("j.f.Truncate: %w", err)
}
_, err = j.f.Seek(0, io.SeekStart)
if err != nil {
return fmt.Errorf("j.f.Seek: %w", err)
}
// Encrypt
if j.encryptionKey != "" {
key, iv := crypto_utils.SHA256hash([]byte(j.encryptionKey)), crypto_utils.MD5hash([]byte(j.encryptionKey))
data, err = crypto_utils.AesCBCEncrypt(&padding.PKCS7Padding{}, data, key, iv)
if err != nil {
return fmt.Errorf("crypto_utils.AesCBCEncrypt: %w", err)
}
}
_, err = j.f.Write(data)
if err != nil {
return fmt.Errorf("j.f.Write: %w", err)
}
return nil
}
func (j *CookieJarWrapper) Load() error {
return j.load()
}
func (j *CookieJarWrapper) load() error {
if j.f == nil {
return gokhttp_constants.ErrCookieJarNotPersistent
}
// Read from file
_, err := j.f.Seek(0, io.SeekStart)
if err != nil {
return fmt.Errorf("j.f.Seek: %w", err)
}
data, err := io.ReadAll(j.f)
if err != nil {
return fmt.Errorf("io.ReadAll: %w", err)
}
// Decrypt
if j.encryptionKey != "" {
key, iv := crypto_utils.SHA256hash([]byte(j.encryptionKey)), crypto_utils.MD5hash([]byte(j.encryptionKey))
data, err = crypto_utils.AesCBCDecrypt(&padding.PKCS7Padding{}, data, key, iv)
if err != nil {
return fmt.Errorf("crypto_utils.AesCBCDecrypt: %w", err)
}
}
entries := map[string][]*http.Cookie{}
err = msgpack.Unmarshal(data, &entries)
if err != nil {
return fmt.Errorf("msgpack.Marshal: %w", err)
}
for key, cooks := range entries {
uKey, err := url.Parse(key)
if err != nil {
return fmt.Errorf("url.Parse: %w", err)
}
j.SetCookies(uKey, cooks)
}
return nil
}