Skip to content

Commit

Permalink
massive refactoring for interoperability
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 10, 2018
1 parent 5a3c7fd commit 292d7cc
Show file tree
Hide file tree
Showing 66 changed files with 1,504 additions and 1,189 deletions.
2 changes: 0 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
package app

//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg app -path App
53 changes: 21 additions & 32 deletions app/dispatcher/impl/default.go → app/dispatcher/default.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package impl
package dispatcher

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

import (
"context"
"time"

"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
Expand All @@ -21,31 +19,27 @@ var (
errSniffingTimeout = newError("timeout on sniffing")
)

var (
_ app.Application = (*DefaultDispatcher)(nil)
)

// DefaultDispatcher is a default implementation of Dispatcher.
type DefaultDispatcher struct {
ohm proxyman.OutboundHandlerManager
router *router.Router
ohm core.OutboundHandlerManager
router core.Router
}

// NewDefaultDispatcher create a new DefaultDispatcher.
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, newError("no space in context")
func NewDefaultDispatcher(ctx context.Context, config *Config) (*DefaultDispatcher, error) {
v := core.FromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}

d := &DefaultDispatcher{
ohm: v.OutboundHandlerManager(),
router: v.Router(),
}

if err := v.RegisterFeature((*core.Dispatcher)(nil), d); err != nil {
return nil, newError("unable to register Dispatcher")
}
d := &DefaultDispatcher{}
space.On(app.SpaceInitializing, func(interface{}) error {
d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
if d.ohm == nil {
return newError("OutboundHandlerManager is not found in the space")
}
d.router = router.FromSpace(space)
return nil
})
return d, nil
}

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

// Interface implements app.Application.
func (*DefaultDispatcher) Interface() interface{} {
return (*dispatcher.Interface)(nil)
}

// Dispatch implements Dispatcher.Interface.
// Dispatch implements core.Dispatcher.
func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
if !destination.IsValid() {
panic("Dispatcher: Invalid destination.")
Expand Down Expand Up @@ -120,7 +109,7 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.OutboundRay, destination net.Destination) {
dispatcher := d.ohm.GetDefaultHandler()
if d.router != nil {
if tag, err := d.router.TakeDetour(ctx); err == nil {
if tag, err := d.router.PickRoute(ctx); err == nil {
if handler := d.ohm.GetHandler(tag); handler != nil {
newError("taking detour [", tag, "] for [", destination, "]").WriteToLog()
dispatcher = handler
Expand All @@ -135,7 +124,7 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, outbound ray.Out
}

func init() {
common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewDefaultDispatcher(ctx, config.(*dispatcher.Config))
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewDefaultDispatcher(ctx, config.(*Config))
}))
}
20 changes: 1 addition & 19 deletions app/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
package dispatcher

import (
"context"

"v2ray.com/core/app"
"v2ray.com/core/common/net"
"v2ray.com/core/transport/ray"
)

// Interface dispatch a packet and possibly further network payload to its destination.
type Interface interface {
Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error)
}

func FromSpace(space app.Space) Interface {
if app := space.GetApplication((*Interface)(nil)); app != nil {
return app.(Interface)
}
return nil
}
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dispatcher -path App,Dispatcher
5 changes: 5 additions & 0 deletions app/dispatcher/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dispatcher

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "Dispatcher") }
7 changes: 0 additions & 7 deletions app/dispatcher/impl/errors.generated.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package impl
package dispatcher

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package impl_test
package dispatcher_test

import (
"testing"

. "v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman"

. "v2ray.com/core/app/dispatcher/impl"
. "v2ray.com/ext/assert"
)

Expand Down
4 changes: 2 additions & 2 deletions app/dns/nameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/miekg/dns"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/net"
Expand Down Expand Up @@ -48,7 +48,7 @@ type UDPNameServer struct {
nextCleanup time.Time
}

func NewUDPNameServer(address net.Destination, dispatcher dispatcher.Interface) *UDPNameServer {
func NewUDPNameServer(address net.Destination, dispatcher core.Dispatcher) *UDPNameServer {
s := &UDPNameServer{
address: address,
requests: make(map[uint16]*PendingRequest),
Expand Down
58 changes: 27 additions & 31 deletions app/dns/server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package dns

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

import (
"context"
"sync"
"time"

dnsmsg "github.com/miekg/dns"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
)
Expand Down Expand Up @@ -41,39 +40,38 @@ type Server struct {
}

func New(ctx context.Context, config *Config) (*Server, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, newError("no space in context")
}
server := &Server{
records: make(map[string]*DomainRecord),
servers: make([]NameServer, len(config.NameServers)),
hosts: config.GetInternalHosts(),
}
space.On(app.SpaceInitializing, func(interface{}) error {
disp := dispatcher.FromSpace(space)
if disp == nil {
return newError("dispatcher is not found in the space")
}
for idx, destPB := range config.NameServers {
address := destPB.Address.AsAddress()
if address.Family().IsDomain() && address.Domain() == "localhost" {
server.servers[idx] = &LocalNameServer{}
} else {
dest := destPB.AsDestination()
if dest.Network == net.Network_Unknown {
dest.Network = net.Network_UDP
}
if dest.Network == net.Network_UDP {
server.servers[idx] = NewUDPNameServer(dest, disp)
}
v := core.FromContext(ctx)
if v == nil {
return nil, newError("V is not in context.")
}

if err := v.RegisterFeature((*core.DNSClient)(nil), server); err != nil {
return nil, newError("unable to register DNSClient.").Base(err)
}

for idx, destPB := range config.NameServers {
address := destPB.Address.AsAddress()
if address.Family().IsDomain() && address.Domain() == "localhost" {
server.servers[idx] = &LocalNameServer{}
} else {
dest := destPB.AsDestination()
if dest.Network == net.Network_Unknown {
dest.Network = net.Network_UDP
}
if dest.Network == net.Network_UDP {
server.servers[idx] = NewUDPNameServer(dest, v.Dispatcher())
}
}
if len(config.NameServers) == 0 {
server.servers = append(server.servers, &LocalNameServer{})
}
return nil
})
}
if len(config.NameServers) == 0 {
server.servers = append(server.servers, &LocalNameServer{})
}

return server, nil
}

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

func (s *Server) Start() error {
net.RegisterIPResolver(s)
return nil
}

func (*Server) Close() {
net.RegisterIPResolver(net.SystemIPResolver())
}

func (s *Server) GetCached(domain string) []net.IP {
Expand Down
60 changes: 28 additions & 32 deletions app/dns/server_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package dns_test

import (
"context"
"testing"

"v2ray.com/core/app"
"v2ray.com/core"
"v2ray.com/core/app/dispatcher"
_ "v2ray.com/core/app/dispatcher/impl"
. "v2ray.com/core/app/dns"
"v2ray.com/core/app/policy"
_ "v2ray.com/core/app/policy/manager"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/policy"
_ "v2ray.com/core/app/proxyman/outbound"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy/freedom"
Expand Down Expand Up @@ -54,50 +50,50 @@ func TestUDPServer(t *testing.T) {

go dnsServer.ListenAndServe()

config := &Config{
NameServers: []*net.Endpoint{
{
Network: net.Network_UDP,
Address: &net.IPOrDomain{
Address: &net.IPOrDomain_Ip{
Ip: []byte{127, 0, 0, 1},
config := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&Config{
NameServers: []*net.Endpoint{
{
Network: net.Network_UDP,
Address: &net.IPOrDomain{
Address: &net.IPOrDomain_Ip{
Ip: []byte{127, 0, 0, 1},
},
},
Port: uint32(port),
},
},
Port: uint32(port),
}),
serial.ToTypedMessage(&dispatcher.Config{}),
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
serial.ToTypedMessage(&policy.Config{}),
},
Outbound: []*core.OutboundHandlerConfig{
&core.OutboundHandlerConfig{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}

ctx := context.Background()
space := app.NewSpace()

ctx = app.ContextWithSpace(ctx, space)
common.Must(app.AddApplicationToSpace(ctx, config))
common.Must(app.AddApplicationToSpace(ctx, &dispatcher.Config{}))
common.Must(app.AddApplicationToSpace(ctx, &proxyman.OutboundConfig{}))
common.Must(app.AddApplicationToSpace(ctx, &policy.Config{}))

om := proxyman.OutboundHandlerManagerFromSpace(space)
om.AddHandler(ctx, &proxyman.OutboundHandlerConfig{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
})
v, err := core.New(config)
assert(err, IsNil)

common.Must(space.Initialize())
common.Must(space.Start())
client := v.DNSClient()

ips, err := net.LookupIP("google.com")
ips, err := client.LookupIP("google.com")
assert(err, IsNil)
assert(len(ips), Equals, 1)
assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})

ips, err = net.LookupIP("facebook.com")
ips, err = client.LookupIP("facebook.com")
assert(err, IsNil)
assert(len(ips), Equals, 1)
assert([]byte(ips[0]), Equals, []byte{9, 9, 9, 9})

dnsServer.Shutdown()

ips, err = net.LookupIP("google.com")
ips, err = client.LookupIP("google.com")
assert(err, IsNil)
assert(len(ips), Equals, 1)
assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8})
Expand Down
Loading

0 comments on commit 292d7cc

Please sign in to comment.