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