session.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Package session provides functions for sessions of incoming requests.
  2. package session // import "v2ray.com/core/common/session"
  3. import (
  4. "context"
  5. "math/rand"
  6. "v2ray.com/core/common/errors"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/protocol"
  9. )
  10. // ID of a session.
  11. type ID uint32
  12. // NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
  13. // The generated ID will never be 0.
  14. func NewID() ID {
  15. for {
  16. id := ID(rand.Uint32())
  17. if id != 0 {
  18. return id
  19. }
  20. }
  21. }
  22. func ExportIDToError(ctx context.Context) errors.ExportOption {
  23. id := IDFromContext(ctx)
  24. return func(h *errors.ExportOptionHolder) {
  25. h.SessionID = uint32(id)
  26. }
  27. }
  28. type Inbound struct {
  29. Source net.Destination
  30. Gateway net.Destination
  31. Tag string
  32. // User is the user that authencates for the inbound. May be nil if the protocol allows anounymous traffic.
  33. User *protocol.MemoryUser
  34. }
  35. type Outbound struct {
  36. Target net.Destination
  37. Gateway net.Address
  38. ResolvedIPs []net.IP
  39. }