Skip to content

Commit 292d7cc

Browse files
committed
massive refactoring for interoperability
1 parent 5a3c7fd commit 292d7cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1504
-1189
lines changed

Diff for: app/app.go

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
package app
2-
3-
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg app -path App

Diff for: app/dispatcher/impl/default.go renamed to app/dispatcher/default.go

+21-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
package impl
1+
package dispatcher
22

33
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg impl -path App,Dispatcher,Default
44

55
import (
66
"context"
77
"time"
88

9-
"v2ray.com/core/app"
10-
"v2ray.com/core/app/dispatcher"
9+
"v2ray.com/core"
1110
"v2ray.com/core/app/proxyman"
12-
"v2ray.com/core/app/router"
1311
"v2ray.com/core/common"
1412
"v2ray.com/core/common/buf"
1513
"v2ray.com/core/common/net"
@@ -21,31 +19,27 @@ var (
2119
errSniffingTimeout = newError("timeout on sniffing")
2220
)
2321

24-
var (
25-
_ app.Application = (*DefaultDispatcher)(nil)
26-
)
27-
2822
// DefaultDispatcher is a default implementation of Dispatcher.
2923
type DefaultDispatcher struct {
30-
ohm proxyman.OutboundHandlerManager
31-
router *router.Router
24+
ohm core.OutboundHandlerManager
25+
router core.Router
3226
}
3327

3428
// NewDefaultDispatcher create a new DefaultDispatcher.
35-
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
36-
space := app.SpaceFromContext(ctx)
37-
if space == nil {
38-
return nil, newError("no space in context")
29+
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
30+
v := core.FromContext(ctx)
31+
if v == nil {
32+
return nil, newError("V is not in context.")
33+
}
34+
35+
d := &DefaultDispatcher{
36+
ohm: v.OutboundHandlerManager(),
37+
router: v.Router(),
38+
}
39+
40+
if err := v.RegisterFeature((*core.Dispatcher)(nil), d); err != nil {
41+
return nil, newError("unable to register Dispatcher")
3942
}
40-
d := &DefaultDispatcher{}
41-
space.On(app.SpaceInitializing, func(interface{}) error {
42-
d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
43-
if d.ohm == nil {
44-
return newError("OutboundHandlerManager is not found in the space")
45-
}
46-
d.router = router.FromSpace(space)
47-
return nil
48-
})
4943
return d, nil
5044
}
5145

@@ -57,12 +51,7 @@ func (*DefaultDispatcher) Start() error {
5751
// Close implements app.Application.
5852
func (*DefaultDispatcher) Close() {}
5953

60-
// Interface implements app.Application.
61-
func (*DefaultDispatcher) Interface() interface{} {
62-
return (*dispatcher.Interface)(nil)
63-
}
64-
65-
// Dispatch implements Dispatcher.Interface.
54+
// Dispatch implements core.Dispatcher.
6655
func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
6756
if !destination.IsValid() {
6857
panic("Dispatcher: Invalid destination.")
@@ -120,7 +109,7 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
120109
func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.OutboundRay, destination net.Destination) {
121110
dispatcher := d.ohm.GetDefaultHandler()
122111
if d.router != nil {
123-
if tag, err := d.router.TakeDetour(ctx); err == nil {
112+
if tag, err := d.router.PickRoute(ctx); err == nil {
124113
if handler := d.ohm.GetHandler(tag); handler != nil {
125114
newError("taking detour [", tag, "] for [", destination, "]").WriteToLog()
126115
dispatcher = handler
@@ -135,7 +124,7 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.Out
135124
}
136125

137126
func init() {
138-
common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
139-
return NewDefaultDispatcher(ctx, config.(*dispatcher.Config))
127+
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
128+
return NewDefaultDispatcher(ctx, config.(*Config))
140129
}))
141130
}

Diff for: app/dispatcher/dispatcher.go

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
11
package dispatcher
22

3-
import (
4-
"context"
5-
6-
"v2ray.com/core/app"
7-
"v2ray.com/core/common/net"
8-
"v2ray.com/core/transport/ray"
9-
)
10-
11-
// Interface dispatch a packet and possibly further network payload to its destination.
12-
type Interface interface {
13-
Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error)
14-
}
15-
16-
func FromSpace(space app.Space) Interface {
17-
if app := space.GetApplication((*Interface)(nil)); app != nil {
18-
return app.(Interface)
19-
}
20-
return nil
21-
}
3+
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dispatcher -path App,Dispatcher

Diff for: app/dispatcher/errors.generated.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package dispatcher
2+
3+
import "v2ray.com/core/common/errors"
4+
5+
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "Dispatcher") }

Diff for: app/dispatcher/impl/errors.generated.go

-7
This file was deleted.

Diff for: app/dispatcher/impl/sniffer.go renamed to app/dispatcher/sniffer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package impl
1+
package dispatcher
22

33
import (
44
"bytes"

Diff for: app/dispatcher/impl/sniffer_test.go renamed to app/dispatcher/sniffer_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package impl_test
1+
package dispatcher_test
22

33
import (
44
"testing"
55

6+
. "v2ray.com/core/app/dispatcher"
67
"v2ray.com/core/app/proxyman"
7-
8-
. "v2ray.com/core/app/dispatcher/impl"
98
. "v2ray.com/ext/assert"
109
)
1110

Diff for: app/dns/nameserver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/miekg/dns"
9-
"v2ray.com/core/app/dispatcher"
9+
"v2ray.com/core"
1010
"v2ray.com/core/common/buf"
1111
"v2ray.com/core/common/dice"
1212
"v2ray.com/core/common/net"
@@ -48,7 +48,7 @@ type UDPNameServer struct {
4848
nextCleanup time.Time
4949
}
5050

51-
func NewUDPNameServer(address net.Destination, dispatcher dispatcher.Interface) *UDPNameServer {
51+
func NewUDPNameServer(address net.Destination, dispatcher core.Dispatcher) *UDPNameServer {
5252
s := &UDPNameServer{
5353
address: address,
5454
requests: make(map[uint16]*PendingRequest),

Diff for: app/dns/server.go

+27-31
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package dns
22

3-
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg server -path App,DNS,Server
3+
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dns -path App,DNS
44

55
import (
66
"context"
77
"sync"
88
"time"
99

1010
dnsmsg "github.com/miekg/dns"
11-
"v2ray.com/core/app"
12-
"v2ray.com/core/app/dispatcher"
11+
"v2ray.com/core"
1312
"v2ray.com/core/common"
1413
"v2ray.com/core/common/net"
1514
)
@@ -41,39 +40,38 @@ type Server struct {
4140
}
4241

4342
func New(ctx context.Context, config *Config) (*Server, error) {
44-
space := app.SpaceFromContext(ctx)
45-
if space == nil {
46-
return nil, newError("no space in context")
47-
}
4843
server := &Server{
4944
records: make(map[string]*DomainRecord),
5045
servers: make([]NameServer, len(config.NameServers)),
5146
hosts: config.GetInternalHosts(),
5247
}
53-
space.On(app.SpaceInitializing, func(interface{}) error {
54-
disp := dispatcher.FromSpace(space)
55-
if disp == nil {
56-
return newError("dispatcher is not found in the space")
57-
}
58-
for idx, destPB := range config.NameServers {
59-
address := destPB.Address.AsAddress()
60-
if address.Family().IsDomain() && address.Domain() == "localhost" {
61-
server.servers[idx] = &LocalNameServer{}
62-
} else {
63-
dest := destPB.AsDestination()
64-
if dest.Network == net.Network_Unknown {
65-
dest.Network = net.Network_UDP
66-
}
67-
if dest.Network == net.Network_UDP {
68-
server.servers[idx] = NewUDPNameServer(dest, disp)
69-
}
48+
v := core.FromContext(ctx)
49+
if v == nil {
50+
return nil, newError("V is not in context.")
51+
}
52+
53+
if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
54+
return nil, newError("unable to register DNSClient.").Base(err)
55+
}
56+
57+
for idx, destPB := range config.NameServers {
58+
address := destPB.Address.AsAddress()
59+
if address.Family().IsDomain() && address.Domain() == "localhost" {
60+
server.servers[idx] = &LocalNameServer{}
61+
} else {
62+
dest := destPB.AsDestination()
63+
if dest.Network == net.Network_Unknown {
64+
dest.Network = net.Network_UDP
65+
}
66+
if dest.Network == net.Network_UDP {
67+
server.servers[idx] = NewUDPNameServer(dest, v.Dispatcher())
7068
}
7169
}
72-
if len(config.NameServers) == 0 {
73-
server.servers = append(server.servers, &LocalNameServer{})
74-
}
75-
return nil
76-
})
70+
}
71+
if len(config.NameServers) == 0 {
72+
server.servers = append(server.servers, &LocalNameServer{})
73+
}
74+
7775
return server, nil
7876
}
7977

@@ -82,12 +80,10 @@ func (*Server) Interface() interface{} {
8280
}
8381

8482
func (s *Server) Start() error {
85-
net.RegisterIPResolver(s)
8683
return nil
8784
}
8885

8986
func (*Server) Close() {
90-
net.RegisterIPResolver(net.SystemIPResolver())
9187
}
9288

9389
func (s *Server) GetCached(domain string) []net.IP {

Diff for: app/dns/server_test.go

+28-32
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package dns_test
22

33
import (
4-
"context"
54
"testing"
65

7-
"v2ray.com/core/app"
6+
"v2ray.com/core"
87
"v2ray.com/core/app/dispatcher"
9-
_ "v2ray.com/core/app/dispatcher/impl"
108
. "v2ray.com/core/app/dns"
11-
"v2ray.com/core/app/policy"
12-
_ "v2ray.com/core/app/policy/manager"
139
"v2ray.com/core/app/proxyman"
10+
"v2ray.com/core/app/policy"
1411
_ "v2ray.com/core/app/proxyman/outbound"
15-
"v2ray.com/core/common"
1612
"v2ray.com/core/common/net"
1713
"v2ray.com/core/common/serial"
1814
"v2ray.com/core/proxy/freedom"
@@ -54,50 +50,50 @@ func TestUDPServer(t *testing.T) {
5450

5551
go dnsServer.ListenAndServe()
5652

57-
config := &Config{
58-
NameServers: []*net.Endpoint{
59-
{
60-
Network: net.Network_UDP,
61-
Address: &net.IPOrDomain{
62-
Address: &net.IPOrDomain_Ip{
63-
Ip: []byte{127, 0, 0, 1},
53+
config := &core.Config{
54+
App: []*serial.TypedMessage{
55+
serial.ToTypedMessage(&Config{
56+
NameServers: []*net.Endpoint{
57+
{
58+
Network: net.Network_UDP,
59+
Address: &net.IPOrDomain{
60+
Address: &net.IPOrDomain_Ip{
61+
Ip: []byte{127, 0, 0, 1},
62+
},
63+
},
64+
Port: uint32(port),
6465
},
6566
},
66-
Port: uint32(port),
67+
}),
68+
serial.ToTypedMessage(&dispatcher.Config{}),
69+
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
70+
serial.ToTypedMessage(&policy.Config{}),
71+
},
72+
Outbound: []*core.OutboundHandlerConfig{
73+
&core.OutboundHandlerConfig{
74+
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
6775
},
6876
},
6977
}
7078

71-
ctx := context.Background()
72-
space := app.NewSpace()
73-
74-
ctx = app.ContextWithSpace(ctx, space)
75-
common.Must(app.AddApplicationToSpace(ctx, config))
76-
common.Must(app.AddApplicationToSpace(ctx, &dispatcher.Config{}))
77-
common.Must(app.AddApplicationToSpace(ctx, &proxyman.OutboundConfig{}))
78-
common.Must(app.AddApplicationToSpace(ctx, &policy.Config{}))
79-
80-
om := proxyman.OutboundHandlerManagerFromSpace(space)
81-
om.AddHandler(ctx, &proxyman.OutboundHandlerConfig{
82-
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
83-
})
79+
v, err := core.New(config)
80+
assert(err, IsNil)
8481

85-
common.Must(space.Initialize())
86-
common.Must(space.Start())
82+
client := v.DNSClient()
8783

88-
ips, err := net.LookupIP("google.com")
84+
ips, err := client.LookupIP("google.com")
8985
assert(err, IsNil)
9086
assert(len(ips), Equals, 1)
9187
assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})
9288

93-
ips, err = net.LookupIP("facebook.com")
89+
ips, err = client.LookupIP("facebook.com")
9490
assert(err, IsNil)
9591
assert(len(ips), Equals, 1)
9692
assert([]byte(ips[0]), Equals, []byte{9, 9, 9, 9})
9793

9894
dnsServer.Shutdown()
9995

100-
ips, err = net.LookupIP("google.com")
96+
ips, err = client.LookupIP("google.com")
10197
assert(err, IsNil)
10298
assert(len(ips), Equals, 1)
10399
assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})

0 commit comments

Comments
 (0)