github.com/v2fly/v2ray-core/v4@v4.45.2/app/reverse/reverse.go (about) 1 //go:build !confonly 2 // +build !confonly 3 4 package reverse 5 6 //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen 7 8 import ( 9 "context" 10 11 core "github.com/v2fly/v2ray-core/v4" 12 "github.com/v2fly/v2ray-core/v4/common" 13 "github.com/v2fly/v2ray-core/v4/common/errors" 14 "github.com/v2fly/v2ray-core/v4/common/net" 15 "github.com/v2fly/v2ray-core/v4/features/outbound" 16 "github.com/v2fly/v2ray-core/v4/features/routing" 17 ) 18 19 const ( 20 internalDomain = "reverse.internal.v2fly.org" 21 ) 22 23 func isDomain(dest net.Destination, domain string) bool { 24 return dest.Address.Family().IsDomain() && dest.Address.Domain() == domain 25 } 26 27 func isInternalDomain(dest net.Destination) bool { 28 return isDomain(dest, internalDomain) 29 } 30 31 func init() { 32 common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { 33 r := new(Reverse) 34 if err := core.RequireFeatures(ctx, func(d routing.Dispatcher, om outbound.Manager) error { 35 return r.Init(config.(*Config), d, om) 36 }); err != nil { 37 return nil, err 38 } 39 return r, nil 40 })) 41 } 42 43 type Reverse struct { 44 bridges []*Bridge 45 portals []*Portal 46 } 47 48 func (r *Reverse) Init(config *Config, d routing.Dispatcher, ohm outbound.Manager) error { 49 for _, bConfig := range config.BridgeConfig { 50 b, err := NewBridge(bConfig, d) 51 if err != nil { 52 return err 53 } 54 r.bridges = append(r.bridges, b) 55 } 56 57 for _, pConfig := range config.PortalConfig { 58 p, err := NewPortal(pConfig, ohm) 59 if err != nil { 60 return err 61 } 62 r.portals = append(r.portals, p) 63 } 64 65 return nil 66 } 67 68 func (r *Reverse) Type() interface{} { 69 return (*Reverse)(nil) 70 } 71 72 func (r *Reverse) Start() error { 73 for _, b := range r.bridges { 74 if err := b.Start(); err != nil { 75 return err 76 } 77 } 78 79 for _, p := range r.portals { 80 if err := p.Start(); err != nil { 81 return err 82 } 83 } 84 85 return nil 86 } 87 88 func (r *Reverse) Close() error { 89 var errs []error 90 for _, b := range r.bridges { 91 errs = append(errs, b.Close()) 92 } 93 94 for _, p := range r.portals { 95 errs = append(errs, p.Close()) 96 } 97 98 return errors.Combine(errs...) 99 }