From ec83281d189886e4c9fecb69f6c19ec6df38ee73 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Tue, 22 Sep 2015 18:11:55 +0200 Subject: [PATCH] Parse config just once --- point.go | 8 ++++++-- proxy/freedom/freedomfactory.go | 6 +++++- proxy/vmess/vmessout.go | 11 +++++++++-- testing/mocks/outboundhandler.go | 6 +++++- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/point.go b/point.go index a03ffeb12..378fceb60 100644 --- a/point.go +++ b/point.go @@ -64,7 +64,8 @@ type InboundConnectionHandler interface { } type OutboundConnectionHandlerFactory interface { - Create(VP *Point, config []byte, firstPacket v2net.Packet) (OutboundConnectionHandler, error) + Initialize(config []byte) error + Create(VP *Point, firstPacket v2net.Packet) (OutboundConnectionHandler, error) } type OutboundConnectionHandler interface { @@ -77,6 +78,9 @@ func (vp *Point) Start() error { if vp.port <= 0 { return log.Error("Invalid port %d", vp.port) } + + vp.ochFactory.Initialize(vp.ochConfig) + inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig) if err != nil { return err @@ -88,7 +92,7 @@ func (vp *Point) Start() error { func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay { ray := NewRay() // TODO: handle error - och, _ := p.ochFactory.Create(p, p.ochConfig, packet) + och, _ := p.ochFactory.Create(p, packet) _ = och.Start(ray) return ray } diff --git a/proxy/freedom/freedomfactory.go b/proxy/freedom/freedomfactory.go index 23b03469c..a6e438bdc 100644 --- a/proxy/freedom/freedomfactory.go +++ b/proxy/freedom/freedomfactory.go @@ -8,7 +8,11 @@ import ( type FreedomFactory struct { } -func (factory FreedomFactory) Create(vp *core.Point, config []byte, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) { +func (factory FreedomFactory) Initialize(config []byte) error { + return nil +} + +func (factory FreedomFactory) Create(vp *core.Point, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) { return NewFreedomConnection(firstPacket), nil } diff --git a/proxy/vmess/vmessout.go b/proxy/vmess/vmessout.go index ee9aac65b..69b04b47d 100644 --- a/proxy/vmess/vmessout.go +++ b/proxy/vmess/vmessout.go @@ -183,18 +183,25 @@ func handleResponse(conn *net.TCPConn, request *protocol.VMessRequest, output ch } type VMessOutboundHandlerFactory struct { + servers []VNextServer } -func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig []byte, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) { +func (factory *VMessOutboundHandlerFactory) Initialize(rawConfig []byte) error { config, err := loadOutboundConfig(rawConfig) if err != nil { panic(log.Error("Failed to load VMess outbound config: %v", err)) + return err } servers := make([]VNextServer, 0, len(config.VNextList)) for _, server := range config.VNextList { servers = append(servers, server.ToVNextServer()) } - return NewVMessOutboundHandler(vp, servers, firstPacket), nil + factory.servers = servers + return nil +} + +func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) { + return NewVMessOutboundHandler(vp, factory.servers, firstPacket), nil } func init() { diff --git a/testing/mocks/outboundhandler.go b/testing/mocks/outboundhandler.go index 9b43d412f..8869668e9 100644 --- a/testing/mocks/outboundhandler.go +++ b/testing/mocks/outboundhandler.go @@ -32,7 +32,11 @@ func (handler *OutboundConnectionHandler) Start(ray core.OutboundRay) error { return nil } -func (handler *OutboundConnectionHandler) Create(point *core.Point, config []byte, packet v2net.Packet) (core.OutboundConnectionHandler, error) { +func (handler *OutboundConnectionHandler) Initialize(config []byte) error { + return nil +} + +func (handler *OutboundConnectionHandler) Create(point *core.Point, packet v2net.Packet) (core.OutboundConnectionHandler, error) { handler.Destination = packet.Destination() return handler, nil }