-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththreadfin.go
379 lines (305 loc) · 8.62 KB
/
threadfin.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2019 marmei. All rights reserved.
// Use of this source code is governed by a MIT license that can be found in the
// LICENSE file.
// GitHub: https://github.com/Threadfin/Threadfin
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"threadfin/src"
)
// GitHubStruct : GitHub Account. Über diesen Account werden die Updates veröffentlicht
type GitHubStruct struct {
Branch string
Repo string
Update bool
User string
TagName string
}
// GitHub : GitHub Account
// If you want to fork this project, enter your Github account here. This prevents a newer version of Threadfin from updating your version.
var GitHub = GitHubStruct{Branch: "master", User: "marcelGoerentz", Repo: "Threadfin", Update: true}
/*
Branch: GitHub Branch
User: GitHub Username
Repo: GitHub Repository
Update: Automatic updates from the GitHub repository [true|false]
*/
// Name : Program name
const Name = "Threadfin"
// Version : Major, Minor, Patch, Build
const Version = "1.8.1.0"
// DBVersion : Database version
const DBVersion = "0.5.0"
// APIVersion : API version
const APIVersion = "2.0.0"
var homeDirectory = fmt.Sprintf("%s%s.%s%s", src.GetUserHomeDirectory(), string(os.PathSeparator), strings.ToLower(Name), string(os.PathSeparator))
var samplePath = fmt.Sprintf("%spath%sto%sthreadfin%s", string(os.PathSeparator), string(os.PathSeparator), string(os.PathSeparator), string(os.PathSeparator))
var sampleRestore = fmt.Sprintf("%spath%sto%sfile%s", string(os.PathSeparator), string(os.PathSeparator), string(os.PathSeparator), string(os.PathSeparator))
var configFolder = flag.String("config", "", ": Config Folder ["+samplePath+"] (default: "+homeDirectory+")")
var port = flag.Int("port", 34400, ": Server port")
var useHttps = flag.Bool("useHttps", false, ": Use Https Webserver [place server.crt and server.key in config folder]")
var restore = flag.String("restore", "", ": Restore from backup ["+sampleRestore+"threadfin_backup.zip]")
var debug = flag.Int("debug", 0, ": Debug level [0 - 3] (default: 0)")
var info = flag.Bool("info", false, ": Show system info")
var h = flag.Bool("h", false, ": Show help")
// Aktiviert den Entwicklungsmodus. Für den Webserver werden dann die lokalen Dateien verwendet.
var dev = flag.Bool("dev", false, ": Activates the developer mode, the source code must be available. The local files for the web interface are used.")
func main() {
cleanUpOldInstances()
webserver := &src.WebServer{}
// Handle signals
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGABRT, syscall.SIGTERM, syscall.SIGHUP)
go handleSignals(sigs, done, webserver)
// Split build from version string
var versionParts = strings.Split(Version, ".")
var system = &src.System
system.APIVersion = APIVersion
system.Build = versionParts[len(versionParts)-1:][0]
system.DBVersion = DBVersion
system.GitHub = GitHub
system.Name = Name
system.Version = strings.Join(versionParts[:len(versionParts)-1], ".")
// Check which version has been build
if Beta {
system.Beta = true
system.Branch = "beta"
} else {
system.Beta = false
system.Branch = "master"
}
// Panic !!!
defer func() {
if r := recover(); r != nil {
fmt.Println()
fmt.Println("* * * * * FATAL ERROR * * * * *")
fmt.Println("OS: ", runtime.GOOS)
fmt.Println("Arch:", runtime.GOARCH)
fmt.Println("Err: ", r)
fmt.Println()
pc := make([]uintptr, 20)
runtime.Callers(2, pc)
for i := range pc {
if runtime.FuncForPC(pc[i]) != nil {
f := runtime.FuncForPC(pc[i])
file, line := f.FileLine(pc[i])
if string(file)[0:1] != "?" {
fmt.Printf("%s:%d %s\n", filepath.Base(file), line, f.Name())
}
}
}
fmt.Println()
fmt.Println("* * * * * * * * * * * * * * * *")
}
}()
flag.Parse()
if *h {
flag.Usage()
return
}
system.Dev = *dev
// Show system information
if *info {
system.Flag.Info = true
err := src.Init()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
src.ShowSystemInfo()
return
}
// Webserver port
if *port != 0 {
system.Flag.Port = fmt.Sprintf("%d", *port)
}
// Https webserver
system.Flag.UseHttps = *useHttps
// Debug Level
system.Flag.Debug = *debug
if system.Flag.Debug > 3 {
flag.Usage()
return
}
// Config folder place
if len(*configFolder) > 0 {
system.Folder.Config = *configFolder
}
// Restore from backup
if len(*restore) > 0 {
system.Flag.Restore = *restore
err := src.Init()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
err = src.ThreadfinRestoreFromCLI(*restore)
if err != nil {
src.ShowError(err, 0)
}
os.Exit(0)
}
// Initialize threadfin
err := src.Init()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
// Update binary
if src.BinaryUpdate(false) {
os.Exit(0)
}
// Build the database
err = src.StartSystem(false)
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
// Update playlists and xml files
err = src.InitMaintenance()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
// Start the Webserver
err = webserver.StartWebserver()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
// Wait for the Signal to end the program
<-done
src.ShowInfo("Threadfin:Exiting Threadfin")
}
/*
handleSignals should be called in a go routine and will handle incoming system signals
It will make sure that all running processes will be killed before exiting the program
*/
func handleSignals(sigs chan os.Signal, done chan bool, webserver *src.WebServer) {
for sig := range sigs {
switch sig {
case syscall.SIGHUP:
src.ShowInfo("Threadfin:Updating configuration")
continue
case syscall.SIGINT, syscall.SIGABRT, syscall.SIGTERM:
CloseWebserverGracefully(webserver)
// Send signal that everything has ended
done <- true
return
default:
src.ShowDebug("Threadfin: Uncatched signal!", 1)
continue
}
}
time.Sleep(100 * time.Millisecond)
}
func cleanUpOldInstances() {
killAllProcesses()
}
//
func CloseWebserverGracefully(webserver *src.WebServer){
// Lock against reconnection for clients
webserver.SM.LockAgainstNewStreams = true
src.ShowInfo("Threadfin:Stop all streams")
// Stop all streams
stopAllStreams(webserver)
src.ShowInfo("Threadfin:Killing all processes")
// Kill all processes
killAllProcesses()
// Shutdown the webserver gracefully
shutdownWebserver(webserver)
}
// stopAllStreams will stop all existing streams and buffers
func stopAllStreams(webserver *src.WebServer) {
if webserver != nil {
if webserver.SM != nil {
webserver.SM.StopAllStreams()
}
}
}
/*
getTempFolder will get the first temp folder within the threadfin temp folder or returns an empty string if there is no folder
*/
func getTempFolder() string {
tempFolder := os.TempDir() + string(os.PathSeparator) + strings.ToLower(Name) + string(os.PathSeparator)
folders, err := os.ReadDir(tempFolder)
if err == nil {
for _, folder := range folders {
return fmt.Sprintf("%s%s", tempFolder, folder.Name())
}
}
return ""
}
/*
getPIDsFromFile will open the PIDs file within the given Folder and returns the list of PIDs saved in it
*/
func getPIDsFromFile(tempFolder string) ([]string, error) {
pids := []string{}
// Open the file
pidsFile := tempFolder + string(os.PathSeparator) + "PIDs"
_, err_stat := os.Stat(pidsFile)
if os.IsNotExist(err_stat) {
return pids, nil // Return early if the file doesn't exist
}
file, err_open := os.Open(pidsFile)
if err_open != nil {
return nil, err_open
}
defer file.Close() // Close the file when done
// Create a scanner
scanner := bufio.NewScanner(file)
// Read line by line
for scanner.Scan() {
line := scanner.Text()
pids = append(pids, line)
}
return pids, nil
}
// killProcess kills a process by its PID
func killProcess(pid string) error {
pidInt, err := strconv.Atoi(pid)
if err != nil {
return err
}
proc, err := os.FindProcess(pidInt)
if err != nil {
return err
}
return proc.Kill()
}
// killAllProcesses kills processes that had been saved in PID
func killAllProcesses() {
tempFolder := getTempFolder()
if tempFolder != "" {
pids, err := getPIDsFromFile(tempFolder)
if err == nil {
for _, pid := range pids {
src.ShowDebug(fmt.Sprintf("Threadfin:Killing process: %s\n", pid), 1)
killProcess(pid)
}
}
}
}
// shutdownWebserver will stop the webserer
func shutdownWebserver(webserver *src.WebServer) {
if webserver.Server != nil {
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
defer cancel()
err := webserver.Server.Shutdown(ctx)
if err != nil {
src.ShowError(err, 0) // TODO: Add error code
}
}
}