vpoint.go 1.6 KB

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