This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imager.go
273 lines (227 loc) · 7 KB
/
imager.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//Written by Lukas Marckmiller
//This file controls the image creation and hash validation.
package main
import (
"bufio"
"fmt"
"github.com/jaypipes/ghw"
"io"
"os/exec"
"regexp"
"strings"
)
type ImageJob struct {
Running bool `json:"running"`
Id string `json:"id"`
Option ImageOption `json:"option"`
CmdIf *exec.Cmd `json:"cmd_if"`
CmdOf *exec.Cmd `json:"cmd_of"`
Hashes Hashes `json:"hashes"`
HashResult HashResult `json:"hash_result"`
}
type ImageOption struct {
Type ImageType `json:"type"`
Target ImageTarget `json:"target"`
Compressed bool `json:"compressed"`
}
type ImageType int
type ImageTarget int
const (
DefaultExtension = ".img"
DefaultShell = "sh"
//DC3DD
AquisitionTool = "dc3dd"
InputFileArg = "if="
InputFileSubDir = "/dev/"
InputArgs = "verb=on" // log=/home/lukas/rfa/log" //tilde (~) doesnt get resolved for log param
OutputFileArgs = "hof="
OutputArgs = "verb=on" //log=/home/lab02/rfa/log Caution with log on output!! after finishing the copy process it seems all output is written to the log file which can take a long time
Full ImageType = 0
Part ImageType = 1
Remote ImageTarget = 0
Local ImageTarget = 1
)
var (
md5Regex = regexp.MustCompile(`[a-f0-9]{32} \(md5\)`)
sha256Regex = regexp.MustCompile(`[A-Fa-f0-9]{64} \(sha256\)`)
//Used for progress reports. Contains only one line per call.
commandIfOutput strings.Builder
commandOfOutput strings.Builder
//Used for logging. Contains complete Output
bufferedOutput strings.Builder
bufferedInput strings.Builder
pipeWriter *io.PipeWriter
pipeReader *io.PipeReader
)
type Hashes struct {
Md5Input string `json:"md_5_input"`
Md5Output string `json:"md_5_output"`
Sha256Input string `json:"sha_256_input"`
Sha256Output string `json:"sha_256_output"`
}
type HashResult struct {
Md5Valid bool `json:"md_5_valid"`
Sha256Valid bool `json:"sha_256_valid"`
}
//Returns incremental cachedOutput as string from input and output command.
func (i *ImageJob) getCachedOutput() (string, string) {
sI := commandIfOutput.String()
sO := commandOfOutput.String()
commandIfOutput.Reset()
commandOfOutput.Reset()
return sI, sO
}
//TODO - Implement Progress by using ofs and check if each junk gets reported to stderr, when this is the case compute max chunk len before and then you know how many chunks left
//Runs and creates the image with preconfigured tools and settings for a dev.
//The mountTarget is only used if ImageJob.imageOptions.target is local.
//Output of the commands is written to a buffer which can be obtained by calling getCachedOutput() for only the newest output or using buffered... vars.
func (i *ImageJob) run(dev string, mountTarget ghw.Partition, imgName string) error {
defer func() { i.Running = false }()
i.Running = true
_, err := exec.LookPath(AquisitionTool)
if err != nil {
fmt.Println("Installing " + AquisitionTool + "required")
return err
}
pipeReader, pipeWriter = io.Pipe()
defer pipeWriter.Close()
extension := DefaultExtension
var commandIf string
var commandOf string
//Set defaults
commandIf = fmt.Sprintf("%s hash=sha256 hash=md5 hlog=%s.hash %s %s%s%s", AquisitionTool, imgName, InputArgs, InputFileArg, InputFileSubDir, dev)
//Automatically compress/decompress transmission.
if i.Option.Target == Remote {
commandIf += " | gzip -c -1"
if i.Option.Compressed {
extension += ".gz"
commandOf = fmt.Sprintf("ssh %s -C '%s hash=sha256 hash=md5 hlog=%s.hash %s %s%s%s'", app.Server, AquisitionTool, imgName, OutputArgs, OutputFileArgs, imgName, extension)
} else {
commandOf = fmt.Sprintf("ssh %s -C 'gzip -d | %s hash=sha256 hash=md5 hlog=%s.hash %s %s%s%s'", app.Server, AquisitionTool, imgName, OutputArgs, OutputFileArgs, imgName, extension)
}
} else {
//Compress local image if option is set
if i.Option.Compressed {
commandIf += " | gzip -c -1"
extension += ".gz"
}
commandOf = fmt.Sprintf("%s hash=sha256 hash=md5 hlog=%s.hash %s %s%s%s", AquisitionTool, imgName, OutputArgs, OutputFileArgs, mountTarget.MountPoint+"/"+imgName, extension)
}
fmt.Println(commandIf, commandOf)
i.CmdIf = exec.Command(DefaultShell, "-c", commandIf)
i.CmdIf.Stdout = pipeWriter
i.CmdOf = exec.Command(DefaultShell, "-c", commandOf)
i.CmdOf.Stdin = pipeReader
readerStderrIf, err := i.CmdIf.StderrPipe()
if err != nil {
fmt.Println(err)
return err
}
readerStderrOf, err := i.CmdOf.StderrPipe()
if err != nil {
fmt.Println(err)
return err
}
if err = i.CmdIf.Start(); err != nil {
fmt.Println(err)
return err
}
if err = i.CmdOf.Start(); err != nil {
fmt.Println(err)
return err
}
go func() {
defer fmt.Println("StderrIf done")
reader := bufio.NewReader(readerStderrIf)
for {
line, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
fmt.Println(err)
break
}
}
m := string(line)
if m != "" {
commandIfOutput.WriteString(m)
bufferedInput.WriteString(m)
}
// fmt.Println("StderrIf: " + m)
}
}()
go func() {
defer fmt.Println("StderrOf done")
reader := bufio.NewReader(readerStderrOf)
for {
line, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
fmt.Println(err)
break
}
}
m := string(line)
if m != "" {
commandOfOutput.WriteString(m)
bufferedOutput.WriteString(m)
}
//fmt.Println("StderrOf: " + m)
}
}()
fmt.Println("Waiting for Input Command")
if err := i.CmdIf.Wait(); err != nil {
fmt.Println(err)
return err
} else {
fmt.Println("Input Command ended successfully")
}
fmt.Println("Writer closing")
if err := pipeWriter.Close(); err != nil {
fmt.Println(err)
return err
}
if err := i.CmdOf.Wait(); err != nil {
fmt.Println(err)
return err
} else {
fmt.Println("Output Command ended successfully")
}
//TODO Transfer Hash Log to Server
i.HashResult = i.verfiyHashes()
bufferedOutput.Reset()
bufferedInput.Reset()
fmt.Println("Done")
return nil
}
//Tries to close the associated pipe and to kill the input and output process
//Returns an error if not successful
func (i *ImageJob) cancel() error {
_ = pipeWriter.Close()
_ = pipeReader.Close()
if err := i.CmdIf.Process.Kill(); err != nil {
return err
}
if err := i.CmdOf.Process.Kill(); err != nil {
return err
}
return nil
}
//Is called after Image is successfully created.
//Checks if both hashes are equal for the input and the created image.
func (i *ImageJob) verfiyHashes() (hashResult HashResult) {
i.Hashes.Md5Input = md5Regex.FindString(bufferedInput.String())
i.Hashes.Sha256Input = sha256Regex.FindString(bufferedInput.String())
i.Hashes.Md5Output = md5Regex.FindString(bufferedOutput.String())
i.Hashes.Sha256Output = sha256Regex.FindString(bufferedOutput.String())
if i.Hashes.Md5Output != "" && i.Hashes.Md5Output == i.Hashes.Md5Input {
hashResult.Md5Valid = true
}
if i.Hashes.Sha256Output != "" && i.Hashes.Sha256Input == i.Hashes.Sha256Output {
hashResult.Sha256Valid = true
}
return hashResult
}