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

     1  package inbound
     2  
     3  import (
     4  	C "github.com/metacubex/mihomo/constant"
     5  	LC "github.com/metacubex/mihomo/listener/config"
     6  	"github.com/metacubex/mihomo/listener/sing_shadowsocks"
     7  	"github.com/metacubex/mihomo/log"
     8  )
     9  
    10  type ShadowSocksOption struct {
    11  	BaseOption
    12  	Password  string    `inbound:"password"`
    13  	Cipher    string    `inbound:"cipher"`
    14  	UDP       bool      `inbound:"udp,omitempty"`
    15  	MuxOption MuxOption `inbound:"mux-option,omitempty"`
    16  }
    17  
    18  func (o ShadowSocksOption) Equal(config C.InboundConfig) bool {
    19  	return optionToString(o) == optionToString(config)
    20  }
    21  
    22  type ShadowSocks struct {
    23  	*Base
    24  	config *ShadowSocksOption
    25  	l      C.MultiAddrListener
    26  	ss     LC.ShadowsocksServer
    27  }
    28  
    29  func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
    30  	base, err := NewBase(&options.BaseOption)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return &ShadowSocks{
    35  		Base:   base,
    36  		config: options,
    37  		ss: LC.ShadowsocksServer{
    38  			Enable:    true,
    39  			Listen:    base.RawAddress(),
    40  			Password:  options.Password,
    41  			Cipher:    options.Cipher,
    42  			Udp:       options.UDP,
    43  			MuxOption: options.MuxOption.Build(),
    44  		},
    45  	}, nil
    46  }
    47  
    48  // Config implements constant.InboundListener
    49  func (s *ShadowSocks) Config() C.InboundConfig {
    50  	return s.config
    51  }
    52  
    53  // Address implements constant.InboundListener
    54  func (s *ShadowSocks) Address() string {
    55  	if s.l != nil {
    56  		for _, addr := range s.l.AddrList() {
    57  			return addr.String()
    58  		}
    59  	}
    60  	return ""
    61  }
    62  
    63  // Listen implements constant.InboundListener
    64  func (s *ShadowSocks) Listen(tunnel C.Tunnel) error {
    65  	var err error
    66  	s.l, err = sing_shadowsocks.New(s.ss, tunnel, s.Additions()...)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	log.Infoln("ShadowSocks[%s] proxy listening at: %s", s.Name(), s.Address())
    71  	return nil
    72  }
    73  
    74  // Close implements constant.InboundListener
    75  func (s *ShadowSocks) Close() error {
    76  	return s.l.Close()
    77  }
    78  
    79  var _ C.InboundListener = (*ShadowSocks)(nil)