You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/point.go

99 lines
2.6 KiB

9 years ago
package core
import (
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
9 years ago
)
var (
inboundFactories = make(map[string]InboundConnectionHandlerFactory)
outboundFactories = make(map[string]OutboundConnectionHandlerFactory)
)
func RegisterInboundConnectionHandlerFactory(name string, factory InboundConnectionHandlerFactory) error {
// TODO check name
inboundFactories[name] = factory
return nil
}
func RegisterOutboundConnectionHandlerFactory(name string, factory OutboundConnectionHandlerFactory) error {
// TODO check name
outboundFactories[name] = factory
return nil
}
// Point is an single server in V2Ray system.
type Point struct {
port uint16
9 years ago
ichFactory InboundConnectionHandlerFactory
ichConfig []byte
9 years ago
ochFactory OutboundConnectionHandlerFactory
ochConfig []byte
9 years ago
}
// NewPoint returns a new Point server based on given configuration.
9 years ago
// The server is not started at this point.
func NewPoint(config PointConfig) (*Point, error) {
var vpoint = new(Point)
vpoint.port = config.Port()
ichFactory, ok := inboundFactories[config.InboundConfig().Protocol()]
if !ok {
panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig().Protocol()))
}
9 years ago
vpoint.ichFactory = ichFactory
vpoint.ichConfig = config.InboundConfig().Content()
ochFactory, ok := outboundFactories[config.OutboundConfig().Protocol()]
if !ok {
panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig().Protocol))
}
9 years ago
vpoint.ochFactory = ochFactory
vpoint.ochConfig = config.OutboundConfig().Content()
9 years ago
9 years ago
return vpoint, nil
9 years ago
}
9 years ago
type InboundConnectionHandlerFactory interface {
Create(vp *Point, config []byte) (InboundConnectionHandler, error)
9 years ago
}
type InboundConnectionHandler interface {
9 years ago
Listen(port uint16) error
9 years ago
}
9 years ago
type OutboundConnectionHandlerFactory interface {
Initialize(config []byte) error
Create(VP *Point, firstPacket v2net.Packet) (OutboundConnectionHandler, error)
9 years ago
}
type OutboundConnectionHandler interface {
Start(ray OutboundRay) error
9 years ago
}
// Start starts the Point server, and return any error during the process.
9 years ago
// In the case of any errors, the state of the server is unpredicatable.
func (vp *Point) Start() error {
if vp.port <= 0 {
return log.Error("Invalid port %d", vp.port)
9 years ago
}
vp.ochFactory.Initialize(vp.ochConfig)
inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
9 years ago
if err != nil {
return err
}
err = inboundConnectionHandler.Listen(vp.port)
9 years ago
return nil
}
9 years ago
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
ray := NewRay()
9 years ago
// TODO: handle error
och, _ := p.ochFactory.Create(p, packet)
9 years ago
_ = och.Start(ray)
return ray
}