github.com/igoogolx/clash@v1.19.8/adapter/parser.go (about)

     1  package adapter
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/igoogolx/clash/adapter/outbound"
     7  	"github.com/igoogolx/clash/common/structure"
     8  	C "github.com/igoogolx/clash/constant"
     9  )
    10  
    11  func ParseProxy(mapping map[string]any) (C.Proxy, error) {
    12  	decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true})
    13  	proxyType, existType := mapping["type"].(string)
    14  	if !existType {
    15  		return nil, fmt.Errorf("missing type")
    16  	}
    17  
    18  	var (
    19  		proxy C.ProxyAdapter
    20  		err   error
    21  	)
    22  	switch proxyType {
    23  	case "ss":
    24  		ssOption := &outbound.ShadowSocksOption{}
    25  		err = decoder.Decode(mapping, ssOption)
    26  		if err != nil {
    27  			break
    28  		}
    29  		proxy, err = outbound.NewShadowSocks(*ssOption)
    30  	case "ssr":
    31  		ssrOption := &outbound.ShadowSocksROption{}
    32  		err = decoder.Decode(mapping, ssrOption)
    33  		if err != nil {
    34  			break
    35  		}
    36  		proxy, err = outbound.NewShadowSocksR(*ssrOption)
    37  	case "socks5":
    38  		socksOption := &outbound.Socks5Option{}
    39  		err = decoder.Decode(mapping, socksOption)
    40  		if err != nil {
    41  			break
    42  		}
    43  		proxy = outbound.NewSocks5(*socksOption)
    44  	case "http":
    45  		httpOption := &outbound.HttpOption{}
    46  		err = decoder.Decode(mapping, httpOption)
    47  		if err != nil {
    48  			break
    49  		}
    50  		proxy = outbound.NewHttp(*httpOption)
    51  	case "vless":
    52  		fallthrough
    53  	case "vmess":
    54  		vmessOption := &outbound.VmessOption{
    55  			HTTPOpts: outbound.HTTPOptions{
    56  				Method: "GET",
    57  				Path:   []string{"/"},
    58  			},
    59  		}
    60  		err = decoder.Decode(mapping, vmessOption)
    61  		if err != nil {
    62  			break
    63  		}
    64  		if proxyType == "vless" {
    65  			proxy, err = outbound.NewVless(*vmessOption)
    66  		} else {
    67  			proxy, err = outbound.NewVmess(*vmessOption)
    68  		}
    69  	case "snell":
    70  		snellOption := &outbound.SnellOption{}
    71  		err = decoder.Decode(mapping, snellOption)
    72  		if err != nil {
    73  			break
    74  		}
    75  		proxy, err = outbound.NewSnell(*snellOption)
    76  	case "trojan":
    77  		trojanOption := &outbound.TrojanOption{}
    78  		err = decoder.Decode(mapping, trojanOption)
    79  		if err != nil {
    80  			break
    81  		}
    82  		proxy, err = outbound.NewTrojan(*trojanOption)
    83  	default:
    84  		return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
    85  	}
    86  
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	return NewProxy(proxy), nil
    92  }