-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
309 lines (264 loc) · 6.42 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
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
package gamma
import (
"encoding/json"
"github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"path/filepath"
"sync"
"time"
)
type Service struct {
Enabled bool `yaml:"enabled"`
Bind string `yaml:"bind"`
}
type Ping struct {
Edition string `yaml:"edition"`
VersionName string `yaml:"versionName"`
VersionProtocol int `yaml:"versionProtocol"`
Description string `yaml:"description"`
PlayerCount int `yaml:"playerCount"`
MaxPlayerCount int `yaml:"maxPlayerCount"`
Gamemode string `yaml:"gamemode"`
GamemodeNumeric int `yaml:"gamemodeNumeric"`
}
type GlobalConfig struct {
Prometheus Service
Api Service
Ping Ping
Debug bool `yaml:"debug"`
ReceiveProxyProtocol bool `yaml:"receiveProxyProtocol"`
GenericJoinResponse string `yaml:"genericJoinResponse"`
}
type ProxyConfig struct {
sync.RWMutex
watcher *fsnotify.Watcher
removeCallback func()
changeCallback func()
Domains []string `json:"domains"`
ListenTo string `json:"listenTo"`
ProxyTo string `json:"proxyTo"`
ProxyBind string `json:"proxyBind"`
DialTimeout int `json:"dialTimeout"`
DialTimeoutMessage string `json:"dialTimeoutMessage"`
SendProxyProtocol bool `json:"sendProxyProtocol"`
}
var GammaConfig GlobalConfig
var DefaultConfig = GlobalConfig{
Debug: false,
GenericJoinResponse: "There is no proxy associated with this domain. Please check your configuration.",
ReceiveProxyProtocol: false,
Prometheus: Service{
Enabled: false,
Bind: ":9060",
},
Api: Service{
Enabled: false,
Bind: ":5000",
},
Ping: Ping{
Edition: "MCPE",
VersionName: "1.19.50",
VersionProtocol: 560,
Description: "Gamma proxy",
PlayerCount: 0,
MaxPlayerCount: 10,
Gamemode: "SURVIVAL",
GamemodeNumeric: 1,
},
}
var DefaultProxyConfig = ProxyConfig{
Domains: []string{"localhost"},
ListenTo: ":19132",
ProxyTo: "localhost:19133",
ProxyBind: "",
DialTimeout: 1000,
DialTimeoutMessage: "Sorry but the server is offline.",
SendProxyProtocol: false,
}
func LoadGlobalConfig() error {
log.Println("Loading config.yml")
ymlFile, err := ioutil.ReadFile("config.yml")
if err != nil {
return err
}
var config = DefaultConfig
err = yaml.Unmarshal(ymlFile, &config)
if err != nil {
return err
}
GammaConfig = config
return nil
}
func readFilePaths(path string) ([]string, error) {
var filePaths []string
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, file := range files {
if file.IsDir() {
continue
}
filePaths = append(filePaths, filepath.Join(path, file.Name()))
}
return filePaths, err
}
func LoadProxyConfigsFromPath(path string) ([]*ProxyConfig, error) {
filePaths, err := readFilePaths(path)
if err != nil {
return nil, err
}
var cfgs []*ProxyConfig
for _, filePath := range filePaths {
cfg, err := NewProxyConfigFromPath(filePath)
if err != nil {
return nil, err
}
cfgs = append(cfgs, cfg)
}
return cfgs, nil
}
func NewProxyConfigFromPath(path string) (*ProxyConfig, error) {
log.Println("Loading", path)
cfg, err := LoadFromPath(path)
if err != nil {
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
cfg.watcher = watcher
go func() {
defer watcher.Close()
log.Printf("Starting to watch %s", path)
cfg.watch(path, time.Millisecond*50)
log.Printf("Stopping to watch %s", path)
}()
if err := watcher.Add(path); err != nil {
return nil, err
}
return cfg, err
}
func LoadFromPath(path string) (*ProxyConfig, error) {
var config *ProxyConfig
defaultCfg, err := json.Marshal(&DefaultProxyConfig)
if err != nil {
return nil, err
}
err = json.Unmarshal(defaultCfg, &config)
if err != nil {
return nil, err
}
bb, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err := json.Unmarshal(bb, &config); err != nil {
return nil, err
}
return config, err
}
func WatchProxyConfigFolder(path string, out chan *ProxyConfig) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
if err := watcher.Add(path); err != nil {
return err
}
defer close(out)
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return nil
}
if event.Op&fsnotify.Create == fsnotify.Create && filepath.Ext(event.Name) == ".json" {
proxyCfg, err := NewProxyConfigFromPath(event.Name)
if err != nil {
log.Printf("Failed loading %s; error %s", event.Name, err)
continue
}
out <- proxyCfg
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
log.Printf("Failed watching %s; error %s", path, err)
}
}
}
func (cfg *ProxyConfig) watch(path string, interval time.Duration) {
// The interval protects the watcher from write event spams
// This is necessary due to how some text editors handle file safes
tick := time.Tick(interval)
var lastEvent *fsnotify.Event
for {
select {
case <-tick:
if lastEvent == nil {
continue
}
cfg.onConfigWrite(*lastEvent)
lastEvent = nil
case event, ok := <-cfg.watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
cfg.removeCallback()
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
lastEvent = &event
}
case err, ok := <-cfg.watcher.Errors:
if !ok {
return
}
log.Printf("Failed watching %s; error %s", path, err)
}
}
}
func (cfg *ProxyConfig) onConfigWrite(event fsnotify.Event) {
log.Println("Updating", event.Name)
if err := cfg.LoadFromPath(event.Name); err != nil {
log.Printf("Failed update on %s; error %s", event.Name, err)
return
}
cfg.changeCallback()
}
// LoadFromPath loads the ProxyConfig from a file
func (cfg *ProxyConfig) LoadFromPath(path string) error {
cfg.Lock()
defer cfg.Unlock()
var defaultCfg map[string]interface{}
bb, err := json.Marshal(&DefaultProxyConfig)
if err != nil {
return err
}
if err := json.Unmarshal(bb, &defaultCfg); err != nil {
return err
}
bb, err = ioutil.ReadFile(path)
if err != nil {
return err
}
var loadedCfg map[string]interface{}
if err := json.Unmarshal(bb, &loadedCfg); err != nil {
return err
}
for k, v := range loadedCfg {
defaultCfg[k] = v
}
bb, err = json.Marshal(defaultCfg)
if err != nil {
return err
}
return json.Unmarshal(bb, cfg)
}