mihomo/adapter/parser.go

140 lines
3.5 KiB
Go
Raw Permalink Normal View History

2021-06-10 14:05:56 +08:00
package adapter
2019-12-08 12:17:24 +08:00
import (
"fmt"
tlsC "github.com/Dreamacro/clash/component/tls"
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/adapter/outbound"
2019-12-08 12:17:24 +08:00
"github.com/Dreamacro/clash/common/structure"
C "github.com/Dreamacro/clash/constant"
)
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
2022-12-04 21:53:13 +08:00
decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true, KeyReplacer: structure.DefaultKeyReplacer})
2019-12-08 12:17:24 +08:00
proxyType, existType := mapping["type"].(string)
if !existType {
2020-08-25 22:19:59 +08:00
return nil, fmt.Errorf("missing type")
2019-12-08 12:17:24 +08:00
}
2020-08-25 22:19:59 +08:00
var (
proxy C.ProxyAdapter
err error
)
2019-12-08 12:17:24 +08:00
switch proxyType {
case "ss":
2023-03-14 17:45:37 +08:00
ssOption := &outbound.ShadowSocksOption{ClientFingerprint: tlsC.GetGlobalFingerprint()}
2019-12-08 12:17:24 +08:00
err = decoder.Decode(mapping, ssOption)
if err != nil {
break
}
2021-06-10 14:05:56 +08:00
proxy, err = outbound.NewShadowSocks(*ssOption)
case "ssr":
2021-06-10 14:05:56 +08:00
ssrOption := &outbound.ShadowSocksROption{}
err = decoder.Decode(mapping, ssrOption)
if err != nil {
break
}
2021-06-10 14:05:56 +08:00
proxy, err = outbound.NewShadowSocksR(*ssrOption)
2019-12-08 12:17:24 +08:00
case "socks5":
2021-06-10 14:05:56 +08:00
socksOption := &outbound.Socks5Option{}
2019-12-08 12:17:24 +08:00
err = decoder.Decode(mapping, socksOption)
if err != nil {
break
}
2022-07-11 13:42:28 +08:00
proxy, err = outbound.NewSocks5(*socksOption)
2019-12-08 12:17:24 +08:00
case "http":
2021-06-10 14:05:56 +08:00
httpOption := &outbound.HttpOption{}
2019-12-08 12:17:24 +08:00
err = decoder.Decode(mapping, httpOption)
if err != nil {
break
}
2022-07-11 13:42:28 +08:00
proxy, err = outbound.NewHttp(*httpOption)
2019-12-08 12:17:24 +08:00
case "vmess":
2021-06-10 14:05:56 +08:00
vmessOption := &outbound.VmessOption{
HTTPOpts: outbound.HTTPOptions{
Method: "GET",
Path: []string{"/"},
},
2023-03-14 17:45:37 +08:00
ClientFingerprint: tlsC.GetGlobalFingerprint(),
}
2019-12-08 12:17:24 +08:00
err = decoder.Decode(mapping, vmessOption)
if err != nil {
break
}
2021-06-10 14:05:56 +08:00
proxy, err = outbound.NewVmess(*vmessOption)
2021-11-17 16:03:47 +08:00
case "vless":
2023-03-14 17:45:37 +08:00
vlessOption := &outbound.VlessOption{ClientFingerprint: tlsC.GetGlobalFingerprint()}
2021-11-17 16:03:47 +08:00
err = decoder.Decode(mapping, vlessOption)
if err != nil {
break
}
proxy, err = outbound.NewVless(*vlessOption)
2019-12-08 12:17:24 +08:00
case "snell":
2021-06-10 14:05:56 +08:00
snellOption := &outbound.SnellOption{}
2019-12-08 12:17:24 +08:00
err = decoder.Decode(mapping, snellOption)
if err != nil {
break
}
2021-06-10 14:05:56 +08:00
proxy, err = outbound.NewSnell(*snellOption)
2020-03-19 20:26:53 +08:00
case "trojan":
2023-03-14 17:45:37 +08:00
trojanOption := &outbound.TrojanOption{ClientFingerprint: tlsC.GetGlobalFingerprint()}
2020-03-19 20:26:53 +08:00
err = decoder.Decode(mapping, trojanOption)
if err != nil {
break
}
2021-06-10 14:05:56 +08:00
proxy, err = outbound.NewTrojan(*trojanOption)
2022-06-07 13:38:45 +08:00
case "hysteria":
hyOption := &outbound.HysteriaOption{}
err = decoder.Decode(mapping, hyOption)
if err != nil {
break
}
proxy, err = outbound.NewHysteria(*hyOption)
2022-11-09 18:44:06 +08:00
case "wireguard":
2022-11-25 08:08:14 +08:00
wgOption := &outbound.WireGuardOption{}
err = decoder.Decode(mapping, wgOption)
if err != nil {
break
}
proxy, err = outbound.NewWireGuard(*wgOption)
case "tuic":
tuicOption := &outbound.TuicOption{}
err = decoder.Decode(mapping, tuicOption)
2022-11-09 18:44:06 +08:00
if err != nil {
break
}
2022-11-25 08:08:14 +08:00
proxy, err = outbound.NewTuic(*tuicOption)
2023-08-24 23:33:03 +08:00
case "direct":
directOption := &outbound.DirectOption{}
err = decoder.Decode(mapping, directOption)
if err != nil {
break
}
proxy = outbound.NewDirectWithOption(*directOption)
2019-12-08 12:17:24 +08:00
default:
2020-08-25 22:19:59 +08:00
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
2019-12-08 12:17:24 +08:00
}
if err != nil {
return nil, err
}
if muxMapping, muxExist := mapping["smux"].(map[string]any); muxExist {
muxOption := &outbound.SingMuxOption{}
err = decoder.Decode(muxMapping, muxOption)
if err != nil {
return nil, err
}
if muxOption.Enabled {
proxy, err = outbound.NewSingMux(*muxOption, proxy, proxy.(outbound.ProxyBase))
if err != nil {
return nil, err
}
}
}
2019-12-08 12:17:24 +08:00
return NewProxy(proxy), nil
}