mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
introduce app.Space
This commit is contained in:
parent
db7d48e48f
commit
15e6e6d80c
23
app/space.go
Normal file
23
app/space.go
Normal 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
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ import (
|
||||
// A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand.
|
||||
type InboundConnectionHandlerFactory interface {
|
||||
// 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.
|
||||
|
@ -14,16 +14,16 @@ import (
|
||||
)
|
||||
|
||||
type DokodemoDoor struct {
|
||||
config *json.DokodemoConfig
|
||||
accepting bool
|
||||
address v2net.Address
|
||||
dispatcher app.PacketDispatcher
|
||||
config *json.DokodemoConfig
|
||||
accepting bool
|
||||
address v2net.Address
|
||||
space *app.Space
|
||||
}
|
||||
|
||||
func NewDokodemoDoor(dispatcher app.PacketDispatcher, config *json.DokodemoConfig) *DokodemoDoor {
|
||||
func NewDokodemoDoor(space *app.Space, config *json.DokodemoConfig) *DokodemoDoor {
|
||||
d := &DokodemoDoor{
|
||||
config: config,
|
||||
dispatcher: dispatcher,
|
||||
config: config,
|
||||
space: space,
|
||||
}
|
||||
ip := net.ParseIP(config.Host)
|
||||
if ip != nil {
|
||||
@ -79,7 +79,7 @@ func (this *DokodemoDoor) handleUDPPackets(udpConn *net.UDPConn) {
|
||||
}
|
||||
|
||||
packet := v2net.NewPacket(v2net.NewUDPDestination(this.address), buffer, false)
|
||||
ray := this.dispatcher.DispatchToOutbound(packet)
|
||||
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
||||
close(ray.InboundInput())
|
||||
|
||||
for payload := range ray.InboundOutput() {
|
||||
@ -120,7 +120,7 @@ func (this *DokodemoDoor) HandleTCPConnection(conn *net.TCPConn) {
|
||||
defer conn.Close()
|
||||
|
||||
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
|
||||
inputFinish.Lock()
|
||||
|
@ -9,9 +9,9 @@ import (
|
||||
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)
|
||||
return NewDokodemoDoor(dispatcher, config), nil
|
||||
return NewDokodemoDoor(space, config), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -24,15 +24,15 @@ var (
|
||||
|
||||
// SocksServer is a SOCKS 5 proxy server
|
||||
type SocksServer struct {
|
||||
accepting bool
|
||||
dispatcher app.PacketDispatcher
|
||||
config config.SocksConfig
|
||||
accepting bool
|
||||
space *app.Space
|
||||
config config.SocksConfig
|
||||
}
|
||||
|
||||
func NewSocksServer(dispatcher app.PacketDispatcher, config config.SocksConfig) *SocksServer {
|
||||
func NewSocksServer(space *app.Space, config config.SocksConfig) *SocksServer {
|
||||
return &SocksServer{
|
||||
dispatcher: dispatcher,
|
||||
config: config,
|
||||
space: space,
|
||||
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) {
|
||||
ray := this.dispatcher.DispatchToOutbound(firstPacket)
|
||||
ray := this.space.PacketDispatcher().DispatchToOutbound(firstPacket)
|
||||
input := ray.InboundInput()
|
||||
output := ray.InboundOutput()
|
||||
|
||||
|
@ -9,8 +9,8 @@ import (
|
||||
type SocksServerFactory struct {
|
||||
}
|
||||
|
||||
func (this SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||
return NewSocksServer(dispatcher, rawConfig.(config.SocksConfig)), nil
|
||||
func (this SocksServerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||
return NewSocksServer(space, rawConfig.(config.SocksConfig)), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -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) {
|
||||
ray := this.dispatcher.DispatchToOutbound(packet)
|
||||
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
||||
close(ray.InboundInput())
|
||||
|
||||
for data := range ray.InboundOutput() {
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
|
||||
type InboundConnectionHandler struct {
|
||||
Port v2net.Port
|
||||
Dispatcher app.PacketDispatcher
|
||||
space *app.Space
|
||||
ConnInput io.Reader
|
||||
ConnOutput io.Writer
|
||||
}
|
||||
@ -22,7 +22,7 @@ func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
|
||||
}
|
||||
|
||||
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
|
||||
ray := this.Dispatcher.DispatchToOutbound(packet)
|
||||
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
||||
|
||||
input := ray.InboundInput()
|
||||
output := ray.InboundOutput()
|
||||
@ -49,7 +49,7 @@ func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *InboundConnectionHandler) Create(dispatcher app.PacketDispatcher, config interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||
this.Dispatcher = dispatcher
|
||||
func (this *InboundConnectionHandler) Create(space *app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||
this.space = space
|
||||
return this, nil
|
||||
}
|
||||
|
@ -20,15 +20,15 @@ import (
|
||||
|
||||
// Inbound connection handler that handles messages in VMess format.
|
||||
type VMessInboundHandler struct {
|
||||
dispatcher app.PacketDispatcher
|
||||
clients user.UserSet
|
||||
accepting bool
|
||||
space *app.Space
|
||||
clients user.UserSet
|
||||
accepting bool
|
||||
}
|
||||
|
||||
func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSet) *VMessInboundHandler {
|
||||
func NewVMessInboundHandler(space *app.Space, clients user.UserSet) *VMessInboundHandler {
|
||||
return &VMessInboundHandler{
|
||||
dispatcher: dispatcher,
|
||||
clients: clients,
|
||||
space: space,
|
||||
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.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()
|
||||
output := ray.InboundOutput()
|
||||
var readFinish, writeFinish sync.Mutex
|
||||
@ -142,7 +142,7 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha
|
||||
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)
|
||||
|
||||
allowedClients := user.NewTimedUserSet()
|
||||
@ -150,7 +150,7 @@ func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher,
|
||||
allowedClients.AddUser(user)
|
||||
}
|
||||
|
||||
return NewVMessInboundHandler(dispatcher, allowedClients), nil
|
||||
return NewVMessInboundHandler(space, allowedClients), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package point
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/common/retry"
|
||||
@ -15,7 +16,7 @@ type InboundConnectionHandlerWithPort struct {
|
||||
|
||||
// Handler for inbound detour connections.
|
||||
type InboundDetourHandler struct {
|
||||
point *Point
|
||||
space *app.Space
|
||||
config config.InboundDetourConfig
|
||||
ich []*InboundConnectionHandlerWithPort
|
||||
}
|
||||
@ -31,7 +32,7 @@ func (this *InboundDetourHandler) Initialize() error {
|
||||
this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1)
|
||||
for i := ports.From(); i <= ports.To(); i++ {
|
||||
ichConfig := this.config.Settings()
|
||||
ich, err := ichFactory.Create(this.point, ichConfig)
|
||||
ich, err := ichFactory.Create(this.space, ichConfig)
|
||||
if err != nil {
|
||||
log.Error("Failed to create inbound connection handler: %v", err)
|
||||
return err
|
||||
|
@ -5,6 +5,7 @@
|
||||
package point
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
"github.com/v2ray/v2ray-core/app/router"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
@ -22,6 +23,7 @@ type Point struct {
|
||||
idh []*InboundDetourHandler
|
||||
odh map[string]connhandler.OutboundConnectionHandler
|
||||
router router.Router
|
||||
space *app.Space
|
||||
}
|
||||
|
||||
// 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())
|
||||
}
|
||||
|
||||
vpoint.space = app.NewSpace()
|
||||
vpoint.space.Bind(vpoint)
|
||||
|
||||
ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
|
||||
if ichFactory == nil {
|
||||
log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
|
||||
return nil, config.BadConfiguration
|
||||
}
|
||||
ichConfig := pConfig.InboundConfig().Settings()
|
||||
ich, err := ichFactory.Create(vpoint, ichConfig)
|
||||
ich, err := ichFactory.Create(vpoint.space, ichConfig)
|
||||
if err != nil {
|
||||
log.Error("Failed to create inbound connection handler: %v", err)
|
||||
return nil, err
|
||||
@ -80,7 +85,7 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
|
||||
vpoint.idh = make([]*InboundDetourHandler, len(detours))
|
||||
for idx, detourConfig := range detours {
|
||||
detourHandler := &InboundDetourHandler{
|
||||
point: vpoint,
|
||||
space: vpoint.space,
|
||||
config: detourConfig,
|
||||
}
|
||||
err := detourHandler.Initialize()
|
||||
|
Loading…
Reference in New Issue
Block a user