github.com/metacubex/mihomo@v1.18.5/listener/inbound/redir.go (about)

     1  package inbound
     2  
     3  import (
     4  	C "github.com/metacubex/mihomo/constant"
     5  	"github.com/metacubex/mihomo/listener/redir"
     6  	"github.com/metacubex/mihomo/log"
     7  )
     8  
     9  type RedirOption struct {
    10  	BaseOption
    11  }
    12  
    13  func (o RedirOption) Equal(config C.InboundConfig) bool {
    14  	return optionToString(o) == optionToString(config)
    15  }
    16  
    17  type Redir struct {
    18  	*Base
    19  	config *RedirOption
    20  	l      *redir.Listener
    21  }
    22  
    23  func NewRedir(options *RedirOption) (*Redir, error) {
    24  	base, err := NewBase(&options.BaseOption)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return &Redir{
    29  		Base:   base,
    30  		config: options,
    31  	}, nil
    32  }
    33  
    34  // Config implements constant.InboundListener
    35  func (r *Redir) Config() C.InboundConfig {
    36  	return r.config
    37  }
    38  
    39  // Address implements constant.InboundListener
    40  func (r *Redir) Address() string {
    41  	return r.l.Address()
    42  }
    43  
    44  // Listen implements constant.InboundListener
    45  func (r *Redir) Listen(tunnel C.Tunnel) error {
    46  	var err error
    47  	r.l, err = redir.New(r.RawAddress(), tunnel, r.Additions()...)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	log.Infoln("Redir[%s] proxy listening at: %s", r.Name(), r.Address())
    52  	return nil
    53  }
    54  
    55  // Close implements constant.InboundListener
    56  func (r *Redir) Close() error {
    57  	if r.l != nil {
    58  		r.l.Close()
    59  	}
    60  	return nil
    61  }
    62  
    63  var _ C.InboundListener = (*Redir)(nil)