-
Notifications
You must be signed in to change notification settings - Fork 306
/
run.go
90 lines (80 loc) · 2.3 KB
/
run.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
// Copyright (C) 2020, IrineSistiana
//
// This file is part of mosdns.
//
// mosdns is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mosdns is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package coremain
import (
"errors"
"fmt"
"github.com/IrineSistiana/mosdns/dispatcher/handler"
"github.com/IrineSistiana/mosdns/dispatcher/mlog"
_ "github.com/IrineSistiana/mosdns/dispatcher/plugin"
"os"
"os/signal"
"syscall"
)
// Run starts mosdns, it blocks.
func Run(c string) {
//wait for signals
go func() {
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
s := <-osSignals
mlog.Entry().Infof("received signal: %v, bye", s)
os.Exit(0)
}()
err := loadConfig(c, 0)
if err != nil {
mlog.Entry().Fatal(err)
}
mlog.Entry().Info("all plugins are successfully loaded")
mlog.Entry().Debugf("loaded plugins: %v", handler.GetAllPluginTag())
select {}
}
const (
maxIncludeDepth = 10
)
// Init loads plugins from config
func loadConfig(f string, depth int) error {
if depth >= maxIncludeDepth {
return errors.New("max include depth reached")
}
depth++
mlog.Entry().Infof("loading config file: %s", f)
c, err := parseConfig(f)
if err != nil {
return err
}
for i, pluginConfig := range c.Plugin {
if len(pluginConfig.Tag) == 0 {
mlog.Entry().Warnf("plugin at index %d has a empty tag, ignore it", i)
continue
}
if err := handler.InitAndRegPlugin(pluginConfig); err != nil {
return fmt.Errorf("failed to register plugin %d %s: %w", i, pluginConfig.Tag, err)
}
mlog.Entry().Infof("plugin %s loaded", pluginConfig.Tag)
}
for _, include := range c.Include {
if len(include) == 0 {
continue
}
err := loadConfig(include, depth)
if err != nil {
return err
}
}
return nil
}