github.com/metacubex/mihomo@v1.18.5/listener/inbound/mixed.go (about) 1 package inbound 2 3 import ( 4 "fmt" 5 6 C "github.com/metacubex/mihomo/constant" 7 "github.com/metacubex/mihomo/log" 8 9 "github.com/metacubex/mihomo/listener/mixed" 10 "github.com/metacubex/mihomo/listener/socks" 11 ) 12 13 type MixedOption struct { 14 BaseOption 15 UDP bool `inbound:"udp,omitempty"` 16 } 17 18 func (o MixedOption) Equal(config C.InboundConfig) bool { 19 return optionToString(o) == optionToString(config) 20 } 21 22 type Mixed struct { 23 *Base 24 config *MixedOption 25 l *mixed.Listener 26 lUDP *socks.UDPListener 27 udp bool 28 } 29 30 func NewMixed(options *MixedOption) (*Mixed, error) { 31 base, err := NewBase(&options.BaseOption) 32 if err != nil { 33 return nil, err 34 } 35 return &Mixed{ 36 Base: base, 37 config: options, 38 udp: options.UDP, 39 }, nil 40 } 41 42 // Config implements constant.InboundListener 43 func (m *Mixed) Config() C.InboundConfig { 44 return m.config 45 } 46 47 // Address implements constant.InboundListener 48 func (m *Mixed) Address() string { 49 return m.l.Address() 50 } 51 52 // Listen implements constant.InboundListener 53 func (m *Mixed) Listen(tunnel C.Tunnel) error { 54 var err error 55 m.l, err = mixed.New(m.RawAddress(), tunnel, m.Additions()...) 56 if err != nil { 57 return err 58 } 59 if m.udp { 60 m.lUDP, err = socks.NewUDP(m.RawAddress(), tunnel, m.Additions()...) 61 if err != nil { 62 return err 63 } 64 } 65 log.Infoln("Mixed(http+socks)[%s] proxy listening at: %s", m.Name(), m.Address()) 66 return nil 67 } 68 69 // Close implements constant.InboundListener 70 func (m *Mixed) Close() error { 71 var err error 72 if m.l != nil { 73 if tcpErr := m.l.Close(); tcpErr != nil { 74 err = tcpErr 75 } 76 } 77 if m.udp && m.lUDP != nil { 78 if udpErr := m.lUDP.Close(); udpErr != nil { 79 if err == nil { 80 err = udpErr 81 } else { 82 return fmt.Errorf("close tcp err: %s, close udp err: %s", err.Error(), udpErr.Error()) 83 } 84 } 85 } 86 return err 87 } 88 89 var _ C.InboundListener = (*Mixed)(nil)