-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.go
168 lines (136 loc) · 4.09 KB
/
patch.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
package chromedriver
import (
"bytes"
"crypto/rand"
"fmt"
"log"
"os"
"path"
"regexp"
"runtime"
"strings"
)
var binaryName = "chromedriver"
var (
re = regexp.MustCompile("cdc_.{22}")
letters = []byte("abcdefghijklmnopqrstuvwxyz")
)
type Binary struct {
path string
platform string
DataDir string
}
func Patch(bPath string) (b Binary, err error) {
b.path = bPath
log.Printf("PATCHING BINARY")
//driver = patchDriver(driver)
//if _, err := os.Stat(bPath); err == nil {
// if err := os.Remove(bPath); err != nil {
// return b, fmt.Errorf("failed to remove old driver '%s': %w", bPath, err)
// } else {
// log.Printf("removed existing binary...")
// }
//} else {
// log.Printf("no binary to remove, continuing...")
//}
// TODO: In our case here, its NOT writeFile, we need to READ the file then
// maybe write it after we tweak the []byte
// TODO: We do this to move it to bin folder
if err = b.dataDir(); err != nil {
return b, err
}
log.Printf("PATCHING BINARY doing the find and replace for the cdc...")
if err = b.patch(); err != nil {
log.Printf("patching FAILED!")
return b, err
} else {
log.Printf("patching complete!")
}
return b, nil
}
func (b *Binary) dataDir() error {
home, err := os.UserHomeDir()
if err != nil {
log.Printf("failed to locate home folder: %v", err)
return err
}
b.platform = runtime.GOOS
switch b.platform {
case "linux":
b.DataDir = path.Join(home, ".local/share/undetected_chromedriver")
case "darwin":
b.DataDir = path.Join(home, "appdata/roaming/undetected_chromedriver")
case "windows":
b.DataDir = path.Join(home, "Library/Application Support/undetected_chromedriver")
default:
log.Printf("os is not supported")
return fmt.Errorf("OS not supported: %s", runtime.GOOS)
}
if _, err := os.Stat(b.DataDir); os.IsNotExist(err) {
if err := os.MkdirAll(b.DataDir, 0750); err != nil {
log.Printf("failed to create data dir: %v", err)
return fmt.Errorf("failed to create data dir '%s': %w", b.DataDir, err)
} else {
log.Printf("created data dir: %v", b.DataDir)
}
} else {
log.Printf("os.Stat(b.DataDir) failed so must exist already")
log.Printf("data dir already exists, now should be checking for the binary so we can REMOVE it!")
// os.Stat(b.DataDir + binaryName); os.IsNotExist(err) {\
//}
}
return nil
}
func (b *Binary) patch() error {
if _, err := os.Stat(b.path); os.IsNotExist(err) {
log.Printf("binary we just downloaded is MISSING!")
return fmt.Errorf("binary is missing!!!")
} else {
log.Printf("binary EXISTS lets open it!")
}
log.Printf("attempting to load chromedriver binary at: %v", b.path)
binary, err := os.ReadFile(b.path)
if err != nil {
log.Printf("binary failed to load from b.path: %v", err)
return err
}
if !re.Match(binary) {
log.Printf("failed to find cdc in the chromedriver binary!")
return nil
}
newBinary := re.ReplaceAll(binary, randomCDC())
if bytes.Equal(newBinary, binary) {
log.Printf("failed to make changes to the binary! something went terribly wrong!")
panic(fmt.Errorf("failed to patch the binary!"))
} else {
log.Printf("success! binary has been patched!")
log.Printf("Assigning binary path; must come after a move & therefore after a patch!")
b.path = path.Join(b.DataDir, binaryName)
if err = os.WriteFile(b.path, newBinary, 0755); err != nil {
log.Printf("failed to write new binary!!! OH NOES: %v", b.path)
return err
}
}
log.Printf("new PATCHED binary path b.path len(newBinary)(%v): %v", len(newBinary), b.path)
log.Printf("SUCCESS!")
return nil
}
// TODO It should NOT be fixed size, thats too easy stupid
// AND THERE ARE TWO SPOTS that need updating, dont look for just 1 like an
// IDIOT!
func randomCDC() []byte {
cdc := make([]byte, 26)
if _, err := rand.Read(cdc); err != nil {
// Shouldn't happen, but just in case.
return []byte("xvx_plxklvnobnowmrmiIMvqlb")
}
for i, val := range cdc {
cdc[i] = letters[int(val)%len(letters)]
}
cdc[2] = cdc[0]
cdc[3] = '_'
cdc[20] = strings.ToUpper(string(cdc[20]))[0]
cdc[21] = strings.ToUpper(string(cdc[21]))[0]
log.Printf("new cdc value: %v", string(cdc))
return cdc
}