github.com/imannamdari/v2ray-core/v5@v5.0.5/app/reverse/reverse.go (about)

     1  package reverse
     2  
     3  //go:generate go run github.com/imannamdari/v2ray-core/v5/common/errors/errorgen
     4  
     5  import (
     6  	"context"
     7  
     8  	core "github.com/imannamdari/v2ray-core/v5"
     9  	"github.com/imannamdari/v2ray-core/v5/common"
    10  	"github.com/imannamdari/v2ray-core/v5/common/errors"
    11  	"github.com/imannamdari/v2ray-core/v5/common/net"
    12  	"github.com/imannamdari/v2ray-core/v5/features/outbound"
    13  	"github.com/imannamdari/v2ray-core/v5/features/routing"
    14  )
    15  
    16  const (
    17  	internalDomain = "reverse.internal.v2fly.org"
    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(ctx, 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(ctx context.Context, config *Config, d routing.Dispatcher, ohm outbound.Manager) error {
    46  	for _, bConfig := range config.BridgeConfig {
    47  		b, err := NewBridge(ctx, 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(ctx, 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  }