mihomo/listener/parse.go

114 lines
2.9 KiB
Go
Raw Permalink Normal View History

2022-12-04 13:37:14 +08:00
package listener
import (
"fmt"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/common/structure"
C "github.com/metacubex/mihomo/constant"
IN "github.com/metacubex/mihomo/listener/inbound"
2022-12-04 13:37:14 +08:00
)
2022-12-04 21:53:13 +08:00
func ParseListener(mapping map[string]any) (C.InboundListener, error) {
decoder := structure.NewDecoder(structure.Option{TagName: "inbound", WeaklyTypedInput: true, KeyReplacer: structure.DefaultKeyReplacer})
2022-12-04 13:37:14 +08:00
proxyType, existType := mapping["type"].(string)
if !existType {
return nil, fmt.Errorf("missing type")
}
var (
2022-12-04 21:53:13 +08:00
listener C.InboundListener
2022-12-04 13:37:14 +08:00
err error
)
switch proxyType {
case "socks":
2022-12-04 21:53:13 +08:00
socksOption := &IN.SocksOption{UDP: true}
err = decoder.Decode(mapping, socksOption)
if err != nil {
return nil, err
}
2022-12-04 13:37:14 +08:00
listener, err = IN.NewSocks(socksOption)
case "http":
httpOption := &IN.HTTPOption{}
err = decoder.Decode(mapping, httpOption)
if err != nil {
return nil, err
}
2022-12-04 13:37:14 +08:00
listener, err = IN.NewHTTP(httpOption)
case "tproxy":
2022-12-04 21:53:13 +08:00
tproxyOption := &IN.TProxyOption{UDP: true}
err = decoder.Decode(mapping, tproxyOption)
if err != nil {
return nil, err
}
2022-12-04 13:37:14 +08:00
listener, err = IN.NewTProxy(tproxyOption)
case "redir":
redirOption := &IN.RedirOption{}
err = decoder.Decode(mapping, redirOption)
if err != nil {
return nil, err
}
2022-12-04 13:37:14 +08:00
listener, err = IN.NewRedir(redirOption)
case "mixed":
2022-12-04 21:53:13 +08:00
mixedOption := &IN.MixedOption{UDP: true}
err = decoder.Decode(mapping, mixedOption)
if err != nil {
return nil, err
}
2022-12-04 13:37:14 +08:00
listener, err = IN.NewMixed(mixedOption)
2022-12-05 17:03:12 +08:00
case "tunnel":
tunnelOption := &IN.TunnelOption{}
err = decoder.Decode(mapping, tunnelOption)
if err != nil {
return nil, err
}
listener, err = IN.NewTunnel(tunnelOption)
2022-12-05 17:43:50 +08:00
case "tun":
tunOption := &IN.TunOption{
Stack: C.TunGvisor.String(),
DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query
}
err = decoder.Decode(mapping, tunOption)
if err != nil {
return nil, err
}
listener, err = IN.NewTun(tunOption)
case "shadowsocks":
shadowsocksOption := &IN.ShadowSocksOption{UDP: true}
err = decoder.Decode(mapping, shadowsocksOption)
if err != nil {
return nil, err
}
listener, err = IN.NewShadowSocks(shadowsocksOption)
case "vmess":
vmessOption := &IN.VmessOption{}
err = decoder.Decode(mapping, vmessOption)
if err != nil {
return nil, err
}
listener, err = IN.NewVmess(vmessOption)
2023-09-21 14:52:26 +08:00
case "hysteria2":
hysteria2Option := &IN.Hysteria2Option{}
err = decoder.Decode(mapping, hysteria2Option)
if err != nil {
return nil, err
}
listener, err = IN.NewHysteria2(hysteria2Option)
2022-12-04 23:05:13 +08:00
case "tuic":
tuicOption := &IN.TuicOption{
MaxIdleTime: 15000,
AuthenticationTimeout: 1000,
ALPN: []string{"h3"},
MaxUdpRelayPacketSize: 1500,
2023-01-17 01:07:28 +08:00
CongestionController: "bbr",
2022-12-04 23:05:13 +08:00
}
err = decoder.Decode(mapping, tuicOption)
if err != nil {
return nil, err
}
listener, err = IN.NewTuic(tuicOption)
2022-12-04 13:37:14 +08:00
default:
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
}
return listener, err
}