-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
172 lines (127 loc) · 3.79 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/bestserversio/spy/internal/config"
"github.com/bestserversio/spy/internal/scanners"
"github.com/bestserversio/spy/internal/servers"
"github.com/bestserversio/spy/internal/utils"
"github.com/bestserversio/spy/internal/vms"
)
const VERSION = "1.0.0"
const HELPMENU = `
./spy [OPTIONS]\n\n
-l --list => Prints config and exits.\n
-v --version => Prints version and exits.\n
-h --help => Prints help menu and exits.\n
-c --cfg => Path to config file.\n
`
func main() {
// Command line options and parse command line.
var list bool
var version bool
var help bool
flag.BoolVar(&list, "l", false, "Prints config settings and exits.")
flag.BoolVar(&list, "list", false, "Prints config settings and exits.")
flag.BoolVar(&version, "v", false, "Prints version and exits.")
flag.BoolVar(&version, "version", false, "Prints number and exits.")
flag.BoolVar(&help, "h", false, "Prints help menu and exits.")
flag.BoolVar(&help, "help", false, "Prints help menu and exits.")
cfgPath := flag.String("cfg", "/etc/bestservers/spy.json", "Path to config file.")
flag.Parse()
// Check for version.
if version {
fmt.Println(VERSION)
os.Exit(0)
}
// Check for help menu.
if help {
fmt.Print(HELPMENU)
os.Exit(0)
}
// Initialize config.
cfg := config.Config{}
// Load defaults.
cfg.LoadDefaults()
// Attempt to load config.
err := cfg.LoadFromFs(*cfgPath)
if err != nil {
fmt.Println("Error loading config file. Resorting to defaults...")
fmt.Println(err)
}
utils.DebugMsg(2, &cfg, "[CFG] Initial config loaded...")
// Check if we want to print our config settings.
if list {
cfg.PrintConfig()
os.Exit(0)
}
// Check for web API updating.
if cfg.WebApi.Enabled {
go func() {
for {
// Get web API interval.
interval := time.Duration(cfg.WebApi.Interval) * time.Second
if interval < 1 {
interval = time.Second * 60
}
// Make sure web config is still enabled.
if !cfg.WebApi.Enabled {
time.Sleep(interval)
continue
}
utils.DebugMsg(3, &cfg, "[WEB_API] Retrieving web API from '%s%s'.", cfg.WebApi.Host, cfg.WebApi.Endpoint)
data, err := cfg.LoadFromWeb()
if err != nil {
utils.DebugMsg(1, &cfg, "[WEB_API] Failed to retrieve web API from '%s%s'.", cfg.WebApi.Host, cfg.WebApi.Endpoint)
time.Sleep(interval)
continue
}
utils.DebugMsg(6, &cfg, "[WEB_API] Loading JSON => %s", data)
// Check if we need to save new config to the file system.
if cfg.WebApi.SaveToFs {
jsonData, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
utils.DebugMsg(1, &cfg, "[WEB_API] Failed to marshal CFG structure when saving to file system due to error : %s", err.Error())
} else {
err = os.WriteFile(*cfgPath, jsonData, 0644)
if err != nil {
utils.DebugMsg(0, &cfg, "[WEB_API] Failed to write web config to file system (%s) due to error :: %s", *cfgPath, err.Error())
} else {
utils.DebugMsg(2, &cfg, "[WEB_API] Successfully wrote new data to file system (%s)!", *cfgPath)
}
}
}
// Resetup VMS.
vms.SetupVms(&cfg)
// Resetup scanners.
scanners.SetupScanners(&cfg)
// If we have no interval, close this function now.
if cfg.WebApi.Interval < 1 {
return
}
time.Sleep(interval)
}
}()
}
// Setup remove inactive.
go servers.RemoveInactive(&cfg)
// Setup remove dups.
go servers.RemoveDups(&cfg)
// Setup remove timed out.
go servers.RemoveTimedOut(&cfg)
// Setup VMS.
vms.SetupVms(&cfg)
// Setup scanners.
scanners.SetupScanners(&cfg)
// Make a signal.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
<-sigc
utils.DebugMsg(0, &cfg, "[MAIN] Exiting...")
os.Exit(0)
}