-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
249 lines (200 loc) · 6.63 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
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2021 Alex jeannopoulos. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/droundy/goopt"
"github.com/hako/durafmt"
"github.com/potakhov/loge"
"github.com/speps/go-hashids/v2"
"gopkg.in/olahol/melody.v1"
"io/fs"
"net/http"
"os"
"time"
)
var (
// MaxMultipartMemory max multipart upload size
MaxMultipartMemory int64 = 8 << 20 // 8 MiB
// BuildDate date string of when build was performed filled in by -X compile flag
BuildDate string
// GitRepo string of the git repo url when build was performed filled in by -X compile flag
GitRepo string
// BuiltBy date string of who performed build filled in by -X compile flag
BuiltBy string
// CommitDate date string of when commit of the build was performed filled in by -X compile flag
CommitDate string
// Branch string of branch in the git repo filled in by -X compile flag
Branch string
// LatestCommit date string of when build was performed filled in by -X compile flag
LatestCommit string
// Version string of build filled in by -X compile flag
Version string
// OsSignal signal used to shut down
OsSignal chan os.Signal
saveDir = goopt.String([]string{"--saveDir"}, "/tmp", "save directory")
webDir = goopt.String([]string{"--webDir"}, "/tmp/web", "web assets directory")
serverHost = goopt.String([]string{"--host"}, "0.0.0.0", "host for server")
publicUrl = goopt.String([]string{"--publicUrl"}, "http://127.0.0.1:8080", "public url")
publicBinEndpoint = goopt.String([]string{"--publicBinEndpoint"}, "127.0.0.1 8081", "public binary ip/port")
httpPort = goopt.Int([]string{"--port"}, 8080, "port for server")
tcpPort = goopt.Int([]string{"--tcpPort"}, 8081, "tcp port for server")
exportTemplates = goopt.Flag([]string{"--export"}, nil, "export templates to --webDir value.", "")
purgeOlderThanStr = goopt.String([]string{"--purgeOlderThan"}, "24h", "Purge sessions from disk older than value. 0 will disable.")
maxSessionSz = goopt.Int([]string{"--maxSessionSize"}, 1, "maximum session size in mb.")
hasher *hashids.HashID
webFS fs.FS
webDirHTTPFS http.FileSystem
staticDirHTTPFS http.FileSystem
m melody.Melody
duraFormatOverride durafmt.Units
purgeOlderThan *durafmt.Durafmt
maxSessionSize int
maxSessionSizeFormatted string
)
func init() {
// Setup goopts
goopt.Description = func() string {
return "Http and TCP logger endpoint"
}
goopt.Author = "Alex Jeannopoulos"
goopt.ExtraUsage = ``
goopt.Summary = `
dumpr
dumpr will create and http and tcp listener and log connections and inbound traffic to a log file.
`
goopt.Version = fmt.Sprintf(
`dumpr! build information
Version : %s
Git repo : %s
Git commit : %s
Git branch : %s
Commit date : %s
Build date : %s
Built By : %s
`, Version, GitRepo, LatestCommit, Branch, CommitDate, BuildDate, BuiltBy)
//Parse options
goopt.Parse(nil)
var err error
duraFormatOverride, err = durafmt.DefaultUnitsCoder.Decode("y:y,w:w,d:d,h:h,m:m,s:s,ms:ms,μs:μs")
if err != nil {
panic(err)
}
}
func main() {
var err error
purgeOlderThan, err = durafmt.ParseString(*purgeOlderThanStr)
if err != nil {
fmt.Printf("Invalid field: purgeInterval - %v\n", err)
os.Exit(1)
}
maxSessionSize = *maxSessionSz << (10 * 2) // 2 refers to the constants ByteSize MB
maxSessionSizeFormatted = ByteCountDecimal(int64(maxSessionSize))
OsSignal = make(chan os.Signal, 1)
hd := hashids.NewData()
hd.Salt = "dumpr salt"
hd.MinLength = 30
hasher, _ = hashids.NewWithData(hd)
logeShutdown := loge.Init(
loge.Path("."),
loge.EnableOutputConsole(true),
loge.EnableOutputFile(false),
loge.ConsoleOutput(os.Stdout),
loge.EnableDebug(),
loge.EnableError(),
loge.EnableInfo(),
loge.EnableWarning(),
//loge.Transports(func(list loge.TransactionList) []loge.Transport {
// // transport := loge.WrapTransport(list, c)
// return []loge.Transport{transport}
//}),
)
defer logeShutdown()
webFS, err = SetupFS()
if err != nil {
fmt.Printf("Error initializing assets fs, error: %v\n", err)
return
}
webDirHTTPFS = http.FS(webFS)
staticDirHTTPFS = EmbedFolder(webFS, "assets", false)
if *exportTemplates {
fmt.Printf("Exporting templates to %s\n", *webDir)
err = copyTemplatesToTarget(*webDir)
if err != nil {
fmt.Printf("Error saving templates, error: %v\n", err)
}
return
}
db, err = InitializeDB()
if err != nil {
fmt.Printf("Error initializing db, error: %v\n", err)
return
}
defer func() {
_ = db.Close()
}()
sessionList, err := LoadSessions()
if err != nil {
fmt.Printf("Error loading sessions, error: %v\n", err)
return
}
fmt.Printf("Loaded from db %d sessions\n", len(sessionList))
if purgeOlderThan.Duration() > 0 {
go LaunchSessionReaper()
}
go LaunchSessionUpdater()
err = InitializeAutoResponders()
if err != nil {
fmt.Printf("Error loading auto responders, error: %v\n", err)
return
}
err = GinServer()
if err != nil {
fmt.Printf("Error launching web endpoint, error: %v\n", err)
return
}
err = SpawnTCPListener(*serverHost, *tcpPort)
if err != nil {
fmt.Printf("Error launching tcp endpoint, error: %v\n", err)
return
}
LoopForever(func() {
fmt.Printf("Saving all sessions\n")
SaveAllSessions()
fmt.Printf("Saved all sessions\n")
})
}
// LaunchSessionReaper launches the session reaper that will clean up sessions older than purgeOlderThan option.
func LaunchSessionReaper() {
fmt.Printf("launching cleanup process, will delete sessions older than %v\n", purgeOlderThan)
for {
fmt.Printf("Running session cleanup: %v+\n", time.Now().Format(time.ANSIC))
purgeSessionList := make([]*Session, 0)
for _, v := range Sessions {
purgeTime := v.StartTime.Add(purgeOlderThan.Duration())
//fmt.Printf("Session Start Time%v : %v\n", v.StartTime, purgeTime)
if time.Now().After(purgeTime) && !v.Active {
//fmt.Printf("File older than %v : %v\n", purgeOlderThan, purgeTime)
purgeSessionList = append(purgeSessionList, v)
}
}
for _, v := range purgeSessionList {
//fmt.Printf("[%d] purging session: %s\n", i, v.Key)
PurgeSession(v)
}
time.Sleep(time.Minute * 1)
}
}
// LaunchSessionUpdater launches the session updater that will send updates for active sessions.
func LaunchSessionUpdater() {
fmt.Printf("launching session updater process, will update sessions every 10 seconds\n")
for {
for _, v := range Sessions {
if v.Active {
Broadcast(SessionUpdated, v.ToApiSession())
}
}
time.Sleep(time.Second * 10)
}
}