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