-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
235 lines (210 loc) · 6.91 KB
/
config.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
package main
import (
"embed"
"fmt"
"log"
"os"
"regexp"
"time"
"github.com/spf13/viper"
"github.com/toxyl/glog"
"github.com/toxyl/gutils"
)
const (
INTERVAL_UI_STATS_UPDATE = 10 * time.Second
INTERVAL_STATS_BROADCAST = 60 * time.Second
// INTERVAL_OVERLAYFS_CLEANUP = 30 * time.Second
INTERVAL_SESSIONS_CLEANUP = 1 * time.Minute
INTERVAL_SYNC_CLEANUP = 60 * time.Second
DELAY_OVERLAYFS_MKDIR = 100 * time.Millisecond
CLEANUP_SYNC_MIN_AGE = 120 * time.Second
)
var (
regexEnvVarPrefixes = regexp.MustCompile(`[A-Z_\-0-9]+=.*?\s+(.*)`)
)
//go:embed commands/*
var fsCommandTemplates embed.FS
//go:embed webinterface/*
var fsWebinterfaceTemplates embed.FS
type Config struct {
Debug struct {
FakeShell bool `mapstructure:"fake_shell"`
SyncCommands bool `mapstructure:"sync_commands"`
SyncServer bool `mapstructure:"sync_server"`
SyncClient bool `mapstructure:"sync_client"`
OSSHServer bool `mapstructure:"ossh_server"`
Sessions bool `mapstructure:"sessions"`
UIServer bool `mapstructure:"ui_server"`
OverlayFS bool `mapstructure:"overlay_fs"`
} `mapstructure:"debug"`
PathData string `mapstructure:"path_data"`
PathPayloads string `mapstructure:"path_payloads"`
PathPasswords string `mapstructure:"path_passwords"`
PathUsers string `mapstructure:"path_users"`
PathHosts string `mapstructure:"path_hosts"`
PathCommands string `mapstructure:"path_commands"`
PathWebinterface string `mapstructure:"path_webinterface"`
PathCaptures string `mapstructure:"path_captures"`
PathFFS string `mapstructure:"path_ffs"`
HostName string `mapstructure:"host_name"`
Version string `mapstructure:"version"`
IPWhitelist []string `mapstructure:"ip_whitelist"`
Hostnames []struct {
Name string `mapstructure:"name"`
IP string `mapstructure:"ip"`
} `mapstructure:"hostnames"`
Servers []struct {
Host string `mapstructure:"host"`
Port uint `mapstructure:"port"`
} `mapstructure:"servers"`
MaxIdleTimeout uint `mapstructure:"max_idle"`
MaxSessionAge uint `mapstructure:"max_session_age"`
InputDelay uint `mapstructure:"input_delay"`
Ratelimit float64 `mapstructure:"ratelimit"`
Webinterface struct {
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Port uint `mapstructure:"port"`
CertFile string `mapstructure:"cert_file"`
KeyFile string `mapstructure:"key_file"`
} `mapstructure:"webinterface"`
MetricsServer struct {
Host string `mapstructure:"host"`
Port uint `mapstructure:"port"`
} `mapstructure:"metrics_server"`
SyncServer struct {
Host string `mapstructure:"host"`
Port uint `mapstructure:"port"`
} `mapstructure:"sync_server"`
Sync struct {
Interval int `mapstructure:"interval"`
Nodes []SyncNode `mapstructure:"nodes"`
} `mapstructure:"sync"`
Commands struct {
Rewriters [][]string `mapstructure:"rewriters"`
Simple [][]string `mapstructure:"simple"`
Exit []string `mapstructure:"exit"`
PermissionDenied []string `mapstructure:"permission_denied"`
DiskError []string `mapstructure:"disk_error"`
CommandNotFound []string `mapstructure:"command_not_found"`
FileNotFound []string `mapstructure:"file_not_found"`
NotImplemented []string `mapstructure:"not_implemented"`
Bullshit []string `mapstructure:"bullshit"`
} `mapstructure:"commands"`
}
var cfgFile string = ""
var Conf Config
var LogGlobal *glog.Logger = glog.NewLogger("Global", glog.Gray, false, logMessageHandler)
func logMessageHandler(msg string) {
fmt.Print(msg)
if SrvUI != nil {
SrvUI.PushLog(msg)
}
}
func isIPWhitelisted(ip string) bool {
for _, wip := range Conf.IPWhitelist {
if ip == wip {
return true
}
}
return false
}
func colorConnID(user, host string, port int) string {
addr := glog.AddrIPv4Port(host, port, true)
if user == "" {
return addr
}
return fmt.Sprintf("%s > %s", addr, glog.Wrap(user, glog.Green))
}
func initPath(p, d string) string {
if p == "" {
p = fmt.Sprintf("%s/%s", Conf.PathData, d)
}
return p
}
func InitPaths() {
Conf.PathData = initPath(Conf.PathData, "/etc/ossh")
Conf.PathCaptures = initPath(Conf.PathCaptures, "captures")
Conf.PathCommands = initPath(Conf.PathCommands, "commands")
Conf.PathWebinterface = initPath(Conf.PathWebinterface, "webinterface")
Conf.PathFFS = initPath(Conf.PathFFS, "ffs")
Conf.PathPayloads = initPath(Conf.PathPayloads, "payloads.txt")
Conf.PathHosts = initPath(Conf.PathHosts, "hosts.txt")
Conf.PathPasswords = initPath(Conf.PathPasswords, "passwords.txt")
Conf.PathUsers = initPath(Conf.PathUsers, "users.txt")
Conf.Webinterface.CertFile = initPath(Conf.Webinterface.CertFile, "ossh.crt")
Conf.Webinterface.KeyFile = initPath(Conf.Webinterface.KeyFile, "ossh.key")
err := gutils.MkDirs(
Conf.PathCommands,
Conf.PathCaptures,
fmt.Sprintf("%s/%s", Conf.PathCaptures, "payloads"),
fmt.Sprintf("%s/%s", Conf.PathCaptures, "scp-uploads"),
fmt.Sprintf("%s/%s", Conf.PathCaptures, "ssh-keys"),
Conf.PathFFS,
Conf.PathWebinterface,
)
if err != nil {
panic(err)
}
}
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/ossh/")
viper.AddConfigPath("$HOME/.ossh")
viper.AddConfigPath(".")
}
err := viper.ReadInConfig()
if err != nil {
log.Panic(fmt.Errorf("[Config] Fatal error config file: %w", err))
}
cfgFile = viper.ConfigFileUsed()
err = viper.Unmarshal(&Conf)
if err != nil {
log.Panicf("[Config] Unable to decode into Config struct, %v", err)
}
InitPaths()
err = gutils.CopyEmbeddedFSToDisk(fsCommandTemplates, Conf.PathCommands, "commands")
if err != nil {
log.Panicf("[Config] Unable to copy command templates to disk, %v", err)
}
err = gutils.CopyEmbeddedFSToDisk(fsWebinterfaceTemplates, Conf.PathWebinterface, "webinterface")
if err != nil {
log.Panicf("[Config] Unable to copy webinterface templates to disk, %v", err)
}
InitTemplaterFunctions()
InitTemplaterFunctionsHTML()
LogGlobal.OK("Config loaded from %s", glog.WrapOrange(cfgFile))
}
func getConfig() string {
cfg, err := os.ReadFile(cfgFile)
if err != nil {
LogGlobal.Error(
"Could not read config from '%s': %s",
glog.File(cfgFile),
glog.Error(err),
)
}
return string(cfg)
}
func updateConfig(config []byte) error {
pathSrc := viper.ConfigFileUsed()
pathBak := fmt.Sprintf("%s.bak", pathSrc)
err := gutils.CopyFile(pathSrc, pathBak)
if err != nil {
LogGlobal.Error("Failed to backup config from %s to %s!", glog.File(pathSrc), glog.File(pathBak))
return err
}
err = os.WriteFile(pathSrc, config, 0644)
if err != nil {
LogGlobal.Error("Failed to backup config from %s to %s!", glog.File(pathSrc), glog.File(pathBak))
return err
}
LogGlobal.Success("Written new config to: %s", glog.File(pathSrc))
initConfig()
SrvSync.UpdateClients()
return nil
}