vpoint.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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) (*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. return vpoint, nil
  23. }
  24. type InboundConnectionHandlerFactory interface {
  25. Create(vPoint *VPoint) (InboundConnectionHandler, error)
  26. }
  27. type InboundConnectionHandler interface {
  28. Listen(port uint16) error
  29. }
  30. type OutboundConnectionHandlerFactory interface {
  31. Create(vPoint *VPoint, dest v2net.VAddress) (OutboundConnectionHandler, error)
  32. }
  33. type OutboundConnectionHandler interface {
  34. Start(vray OutboundVRay) error
  35. }
  36. // Start starts the VPoint server, and return any error during the process.
  37. // In the case of any errors, the state of the server is unpredicatable.
  38. func (vp *VPoint) Start() error {
  39. if vp.Config.Port <= 0 {
  40. return fmt.Errorf("Invalid port %d", vp.Config.Port)
  41. }
  42. inboundConnectionHandler, err := vp.ichFactory.Create(vp)
  43. if err != nil {
  44. return err
  45. }
  46. err = inboundConnectionHandler.Listen(vp.Config.Port)
  47. return nil
  48. }
  49. func (vp *VPoint) NewInboundConnectionAccepted(destination v2net.VAddress) InboundVRay {
  50. /*
  51. vNextLen := len(vp.Config.VNextList)
  52. if vNextLen > 0 {
  53. vNextIndex := rand.Intn(vNextLen)
  54. vNext := vp.Config.VNextList[vNextIndex]
  55. vNextUser := dest.User
  56. vNextUserLen := len(vNext.Users)
  57. if vNextUserLen > 0 {
  58. vNextUserIndex = rand.Intn(vNextUserLen)
  59. vNextUser = vNext.Users[vNextUserIndex]
  60. }
  61. newDest := VDestination{"tcp", vNext.ServerAddress, vNextUser}
  62. dest = newDest
  63. }*/
  64. ray := NewVRay()
  65. // TODO: handle error
  66. och, _ := vp.ochFactory.Create(vp, destination)
  67. _ = och.Start(ray)
  68. return ray
  69. }