1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-14 15:03:39 -04:00
v2fly/vpoint.go
2015-09-11 14:12:26 +02:00

71 lines
1.8 KiB
Go

package core
import (
"fmt"
v2net "github.com/v2ray/v2ray-core/net"
)
// VPoint is an single server in V2Ray system.
type VPoint struct {
Config VConfig
UserSet *VUserSet
ichFactory InboundConnectionHandlerFactory
ochFactory OutboundConnectionHandlerFactory
}
// NewVPoint returns a new VPoint server based on given configuration.
// The server is not started at this point.
func NewVPoint(config *VConfig, ichFactory InboundConnectionHandlerFactory, ochFactory OutboundConnectionHandlerFactory) (*VPoint, error) {
var vpoint = new(VPoint)
vpoint.Config = *config
vpoint.UserSet = NewVUserSet()
for _, user := range vpoint.Config.AllowedClients {
vpoint.UserSet.AddUser(user)
}
vpoint.ichFactory = ichFactory
vpoint.ochFactory = ochFactory
return vpoint, nil
}
type InboundConnectionHandlerFactory interface {
Create(vPoint *VPoint) (InboundConnectionHandler, error)
}
type InboundConnectionHandler interface {
Listen(port uint16) error
}
type OutboundConnectionHandlerFactory interface {
Create(vPoint *VPoint, dest v2net.VAddress) (OutboundConnectionHandler, error)
}
type OutboundConnectionHandler interface {
Start(vray OutboundVRay) error
}
// 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.
func (vp *VPoint) Start() error {
if vp.Config.Port <= 0 {
return fmt.Errorf("Invalid port %d", vp.Config.Port)
}
inboundConnectionHandler, err := vp.ichFactory.Create(vp)
if err != nil {
return err
}
err = inboundConnectionHandler.Listen(vp.Config.Port)
return nil
}
func (vp *VPoint) NewInboundConnectionAccepted(destination v2net.VAddress) InboundVRay {
ray := NewVRay()
// TODO: handle error
och, _ := vp.ochFactory.Create(vp, destination)
_ = och.Start(ray)
return ray
}