clash/listener/parse.go

75 lines
1.9 KiB
Go
Raw Normal View History

2022-12-04 05:37:14 +00:00
package listener
import (
"fmt"
"github.com/Dreamacro/clash/common/structure"
C "github.com/Dreamacro/clash/constant"
IN "github.com/Dreamacro/clash/listener/inbound"
)
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-04 15:05:13 +00:00
case "tuic":
tuicOption := &IN.TuicOption{
MaxIdleTime: 15000,
AuthenticationTimeout: 1000,
ALPN: []string{"h3"},
MaxUdpRelayPacketSize: 1500,
}
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
}