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

     1  package inbound
     2  
     3  import (
     4  	"encoding/json"
     5  	"net"
     6  	"net/netip"
     7  	"strconv"
     8  
     9  	"github.com/metacubex/mihomo/adapter/inbound"
    10  	C "github.com/metacubex/mihomo/constant"
    11  )
    12  
    13  type Base struct {
    14  	config       *BaseOption
    15  	name         string
    16  	specialRules string
    17  	listenAddr   netip.Addr
    18  	port         int
    19  }
    20  
    21  func NewBase(options *BaseOption) (*Base, error) {
    22  	if options.Listen == "" {
    23  		options.Listen = "0.0.0.0"
    24  	}
    25  	addr, err := netip.ParseAddr(options.Listen)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	return &Base{
    30  		name:         options.Name(),
    31  		listenAddr:   addr,
    32  		specialRules: options.SpecialRules,
    33  		port:         options.Port,
    34  		config:       options,
    35  	}, nil
    36  }
    37  
    38  // Config implements constant.InboundListener
    39  func (b *Base) Config() C.InboundConfig {
    40  	return b.config
    41  }
    42  
    43  // Address implements constant.InboundListener
    44  func (b *Base) Address() string {
    45  	return b.RawAddress()
    46  }
    47  
    48  // Close implements constant.InboundListener
    49  func (*Base) Close() error {
    50  	return nil
    51  }
    52  
    53  // Name implements constant.InboundListener
    54  func (b *Base) Name() string {
    55  	return b.name
    56  }
    57  
    58  // RawAddress implements constant.InboundListener
    59  func (b *Base) RawAddress() string {
    60  	return net.JoinHostPort(b.listenAddr.String(), strconv.Itoa(int(b.port)))
    61  }
    62  
    63  // Listen implements constant.InboundListener
    64  func (*Base) Listen(tunnel C.Tunnel) error {
    65  	return nil
    66  }
    67  
    68  func (b *Base) Additions() []inbound.Addition {
    69  	return b.config.Additions()
    70  }
    71  
    72  var _ C.InboundListener = (*Base)(nil)
    73  
    74  type BaseOption struct {
    75  	NameStr      string `inbound:"name"`
    76  	Listen       string `inbound:"listen,omitempty"`
    77  	Port         int    `inbound:"port,omitempty"`
    78  	SpecialRules string `inbound:"rule,omitempty"`
    79  	SpecialProxy string `inbound:"proxy,omitempty"`
    80  }
    81  
    82  func (o BaseOption) Name() string {
    83  	return o.NameStr
    84  }
    85  
    86  func (o BaseOption) Equal(config C.InboundConfig) bool {
    87  	return optionToString(o) == optionToString(config)
    88  }
    89  
    90  func (o BaseOption) Additions() []inbound.Addition {
    91  	return []inbound.Addition{
    92  		inbound.WithInName(o.NameStr),
    93  		inbound.WithSpecialRules(o.SpecialRules),
    94  		inbound.WithSpecialProxy(o.SpecialProxy),
    95  	}
    96  }
    97  
    98  var _ C.InboundConfig = (*BaseOption)(nil)
    99  
   100  func optionToString(option any) string {
   101  	str, _ := json.Marshal(option)
   102  	return string(str)
   103  }