1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 06:46:14 -04:00
v2fly/vpoint.go

109 lines
2.9 KiB
Go
Raw Normal View History

2015-09-05 11:48:38 -04:00
package core
import (
2015-09-12 05:51:42 -04:00
"io/ioutil"
2015-09-10 18:24:18 -04:00
2015-09-12 05:51:42 -04:00
"github.com/v2ray/v2ray-core/log"
2015-09-10 18:24:18 -04:00
v2net "github.com/v2ray/v2ray-core/net"
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
}
// VPoint is an single server in V2Ray system.
2015-09-05 11:48:38 -04:00
type VPoint struct {
2015-09-12 05:51:42 -04:00
port uint16
2015-09-09 18:50:21 -04:00
ichFactory InboundConnectionHandlerFactory
2015-09-12 05:51:42 -04:00
ichConfig []byte
2015-09-09 18:50:21 -04:00
ochFactory OutboundConnectionHandlerFactory
2015-09-12 05:51:42 -04:00
ochConfig []byte
2015-09-05 11:48:38 -04:00
}
2015-09-07 06:00:46 -04:00
// NewVPoint returns a new VPoint server based on given configuration.
// The server is not started at this point.
2015-09-12 05:51:42 -04:00
func NewVPoint(config VConfig) (*VPoint, error) {
2015-09-07 06:00:46 -04:00
var vpoint = new(VPoint)
2015-09-12 05:51:42 -04:00
vpoint.port = config.Port
ichFactory, ok := inboundFactories[config.InboundConfig.Protocol]
if !ok {
panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig.Protocol))
}
2015-09-11 08:12:26 -04:00
vpoint.ichFactory = ichFactory
2015-09-12 05:51:42 -04:00
if len(config.InboundConfig.File) > 0 {
ichConfig, err := ioutil.ReadFile(config.InboundConfig.File)
if err != nil {
panic(log.Error("Unable to read config file %v", err))
}
vpoint.ichConfig = ichConfig
}
ochFactory, ok := outboundFactories[config.OutboundConfig.Protocol]
if !ok {
panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig.Protocol))
}
2015-09-11 08:12:26 -04:00
vpoint.ochFactory = ochFactory
2015-09-12 05:51:42 -04:00
if len(config.OutboundConfig.File) > 0 {
ochConfig, err := ioutil.ReadFile(config.OutboundConfig.File)
if err != nil {
panic(log.Error("Unable to read config file %v", err))
}
vpoint.ochConfig = ochConfig
}
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-09-12 05:51:42 -04:00
Create(vp *VPoint, config []byte) (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-09-12 05:51:42 -04:00
Create(VP *VPoint, config []byte, dest v2net.VAddress) (OutboundConnectionHandler, error)
2015-09-09 18:50:21 -04:00
}
type OutboundConnectionHandler interface {
2015-09-10 18:24:18 -04:00
Start(vray OutboundVRay) error
2015-09-09 18:50:21 -04:00
}
2015-09-07 06:00:46 -04:00
// Start starts the VPoint server, and return any error during the process.
// In the case of any errors, the state of the server is unpredicatable.
2015-09-05 11:48:38 -04:00
func (vp *VPoint) Start() error {
2015-09-12 05:51:42 -04:00
if vp.port <= 0 {
return log.Error("Invalid port %d", vp.port)
2015-09-06 16:10:42 -04:00
}
2015-09-12 05:51:42 -04:00
inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
2015-09-09 18:50:21 -04:00
if err != nil {
return err
}
2015-09-12 05:51:42 -04:00
err = inboundConnectionHandler.Listen(vp.port)
2015-09-06 16:10:42 -04:00
return nil
}
2015-09-10 18:24:18 -04:00
func (vp *VPoint) NewInboundConnectionAccepted(destination v2net.VAddress) InboundVRay {
ray := NewVRay()
// TODO: handle error
2015-09-12 05:51:42 -04:00
och, _ := vp.ochFactory.Create(vp, vp.ochConfig, destination)
2015-09-10 18:24:18 -04:00
_ = och.Start(ray)
return ray
}