mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-05-17 09:59:07 -04:00
simplify app design
This commit is contained in:
@@ -38,11 +38,11 @@ func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandler
|
||||
port: v2net.Port(config.Port),
|
||||
meta: meta,
|
||||
}
|
||||
space.InitializeApplication(func() error {
|
||||
if !space.HasApp(dispatcher.APP_ID) {
|
||||
space.OnInitialize(func() error {
|
||||
d.packetDispatcher = dispatcher.FromSpace(space)
|
||||
if d.packetDispatcher == nil {
|
||||
return errors.New("Dokodemo: Dispatcher is not found in the space.")
|
||||
}
|
||||
d.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
|
||||
return nil
|
||||
})
|
||||
return d
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
dispatchers "v2ray.com/core/app/dispatcher/impl"
|
||||
_ "v2ray.com/core/app/dispatcher/impl"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/app/proxyman/outbound"
|
||||
_ "v2ray.com/core/app/proxyman/outbound"
|
||||
"v2ray.com/core/common/dice"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
@@ -38,8 +38,10 @@ func TestDokodemoTCP(t *testing.T) {
|
||||
defer tcpServer.Close()
|
||||
|
||||
space := app.NewSpace()
|
||||
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
|
||||
ohm := outbound.New()
|
||||
space.AddApp(new(dispatcher.Config))
|
||||
space.AddApp(new(proxyman.OutboundConfig))
|
||||
|
||||
ohm := proxyman.OutboundHandlerManagerFromSpace(space)
|
||||
ohm.SetDefaultHandler(
|
||||
freedom.New(
|
||||
&freedom.Config{},
|
||||
@@ -50,7 +52,6 @@ func TestDokodemoTCP(t *testing.T) {
|
||||
Network: v2net.Network_TCP,
|
||||
},
|
||||
}))
|
||||
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
|
||||
|
||||
data2Send := "Data to be sent to remote."
|
||||
|
||||
@@ -109,8 +110,10 @@ func TestDokodemoUDP(t *testing.T) {
|
||||
defer udpServer.Close()
|
||||
|
||||
space := app.NewSpace()
|
||||
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
|
||||
ohm := outbound.New()
|
||||
space.AddApp(new(dispatcher.Config))
|
||||
space.AddApp(new(proxyman.OutboundConfig))
|
||||
|
||||
ohm := proxyman.OutboundHandlerManagerFromSpace(space)
|
||||
ohm.SetDefaultHandler(
|
||||
freedom.New(
|
||||
&freedom.Config{},
|
||||
@@ -120,7 +123,6 @@ func TestDokodemoUDP(t *testing.T) {
|
||||
StreamSettings: &internet.StreamConfig{
|
||||
Network: v2net.Network_TCP,
|
||||
}}))
|
||||
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
|
||||
|
||||
data2Send := "Data to be sent to remote."
|
||||
|
||||
|
||||
@@ -32,12 +32,12 @@ func New(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *Hand
|
||||
timeout: config.Timeout,
|
||||
meta: meta,
|
||||
}
|
||||
space.InitializeApplication(func() error {
|
||||
space.OnInitialize(func() error {
|
||||
if config.DomainStrategy == Config_USE_IP {
|
||||
if !space.HasApp(dns.APP_ID) {
|
||||
f.dns = dns.FromSpace(space)
|
||||
if f.dns == nil {
|
||||
return errors.New("Freedom: DNS server is not found in the space.")
|
||||
}
|
||||
f.dns = space.GetApp(dns.APP_ID).(dns.Server)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
dispatchers "v2ray.com/core/app/dispatcher/impl"
|
||||
_ "v2ray.com/core/app/dispatcher/impl"
|
||||
"v2ray.com/core/app/dns"
|
||||
dnsserver "v2ray.com/core/app/dns/server"
|
||||
_ "v2ray.com/core/app/dns/server"
|
||||
"v2ray.com/core/app/proxyman"
|
||||
"v2ray.com/core/app/proxyman/outbound"
|
||||
_ "v2ray.com/core/app/proxyman/outbound"
|
||||
"v2ray.com/core/app/router"
|
||||
"v2ray.com/core/common/buf"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
@@ -70,16 +70,14 @@ func TestIPResolution(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
space := app.NewSpace()
|
||||
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New())
|
||||
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
|
||||
r := router.NewRouter(&router.Config{}, space)
|
||||
space.BindApp(router.APP_ID, r)
|
||||
dnsServer := dnsserver.NewCacheServer(space, &dns.Config{
|
||||
assert.Error(space.AddApp(new(proxyman.OutboundConfig))).IsNil()
|
||||
assert.Error(space.AddApp(new(dispatcher.Config))).IsNil()
|
||||
assert.Error(space.AddApp(new(router.Config))).IsNil()
|
||||
assert.Error(space.AddApp(&dns.Config{
|
||||
Hosts: map[string]*v2net.IPOrDomain{
|
||||
"v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP),
|
||||
},
|
||||
})
|
||||
space.BindApp(dns.APP_ID, dnsServer)
|
||||
})).IsNil()
|
||||
|
||||
freedom := New(
|
||||
&Config{DomainStrategy: Config_USE_IP},
|
||||
|
||||
@@ -33,12 +33,19 @@ type Server struct {
|
||||
}
|
||||
|
||||
// NewServer creates a new HTTP inbound handler.
|
||||
func NewServer(config *ServerConfig, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server {
|
||||
return &Server{
|
||||
packetDispatcher: packetDispatcher,
|
||||
config: config,
|
||||
meta: meta,
|
||||
func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) *Server {
|
||||
s := &Server{
|
||||
config: config,
|
||||
meta: meta,
|
||||
}
|
||||
space.OnInitialize(func() error {
|
||||
s.packetDispatcher = dispatcher.FromSpace(space)
|
||||
if s.packetDispatcher == nil {
|
||||
return errors.New("HTTP|Server: Dispatcher not found in space.")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
// Port implements InboundHandler.Port().
|
||||
@@ -291,13 +298,7 @@ func (v *ServerFactory) StreamCapability() v2net.NetworkList {
|
||||
|
||||
// Create implements InboundHandlerFactory.Create().
|
||||
func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
|
||||
if !space.HasApp(dispatcher.APP_ID) {
|
||||
return nil, common.ErrBadConfiguration
|
||||
}
|
||||
return NewServer(
|
||||
rawConfig.(*ServerConfig),
|
||||
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
|
||||
meta), nil
|
||||
return NewServer(rawConfig.(*ServerConfig), space, meta), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -6,13 +6,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
testdispatcher "v2ray.com/core/app/dispatcher/testing"
|
||||
"v2ray.com/core/common/dice"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
. "v2ray.com/core/proxy/http"
|
||||
"v2ray.com/core/testing/assert"
|
||||
"v2ray.com/core/transport/internet"
|
||||
|
||||
_ "v2ray.com/core/transport/internet/tcp"
|
||||
)
|
||||
@@ -50,30 +45,3 @@ Accept-Language: de,en;q=0.7,en-us;q=0.3
|
||||
assert.String(req.Header.Get("Proxy-Connection")).Equals("")
|
||||
assert.String(req.Header.Get("Proxy-Authenticate")).Equals("")
|
||||
}
|
||||
|
||||
func TestNormalGetRequest(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
|
||||
|
||||
port := v2net.Port(dice.Roll(20000) + 10000)
|
||||
httpProxy := NewServer(
|
||||
&ServerConfig{},
|
||||
testPacketDispatcher,
|
||||
&proxy.InboundHandlerMeta{
|
||||
Address: v2net.LocalHostIP,
|
||||
Port: port,
|
||||
StreamSettings: &internet.StreamConfig{
|
||||
Network: v2net.Network_TCP,
|
||||
}})
|
||||
defer httpProxy.Close()
|
||||
|
||||
err := httpProxy.Start()
|
||||
assert.Error(err).IsNil()
|
||||
assert.Port(port).Equals(httpProxy.Port())
|
||||
|
||||
httpClient := &http.Client{}
|
||||
resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/")
|
||||
assert.Error(err).IsNil()
|
||||
assert.Int(resp.StatusCode).Equals(400)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package shadowsocks
|
||||
import (
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/bufio"
|
||||
"v2ray.com/core/common/errors"
|
||||
@@ -46,11 +45,11 @@ func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandler
|
||||
account: account,
|
||||
}
|
||||
|
||||
space.InitializeApplication(func() error {
|
||||
if !space.HasApp(dispatcher.APP_ID) {
|
||||
space.OnInitialize(func() error {
|
||||
s.packetDispatcher = dispatcher.FromSpace(space)
|
||||
if s.packetDispatcher == nil {
|
||||
return errors.New("Shadowsocks|Server: Dispatcher is not found in space.")
|
||||
}
|
||||
s.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -228,8 +227,5 @@ func (v *ServerFactory) StreamCapability() v2net.NetworkList {
|
||||
}
|
||||
|
||||
func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
|
||||
if !space.HasApp(dispatcher.APP_ID) {
|
||||
return nil, common.ErrBadConfiguration
|
||||
}
|
||||
return NewServer(rawConfig.(*ServerConfig), space, meta)
|
||||
}
|
||||
|
||||
@@ -41,11 +41,11 @@ func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandler
|
||||
config: config,
|
||||
meta: meta,
|
||||
}
|
||||
space.InitializeApplication(func() error {
|
||||
if !space.HasApp(dispatcher.APP_ID) {
|
||||
space.OnInitialize(func() error {
|
||||
s.packetDispatcher = dispatcher.FromSpace(space)
|
||||
if s.packetDispatcher == nil {
|
||||
return errors.New("Socks|Server: Dispatcher is not found in the space.")
|
||||
}
|
||||
s.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
|
||||
return nil
|
||||
})
|
||||
return s
|
||||
|
||||
@@ -261,9 +261,6 @@ func (v *Factory) StreamCapability() v2net.NetworkList {
|
||||
}
|
||||
|
||||
func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
|
||||
if !space.HasApp(dispatcher.APP_ID) {
|
||||
return nil, common.ErrBadConfiguration
|
||||
}
|
||||
config := rawConfig.(*Config)
|
||||
|
||||
allowedClients := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
|
||||
@@ -272,16 +269,23 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Inb
|
||||
}
|
||||
|
||||
handler := &VMessInboundHandler{
|
||||
packetDispatcher: space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
|
||||
clients: allowedClients,
|
||||
detours: config.Detour,
|
||||
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
|
||||
meta: meta,
|
||||
clients: allowedClients,
|
||||
detours: config.Detour,
|
||||
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
|
||||
meta: meta,
|
||||
}
|
||||
|
||||
if space.HasApp(proxyman.APP_ID_INBOUND_MANAGER) {
|
||||
handler.inboundHandlerManager = space.GetApp(proxyman.APP_ID_INBOUND_MANAGER).(proxyman.InboundHandlerManager)
|
||||
}
|
||||
space.OnInitialize(func() error {
|
||||
handler.packetDispatcher = dispatcher.FromSpace(space)
|
||||
if handler.packetDispatcher == nil {
|
||||
return errors.New("VMess|Inbound: Dispatcher is not found in space.")
|
||||
}
|
||||
handler.inboundHandlerManager = proxyman.InboundHandlerManagerFromSpace(space)
|
||||
if handler.inboundHandlerManager == nil {
|
||||
return errors.New("VMess|Inbound: InboundHandlerManager is not found is space.")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user