github.com/chwjbn/xclash@v0.2.0/adapter/parser.go (about)

     1  package adapter
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chwjbn/xclash/adapter/outbound"
     7  	"github.com/chwjbn/xclash/common/structure"
     8  	C "github.com/chwjbn/xclash/constant"
     9  )
    10  
    11  func ParseProxy(mapping map[string]interface{}) (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 "vmess":
    52  		vmessOption := &outbound.VmessOption{
    53  			HTTPOpts: outbound.HTTPOptions{
    54  				Method: "GET",
    55  				Path:   []string{"/"},
    56  			},
    57  		}
    58  		err = decoder.Decode(mapping, vmessOption)
    59  		if err != nil {
    60  			break
    61  		}
    62  		proxy, err = outbound.NewVmess(*vmessOption)
    63  	case "snell":
    64  		snellOption := &outbound.SnellOption{}
    65  		err = decoder.Decode(mapping, snellOption)
    66  		if err != nil {
    67  			break
    68  		}
    69  		proxy, err = outbound.NewSnell(*snellOption)
    70  	case "trojan":
    71  		trojanOption := &outbound.TrojanOption{}
    72  		err = decoder.Decode(mapping, trojanOption)
    73  		if err != nil {
    74  			break
    75  		}
    76  		proxy, err = outbound.NewTrojan(*trojanOption)
    77  	default:
    78  		return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
    79  	}
    80  
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return NewProxy(proxy), nil
    86  }