1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

introduce app.Space

This commit is contained in:
v2ray 2015-12-05 22:55:45 +01:00
parent db7d48e48f
commit 15e6e6d80c
11 changed files with 68 additions and 39 deletions

23
app/space.go Normal file
View File

@ -0,0 +1,23 @@
package app
type Space struct {
packetDispatcher PacketDispatcher
}
func NewSpace() *Space {
return new(Space)
}
func (this *Space) HasPacketDispatcher() bool {
return this.packetDispatcher != nil
}
func (this *Space) PacketDispatcher() PacketDispatcher {
return this.packetDispatcher
}
func (this *Space) Bind(object interface{}) {
if packetDispatcher, ok := object.(PacketDispatcher); ok {
this.packetDispatcher = packetDispatcher
}
}

View File

@ -8,7 +8,7 @@ import (
// A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand. // A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand.
type InboundConnectionHandlerFactory interface { type InboundConnectionHandlerFactory interface {
// Create creates a new InboundConnectionHandler with given configuration. // Create creates a new InboundConnectionHandler with given configuration.
Create(dispatch app.PacketDispatcher, config interface{}) (InboundConnectionHandler, error) Create(space *app.Space, config interface{}) (InboundConnectionHandler, error)
} }
// A InboundConnectionHandler handles inbound network connections to V2Ray. // A InboundConnectionHandler handles inbound network connections to V2Ray.

View File

@ -14,16 +14,16 @@ import (
) )
type DokodemoDoor struct { type DokodemoDoor struct {
config *json.DokodemoConfig config *json.DokodemoConfig
accepting bool accepting bool
address v2net.Address address v2net.Address
dispatcher app.PacketDispatcher space *app.Space
} }
func NewDokodemoDoor(dispatcher app.PacketDispatcher, config *json.DokodemoConfig) *DokodemoDoor { func NewDokodemoDoor(space *app.Space, config *json.DokodemoConfig) *DokodemoDoor {
d := &DokodemoDoor{ d := &DokodemoDoor{
config: config, config: config,
dispatcher: dispatcher, space: space,
} }
ip := net.ParseIP(config.Host) ip := net.ParseIP(config.Host)
if ip != nil { if ip != nil {
@ -79,7 +79,7 @@ func (this *DokodemoDoor) handleUDPPackets(udpConn *net.UDPConn) {
} }
packet := v2net.NewPacket(v2net.NewUDPDestination(this.address), buffer, false) packet := v2net.NewPacket(v2net.NewUDPDestination(this.address), buffer, false)
ray := this.dispatcher.DispatchToOutbound(packet) ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
close(ray.InboundInput()) close(ray.InboundInput())
for payload := range ray.InboundOutput() { for payload := range ray.InboundOutput() {
@ -120,7 +120,7 @@ func (this *DokodemoDoor) HandleTCPConnection(conn *net.TCPConn) {
defer conn.Close() defer conn.Close()
packet := v2net.NewPacket(v2net.NewTCPDestination(this.address), nil, true) packet := v2net.NewPacket(v2net.NewTCPDestination(this.address), nil, true)
ray := this.dispatcher.DispatchToOutbound(packet) ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
var inputFinish, outputFinish sync.Mutex var inputFinish, outputFinish sync.Mutex
inputFinish.Lock() inputFinish.Lock()

View File

@ -9,9 +9,9 @@ import (
type DokodemoDoorFactory struct { type DokodemoDoorFactory struct {
} }
func (this DokodemoDoorFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { func (this DokodemoDoorFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
config := rawConfig.(*json.DokodemoConfig) config := rawConfig.(*json.DokodemoConfig)
return NewDokodemoDoor(dispatcher, config), nil return NewDokodemoDoor(space, config), nil
} }
func init() { func init() {

View File

@ -24,15 +24,15 @@ var (
// SocksServer is a SOCKS 5 proxy server // SocksServer is a SOCKS 5 proxy server
type SocksServer struct { type SocksServer struct {
accepting bool accepting bool
dispatcher app.PacketDispatcher space *app.Space
config config.SocksConfig config config.SocksConfig
} }
func NewSocksServer(dispatcher app.PacketDispatcher, config config.SocksConfig) *SocksServer { func NewSocksServer(space *app.Space, config config.SocksConfig) *SocksServer {
return &SocksServer{ return &SocksServer{
dispatcher: dispatcher, space: space,
config: config, config: config,
} }
} }
@ -252,7 +252,7 @@ func (this *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth p
} }
func (this *SocksServer) transport(reader io.Reader, writer io.Writer, firstPacket v2net.Packet) { func (this *SocksServer) transport(reader io.Reader, writer io.Writer, firstPacket v2net.Packet) {
ray := this.dispatcher.DispatchToOutbound(firstPacket) ray := this.space.PacketDispatcher().DispatchToOutbound(firstPacket)
input := ray.InboundInput() input := ray.InboundInput()
output := ray.InboundOutput() output := ray.InboundOutput()

View File

@ -9,8 +9,8 @@ import (
type SocksServerFactory struct { type SocksServerFactory struct {
} }
func (this SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { func (this SocksServerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
return NewSocksServer(dispatcher, rawConfig.(config.SocksConfig)), nil return NewSocksServer(space, rawConfig.(config.SocksConfig)), nil
} }
func init() { func init() {

View File

@ -65,7 +65,7 @@ func (this *SocksServer) AcceptPackets(conn *net.UDPConn) error {
} }
func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) { func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
ray := this.dispatcher.DispatchToOutbound(packet) ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
close(ray.InboundInput()) close(ray.InboundInput())
for data := range ray.InboundOutput() { for data := range ray.InboundOutput() {

View File

@ -11,7 +11,7 @@ import (
type InboundConnectionHandler struct { type InboundConnectionHandler struct {
Port v2net.Port Port v2net.Port
Dispatcher app.PacketDispatcher space *app.Space
ConnInput io.Reader ConnInput io.Reader
ConnOutput io.Writer ConnOutput io.Writer
} }
@ -22,7 +22,7 @@ func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
} }
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error { func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
ray := this.Dispatcher.DispatchToOutbound(packet) ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
input := ray.InboundInput() input := ray.InboundInput()
output := ray.InboundOutput() output := ray.InboundOutput()
@ -49,7 +49,7 @@ func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
return nil return nil
} }
func (this *InboundConnectionHandler) Create(dispatcher app.PacketDispatcher, config interface{}) (connhandler.InboundConnectionHandler, error) { func (this *InboundConnectionHandler) Create(space *app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) {
this.Dispatcher = dispatcher this.space = space
return this, nil return this, nil
} }

View File

@ -20,15 +20,15 @@ import (
// Inbound connection handler that handles messages in VMess format. // Inbound connection handler that handles messages in VMess format.
type VMessInboundHandler struct { type VMessInboundHandler struct {
dispatcher app.PacketDispatcher space *app.Space
clients user.UserSet clients user.UserSet
accepting bool accepting bool
} }
func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSet) *VMessInboundHandler { func NewVMessInboundHandler(space *app.Space, clients user.UserSet) *VMessInboundHandler {
return &VMessInboundHandler{ return &VMessInboundHandler{
dispatcher: dispatcher, space: space,
clients: clients, clients: clients,
} }
} }
@ -78,7 +78,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error
log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "") log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "")
log.Debug("VMessIn: Received request for %s", request.Address.String()) log.Debug("VMessIn: Received request for %s", request.Address.String())
ray := this.dispatcher.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true)) ray := this.space.PacketDispatcher().DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
input := ray.InboundInput() input := ray.InboundInput()
output := ray.InboundOutput() output := ray.InboundOutput()
var readFinish, writeFinish sync.Mutex var readFinish, writeFinish sync.Mutex
@ -142,7 +142,7 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha
type VMessInboundHandlerFactory struct { type VMessInboundHandlerFactory struct {
} }
func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { func (this *VMessInboundHandlerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
config := rawConfig.(config.Inbound) config := rawConfig.(config.Inbound)
allowedClients := user.NewTimedUserSet() allowedClients := user.NewTimedUserSet()
@ -150,7 +150,7 @@ func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher,
allowedClients.AddUser(user) allowedClients.AddUser(user)
} }
return NewVMessInboundHandler(dispatcher, allowedClients), nil return NewVMessInboundHandler(space, allowedClients), nil
} }
func init() { func init() {

View File

@ -1,6 +1,7 @@
package point package point
import ( import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry" "github.com/v2ray/v2ray-core/common/retry"
@ -15,7 +16,7 @@ type InboundConnectionHandlerWithPort struct {
// Handler for inbound detour connections. // Handler for inbound detour connections.
type InboundDetourHandler struct { type InboundDetourHandler struct {
point *Point space *app.Space
config config.InboundDetourConfig config config.InboundDetourConfig
ich []*InboundConnectionHandlerWithPort ich []*InboundConnectionHandlerWithPort
} }
@ -31,7 +32,7 @@ func (this *InboundDetourHandler) Initialize() error {
this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1) this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1)
for i := ports.From(); i <= ports.To(); i++ { for i := ports.From(); i <= ports.To(); i++ {
ichConfig := this.config.Settings() ichConfig := this.config.Settings()
ich, err := ichFactory.Create(this.point, ichConfig) ich, err := ichFactory.Create(this.space, ichConfig)
if err != nil { if err != nil {
log.Error("Failed to create inbound connection handler: %v", err) log.Error("Failed to create inbound connection handler: %v", err)
return err return err

View File

@ -5,6 +5,7 @@
package point package point
import ( import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/router" "github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
@ -22,6 +23,7 @@ type Point struct {
idh []*InboundDetourHandler idh []*InboundDetourHandler
odh map[string]connhandler.OutboundConnectionHandler odh map[string]connhandler.OutboundConnectionHandler
router router.Router router router.Router
space *app.Space
} }
// NewPoint returns a new Point server based on given configuration. // NewPoint returns a new Point server based on given configuration.
@ -49,13 +51,16 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
log.SetLogLevel(logConfig.LogLevel()) log.SetLogLevel(logConfig.LogLevel())
} }
vpoint.space = app.NewSpace()
vpoint.space.Bind(vpoint)
ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol()) ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
if ichFactory == nil { if ichFactory == nil {
log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol()) log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
return nil, config.BadConfiguration return nil, config.BadConfiguration
} }
ichConfig := pConfig.InboundConfig().Settings() ichConfig := pConfig.InboundConfig().Settings()
ich, err := ichFactory.Create(vpoint, ichConfig) ich, err := ichFactory.Create(vpoint.space, ichConfig)
if err != nil { if err != nil {
log.Error("Failed to create inbound connection handler: %v", err) log.Error("Failed to create inbound connection handler: %v", err)
return nil, err return nil, err
@ -80,7 +85,7 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
vpoint.idh = make([]*InboundDetourHandler, len(detours)) vpoint.idh = make([]*InboundDetourHandler, len(detours))
for idx, detourConfig := range detours { for idx, detourConfig := range detours {
detourHandler := &InboundDetourHandler{ detourHandler := &InboundDetourHandler{
point: vpoint, space: vpoint.space,
config: detourConfig, config: detourConfig,
} }
err := detourHandler.Initialize() err := detourHandler.Initialize()