-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (199 loc) · 5.48 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
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
package main
import (
"bytes"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"logviewer/config"
"logviewer/cyclogmodel"
"logviewer/logdog"
"logviewer/loginfo"
"logviewer/utils"
"os"
"path/filepath"
"sort"
"strings"
)
const (
// LogFileExt log file pattern
LogFileExt = "*.LOG"
// Version tool version
Version = "0.0.1"
// Purple color
Purple = "\033[1;34m"
// Normal color
Normal = "\033[0m"
)
func writeLogs(logs []loginfo.LogInfo, r io.Writer, conf config.Config) int {
count := 0
for _, log := range logs {
str := conf.Format
logIDString := fmt.Sprintf("[0x%04x] ", log.LogID)
str = strings.ReplaceAll(str, "%y", fmt.Sprintf("%d", log.Date.Year()))
str = strings.ReplaceAll(str, "%k", fmt.Sprintf("%d", log.Date.Month()))
str = strings.ReplaceAll(str, "%d", fmt.Sprintf("%d", log.Date.Day()))
str = strings.ReplaceAll(str, "%h", fmt.Sprintf("%02d", log.Date.Hour()))
str = strings.ReplaceAll(str, "%m", fmt.Sprintf("%02d", log.Date.Minute()))
str = strings.ReplaceAll(str, "%s", fmt.Sprintf("%02d", log.Date.Second()))
str = strings.ReplaceAll(str, "%n", fmt.Sprintf("%09d", log.Date.Nanosecond()))
str = strings.ReplaceAll(str, "%i", logIDString)
str = strings.ReplaceAll(str, "%t", log.Text)
str = strings.ReplaceAll(str, "%x", log.Source)
str = strings.ReplaceAll(str, "%p", string(log.Pid))
str = strings.ReplaceAll(str, "%z", string(log.Tid))
str = strings.ReplaceAll(str, "%l", log.LogLevel)
io.WriteString(r, str+"\n")
count++
}
return count
}
func encrypted(path string, conf config.Config) (decryptedData []byte, err error) {
name := strings.TrimSuffix(
strings.TrimSuffix(path, filepath.Ext(path)),
"_enckey")
archive := name + ".enc"
key := name + "_enckey.enc"
factoryKeyData, err := ioutil.ReadFile(conf.Key.Factory)
if err != nil {
return
}
privateKeyData, err := ioutil.ReadFile(conf.Key.Installed)
if err != nil {
return
}
factoryKey, err := utils.BytesToPrivateKey(factoryKeyData)
if err != nil {
return
}
privateKey, err := utils.BytesToPrivateKey(privateKeyData)
if err != nil {
return
}
keyData, err := ioutil.ReadFile(key)
if err != nil {
return
}
decryptedKey, err := utils.RsaDecrypt(keyData, privateKey, factoryKey)
if err != nil {
return
}
data, err := ioutil.ReadFile(archive)
if err != nil {
return
}
hx := hex.EncodeToString(decryptedKey)
decryptedData, err = utils.Aes256Decrypt(data, hx[0:64], hx[64:96])
return
}
func parseArg(args string, conf config.Config) (logFolder string, err error) {
path, err := os.Stat(args)
if err != nil {
return
}
isArchive := false
var archiveReader io.Reader
switch mode := path.Mode(); {
case mode.IsDir():
logFolder = path.Name()
case mode.IsRegular():
switch filepath.Ext(path.Name()) {
case ".enc":
var data []byte
if data, err = encrypted(path.Name(), conf); err != nil {
return
}
isArchive = true
archiveReader = bytes.NewReader(data)
case ".gz":
var file *os.File
file, err = os.Open(path.Name())
if err != nil {
return
}
isArchive = true
archiveReader = file
}
}
if isArchive {
if !conf.UseTemp {
logFolder = path.Name() + ".ext"
} else {
logFolder, err = ioutil.TempDir("", path.Name())
if err != nil {
return
}
}
utils.Untar(logFolder, archiveReader)
}
return
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%sGoLogdog v%s%s\n", Purple, Version, Normal)
fmt.Fprintf(os.Stderr, "Usage: [ARGS] (tar.gz|enc|/)\n")
fmt.Fprintf(os.Stderr, "Format:\n")
fmt.Fprintf(os.Stderr, "%%y year\n")
fmt.Fprintf(os.Stderr, "%%k month\n")
fmt.Fprintf(os.Stderr, "%%d day\n")
fmt.Fprintf(os.Stderr, "%%h hours\n")
fmt.Fprintf(os.Stderr, "%%m minutes\n")
fmt.Fprintf(os.Stderr, "%%s seconds\n")
fmt.Fprintf(os.Stderr, "%%n nanoseconds\n")
fmt.Fprintf(os.Stderr, "%%i logID\n")
fmt.Fprintf(os.Stderr, "%%t text\n")
fmt.Fprintf(os.Stderr, "%%x source\n")
fmt.Fprintf(os.Stderr, "%%p pid\n")
fmt.Fprintf(os.Stderr, "%%z tid\n")
fmt.Fprintf(os.Stderr, "%%l logLevel\n")
fmt.Fprintf(os.Stderr, "Arguments:\n")
flag.PrintDefaults()
}
configFile := flag.String("c", config.ConfigFile, "Configuration file")
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
var configuration config.Config
configuration, err := config.GetConfig(*configFile)
if err != nil {
fmt.Println("Can't read configuration, using default")
configuration = config.GetDefaultConfig()
}
logFolder, err := parseArg(os.Args[1], configuration)
if err != nil {
panic(err)
}
defer os.RemoveAll(logFolder)
outputFile, err := os.Create(os.Args[1] + ".log")
if err != nil {
panic(err)
}
defer outputFile.Close()
model := cyclogmodel.MakeCycLogModel()
parser := logdog.MakeLogDogParser(&model)
var logs []loginfo.LogInfo
for _, file := range configuration.Target.Logdog {
// File
logPath, err := utils.FindFile(logFolder, file+LogFileExt)
if err != nil {
fmt.Printf("File \"%s\" not found\n", logFolder+"/"+file+LogFileExt)
continue
}
// csv
csvFile := configuration.Target.LogdogFormat + "/" + file + ".csv"
format, err := cyclogmodel.ReadFormatData(csvFile)
if err != nil {
fmt.Printf("File \"%s\" not found\n", csvFile)
continue
}
logs = append(logs, parser.Parse(logPath, format)...)
}
sort.Slice(logs, func(i, j int) bool {
return logs[i].Date.Before(logs[j].Date)
})
logsWritten := writeLogs(logs, outputFile, configuration)
fmt.Printf("Written %d logs -> \"%s\"\n", logsWritten, outputFile.Name())
}