2021-06-10 14:05:56 +08:00
|
|
|
package adapter
|
2019-12-08 12:17:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-03-16 12:10:13 +08:00
|
|
|
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
2019-12-08 12:17:24 +08:00
|
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true})
|
|
|
|
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":
|
2021-06-10 14:05:56 +08:00
|
|
|
ssOption := &outbound.ShadowSocksOption{}
|
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)
|
2020-07-22 23:02:15 +08:00
|
|
|
case "ssr":
|
2021-06-10 14:05:56 +08:00
|
|
|
ssrOption := &outbound.ShadowSocksROption{}
|
2020-07-22 23:02:15 +08:00
|
|
|
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{
|
2020-03-31 16:07:21 +08:00
|
|
|
Method: "GET",
|
|
|
|
Path: []string{"/"},
|
|
|
|
},
|
|
|
|
}
|
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":
|
|
|
|
vlessOption := &outbound.VlessOption{}
|
|
|
|
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":
|
2021-06-10 14:05:56 +08:00
|
|
|
trojanOption := &outbound.TrojanOption{}
|
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)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewProxy(proxy), nil
|
|
|
|
}
|