2015-09-05 11:48:38 -04:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2015-09-06 16:10:42 -04:00
|
|
|
"fmt"
|
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-08 07:18:55 -04:00
|
|
|
// VPoint is an single server in V2Ray system.
|
2015-09-05 11:48:38 -04:00
|
|
|
type VPoint struct {
|
2015-09-10 18:24:18 -04:00
|
|
|
Config VConfig
|
2015-09-09 18:50:21 -04:00
|
|
|
ichFactory InboundConnectionHandlerFactory
|
|
|
|
ochFactory OutboundConnectionHandlerFactory
|
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-11 08:12:09 -04:00
|
|
|
func NewVPoint(config *VConfig, ichFactory InboundConnectionHandlerFactory, ochFactory OutboundConnectionHandlerFactory) (*VPoint, error) {
|
2015-09-07 06:00:46 -04:00
|
|
|
var vpoint = new(VPoint)
|
2015-09-10 18:24:18 -04:00
|
|
|
vpoint.Config = *config
|
2015-09-11 08:12:26 -04:00
|
|
|
vpoint.ichFactory = ichFactory
|
|
|
|
vpoint.ochFactory = ochFactory
|
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 {
|
|
|
|
Create(vPoint *VPoint) (InboundConnectionHandler, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 18:24:18 -04:00
|
|
|
Create(vPoint *VPoint, 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-10 18:24:18 -04:00
|
|
|
if vp.Config.Port <= 0 {
|
|
|
|
return fmt.Errorf("Invalid port %d", vp.Config.Port)
|
2015-09-06 16:10:42 -04:00
|
|
|
}
|
2015-09-09 18:50:21 -04:00
|
|
|
inboundConnectionHandler, err := vp.ichFactory.Create(vp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
err = inboundConnectionHandler.Listen(vp.Config.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
|
|
|
|
och, _ := vp.ochFactory.Create(vp, destination)
|
|
|
|
_ = och.Start(ray)
|
|
|
|
return ray
|
|
|
|
}
|