-
Notifications
You must be signed in to change notification settings - Fork 1
/
threadfin.go
279 lines (217 loc) · 6.44 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
// 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"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"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: "Main", 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 : Programmname
const Name = "Threadfin"
// Version : Version, die Build Nummer wird in der main func geparst.
const Version = "1.7.0"
// DBVersion : Datanbank 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 gitBranch = flag.String("branch", "", ": Git Branch [main|beta] (default: main)")
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() {
// Build-Nummer von der Versionsnummer trennen
var build = strings.Split(Version, ".")
var system = &src.System
system.APIVersion = APIVersion
system.Branch = strings.ToTitle(GitHub.Branch)
system.Build = build[len(build)-1:][0]
system.DBVersion = DBVersion
system.GitHub = GitHub
system.Name = Name
system.Version = strings.Join(build[0:len(build)-1], ".")
// 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
// Systeminformationen anzeigen
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
// Kill all remaining processes and remove PIDs file
tempFolder := os.TempDir() + string(os.PathSeparator) + strings.ToLower(Name) + string(os.PathSeparator)
folders, err := os.ReadDir(tempFolder)
if err == nil {
for _, folder := range folders {
folderName := fmt.Sprintf("%s%s", tempFolder, folder.Name())
pids, err := getPIDsFromFile(folderName)
if err != nil {
fmt.Printf("Error scanning file PIDs: %v", err)
} else {
if len(pids) > 0 {
for _, pid := range pids {
err := killProcess(pid)
if err != nil {
fmt.Printf("Error killing process %s: %v", pid, err)
} else {
fmt.Printf("Successfully killed process %s", pid)
}
}
os.Remove(folderName + string(os.PathSeparator) + "PIDs")
}
}
}
}
// Branch
system.Flag.Branch = *gitBranch
if len(system.Flag.Branch) > 0 {
fmt.Println("Git Branch is now:", system.Flag.Branch)
}
// Debug Level
system.Flag.Debug = *debug
if system.Flag.Debug > 3 {
flag.Usage()
return
}
// Speicherort für die Konfigurationsdateien
if len(*configFolder) > 0 {
system.Folder.Config = *configFolder
}
// Backup wiederherstellen
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)
}
err = src.Init()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
err = src.BinaryUpdate()
if err != nil {
src.ShowError(err, 0)
}
err = src.StartSystem(false)
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
err = src.InitMaintenance()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
err = src.StartWebserver()
if err != nil {
src.ShowError(err, 0)
os.Exit(0)
}
}
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 {
cmd := exec.Command("kill", "-9", pid)
return cmd.Run()
}