Skip to content

Commit b424f99

Browse files
committed
refactor: Outbound post initializing
1 parent 827b180 commit b424f99

File tree

9 files changed

+69
-27
lines changed

9 files changed

+69
-27
lines changed

adapter/outbound.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77

88
"github.com/layou233/zbproxy/v3/common/bufio"
99
"github.com/layou233/zbproxy/v3/common/network"
10-
"github.com/layou233/zbproxy/v3/config"
1110
)
1211

1312
type Outbound interface {
1413
Name() string
15-
PostInitialize(router Router) error
16-
Reload(newConfig *config.Outbound) error
14+
PostInitialize(router Router, provider RouteResourceProvider) error
15+
Reload(options OutboundReloadOptions) error
1716
DialContext(ctx context.Context, network string, address string) (net.Conn, error)
1817
}
1918

adapter/reload.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package adapter
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/layou233/zbproxy/v3/common/set"
7+
"github.com/layou233/zbproxy/v3/config"
8+
)
9+
10+
type OutboundReloadOptions struct {
11+
Router Router
12+
Config *config.Outbound
13+
Lists map[string]set.StringSet
14+
}
15+
16+
var _ RouteResourceProvider = (*OutboundReloadOptions)(nil)
17+
18+
func (o *OutboundReloadOptions) FindOutboundByName(name string) (Outbound, error) {
19+
// TODO: also implement independent outbound finder
20+
return o.Router.FindOutboundByName(name)
21+
}
22+
23+
func (o *OutboundReloadOptions) FindListsByTag(tags []string) ([]set.StringSet, error) {
24+
if o.Lists != nil {
25+
lists := make([]set.StringSet, 0, len(tags))
26+
for _, tag := range tags {
27+
list, ok := o.Lists[tag]
28+
if !ok {
29+
return nil, fmt.Errorf("list not found [%s]", tag)
30+
}
31+
lists = append(lists, list)
32+
}
33+
return lists, nil
34+
}
35+
return o.Router.FindListsByTag(tags)
36+
}

adapter/router.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import (
77
)
88

99
type Router interface {
10+
RouteResourceProvider
11+
HandleConnection(conn net.Conn, metadata *Metadata)
12+
}
13+
14+
type RouteResourceProvider interface {
1015
FindOutboundByName(name string) (Outbound, error)
1116
FindListsByTag(tags []string) ([]set.StringSet, error)
12-
HandleConnection(conn net.Conn, metadata *Metadata)
1317
}

protocol/minecraft/outbound.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ func (o *Outbound) Name() string {
6565
return ""
6666
}
6767

68-
func (o *Outbound) PostInitialize(router adapter.Router) error {
68+
func (o *Outbound) PostInitialize(router adapter.Router, provider adapter.RouteResourceProvider) error {
6969
var err error
7070
if o.config.Minecraft.HostnameAccess.Mode != access.DefaultMode {
71-
o.hostnameAccessLists, err = router.FindListsByTag(o.config.Minecraft.HostnameAccess.ListTags)
71+
o.hostnameAccessLists, err = provider.FindListsByTag(o.config.Minecraft.HostnameAccess.ListTags)
7272
if err != nil {
7373
return common.Cause("load access control lists: ", err)
7474
}
7575
}
7676
if o.config.Minecraft.NameAccess.Mode != access.DefaultMode {
77-
o.nameAccessLists, err = router.FindListsByTag(o.config.Minecraft.NameAccess.ListTags)
77+
o.nameAccessLists, err = provider.FindListsByTag(o.config.Minecraft.NameAccess.ListTags)
7878
if err != nil {
7979
return common.Cause("load access control lists: ", err)
8080
}
@@ -148,7 +148,7 @@ func (o *Outbound) PostInitialize(router adapter.Router) error {
148148
if o.config.SocketOptions != nil {
149149
return errors.New("socket options are not available when dialer is specified")
150150
}
151-
o.dialer, err = router.FindOutboundByName(o.config.Dialer)
151+
o.dialer, err = provider.FindOutboundByName(o.config.Dialer)
152152
if err != nil {
153153
return err
154154
}
@@ -175,11 +175,11 @@ func (o *Outbound) PostInitialize(router adapter.Router) error {
175175
return nil
176176
}
177177

178-
func (o *Outbound) Reload(newConfig *config.Outbound) error {
179-
o.config = newConfig
178+
func (o *Outbound) Reload(options adapter.OutboundReloadOptions) error {
179+
o.config = options.Config
180180
o.hostnameAccessLists = nil
181181
o.nameAccessLists = nil
182-
return o.PostInitialize(o.router)
182+
return o.PostInitialize(o.router, &options)
183183
}
184184

185185
func (o *Outbound) connectServer(ctx context.Context, metadata *adapter.Metadata) (net.Conn, error) {
@@ -311,8 +311,8 @@ func (o *Outbound) InjectConnection(ctx context.Context, conn *bufio.CachedConn,
311311
case pingModeDisconnect:
312312
// do nothing and disconnect
313313
case pingMode0ms:
314-
buffer.WriteByte(1) // Client bound : Ping Response
315-
buffer.Extend(8) // size of int64 timestamp
314+
buffer.WriteByte(1) // Client bound : Ping Response
315+
buffer.WriteZeroN(8) // size of int64 timestamp
316316
err = clientMC.WritePacket(buffer)
317317
buffer.Release()
318318
if err != nil {

protocol/outbound.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ func (o *Plain) Name() string {
5353
return ""
5454
}
5555

56-
func (o *Plain) PostInitialize(router adapter.Router) error {
56+
func (o *Plain) PostInitialize(router adapter.Router, provider adapter.RouteResourceProvider) error {
5757
var err error
5858
if o.config.Dialer != "" {
5959
if o.config.SocketOptions != nil {
6060
return errors.New("socket options are not available when dialer is specified")
6161
}
62-
o.dialer, err = router.FindOutboundByName(o.config.Dialer)
62+
o.dialer, err = provider.FindOutboundByName(o.config.Dialer)
6363
if err != nil {
6464
return err
6565
}
@@ -86,9 +86,9 @@ func (o *Plain) PostInitialize(router adapter.Router) error {
8686
return nil
8787
}
8888

89-
func (o *Plain) Reload(newConfig *config.Outbound) error {
90-
o.config = newConfig
91-
return o.PostInitialize(o.router)
89+
func (o *Plain) Reload(options adapter.OutboundReloadOptions) error {
90+
o.config = options.Config
91+
return o.PostInitialize(o.router, &options)
9292
}
9393

9494
func (o *Plain) DialContext(ctx context.Context, network string, address string) (net.Conn, error) {

route/builtin_outbounds.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/layou233/zbproxy/v3/adapter"
88
"github.com/layou233/zbproxy/v3/common/bufio"
9-
"github.com/layou233/zbproxy/v3/config"
109
)
1110

1211
type rejectOutbound struct{}
@@ -20,11 +19,11 @@ func (r rejectOutbound) Name() string {
2019
return "REJECT"
2120
}
2221

23-
func (r rejectOutbound) PostInitialize(adapter.Router) error {
22+
func (r rejectOutbound) PostInitialize(adapter.Router, adapter.RouteResourceProvider) error {
2423
return nil
2524
}
2625

27-
func (r rejectOutbound) Reload(*config.Outbound) error {
26+
func (r rejectOutbound) Reload(adapter.OutboundReloadOptions) error {
2827
return nil
2928
}
3029

@@ -47,11 +46,11 @@ func (r resetOutbound) Name() string {
4746
return "RESET"
4847
}
4948

50-
func (r resetOutbound) PostInitialize(adapter.Router) error {
49+
func (r resetOutbound) PostInitialize(adapter.Router, adapter.RouteResourceProvider) error {
5150
return nil
5251
}
5352

54-
func (r resetOutbound) Reload(*config.Outbound) error {
53+
func (r resetOutbound) Reload(adapter.OutboundReloadOptions) error {
5554
return nil
5655
}
5756

route/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (r *Router) Initialize(ctx context.Context, logger *log.Logger, options Rou
6363
r.defaultOutbound, _ = protocol.NewOutbound(r.logger, &config.Outbound{
6464
Name: "default",
6565
})
66-
r.defaultOutbound.PostInitialize(r) // this is dangerous since the router is not fully initialized yet
66+
r.defaultOutbound.PostInitialize(r, r) // this is dangerous since the router is not fully initialized yet
6767
}
6868
r.ctx = ctx
6969
r.logger = logger

service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (s *Service) Start(ctx context.Context) error {
129129
if err != nil {
130130
return common.Cause("initialize legacy Minecraft outbound: ", err)
131131
}
132-
err = s.legacyOutbound.PostInitialize(s.router)
132+
err = s.legacyOutbound.PostInitialize(s.router, s.router)
133133
if err != nil {
134134
return common.Cause("post initialize legacy Minecraft outbound: ", err)
135135
}

zbproxy.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (i *Instance) Start() error {
112112
return common.Cause("initialize router: ", err)
113113
}
114114
for _, outbound := range outboundMap {
115-
err = outbound.PostInitialize(i.router)
115+
err = outbound.PostInitialize(i.router, i.router)
116116
if err != nil {
117117
return common.Cause("post initialize outbound ["+outbound.Name()+"]: ", err)
118118
}
@@ -143,7 +143,11 @@ func (i *Instance) UpdateConfig() {
143143
newOutboundMap := make(map[string]adapter.Outbound, len(i.config.Outbounds))
144144
for _, outboundConfig := range i.config.Outbounds {
145145
if oldOutbound, ok := i.outboundMap[outboundConfig.Name]; ok {
146-
err := oldOutbound.Reload(outboundConfig)
146+
err := oldOutbound.Reload(adapter.OutboundReloadOptions{
147+
Router: i.router,
148+
Config: outboundConfig,
149+
Lists: i.config.Lists,
150+
})
147151
if err != nil {
148152
i.logger.Error().Str("outbound", outboundConfig.Name).Err(err).Msg("Error when updating outbounds")
149153
return

0 commit comments

Comments
 (0)