github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/app/reverse/reverse.go (about)

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