-
Notifications
You must be signed in to change notification settings - Fork 43
/
zbproxy.go
199 lines (180 loc) · 5.61 KB
/
zbproxy.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
package zbproxy
import (
"context"
"errors"
"time"
"github.com/layou233/zbproxy/v3/adapter"
"github.com/layou233/zbproxy/v3/common"
"github.com/layou233/zbproxy/v3/config"
"github.com/layou233/zbproxy/v3/protocol"
"github.com/layou233/zbproxy/v3/route"
"github.com/layou233/zbproxy/v3/service"
"github.com/phuslu/log"
)
type Options struct {
Config *config.Root
LogWriter log.Writer
ConfigFilePath string
RuleRegistry map[string]route.CustomRuleInitializer
SnifferRegistry map[string]protocol.SnifferFunc
DisableReload bool
}
type Instance struct {
ctx context.Context
logger *log.Logger
config *config.Root
router *route.Router
serviceMap map[string]adapter.Service
outboundMap map[string]adapter.Outbound
ruleRegistry map[string]route.CustomRuleInitializer
snifferRegistry map[string]protocol.SnifferFunc
}
func NewInstance(ctx context.Context, options Options) (*Instance, error) {
instance := &Instance{
ctx: ctx,
logger: &log.Logger{
TimeFormat: "15:04:05",
Writer: options.LogWriter,
},
config: options.Config,
ruleRegistry: options.RuleRegistry,
snifferRegistry: options.SnifferRegistry,
}
if options.LogWriter == nil {
instance.logger.Writer = &log.ConsoleWriter{
ColorOutput: true,
EndWithMessage: true,
}
}
if options.Config == nil {
if options.ConfigFilePath != "" {
newConfig, err := config.LoadConfigFromFile(
ctx, options.ConfigFilePath, !options.DisableReload, instance.logger)
if err != nil {
return nil, err
}
instance.config = newConfig
if !options.DisableReload {
// configure handler here
// if you provide config directly from option
// then configure it by yourself like this
newConfig.SetUpdateHandler(instance.UpdateConfig)
}
} else {
return nil, errors.New("no config provided for ZBProxy")
}
}
instance.logger.Level = instance.config.Log.Level
return instance, nil
}
func (i *Instance) Logger() *log.Logger {
return i.logger
}
func (i *Instance) Router() *route.Router {
return i.router
}
func (i *Instance) Start() error {
var err error
startTime := time.Now()
// initialize outbounds
outboundMap := make(map[string]adapter.Outbound, len(i.config.Outbounds))
for _, outboundConfig := range i.config.Outbounds {
var outbound adapter.Outbound
outbound, err = protocol.NewOutbound(i.logger, outboundConfig)
if err != nil {
return common.Cause("initialize outbound ["+outboundConfig.Name+"]: ", err)
}
outboundMap[outbound.Name()] = outbound
}
i.outboundMap = outboundMap
// initialize router
i.router = &route.Router{}
err = i.router.Initialize(i.ctx, i.logger, route.RouterOptions{
Config: &i.config.Router,
OutboundMap: outboundMap,
ListMap: i.config.Lists,
RuleRegistry: i.ruleRegistry,
SnifferRegistry: i.snifferRegistry,
})
if err != nil {
return common.Cause("initialize router: ", err)
}
for _, outbound := range outboundMap {
err = outbound.PostInitialize(i.router)
if err != nil {
return common.Cause("post initialize outbound ["+outbound.Name()+"]: ", err)
}
}
// initialize services
i.serviceMap = make(map[string]adapter.Service, len(i.config.Services))
for _, serviceConfig := range i.config.Services {
newService := service.NewService(i.logger, serviceConfig)
newService.UpdateRouter(i.router)
err = newService.Start(i.ctx)
if err != nil {
return common.Cause("start service ["+serviceConfig.Name+"]: ", err)
}
i.serviceMap[serviceConfig.Name] = newService
}
i.logger.Info().Str("duration", time.Now().Sub(startTime).String()).Msg("ZBProxy started")
return nil
}
func (i *Instance) Reload() bool {
return i.config.Reload()
}
func (i *Instance) UpdateConfig() {
// update outbounds
newOutboundMap := make(map[string]adapter.Outbound, len(i.config.Outbounds))
for _, outboundConfig := range i.config.Outbounds {
if oldOutbound, ok := i.outboundMap[outboundConfig.Name]; ok {
err := oldOutbound.Reload(outboundConfig)
if err != nil {
i.logger.Error().Str("outbound", outboundConfig.Name).Err(err).Msg("Error when updating outbounds")
return
}
newOutboundMap[outboundConfig.Name] = oldOutbound
} else {
newOutbound, err := protocol.NewOutbound(i.logger, outboundConfig)
if err != nil {
i.logger.Error().Str("outbound", outboundConfig.Name).Err(err).Msg("Error when initializing outbounds")
return
}
newOutboundMap[outboundConfig.Name] = newOutbound
}
}
// update router
err := i.router.UpdateConfig(route.RouterOptions{
Config: &i.config.Router,
OutboundMap: newOutboundMap,
ListMap: i.config.Lists,
RuleRegistry: i.ruleRegistry,
SnifferRegistry: i.snifferRegistry,
})
if err != nil {
i.logger.Error().Err(err).Msg("Error when updating router")
return
}
// update services
newServiceMap := make(map[string]adapter.Service, len(i.config.Services))
for _, serviceConfig := range i.config.Services {
if oldService, ok := i.serviceMap[serviceConfig.Name]; ok {
err = oldService.Reload(i.ctx, serviceConfig)
if err != nil {
i.logger.Error().Str("service", serviceConfig.Name).Err(err).Msg("Error when updating services")
return
}
newServiceMap[serviceConfig.Name] = oldService
} else {
newService := service.NewService(i.logger, serviceConfig)
newService.UpdateRouter(i.router)
err = newService.Start(i.ctx)
if err != nil {
i.logger.Error().Str("service", serviceConfig.Name).Err(err).Msg("Error when initializing services")
return
}
newServiceMap[serviceConfig.Name] = newService
}
}
i.outboundMap = newOutboundMap
i.serviceMap = newServiceMap
}