vpoint.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package core
  2. import (
  3. "fmt"
  4. )
  5. // VPoint is an single server in V2Ray system.
  6. type VPoint struct {
  7. config VConfig
  8. ichFactory InboundConnectionHandlerFactory
  9. ochFactory OutboundConnectionHandlerFactory
  10. }
  11. // NewVPoint returns a new VPoint server based on given configuration.
  12. // The server is not started at this point.
  13. func NewVPoint(config *VConfig) (*VPoint, error) {
  14. var vpoint = new(VPoint)
  15. vpoint.config = *config
  16. return vpoint, nil
  17. }
  18. type InboundConnectionHandlerFactory interface {
  19. Create(vPoint *VPoint) (InboundConnectionHandler, error)
  20. }
  21. type InboundConnectionHandler interface {
  22. Listen(port uint16) error
  23. }
  24. type OutboundConnectionHandlerFactory interface {
  25. Create(vPoint *VPoint) (OutboundConnectionHandler, error)
  26. }
  27. type OutboundConnectionHandler interface {
  28. Start(vray *OutboundVRay) error
  29. }
  30. // Start starts the VPoint server, and return any error during the process.
  31. // In the case of any errors, the state of the server is unpredicatable.
  32. func (vp *VPoint) Start() error {
  33. if vp.config.Port <= 0 {
  34. return fmt.Errorf("Invalid port %d", vp.config.Port)
  35. }
  36. inboundConnectionHandler, err := vp.ichFactory.Create(vp)
  37. if err != nil {
  38. return err
  39. }
  40. err = inboundConnectionHandler.Listen(vp.config.Port)
  41. return nil
  42. }