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
|
|
|
|
}
|
2021-06-10 14:05:56 +08:00
|
|
|
proxy = 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
|
|
|
|
}
|
2021-06-10 14:05:56 +08:00
|
|
|
proxy = outbound.NewHttp(*httpOption)
|
2023-08-24 13:24:45 +08:00
|
|
|
case "vless":
|
|
|
|
fallthrough
|
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
|
|
|
|
}
|
2023-08-24 13:24:45 +08:00
|
|
|
if proxyType == "vless" {
|
|
|
|
proxy, err = outbound.NewVless(*vmessOption)
|
|
|
|
} else {
|
|
|
|
proxy, err = outbound.NewVmess(*vmessOption)
|
|
|
|
}
|
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)
|
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
|
|
|
|
}
|