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

     1  package listener
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/metacubex/mihomo/common/structure"
     7  	C "github.com/metacubex/mihomo/constant"
     8  	IN "github.com/metacubex/mihomo/listener/inbound"
     9  )
    10  
    11  func ParseListener(mapping map[string]any) (C.InboundListener, error) {
    12  	decoder := structure.NewDecoder(structure.Option{TagName: "inbound", WeaklyTypedInput: true, KeyReplacer: structure.DefaultKeyReplacer})
    13  	proxyType, existType := mapping["type"].(string)
    14  	if !existType {
    15  		return nil, fmt.Errorf("missing type")
    16  	}
    17  
    18  	var (
    19  		listener C.InboundListener
    20  		err      error
    21  	)
    22  	switch proxyType {
    23  	case "socks":
    24  		socksOption := &IN.SocksOption{UDP: true}
    25  		err = decoder.Decode(mapping, socksOption)
    26  		if err != nil {
    27  			return nil, err
    28  		}
    29  		listener, err = IN.NewSocks(socksOption)
    30  	case "http":
    31  		httpOption := &IN.HTTPOption{}
    32  		err = decoder.Decode(mapping, httpOption)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		listener, err = IN.NewHTTP(httpOption)
    37  	case "tproxy":
    38  		tproxyOption := &IN.TProxyOption{UDP: true}
    39  		err = decoder.Decode(mapping, tproxyOption)
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  		listener, err = IN.NewTProxy(tproxyOption)
    44  	case "redir":
    45  		redirOption := &IN.RedirOption{}
    46  		err = decoder.Decode(mapping, redirOption)
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  		listener, err = IN.NewRedir(redirOption)
    51  	case "mixed":
    52  		mixedOption := &IN.MixedOption{UDP: true}
    53  		err = decoder.Decode(mapping, mixedOption)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  		listener, err = IN.NewMixed(mixedOption)
    58  	case "tunnel":
    59  		tunnelOption := &IN.TunnelOption{}
    60  		err = decoder.Decode(mapping, tunnelOption)
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  		listener, err = IN.NewTunnel(tunnelOption)
    65  	case "tun":
    66  		tunOption := &IN.TunOption{
    67  			Stack:     C.TunGvisor.String(),
    68  			DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query
    69  		}
    70  		err = decoder.Decode(mapping, tunOption)
    71  		if err != nil {
    72  			return nil, err
    73  		}
    74  		listener, err = IN.NewTun(tunOption)
    75  	case "shadowsocks":
    76  		shadowsocksOption := &IN.ShadowSocksOption{UDP: true}
    77  		err = decoder.Decode(mapping, shadowsocksOption)
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  		listener, err = IN.NewShadowSocks(shadowsocksOption)
    82  	case "vmess":
    83  		vmessOption := &IN.VmessOption{}
    84  		err = decoder.Decode(mapping, vmessOption)
    85  		if err != nil {
    86  			return nil, err
    87  		}
    88  		listener, err = IN.NewVmess(vmessOption)
    89  	case "hysteria2":
    90  		hysteria2Option := &IN.Hysteria2Option{}
    91  		err = decoder.Decode(mapping, hysteria2Option)
    92  		if err != nil {
    93  			return nil, err
    94  		}
    95  		listener, err = IN.NewHysteria2(hysteria2Option)
    96  	case "tuic":
    97  		tuicOption := &IN.TuicOption{
    98  			MaxIdleTime:           15000,
    99  			AuthenticationTimeout: 1000,
   100  			ALPN:                  []string{"h3"},
   101  			MaxUdpRelayPacketSize: 1500,
   102  			CongestionController:  "bbr",
   103  		}
   104  		err = decoder.Decode(mapping, tuicOption)
   105  		if err != nil {
   106  			return nil, err
   107  		}
   108  		listener, err = IN.NewTuic(tuicOption)
   109  	default:
   110  		return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
   111  	}
   112  	return listener, err
   113  }