2015-09-05 15:48:38 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2015-09-19 22:50:21 +00:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 21:54:36 +00:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-09-05 15:48:38 +00:00
|
|
|
)
|
|
|
|
|
2015-09-12 09:51:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-12 20:11:54 +00:00
|
|
|
// Point is an single server in V2Ray system.
|
|
|
|
type Point struct {
|
2015-09-12 09:51:42 +00:00
|
|
|
port uint16
|
2015-09-09 22:50:21 +00:00
|
|
|
ichFactory InboundConnectionHandlerFactory
|
2015-09-12 09:51:42 +00:00
|
|
|
ichConfig []byte
|
2015-09-09 22:50:21 +00:00
|
|
|
ochFactory OutboundConnectionHandlerFactory
|
2015-09-12 09:51:42 +00:00
|
|
|
ochConfig []byte
|
2015-09-05 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 20:11:54 +00:00
|
|
|
// NewPoint returns a new Point server based on given configuration.
|
2015-09-07 10:00:46 +00:00
|
|
|
// The server is not started at this point.
|
2015-09-19 13:07:10 +00:00
|
|
|
func NewPoint(config PointConfig) (*Point, error) {
|
2015-09-12 20:11:54 +00:00
|
|
|
var vpoint = new(Point)
|
2015-09-19 13:07:10 +00:00
|
|
|
vpoint.port = config.Port()
|
2015-09-12 09:51:42 +00:00
|
|
|
|
2015-09-19 13:07:10 +00:00
|
|
|
ichFactory, ok := inboundFactories[config.InboundConfig().Protocol()]
|
2015-09-12 09:51:42 +00:00
|
|
|
if !ok {
|
2015-09-19 13:07:10 +00:00
|
|
|
panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig().Protocol()))
|
2015-09-12 09:51:42 +00:00
|
|
|
}
|
2015-09-11 12:12:26 +00:00
|
|
|
vpoint.ichFactory = ichFactory
|
2015-09-19 13:35:20 +00:00
|
|
|
vpoint.ichConfig = config.InboundConfig().Content()
|
2015-09-12 09:51:42 +00:00
|
|
|
|
2015-09-19 13:07:10 +00:00
|
|
|
ochFactory, ok := outboundFactories[config.OutboundConfig().Protocol()]
|
2015-09-12 09:51:42 +00:00
|
|
|
if !ok {
|
2015-09-19 13:07:10 +00:00
|
|
|
panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig().Protocol))
|
2015-09-12 09:51:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 12:12:26 +00:00
|
|
|
vpoint.ochFactory = ochFactory
|
2015-09-19 13:35:20 +00:00
|
|
|
vpoint.ochConfig = config.OutboundConfig().Content()
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2015-09-06 20:10:42 +00:00
|
|
|
return vpoint, nil
|
2015-09-05 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 22:50:21 +00:00
|
|
|
type InboundConnectionHandlerFactory interface {
|
2015-09-12 20:11:54 +00:00
|
|
|
Create(vp *Point, config []byte) (InboundConnectionHandler, error)
|
2015-09-09 22:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type InboundConnectionHandler interface {
|
2015-09-06 20:10:42 +00:00
|
|
|
Listen(port uint16) error
|
2015-09-05 15:48:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 22:50:21 +00:00
|
|
|
type OutboundConnectionHandlerFactory interface {
|
2015-09-22 16:11:55 +00:00
|
|
|
Initialize(config []byte) error
|
|
|
|
Create(VP *Point, firstPacket v2net.Packet) (OutboundConnectionHandler, error)
|
2015-09-09 22:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type OutboundConnectionHandler interface {
|
2015-09-12 20:11:54 +00:00
|
|
|
Start(ray OutboundRay) error
|
2015-09-09 22:50:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 20:11:54 +00:00
|
|
|
// Start starts the Point server, and return any error during the process.
|
2015-09-07 10:00:46 +00:00
|
|
|
// In the case of any errors, the state of the server is unpredicatable.
|
2015-09-12 20:11:54 +00:00
|
|
|
func (vp *Point) Start() error {
|
2015-09-12 09:51:42 +00:00
|
|
|
if vp.port <= 0 {
|
|
|
|
return log.Error("Invalid port %d", vp.port)
|
2015-09-06 20:10:42 +00:00
|
|
|
}
|
2015-09-22 16:11:55 +00:00
|
|
|
|
|
|
|
vp.ochFactory.Initialize(vp.ochConfig)
|
|
|
|
|
2015-09-12 09:51:42 +00:00
|
|
|
inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
|
2015-09-09 22:50:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-12 09:51:42 +00:00
|
|
|
err = inboundConnectionHandler.Listen(vp.port)
|
2015-09-06 20:10:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2015-09-22 12:45:03 +00:00
|
|
|
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
|
2015-09-12 20:11:54 +00:00
|
|
|
ray := NewRay()
|
2015-09-10 22:24:18 +00:00
|
|
|
// TODO: handle error
|
2015-09-22 16:11:55 +00:00
|
|
|
och, _ := p.ochFactory.Create(p, packet)
|
2015-09-10 22:24:18 +00:00
|
|
|
_ = och.Start(ray)
|
|
|
|
return ray
|
|
|
|
}
|