clash/listener/parse.go

114 lines
2.9 KiB
Go
Raw Normal View History

2022-12-04 05:37:14 +00:00
package listener
import (
"fmt"
2023-11-03 13:01:45 +00:00
"github.com/metacubex/mihomo/common/structure"
C "github.com/metacubex/mihomo/constant"
IN "github.com/metacubex/mihomo/listener/inbound"
2022-12-04 05:37:14 +00:00
)
2022-12-04 13:53:13 +00: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 05:37:14 +00:00
proxyType, existType := mapping["type"].(string)
if !existType {
return nil, fmt.Errorf("missing type")
}
var (
2022-12-04 13:53:13 +00:00
listener C.InboundListener
2022-12-04 05:37:14 +00:00
err error
)
switch proxyType {
case "socks":
2022-12-04 13:53:13 +00:00
socksOption := &IN.SocksOption{UDP: true}
err = decoder.Decode(mapping, socksOption)
if err != nil {
return nil, err
}
2022-12-04 05:37:14 +00: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 05:37:14 +00:00
listener, err = IN.NewHTTP(httpOption)
case "tproxy":
2022-12-04 13:53:13 +00:00
tproxyOption := &IN.TProxyOption{UDP: true}
err = decoder.Decode(mapping, tproxyOption)
if err != nil {
return nil, err
}
2022-12-04 05:37:14 +00: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 05:37:14 +00:00
listener, err = IN.NewRedir(redirOption)
case "mixed":
2022-12-04 13:53:13 +00:00
mixedOption := &IN.MixedOption{UDP: true}
err = decoder.Decode(mapping, mixedOption)
if err != nil {
return nil, err
}
2022-12-04 05:37:14 +00:00
listener, err = IN.NewMixed(mixedOption)
2022-12-05 09:03:12 +00: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 09:43:50 +00: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 06:52:26 +00: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 15:05:13 +00:00
case "tuic":
tuicOption := &IN.TuicOption{
MaxIdleTime: 15000,
AuthenticationTimeout: 1000,
ALPN: []string{"h3"},
MaxUdpRelayPacketSize: 1500,
2023-01-16 17:07:28 +00:00
CongestionController: "bbr",
2022-12-04 15:05:13 +00:00
}
err = decoder.Decode(mapping, tuicOption)
if err != nil {
return nil, err
}
listener, err = IN.NewTuic(tuicOption)
2022-12-04 05:37:14 +00:00
default:
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
}
return listener, err
}