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

103 lines
2.9 KiB

9 years ago
package core
import (
"github.com/v2ray/v2ray-core/common/errors"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/config"
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
ich InboundConnectionHandler
och OutboundConnectionHandler
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(pConfig config.PointConfig) (*Point, error) {
var vpoint = new(Point)
vpoint.port = pConfig.Port()
ichFactory, ok := inboundFactories[pConfig.InboundConfig().Protocol()]
if !ok {
log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
return nil, errors.NewBadConfigurationError()
}
ichConfig := pConfig.InboundConfig().Settings(config.TypeInbound)
ich, err := ichFactory.Create(vpoint, ichConfig)
if err != nil {
log.Error("Failed to create inbound connection handler: %v", err)
return nil, err
}
vpoint.ich = ich
ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()]
if !ok {
log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
return nil, errors.NewBadConfigurationError()
}
ochConfig := pConfig.OutboundConfig().Settings(config.TypeOutbound)
och, err := ochFactory.Create(vpoint, ochConfig)
if err != nil {
log.Error("Failed to create outbound connection handler: %v", err)
return nil, err
}
vpoint.och = och
9 years ago
9 years ago
return vpoint, nil
9 years ago
}
9 years ago
type InboundConnectionHandlerFactory interface {
Create(vp *Point, config interface{}) (InboundConnectionHandler, error)
9 years ago
}
type InboundConnectionHandler interface {
9 years ago
Listen(port uint16) error
9 years ago
}
9 years ago
type OutboundConnectionHandlerFactory interface {
Create(VP *Point, config interface{}) (OutboundConnectionHandler, error)
9 years ago
}
type OutboundConnectionHandler interface {
Dispatch(firstPacket v2net.Packet, 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 {
log.Error("Invalid port %d", vp.port)
return errors.NewBadConfigurationError()
9 years ago
}
err := vp.ich.Listen(vp.port)
// TODO: handle error
return err
9 years ago
}
9 years ago
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
ray := NewRay()
go p.och.Dispatch(packet, ray)
9 years ago
return ray
}