mihomo/adapters/outbound/parser.go

86 lines
1.8 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outbound
import (
"fmt"
"github.com/Dreamacro/clash/common/structure"
C "github.com/Dreamacro/clash/constant"
)
func ParseProxy(mapping map[string]interface{}) (C.Proxy, error) {
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":
ssOption := &ShadowSocksOption{}
err = decoder.Decode(mapping, ssOption)
if err != nil {
break
}
proxy, err = NewShadowSocks(*ssOption)
case "ssr":
ssrOption := &ShadowSocksROption{}
err = decoder.Decode(mapping, ssrOption)
if err != nil {
break
}
proxy, err = NewShadowSocksR(*ssrOption)
2019-12-08 12:17:24 +08:00
case "socks5":
socksOption := &Socks5Option{}
err = decoder.Decode(mapping, socksOption)
if err != nil {
break
}
proxy = NewSocks5(*socksOption)
case "http":
httpOption := &HttpOption{}
err = decoder.Decode(mapping, httpOption)
if err != nil {
break
}
proxy = NewHttp(*httpOption)
case "vmess":
vmessOption := &VmessOption{
HTTPOpts: HTTPOptions{
Method: "GET",
Path: []string{"/"},
},
}
2019-12-08 12:17:24 +08:00
err = decoder.Decode(mapping, vmessOption)
if err != nil {
break
}
proxy, err = NewVmess(*vmessOption)
case "snell":
snellOption := &SnellOption{}
err = decoder.Decode(mapping, snellOption)
if err != nil {
break
}
proxy, err = NewSnell(*snellOption)
2020-03-19 20:26:53 +08:00
case "trojan":
trojanOption := &TrojanOption{}
err = decoder.Decode(mapping, trojanOption)
if err != nil {
break
}
proxy, err = 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
}