session.go 861 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. )
  9. // ID of a session.
  10. type ID uint32
  11. // NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
  12. // The generated ID will never be 0.
  13. func NewID() ID {
  14. for {
  15. id := ID(rand.Uint32())
  16. if id != 0 {
  17. return id
  18. }
  19. }
  20. }
  21. func ExportIDToError(ctx context.Context) errors.ExportOption {
  22. id := IDFromContext(ctx)
  23. return func(h *errors.ExportOptionHolder) {
  24. h.SessionID = uint32(id)
  25. }
  26. }
  27. type Inbound struct {
  28. Source net.Destination
  29. Gateway net.Destination
  30. Tag string
  31. }
  32. type Outbound struct {
  33. Target net.Destination
  34. Gateway net.Address
  35. ResolvedIPs []net.IP
  36. }