-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
255 lines (211 loc) · 4.75 KB
/
magefile.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//go:build mage
// +build mage
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/magefile/mage/sh"
)
type mutation struct {
// Copy files
From, To string
// Go Replace by regex
Match, Replace, Glob string
}
var (
deps = []string{
"https://github.com/Velocidex/WinPmem",
"https://github.com/Velocidex/go-vhdx",
"https://github.com/Velocidex/go-vmdk",
"https://github.com/Velocidex/go-journalctl",
"https://github.com/Velocidex/velociraptor",
}
golang_url = "https://go.dev/dl/go1.20.14.linux-amd64.tar.gz"
mutations = []mutation{
{From: "../patches/go.mod", To: "velociraptor/go.mod"},
{From: "../patches/go.sum", To: "velociraptor/go.sum"},
{From: "../patches/compat.go", To: "velociraptor/utils/compat.go"},
{Glob: "*/go.mod", Match: "go 1.2", Replace: "// "},
{Glob: "WinPmem/go-winpmem/go.mod", Match: "go 1.2", Replace: "// go 1.2"},
}
)
func installGo() error {
dst := "go"
stat, err := os.Lstat(dst)
if err == nil && stat.Mode().IsDir() {
return nil
}
resp, err := http.Get(golang_url)
if err != nil {
return err
}
defer resp.Body.Close()
gzr, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
switch {
case err == io.EOF:
return nil
case err != nil:
return err
case header == nil:
continue
}
target := filepath.Join(dst, header.Name)
// check the file type
switch header.Typeflag {
// if its a dir and it doesn't exist create it
case tar.TypeDir:
err := os.MkdirAll(target, 0755)
if err != nil {
return err
}
case tar.TypeReg:
fmt.Printf("Creating %v (%v bytes)\n", target, header.Size)
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
_, err = io.Copy(f, tr)
if err != nil {
return err
}
f.Close()
err = os.Chmod(target, os.FileMode(header.Mode))
if err != nil {
return err
}
}
}
}
func replace_string_in_file(filename string, old string, new string) error {
read, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
newContents := strings.Replace(string(read), old, new, -1)
return ioutil.WriteFile(filename, []byte(newContents), 0644)
}
func maybeClone(dep string) error {
base := filepath.Base(dep)
_, err := os.Lstat(base)
if err == nil {
return nil
}
return sh.RunV("git", "clone", "--depth", "1", dep)
}
func copyOutput() error {
basepath, pattern := doublestar.SplitPattern("./output/velociraptor*.exe")
fsys := os.DirFS(basepath)
matches, err := doublestar.Glob(fsys, pattern)
if err != nil {
return err
}
for _, match := range matches {
filename := filepath.Join(basepath, match)
ext := filepath.Ext(filename)
bare := strings.TrimSuffix(filename, ext)
base := filepath.Base(bare)
dst := "../../output/" + base + "-legacy" + ext
fmt.Printf("Replacing %v in %v\n", filename, dst)
err := sh.Copy(dst, filename)
if err != nil {
return err
}
}
return nil
}
func Build() error {
err := os.MkdirAll("build", 0700)
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
defer os.Chdir(cwd)
err = os.Chdir("build")
if err != nil {
return err
}
err = installGo()
if err != nil {
return err
}
for _, dep := range deps {
err = maybeClone(dep)
if err != nil {
return err
}
}
for _, m := range mutations {
if m.From != "" {
fmt.Printf("Copying %v to %v\n", m.From, m.To)
err := sh.Copy(m.To, m.From)
if err != nil {
return err
}
}
if m.Glob != "" {
basepath, pattern := doublestar.SplitPattern(m.Glob)
fsys := os.DirFS(basepath)
matches, err := doublestar.Glob(fsys, pattern)
if err != nil {
return err
}
for _, match := range matches {
filename := filepath.Join(basepath, match)
fmt.Printf("Replacing %v in %v\n", m.Match, filename)
err = replace_string_in_file(filename, m.Match, m.Replace)
if err != nil {
return err
}
}
}
}
// Build steps
err = os.Chdir("velociraptor")
if err != nil {
return err
}
env := make(map[string]string)
env["PATH"] = "../go/go/bin/:" + os.Getenv("PATH")
env["GOPATH"] = ""
go_path, err := filepath.Abs("../go/go/bin/go")
if err != nil {
return err
}
env["MAGEFILE_GOCMD"] = go_path
env["MAGEFILE_VERBOSE"] = "1"
err = sh.RunWithV(env, go_path, "version")
if err != nil {
return err
}
err = sh.RunWithV(env, go_path, "run", "-v", "./make.go", "windows")
if err != nil {
return err
}
err = sh.RunWithV(env, go_path, "run", "-v", "./make.go", "windowsx86")
if err != nil {
return err
}
err = copyOutput()
if err != nil {
return err
}
return nil
}