proxyman.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Package proxyman defines applications for manageing inbound and outbound proxies.
  2. package proxyman
  3. //go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg proxyman -path App,Proxyman
  4. import (
  5. "context"
  6. "v2ray.com/core/app"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/proxy"
  9. "v2ray.com/core/transport/ray"
  10. )
  11. type InboundHandlerManager interface {
  12. GetHandler(ctx context.Context, tag string) (InboundHandler, error)
  13. AddHandler(ctx context.Context, config *InboundHandlerConfig) error
  14. }
  15. type InboundHandler interface {
  16. Start() error
  17. Close()
  18. // For migration
  19. GetRandomInboundProxy() (proxy.Inbound, net.Port, int)
  20. }
  21. type OutboundHandlerManager interface {
  22. GetHandler(tag string) OutboundHandler
  23. GetDefaultHandler() OutboundHandler
  24. AddHandler(ctx context.Context, config *OutboundHandlerConfig) error
  25. }
  26. type OutboundHandler interface {
  27. Dispatch(ctx context.Context, outboundRay ray.OutboundRay)
  28. }
  29. func InboundHandlerManagerFromSpace(space app.Space) InboundHandlerManager {
  30. app := space.GetApplication((*InboundHandlerManager)(nil))
  31. if app == nil {
  32. return nil
  33. }
  34. return app.(InboundHandlerManager)
  35. }
  36. func OutboundHandlerManagerFromSpace(space app.Space) OutboundHandlerManager {
  37. app := space.GetApplication((*OutboundHandlerManager)(nil))
  38. if app == nil {
  39. return nil
  40. }
  41. return app.(OutboundHandlerManager)
  42. }
  43. type key int
  44. const (
  45. protocolsKey key = iota
  46. )
  47. func ContextWithProtocolSniffers(ctx context.Context, list []KnownProtocols) context.Context {
  48. return context.WithValue(ctx, protocolsKey, list)
  49. }
  50. func ProtocoSniffersFromContext(ctx context.Context) []KnownProtocols {
  51. if list, ok := ctx.Value(protocolsKey).([]KnownProtocols); ok {
  52. return list
  53. }
  54. return nil
  55. }