vpoint.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. UserSet *VUserSet
  10. ichFactory InboundConnectionHandlerFactory
  11. ochFactory OutboundConnectionHandlerFactory
  12. }
  13. // NewVPoint returns a new VPoint server based on given configuration.
  14. // The server is not started at this point.
  15. func NewVPoint(config *VConfig, ichFactory InboundConnectionHandlerFactory, ochFactory OutboundConnectionHandlerFactory) (*VPoint, error) {
  16. var vpoint = new(VPoint)
  17. vpoint.Config = *config
  18. vpoint.UserSet = NewVUserSet()
  19. for _, user := range vpoint.Config.AllowedClients {
  20. vpoint.UserSet.AddUser(user)
  21. }
  22. vpoint.ichFactory = ichFactory
  23. vpoint.ochFactory = ochFactory
  24. return vpoint, nil
  25. }
  26. type InboundConnectionHandlerFactory interface {
  27. Create(vPoint *VPoint) (InboundConnectionHandler, error)
  28. }
  29. type InboundConnectionHandler interface {
  30. Listen(port uint16) error
  31. }
  32. type OutboundConnectionHandlerFactory interface {
  33. Create(vPoint *VPoint, dest v2net.VAddress) (OutboundConnectionHandler, error)
  34. }
  35. type OutboundConnectionHandler interface {
  36. Start(vray OutboundVRay) error
  37. }
  38. // Start starts the VPoint server, and return any error during the process.
  39. // In the case of any errors, the state of the server is unpredicatable.
  40. func (vp *VPoint) Start() error {
  41. if vp.Config.Port <= 0 {
  42. return fmt.Errorf("Invalid port %d", vp.Config.Port)
  43. }
  44. inboundConnectionHandler, err := vp.ichFactory.Create(vp)
  45. if err != nil {
  46. return err
  47. }
  48. err = inboundConnectionHandler.Listen(vp.Config.Port)
  49. return nil
  50. }
  51. func (vp *VPoint) NewInboundConnectionAccepted(destination v2net.VAddress) InboundVRay {
  52. ray := NewVRay()
  53. // TODO: handle error
  54. och, _ := vp.ochFactory.Create(vp, destination)
  55. _ = och.Start(ray)
  56. return ray
  57. }