-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
145 lines (123 loc) · 3.04 KB
/
main.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
package main
import (
"bytes"
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
var verbose bool
const (
EnvVerbose = "SELFEXTRACT_VERBOSE"
EnvDir = "SELFEXTRACT_DIR"
EnvStartup = "SELFEXTRACT_STARTUP"
EnvExtractOnly = "SELFEXTRACT_EXTRACT_ONLY"
EnvGraceTimeout = "SELFEXTRACT_GRACE_TIMEOUT"
)
func init() {
verbose = isTruthy(os.Getenv(EnvVerbose))
}
func main() {
stub, payload, key := readSelf()
if payload != nil {
extract(payload, key)
return
}
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "%s [OPTION...] FILE ...\n", os.Args[0])
flag.PrintDefaults()
}
createName := flag.String("f", "selfextract.out", "name of the archive to create")
changeDir := flag.String("C", ".", "change dir before archiving files, only affects input files")
verboseFlg := flag.Bool("v", false, "verbose output")
flag.Parse()
verbose = verbose || *verboseFlg
create(stub, key, *createName, flag.Args(), *changeDir)
}
func debug(v ...interface{}) {
if verbose {
v = append([]interface{}{"selfextract:"}, v...)
log.Println(v...)
}
}
func die(v ...interface{}) {
v = append([]interface{}{"selfextract: FATAL:"}, v...)
log.Fatalln(v...)
}
func isTruthy(s string) bool {
switch strings.ToLower(s) {
case "y", "yes", "true", "1":
return true
default:
return false
}
}
func generateBoundary() []byte {
h := sha512.Sum512([]byte("boundary"))
return h[:]
}
const keyLength = 16
func generateRandomKey() []byte {
buf := make([]byte, keyLength)
_, err := rand.Read(buf)
if err != nil {
die("generating random key:", err)
}
return buf
}
// maxBoundaryOffset is the offset at which we stop looking for a boundary,
// it's just a failsafe mechanism against big, corrupted archives. We set it to
// a value much bigger than the expected size of the compiled stub.
const maxBoundaryOffset = 100e6 // 100 MB
func readSelf() ([]byte, io.ReadCloser, []byte) {
t := time.Now()
exePath, err := os.Executable()
if err != nil {
panic(err)
}
self, err := os.Open(exePath)
if err != nil {
die("opening itself:", exePath, err)
}
debug("opened itself in", time.Since(t))
t = time.Now()
buf := make([]byte, maxBoundaryOffset+keyLength)
n, err := self.Read(buf)
var bufFull bool
if err == io.EOF {
bufFull = true
} else if err != nil {
die("reading itself:", err)
}
buf = buf[:n]
debug("read itself in", time.Since(t))
boundary := generateBoundary()
t = time.Now()
bdyOff := bytes.Index(buf[:maxBoundaryOffset], boundary)
debug("boundary search completed in", time.Since(t))
if bdyOff == -1 {
if bufFull {
die("boundary not found before byte", maxBoundaryOffset)
}
debug("no boundary")
self.Close()
return buf, nil, nil
}
debug("boundary found at", bdyOff)
keyOff := bdyOff + len(boundary)
payloadOff := keyOff + keyLength
key := buf[keyOff:payloadOff]
debug("key:", hex.EncodeToString(key))
_, err = self.Seek(int64(payloadOff), os.SEEK_SET)
if err != nil {
die("seeking to start of payload:", err)
}
buf = buf[:bdyOff]
return buf, self, key
}