mirror of
				https://github.com/bestnite/sub2clash.git
				synced 2025-11-04 04:40:36 +00:00 
			
		
		
		
	Compare commits
	
		
			17 Commits
		
	
	
		
			v0.0.12-be
			...
			v1.0.1
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f5686561f9 | |||
| 83c24170a0 | |||
| 80d91efca4 | |||
| 83a728a415 | |||
| a178d06248 | |||
| 
						 | 
					99d36d93d8 | ||
| 0a9892503d | |||
| ff81d03492 | |||
| 0fa95888cb | |||
| b44703fa0f | |||
| b256c5e809 | |||
| 307c36aa8d | |||
| 06da2e91c2 | |||
| 33d37e631b | |||
| c1012750ff | |||
| 69deed91df | |||
| 6a780a5e27 | 
							
								
								
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -6,5 +6,6 @@ logs
 | 
				
			|||||||
data
 | 
					data
 | 
				
			||||||
.env
 | 
					.env
 | 
				
			||||||
.vscode/settings.json
 | 
					.vscode/settings.json
 | 
				
			||||||
test
 | 
					config.yaml
 | 
				
			||||||
*test.go
 | 
					config.yml
 | 
				
			||||||
 | 
					config.json
 | 
				
			||||||
							
								
								
									
										41
									
								
								API.md
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								API.md
									
									
									
									
									
								
							@@ -1,21 +1,48 @@
 | 
				
			|||||||
# `GET /clash`, `GET /meta`
 | 
					# `GET /convert/:config`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
获取 Clash/Clash.Meta 配置链接
 | 
					获取 Clash/Clash.Meta 配置链接
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					| Path 参数 | 类型   | 说明                                           |
 | 
				
			||||||
 | 
					| --------- | ------ | ---------------------------------------------- |
 | 
				
			||||||
 | 
					| config    | string | Base64 URL Safe 编码后的 JSON 字符串,格式如下 |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## `config` JSON 结构
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| Query 参数         | 类型              | 是否必须                 | 默认值    | 说明                                                                              |
 | 
					| Query 参数         | 类型              | 是否必须                 | 默认值    | 说明                                                                              |
 | 
				
			||||||
| ------------ | ------ | ------------------------ | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
 | 
					| ------------------ | ----------------- | ------------------------ | --------- | --------------------------------------------------------------------------------- |
 | 
				
			||||||
| sub          | string | sub/proxy 至少有一项存在 | -         | 订阅链接,可以在链接结尾加上`#名称`,来给订阅中的节点加上统一前缀(可以输入多个,用 `,` 分隔)                                                                                                                                            |
 | 
					| clashType          | int               | 是                       | 1         | 配置文件类型 (1: Clash, 2: Clash.Meta)                                            |
 | 
				
			||||||
| proxy        | string | sub/proxy 至少有一项存在 | -         | 节点分享链接(可以输入多个,用 `,` 分隔)                                                                                                                                                                                                 |
 | 
					| subscriptions      | []string          | sub/proxy 至少有一项存在 | -         | 订阅链接,可以在链接结尾加上`#名称`,来给订阅中的节点加上统一前缀(可以输入多个) |
 | 
				
			||||||
 | 
					| proxies            | []string          | sub/proxy 至少有一项存在 | -         | 节点分享链接(可以输入多个)                                                      |
 | 
				
			||||||
| refresh            | bool              | 否                       | `false`   | 强制刷新配置(默认缓存 5 分钟)                                                   |
 | 
					| refresh            | bool              | 否                       | `false`   | 强制刷新配置(默认缓存 5 分钟)                                                   |
 | 
				
			||||||
| template           | string            | 否                       | -         | 外部模板链接或内部模板名称                                                        |
 | 
					| template           | string            | 否                       | -         | 外部模板链接或内部模板名称                                                        |
 | 
				
			||||||
| ruleProvider | string | 否                       | -         | 格式 `[Behavior,Url,Group,Prepend,Name],[Behavior,Url,Group,Prepend,Name]...`,其中 `Group` 是该规则集使用的策略组名,`Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到 MATCH 规则之前) |
 | 
					| ruleProviders      | []RuleProvider    | 否                       | -         | 规则                                                                              |
 | 
				
			||||||
| rule         | string | 否                       | -         | 格式 `[Rule,Prepend],[Rule,Prepend]...`,其中 `Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到 MATCH 规则之前)                                                                         |
 | 
					| rules              | []Rule            | 否                       | -         | 规则                                                                              |
 | 
				
			||||||
| autoTest           | bool              | 否                       | `false`   | 国家策略组是否自动测速                                                            |
 | 
					| autoTest           | bool              | 否                       | `false`   | 国家策略组是否自动测速                                                            |
 | 
				
			||||||
| lazy               | bool              | 否                       | `false`   | 自动测速是否启用 lazy                                                             |
 | 
					| lazy               | bool              | 否                       | `false`   | 自动测速是否启用 lazy                                                             |
 | 
				
			||||||
| sort               | string            | 否                       | `nameasc` | 国家策略组排序策略,可选值 `nameasc`、`namedesc`、`sizeasc`、`sizedesc`           |
 | 
					| sort               | string            | 否                       | `nameasc` | 国家策略组排序策略,可选值 `nameasc`、`namedesc`、`sizeasc`、`sizedesc`           |
 | 
				
			||||||
| replace      | string | 否                       | -         | 通过正则表达式重命名节点,格式 `[<ReplaceKey>,<ReplaceTo>],[<ReplaceKey>,<ReplaceTo>]...`                                                                                                                                                 |
 | 
					| replace            | map[string]string | 否                       | -         | 通过正则表达式重命名节点                                                          |
 | 
				
			||||||
| remove             | string            | 否                       | -         | 通过正则表达式删除节点                                                            |
 | 
					| remove             | string            | 否                       | -         | 通过正则表达式删除节点                                                            |
 | 
				
			||||||
| nodeList           | bool              | 否                       | `false`   | 只输出节点                                                                        |
 | 
					| nodeList           | bool              | 否                       | `false`   | 只输出节点                                                                        |
 | 
				
			||||||
 | 
					| ignoreCountryGroup | bool              | 否                       | `false`   | 是否忽略国家分组                                                                  |
 | 
				
			||||||
 | 
					| userAgent          | string            | 否                       | -         | 订阅 user-agent                                                                   |
 | 
				
			||||||
 | 
					| useUDP             | bool              | 否                       | `false`   | 是否使用 UDP                                                                      |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### `RuleProvider` 结构
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					| 字段     | 类型   | 说明                                                             |
 | 
				
			||||||
 | 
					| -------- | ------ | ---------------------------------------------------------------- |
 | 
				
			||||||
 | 
					| behavior | string | rule-set 的 behavior                                             |
 | 
				
			||||||
 | 
					| url      | string | rule-set 的 url                                                  |
 | 
				
			||||||
 | 
					| group    | string | 该规则集使用的策略组名                                           |
 | 
				
			||||||
 | 
					| prepend  | bool   | 如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部 |
 | 
				
			||||||
 | 
					| name     | string | 该 rule-provider 的名称,不能重复                                |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### `Rule` 结构
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					| 字段    | 类型   | 说明                                                             |
 | 
				
			||||||
 | 
					| ------- | ------ | ---------------------------------------------------------------- |
 | 
				
			||||||
 | 
					| rule    | string | 规则                                                             |
 | 
				
			||||||
 | 
					| prepend | bool   | 如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部 |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# `POST /short`
 | 
					# `POST /short`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,7 @@ import (
 | 
				
			|||||||
	"github.com/bestnite/sub2clash/model"
 | 
						"github.com/bestnite/sub2clash/model"
 | 
				
			||||||
	P "github.com/bestnite/sub2clash/model/proxy"
 | 
						P "github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
	"github.com/bestnite/sub2clash/parser"
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/utils"
 | 
				
			||||||
	"go.uber.org/zap"
 | 
						"go.uber.org/zap"
 | 
				
			||||||
	"gopkg.in/yaml.v3"
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -92,7 +93,7 @@ func FetchSubscriptionFromAPI(url string, userAgent string, retryTimes int) ([]b
 | 
				
			|||||||
	return data, nil
 | 
						return data, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func BuildSub(clashType model.ClashType, query model.SubConfig, template string, cacheExpire int64, retryTimes int) (
 | 
					func BuildSub(clashType model.ClashType, query model.ConvertConfig, template string, cacheExpire int64, retryTimes int) (
 | 
				
			||||||
	*model.Subscription, error,
 | 
						*model.Subscription, error,
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
	var temp = &model.Subscription{}
 | 
						var temp = &model.Subscription{}
 | 
				
			||||||
@@ -134,16 +135,16 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	for i := range query.Subs {
 | 
						for i := range query.Subs {
 | 
				
			||||||
		data, err := LoadSubscription(query.Subs[i], query.Refresh, query.UserAgent, cacheExpire, retryTimes)
 | 
							data, err := LoadSubscription(query.Subs[i], query.Refresh, query.UserAgent, cacheExpire, retryTimes)
 | 
				
			||||||
		subName := ""
 | 
					 | 
				
			||||||
		if strings.Contains(query.Subs[i], "#") {
 | 
					 | 
				
			||||||
			subName = query.Subs[i][strings.LastIndex(query.Subs[i], "#")+1:]
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			logger.Logger.Debug(
 | 
								logger.Logger.Debug(
 | 
				
			||||||
				"load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err),
 | 
									"load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err),
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
			return nil, NewSubscriptionLoadError(query.Subs[i], err)
 | 
								return nil, NewSubscriptionLoadError(query.Subs[i], err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							subName := ""
 | 
				
			||||||
 | 
							if strings.Contains(query.Subs[i], "#") {
 | 
				
			||||||
 | 
								subName = query.Subs[i][strings.LastIndex(query.Subs[i], "#")+1:]
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err = yaml.Unmarshal(data, &sub)
 | 
							err = yaml.Unmarshal(data, &sub)
 | 
				
			||||||
		var newProxies []P.Proxy
 | 
							var newProxies []P.Proxy
 | 
				
			||||||
@@ -154,10 +155,13 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
				return nil, NewRegexInvalidError("prefix", err)
 | 
									return nil, NewRegexInvalidError("prefix", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if reg.Match(data) {
 | 
								if reg.Match(data) {
 | 
				
			||||||
				p := parser.ParseProxies(strings.Split(string(data), "\n")...)
 | 
									p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, strings.Split(string(data), "\n")...)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										return nil, err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				newProxies = p
 | 
									newProxies = p
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				base64, err := parser.DecodeBase64(string(data))
 | 
									base64, err := utils.DecodeBase64(string(data), false)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
					logger.Logger.Debug(
 | 
										logger.Logger.Debug(
 | 
				
			||||||
						"parse subscription failed", zap.String("url", query.Subs[i]),
 | 
											"parse subscription failed", zap.String("url", query.Subs[i]),
 | 
				
			||||||
@@ -166,7 +170,10 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
					)
 | 
										)
 | 
				
			||||||
					return nil, NewSubscriptionParseError(err)
 | 
										return nil, NewSubscriptionParseError(err)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				p := parser.ParseProxies(strings.Split(base64, "\n")...)
 | 
									p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, strings.Split(base64, "\n")...)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										return nil, err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				newProxies = p
 | 
									newProxies = p
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
@@ -180,8 +187,12 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
		proxyList = append(proxyList, newProxies...)
 | 
							proxyList = append(proxyList, newProxies...)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(query.Proxy) != 0 {
 | 
						if len(query.Proxies) != 0 {
 | 
				
			||||||
		proxyList = append(proxyList, parser.ParseProxies(query.Proxies...)...)
 | 
							p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, query.Proxies...)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							proxyList = append(proxyList, p...)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for i := range proxyList {
 | 
						for i := range proxyList {
 | 
				
			||||||
@@ -190,6 +201,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 去重
 | 
				
			||||||
	proxies := make(map[string]*P.Proxy)
 | 
						proxies := make(map[string]*P.Proxy)
 | 
				
			||||||
	newProxies := make([]P.Proxy, 0, len(proxyList))
 | 
						newProxies := make([]P.Proxy, 0, len(proxyList))
 | 
				
			||||||
	for i := range proxyList {
 | 
						for i := range proxyList {
 | 
				
			||||||
@@ -206,6 +218,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	proxyList = newProxies
 | 
						proxyList = newProxies
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 移除
 | 
				
			||||||
	if strings.TrimSpace(query.Remove) != "" {
 | 
						if strings.TrimSpace(query.Remove) != "" {
 | 
				
			||||||
		newProxyList := make([]P.Proxy, 0, len(proxyList))
 | 
							newProxyList := make([]P.Proxy, 0, len(proxyList))
 | 
				
			||||||
		for i := range proxyList {
 | 
							for i := range proxyList {
 | 
				
			||||||
@@ -223,29 +236,25 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
		proxyList = newProxyList
 | 
							proxyList = newProxyList
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(query.ReplaceKeys) != 0 {
 | 
						// 替换
 | 
				
			||||||
 | 
						if len(query.Replace) != 0 {
 | 
				
			||||||
		replaceRegs := make([]*regexp.Regexp, 0, len(query.ReplaceKeys))
 | 
							for k, v := range query.Replace {
 | 
				
			||||||
		for _, v := range query.ReplaceKeys {
 | 
								replaceReg, err := regexp.Compile(k)
 | 
				
			||||||
			replaceReg, err := regexp.Compile(v)
 | 
					 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
 | 
									logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
 | 
				
			||||||
				return nil, NewRegexInvalidError("replace", err)
 | 
									return nil, NewRegexInvalidError("replace", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			replaceRegs = append(replaceRegs, replaceReg)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
			for i := range proxyList {
 | 
								for i := range proxyList {
 | 
				
			||||||
 | 
									if replaceReg.MatchString(proxyList[i].Name) {
 | 
				
			||||||
			for j, v := range replaceRegs {
 | 
										proxyList[i].Name = replaceReg.ReplaceAllString(
 | 
				
			||||||
				if v.MatchString(proxyList[i].Name) {
 | 
											proxyList[i].Name, v,
 | 
				
			||||||
					proxyList[i].Name = v.ReplaceAllString(
 | 
					 | 
				
			||||||
						proxyList[i].Name, query.ReplaceTo[j],
 | 
					 | 
				
			||||||
					)
 | 
										)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 重命名有相同名称的节点
 | 
				
			||||||
	names := make(map[string]int)
 | 
						names := make(map[string]int)
 | 
				
			||||||
	for i := range proxyList {
 | 
						for i := range proxyList {
 | 
				
			||||||
		if _, exist := names[proxyList[i].Name]; exist {
 | 
							if _, exist := names[proxyList[i].Name]; exist {
 | 
				
			||||||
@@ -263,6 +272,7 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
 | 
				
			|||||||
	var t = &model.Subscription{}
 | 
						var t = &model.Subscription{}
 | 
				
			||||||
	AddProxy(t, query.AutoTest, query.Lazy, clashType, proxyList...)
 | 
						AddProxy(t, query.AutoTest, query.Lazy, clashType, proxyList...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 排序
 | 
				
			||||||
	switch query.Sort {
 | 
						switch query.Sort {
 | 
				
			||||||
	case "sizeasc":
 | 
						case "sizeasc":
 | 
				
			||||||
		sort.Sort(model.ProxyGroupsSortBySize(t.ProxyGroup))
 | 
							sort.Sort(model.ProxyGroupsSortBySize(t.ProxyGroup))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,11 +13,12 @@ func GetSupportProxyTypes(clashType ClashType) map[string]bool {
 | 
				
			|||||||
	supportProxyTypes := make(map[string]bool)
 | 
						supportProxyTypes := make(map[string]bool)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, parser := range parser.GetAllParsers() {
 | 
						for _, parser := range parser.GetAllParsers() {
 | 
				
			||||||
		if clashType == Clash {
 | 
							switch clashType {
 | 
				
			||||||
 | 
							case Clash:
 | 
				
			||||||
			if parser.SupportClash() {
 | 
								if parser.SupportClash() {
 | 
				
			||||||
				supportProxyTypes[parser.GetType()] = true
 | 
									supportProxyTypes[parser.GetType()] = true
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else if clashType == ClashMeta {
 | 
							case ClashMeta:
 | 
				
			||||||
			if parser.SupportMeta() {
 | 
								if parser.SupportMeta() {
 | 
				
			||||||
				supportProxyTypes[parser.GetType()] = true
 | 
									supportProxyTypes[parser.GetType()] = true
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										92
									
								
								model/convert_config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								model/convert_config.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,92 @@
 | 
				
			|||||||
 | 
					package model
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"net/url"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/utils"
 | 
				
			||||||
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ConvertConfig struct {
 | 
				
			||||||
 | 
						ClashType           ClashType            `json:"clashType" binding:"required"`
 | 
				
			||||||
 | 
						Subs                []string             `json:"subscriptions" binding:""`
 | 
				
			||||||
 | 
						Proxies             []string             `json:"proxies" binding:""`
 | 
				
			||||||
 | 
						Refresh             bool                 `json:"refresh" binding:""`
 | 
				
			||||||
 | 
						Template            string               `json:"template" binding:""`
 | 
				
			||||||
 | 
						RuleProviders       []RuleProviderStruct `json:"ruleProviders" binding:""`
 | 
				
			||||||
 | 
						Rules               []RuleStruct         `json:"rules" binding:""`
 | 
				
			||||||
 | 
						AutoTest            bool                 `json:"autoTest" binding:""`
 | 
				
			||||||
 | 
						Lazy                bool                 `json:"lazy" binding:""`
 | 
				
			||||||
 | 
						Sort                string               `json:"sort" binding:""`
 | 
				
			||||||
 | 
						Remove              string               `json:"remove" binding:""`
 | 
				
			||||||
 | 
						Replace             map[string]string    `json:"replace" binding:""`
 | 
				
			||||||
 | 
						NodeListMode        bool                 `json:"nodeList" binding:""`
 | 
				
			||||||
 | 
						IgnoreCountryGrooup bool                 `json:"ignoreCountryGroup" binding:""`
 | 
				
			||||||
 | 
						UserAgent           string               `json:"userAgent" binding:""`
 | 
				
			||||||
 | 
						UseUDP              bool                 `json:"useUDP" binding:""`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type RuleProviderStruct struct {
 | 
				
			||||||
 | 
						Behavior string `json:"behavior" binding:""`
 | 
				
			||||||
 | 
						Url      string `json:"url" binding:""`
 | 
				
			||||||
 | 
						Group    string `json:"group" binding:""`
 | 
				
			||||||
 | 
						Prepend  bool   `json:"prepend" binding:""`
 | 
				
			||||||
 | 
						Name     string `json:"name" binding:""`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type RuleStruct struct {
 | 
				
			||||||
 | 
						Rule    string `json:"rule" binding:""`
 | 
				
			||||||
 | 
						Prepend bool   `json:"prepend" binding:""`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ParseConvertQuery(c *gin.Context) (ConvertConfig, error) {
 | 
				
			||||||
 | 
						config := c.Param("config")
 | 
				
			||||||
 | 
						queryBytes, err := utils.DecodeBase64(config, true)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return ConvertConfig{}, errors.New("参数错误: " + err.Error())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var query ConvertConfig
 | 
				
			||||||
 | 
						err = json.Unmarshal([]byte(queryBytes), &query)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return ConvertConfig{}, errors.New("参数错误: " + err.Error())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(query.Subs) == 0 && len(query.Proxies) == 0 {
 | 
				
			||||||
 | 
							return ConvertConfig{}, errors.New("参数错误: sub 和 proxy 不能同时为空")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(query.Subs) > 0 {
 | 
				
			||||||
 | 
							for i := range query.Subs {
 | 
				
			||||||
 | 
								if !strings.HasPrefix(query.Subs[i], "http") {
 | 
				
			||||||
 | 
									return ConvertConfig{}, errors.New("参数错误: sub 格式错误")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if _, err := url.ParseRequestURI(query.Subs[i]); err != nil {
 | 
				
			||||||
 | 
									return ConvertConfig{}, errors.New("参数错误: " + err.Error())
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							query.Subs = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if query.Template != "" {
 | 
				
			||||||
 | 
							if strings.HasPrefix(query.Template, "http") {
 | 
				
			||||||
 | 
								uri, err := url.ParseRequestURI(query.Template)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return ConvertConfig{}, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								query.Template = uri.String()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(query.RuleProviders) > 0 {
 | 
				
			||||||
 | 
							names := make(map[string]bool)
 | 
				
			||||||
 | 
							for _, ruleProvider := range query.RuleProviders {
 | 
				
			||||||
 | 
								if _, ok := names[ruleProvider.Name]; ok {
 | 
				
			||||||
 | 
									return ConvertConfig{}, errors.New("参数错误: Rule-Provider 名称重复")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								names[ruleProvider.Name] = true
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							query.RuleProviders = nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return query, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,6 +1,10 @@
 | 
				
			|||||||
package proxy
 | 
					package proxy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "fmt"
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HTTPOptions struct {
 | 
					type HTTPOptions struct {
 | 
				
			||||||
	Method  string              `yaml:"method,omitempty"`
 | 
						Method  string              `yaml:"method,omitempty"`
 | 
				
			||||||
@@ -144,7 +148,137 @@ func (p Proxy) MarshalYAML() (any, error) {
 | 
				
			|||||||
			Name:  p.Name,
 | 
								Name:  p.Name,
 | 
				
			||||||
			Vmess: p.Vmess,
 | 
								Vmess: p.Vmess,
 | 
				
			||||||
		}, nil
 | 
							}, nil
 | 
				
			||||||
 | 
						case "socks5":
 | 
				
			||||||
 | 
							return struct {
 | 
				
			||||||
 | 
								Type  string `yaml:"type"`
 | 
				
			||||||
 | 
								Name  string `yaml:"name"`
 | 
				
			||||||
 | 
								Socks `yaml:",inline"`
 | 
				
			||||||
 | 
							}{
 | 
				
			||||||
 | 
								Type:  p.Type,
 | 
				
			||||||
 | 
								Name:  p.Name,
 | 
				
			||||||
 | 
								Socks: p.Socks,
 | 
				
			||||||
 | 
							}, nil
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		return nil, fmt.Errorf("unsupported proxy type: %s", p.Type)
 | 
							return nil, fmt.Errorf("unsupported proxy type: %s", p.Type)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (p *Proxy) UnmarshalYAML(node *yaml.Node) error {
 | 
				
			||||||
 | 
						var temp struct {
 | 
				
			||||||
 | 
							Type string `yaml:"type"`
 | 
				
			||||||
 | 
							Name string `yaml:"name"`
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := node.Decode(&temp); err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						p.Type = temp.Type
 | 
				
			||||||
 | 
						p.Name = temp.Name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						switch temp.Type {
 | 
				
			||||||
 | 
						case "anytls":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type   string `yaml:"type"`
 | 
				
			||||||
 | 
								Name   string `yaml:"name"`
 | 
				
			||||||
 | 
								Anytls `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.Anytls = data.Anytls
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "hysteria":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type     string `yaml:"type"`
 | 
				
			||||||
 | 
								Name     string `yaml:"name"`
 | 
				
			||||||
 | 
								Hysteria `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.Hysteria = data.Hysteria
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "hysteria2":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type      string `yaml:"type"`
 | 
				
			||||||
 | 
								Name      string `yaml:"name"`
 | 
				
			||||||
 | 
								Hysteria2 `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.Hysteria2 = data.Hysteria2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "ss":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type        string `yaml:"type"`
 | 
				
			||||||
 | 
								Name        string `yaml:"name"`
 | 
				
			||||||
 | 
								ShadowSocks `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.ShadowSocks = data.ShadowSocks
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "ssr":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type         string `yaml:"type"`
 | 
				
			||||||
 | 
								Name         string `yaml:"name"`
 | 
				
			||||||
 | 
								ShadowSocksR `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.ShadowSocksR = data.ShadowSocksR
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "trojan":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type   string `yaml:"type"`
 | 
				
			||||||
 | 
								Name   string `yaml:"name"`
 | 
				
			||||||
 | 
								Trojan `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.Trojan = data.Trojan
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "vless":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type  string `yaml:"type"`
 | 
				
			||||||
 | 
								Name  string `yaml:"name"`
 | 
				
			||||||
 | 
								Vless `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.Vless = data.Vless
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "vmess":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type  string `yaml:"type"`
 | 
				
			||||||
 | 
								Name  string `yaml:"name"`
 | 
				
			||||||
 | 
								Vmess `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.Vmess = data.Vmess
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case "socks5":
 | 
				
			||||||
 | 
							var data struct {
 | 
				
			||||||
 | 
								Type  string `yaml:"type"`
 | 
				
			||||||
 | 
								Name  string `yaml:"name"`
 | 
				
			||||||
 | 
								Socks `yaml:",inline"`
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if err := node.Decode(&data); err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.Socks = data.Socks
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("unsupported proxy type: %s", temp.Type)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,158 +0,0 @@
 | 
				
			|||||||
package model
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"crypto/sha256"
 | 
					 | 
				
			||||||
	"encoding/hex"
 | 
					 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
	"net/url"
 | 
					 | 
				
			||||||
	"regexp"
 | 
					 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type SubConfig struct {
 | 
					 | 
				
			||||||
	Sub                 string               `form:"sub" binding:""`
 | 
					 | 
				
			||||||
	Subs                []string             `form:"-" binding:""`
 | 
					 | 
				
			||||||
	Proxy               string               `form:"proxy" binding:""`
 | 
					 | 
				
			||||||
	Proxies             []string             `form:"-" binding:""`
 | 
					 | 
				
			||||||
	Refresh             bool                 `form:"refresh,default=false" binding:""`
 | 
					 | 
				
			||||||
	Template            string               `form:"template" binding:""`
 | 
					 | 
				
			||||||
	RuleProvider        string               `form:"ruleProvider" binding:""`
 | 
					 | 
				
			||||||
	RuleProviders       []RuleProviderStruct `form:"-" binding:""`
 | 
					 | 
				
			||||||
	Rule                string               `form:"rule" binding:""`
 | 
					 | 
				
			||||||
	Rules               []RuleStruct         `form:"-" binding:""`
 | 
					 | 
				
			||||||
	AutoTest            bool                 `form:"autoTest,default=false" binding:""`
 | 
					 | 
				
			||||||
	Lazy                bool                 `form:"lazy,default=false" binding:""`
 | 
					 | 
				
			||||||
	Sort                string               `form:"sort" binding:""`
 | 
					 | 
				
			||||||
	Remove              string               `form:"remove" binding:""`
 | 
					 | 
				
			||||||
	Replace             string               `form:"replace" binding:""`
 | 
					 | 
				
			||||||
	ReplaceKeys         []string             `form:"-" binding:""`
 | 
					 | 
				
			||||||
	ReplaceTo           []string             `form:"-" binding:""`
 | 
					 | 
				
			||||||
	NodeListMode        bool                 `form:"nodeList,default=false" binding:""`
 | 
					 | 
				
			||||||
	IgnoreCountryGrooup bool                 `form:"ignoreCountryGroup,default=false" binding:""`
 | 
					 | 
				
			||||||
	UserAgent           string               `form:"userAgent" binding:""`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type RuleProviderStruct struct {
 | 
					 | 
				
			||||||
	Behavior string
 | 
					 | 
				
			||||||
	Url      string
 | 
					 | 
				
			||||||
	Group    string
 | 
					 | 
				
			||||||
	Prepend  bool
 | 
					 | 
				
			||||||
	Name     string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type RuleStruct struct {
 | 
					 | 
				
			||||||
	Rule    string
 | 
					 | 
				
			||||||
	Prepend bool
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func ParseSubQuery(c *gin.Context) (SubConfig, error) {
 | 
					 | 
				
			||||||
	var query SubConfig
 | 
					 | 
				
			||||||
	if err := c.ShouldBind(&query); err != nil {
 | 
					 | 
				
			||||||
		return SubConfig{}, errors.New("参数错误: " + err.Error())
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if query.Sub == "" && query.Proxy == "" {
 | 
					 | 
				
			||||||
		return SubConfig{}, errors.New("参数错误: sub 和 proxy 不能同时为空")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if query.Sub != "" {
 | 
					 | 
				
			||||||
		query.Subs = strings.Split(query.Sub, ",")
 | 
					 | 
				
			||||||
		for i := range query.Subs {
 | 
					 | 
				
			||||||
			if !strings.HasPrefix(query.Subs[i], "http") {
 | 
					 | 
				
			||||||
				return SubConfig{}, errors.New("参数错误: sub 格式错误")
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			if _, err := url.ParseRequestURI(query.Subs[i]); err != nil {
 | 
					 | 
				
			||||||
				return SubConfig{}, errors.New("参数错误: " + err.Error())
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		query.Subs = nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if query.Proxy != "" {
 | 
					 | 
				
			||||||
		query.Proxies = strings.Split(query.Proxy, ",")
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		query.Proxies = nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if query.Template != "" {
 | 
					 | 
				
			||||||
		if strings.HasPrefix(query.Template, "http") {
 | 
					 | 
				
			||||||
			uri, err := url.ParseRequestURI(query.Template)
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				return SubConfig{}, err
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			query.Template = uri.String()
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if query.RuleProvider != "" {
 | 
					 | 
				
			||||||
		reg := regexp.MustCompile(`\[(.*?)\]`)
 | 
					 | 
				
			||||||
		ruleProviders := reg.FindAllStringSubmatch(query.RuleProvider, -1)
 | 
					 | 
				
			||||||
		for i := range ruleProviders {
 | 
					 | 
				
			||||||
			length := len(ruleProviders)
 | 
					 | 
				
			||||||
			parts := strings.Split(ruleProviders[length-i-1][1], ",")
 | 
					 | 
				
			||||||
			if len(parts) < 4 {
 | 
					 | 
				
			||||||
				return SubConfig{}, errors.New("参数错误: ruleProvider 格式错误")
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			u := parts[1]
 | 
					 | 
				
			||||||
			uri, err := url.ParseRequestURI(u)
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				return SubConfig{}, errors.New("参数错误: " + err.Error())
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			u = uri.String()
 | 
					 | 
				
			||||||
			if len(parts) == 4 {
 | 
					 | 
				
			||||||
				hash := sha256.Sum224([]byte(u))
 | 
					 | 
				
			||||||
				parts = append(parts, hex.EncodeToString(hash[:]))
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			query.RuleProviders = append(
 | 
					 | 
				
			||||||
				query.RuleProviders, RuleProviderStruct{
 | 
					 | 
				
			||||||
					Behavior: parts[0],
 | 
					 | 
				
			||||||
					Url:      u,
 | 
					 | 
				
			||||||
					Group:    parts[2],
 | 
					 | 
				
			||||||
					Prepend:  parts[3] == "true",
 | 
					 | 
				
			||||||
					Name:     parts[4],
 | 
					 | 
				
			||||||
				},
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		names := make(map[string]bool)
 | 
					 | 
				
			||||||
		for _, ruleProvider := range query.RuleProviders {
 | 
					 | 
				
			||||||
			if _, ok := names[ruleProvider.Name]; ok {
 | 
					 | 
				
			||||||
				return SubConfig{}, errors.New("参数错误: Rule-Provider 名称重复")
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			names[ruleProvider.Name] = true
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		query.RuleProviders = nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if query.Rule != "" {
 | 
					 | 
				
			||||||
		reg := regexp.MustCompile(`\[(.*?)\]`)
 | 
					 | 
				
			||||||
		rules := reg.FindAllStringSubmatch(query.Rule, -1)
 | 
					 | 
				
			||||||
		for i := range rules {
 | 
					 | 
				
			||||||
			length := len(rules)
 | 
					 | 
				
			||||||
			r := rules[length-1-i][1]
 | 
					 | 
				
			||||||
			strings.LastIndex(r, ",")
 | 
					 | 
				
			||||||
			parts := [2]string{}
 | 
					 | 
				
			||||||
			parts[0] = r[:strings.LastIndex(r, ",")]
 | 
					 | 
				
			||||||
			parts[1] = r[strings.LastIndex(r, ",")+1:]
 | 
					 | 
				
			||||||
			query.Rules = append(
 | 
					 | 
				
			||||||
				query.Rules, RuleStruct{
 | 
					 | 
				
			||||||
					Rule:    parts[0],
 | 
					 | 
				
			||||||
					Prepend: parts[1] == "true",
 | 
					 | 
				
			||||||
				},
 | 
					 | 
				
			||||||
			)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		query.Rules = nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if strings.TrimSpace(query.Replace) != "" {
 | 
					 | 
				
			||||||
		reg := regexp.MustCompile(`\[<(.*?)>,<(.*?)>\]`)
 | 
					 | 
				
			||||||
		replaces := reg.FindAllStringSubmatch(query.Replace, -1)
 | 
					 | 
				
			||||||
		for i := range replaces {
 | 
					 | 
				
			||||||
			length := len(replaces[i])
 | 
					 | 
				
			||||||
			if length != 3 {
 | 
					 | 
				
			||||||
				return SubConfig{}, errors.New("参数错误: replace 格式错误")
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			query.ReplaceKeys = append(query.ReplaceKeys, replaces[i][1])
 | 
					 | 
				
			||||||
			query.ReplaceTo = append(query.ReplaceTo, replaces[i][2])
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return query, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -26,18 +26,14 @@ func (p *AnytlsParser) GetType() string {
 | 
				
			|||||||
	return "anytls"
 | 
						return "anytls"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *AnytlsParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *AnytlsParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	link, err := url.Parse(proxy)
 | 
						link, err := url.Parse(proxy)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "url parse error",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	username := link.User.Username()
 | 
						username := link.User.Username()
 | 
				
			||||||
@@ -49,26 +45,15 @@ func (p *AnytlsParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	query := link.Query()
 | 
						query := link.Query()
 | 
				
			||||||
	server := link.Hostname()
 | 
						server := link.Hostname()
 | 
				
			||||||
	if server == "" {
 | 
						if server == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server host")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server host",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	portStr := link.Port()
 | 
						portStr := link.Port()
 | 
				
			||||||
	if portStr == "" {
 | 
						if portStr == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server port")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server port",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	port, err := ParsePort(portStr)
 | 
						port, err := ParsePort(portStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
			Type: ErrInvalidPort,
 | 
					 | 
				
			||||||
			Raw:  portStr,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	insecure, sni := query.Get("insecure"), query.Get("sni")
 | 
						insecure, sni := query.Get("insecure"), query.Get("sni")
 | 
				
			||||||
	insecureBool := insecure == "1"
 | 
						insecureBool := insecure == "1"
 | 
				
			||||||
@@ -87,6 +72,7 @@ func (p *AnytlsParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
			Password:       password,
 | 
								Password:       password,
 | 
				
			||||||
			SNI:            sni,
 | 
								SNI:            sni,
 | 
				
			||||||
			SkipCertVerify: insecureBool,
 | 
								SkipCertVerify: insecureBool,
 | 
				
			||||||
 | 
								UDP:            config.UseUDP,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return result, nil
 | 
						return result, nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,13 @@
 | 
				
			|||||||
package parser
 | 
					package parser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/base64"
 | 
					 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"unicode/utf8"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/bestnite/sub2clash/logger"
 | 
					 | 
				
			||||||
	P "github.com/bestnite/sub2clash/model/proxy"
 | 
						P "github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
	"go.uber.org/zap"
 | 
						"github.com/bestnite/sub2clash/utils"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func hasPrefix(proxy string, prefixes []string) bool {
 | 
					func hasPrefix(proxy string, prefixes []string) bool {
 | 
				
			||||||
@@ -34,8 +33,13 @@ func ParsePort(portStr string) (int, error) {
 | 
				
			|||||||
	return port, nil
 | 
						return port, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// isLikelyBase64 不严格判断是否是合法的 Base64, 很多分享链接不符合 Base64 规范
 | 
				
			||||||
func isLikelyBase64(s string) bool {
 | 
					func isLikelyBase64(s string) bool {
 | 
				
			||||||
	if len(s)%4 == 0 && strings.HasSuffix(s, "=") && !strings.Contains(strings.TrimSuffix(s, "="), "=") {
 | 
						if strings.TrimSpace(s) == "" {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !strings.Contains(strings.TrimSuffix(s, "="), "=") {
 | 
				
			||||||
		s = strings.TrimSuffix(s, "=")
 | 
							s = strings.TrimSuffix(s, "=")
 | 
				
			||||||
		chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 | 
							chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 | 
				
			||||||
		for _, c := range s {
 | 
							for _, c := range s {
 | 
				
			||||||
@@ -43,45 +47,36 @@ func isLikelyBase64(s string) bool {
 | 
				
			|||||||
				return false
 | 
									return false
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return true
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return false
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
func DecodeBase64(s string) (string, error) {
 | 
						decoded, err := utils.DecodeBase64(s, true)
 | 
				
			||||||
	s = strings.TrimSpace(s)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if strings.Contains(s, "-") || strings.Contains(s, "_") {
 | 
					 | 
				
			||||||
		s = strings.Replace(s, "-", "+", -1)
 | 
					 | 
				
			||||||
		s = strings.Replace(s, "_", "/", -1)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if len(s)%4 != 0 {
 | 
					 | 
				
			||||||
		s += strings.Repeat("=", 4-len(s)%4)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	decodeStr, err := base64.StdEncoding.DecodeString(s)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return "", err
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return string(decodeStr), nil
 | 
						if !utf8.ValidString(decoded) {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ParseProxies(proxies ...string) []P.Proxy {
 | 
					type ParseConfig struct {
 | 
				
			||||||
 | 
						UseUDP bool
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ParseProxies(config ParseConfig, proxies ...string) ([]P.Proxy, error) {
 | 
				
			||||||
	var result []P.Proxy
 | 
						var result []P.Proxy
 | 
				
			||||||
	for _, proxy := range proxies {
 | 
						for _, proxy := range proxies {
 | 
				
			||||||
		if proxy != "" {
 | 
							if proxy != "" {
 | 
				
			||||||
			var proxyItem P.Proxy
 | 
								var proxyItem P.Proxy
 | 
				
			||||||
			var err error
 | 
								var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			proxyItem, err = ParseProxyWithRegistry(proxy)
 | 
								proxyItem, err = ParseProxyWithRegistry(config, proxy)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
			if err == nil {
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			result = append(result, proxyItem)
 | 
								result = append(result, proxyItem)
 | 
				
			||||||
			} else {
 | 
					 | 
				
			||||||
				logger.Logger.Debug(
 | 
					 | 
				
			||||||
					"parse proxy failed", zap.String("proxy", proxy), zap.Error(err),
 | 
					 | 
				
			||||||
				)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	}
 | 
						return result, nil
 | 
				
			||||||
	return result
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,5 @@
 | 
				
			|||||||
package parser
 | 
					package parser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ParseError struct {
 | 
					 | 
				
			||||||
	Type    ParseErrorType
 | 
					 | 
				
			||||||
	Message string
 | 
					 | 
				
			||||||
	Raw     string
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type ParseErrorType string
 | 
					type ParseErrorType string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@@ -16,9 +10,6 @@ const (
 | 
				
			|||||||
	ErrInvalidBase64     ParseErrorType = "invalid base64"
 | 
						ErrInvalidBase64     ParseErrorType = "invalid base64"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (e *ParseError) Error() string {
 | 
					func (e ParseErrorType) Error() string {
 | 
				
			||||||
	if e.Message != "" {
 | 
						return string(e)
 | 
				
			||||||
		return string(e.Type) + ": " + e.Message + " \"" + e.Raw + "\""
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return string(e.Type)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,44 +27,28 @@ func (p *HysteriaParser) GetType() string {
 | 
				
			|||||||
	return "hysteria"
 | 
						return "hysteria"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *HysteriaParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *HysteriaParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	link, err := url.Parse(proxy)
 | 
						link, err := url.Parse(proxy)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "url parse error",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	server := link.Hostname()
 | 
						server := link.Hostname()
 | 
				
			||||||
	if server == "" {
 | 
						if server == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server host")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server host",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	portStr := link.Port()
 | 
						portStr := link.Port()
 | 
				
			||||||
	if portStr == "" {
 | 
						if portStr == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server port")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server port",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	port, err := ParsePort(portStr)
 | 
						port, err := ParsePort(portStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidPort,
 | 
					 | 
				
			||||||
			Message: err.Error(),
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	query := link.Query()
 | 
						query := link.Query()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,18 +26,14 @@ func (p *Hysteria2Parser) GetType() string {
 | 
				
			|||||||
	return "hysteria2"
 | 
						return "hysteria2"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *Hysteria2Parser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *Hysteria2Parser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	link, err := url.Parse(proxy)
 | 
						link, err := url.Parse(proxy)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "url parse error",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	username := link.User.Username()
 | 
						username := link.User.Username()
 | 
				
			||||||
@@ -49,26 +45,15 @@ func (p *Hysteria2Parser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	query := link.Query()
 | 
						query := link.Query()
 | 
				
			||||||
	server := link.Hostname()
 | 
						server := link.Hostname()
 | 
				
			||||||
	if server == "" {
 | 
						if server == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server host")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server host",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	portStr := link.Port()
 | 
						portStr := link.Port()
 | 
				
			||||||
	if portStr == "" {
 | 
						if portStr == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server port")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server port",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	port, err := ParsePort(portStr)
 | 
						port, err := ParsePort(portStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
			Type: ErrInvalidPort,
 | 
					 | 
				
			||||||
			Raw:  portStr,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	obfs, obfsPassword, insecure, sni := query.Get("obfs"), query.Get("obfs-password"), query.Get("insecure"), query.Get("sni")
 | 
						obfs, obfsPassword, insecure, sni := query.Get("obfs"), query.Get("obfs-password"), query.Get("insecure"), query.Get("sni")
 | 
				
			||||||
	insecureBool := insecure == "1"
 | 
						insecureBool := insecure == "1"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package parser
 | 
					package parser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -8,7 +9,7 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ProxyParser interface {
 | 
					type ProxyParser interface {
 | 
				
			||||||
	Parse(proxy string) (P.Proxy, error)
 | 
						Parse(config ParseConfig, proxy string) (P.Proxy, error)
 | 
				
			||||||
	GetPrefixes() []string
 | 
						GetPrefixes() []string
 | 
				
			||||||
	GetType() string
 | 
						GetType() string
 | 
				
			||||||
	SupportClash() bool
 | 
						SupportClash() bool
 | 
				
			||||||
@@ -63,17 +64,17 @@ func GetAllPrefixes() []string {
 | 
				
			|||||||
	return prefixes
 | 
						return prefixes
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ParseProxyWithRegistry(proxy string) (P.Proxy, error) {
 | 
					func ParseProxyWithRegistry(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	proxy = strings.TrimSpace(proxy)
 | 
						proxy = strings.TrimSpace(proxy)
 | 
				
			||||||
	if proxy == "" {
 | 
						if proxy == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: "empty proxy string"}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "empty proxy string")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for prefix, parser := range registry.parsers {
 | 
						for prefix, parser := range registry.parsers {
 | 
				
			||||||
		if strings.HasPrefix(proxy, prefix) {
 | 
							if strings.HasPrefix(proxy, prefix) {
 | 
				
			||||||
			return parser.Parse(proxy)
 | 
								return parser.Parse(config, proxy)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy, Message: "unsupported protocol"}
 | 
						return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, "unsupported protocol")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,7 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	P "github.com/bestnite/sub2clash/model/proxy"
 | 
						P "github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/utils"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ShadowsocksParser Shadowsocks协议解析器
 | 
					// ShadowsocksParser Shadowsocks协议解析器
 | 
				
			||||||
@@ -30,9 +31,9 @@ func (p *ShadowsocksParser) GetType() string {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Parse 解析Shadowsocks代理
 | 
					// Parse 解析Shadowsocks代理
 | 
				
			||||||
func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !strings.Contains(proxy, "@") {
 | 
						if !strings.Contains(proxy, "@") {
 | 
				
			||||||
@@ -43,13 +44,9 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
				break
 | 
									break
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		d, err := DecodeBase64(s[0])
 | 
							d, err := utils.DecodeBase64(s[0], true)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return P.Proxy{}, &ParseError{
 | 
								return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
				Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
				Message: "url parse error",
 | 
					 | 
				
			||||||
				Raw:     proxy,
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if len(s) == 2 {
 | 
							if len(s) == 2 {
 | 
				
			||||||
			proxy = "ss://" + d + "#" + s[1]
 | 
								proxy = "ss://" + d + "#" + s[1]
 | 
				
			||||||
@@ -59,59 +56,42 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	link, err := url.Parse(proxy)
 | 
						link, err := url.Parse(proxy)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "url parse error",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	server := link.Hostname()
 | 
						server := link.Hostname()
 | 
				
			||||||
	if server == "" {
 | 
						if server == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server host")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server host",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	portStr := link.Port()
 | 
						portStr := link.Port()
 | 
				
			||||||
	if portStr == "" {
 | 
						if portStr == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server port")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server port",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	port, err := ParsePort(portStr)
 | 
						port, err := ParsePort(portStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type: ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Raw:  proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	method := link.User.Username()
 | 
						method := link.User.Username()
 | 
				
			||||||
	password, _ := link.User.Password()
 | 
						password, hasPassword := link.User.Password()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if password == "" {
 | 
						if !hasPassword && isLikelyBase64(method) {
 | 
				
			||||||
		user, err := DecodeBase64(method)
 | 
							decodedStr, err := utils.DecodeBase64(method, true)
 | 
				
			||||||
		if err == nil {
 | 
							if err == nil {
 | 
				
			||||||
			methodAndPass := strings.SplitN(user, ":", 2)
 | 
								methodAndPass := strings.SplitN(decodedStr, ":", 2)
 | 
				
			||||||
			if len(methodAndPass) == 2 {
 | 
								if len(methodAndPass) == 2 {
 | 
				
			||||||
				method = methodAndPass[0]
 | 
									method = methodAndPass[0]
 | 
				
			||||||
				password = methodAndPass[1]
 | 
									password = methodAndPass[1]
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									method = decodedStr
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if isLikelyBase64(password) {
 | 
						if password != "" && isLikelyBase64(password) {
 | 
				
			||||||
		password, err = DecodeBase64(password)
 | 
							password, err = utils.DecodeBase64(password, true)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return P.Proxy{}, &ParseError{
 | 
								return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
				Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
				Message: "password decode error",
 | 
					 | 
				
			||||||
				Raw:     proxy,
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -129,9 +109,9 @@ func (p *ShadowsocksParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
			Password: password,
 | 
								Password: password,
 | 
				
			||||||
			Server:   server,
 | 
								Server:   server,
 | 
				
			||||||
			Port:     port,
 | 
								Port:     port,
 | 
				
			||||||
 | 
								UDP:      config.UseUDP,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return result, nil
 | 
						return result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,13 @@
 | 
				
			|||||||
package parser
 | 
					package parser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	P "github.com/bestnite/sub2clash/model/proxy"
 | 
						P "github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/utils"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ShadowsocksRParser struct{}
 | 
					type ShadowsocksRParser struct{}
 | 
				
			||||||
@@ -26,9 +28,9 @@ func (p *ShadowsocksRParser) GetType() string {
 | 
				
			|||||||
	return "ssr"
 | 
						return "ssr"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, prefix := range p.GetPrefixes() {
 | 
						for _, prefix := range p.GetPrefixes() {
 | 
				
			||||||
@@ -38,34 +40,29 @@ func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	proxy, err := DecodeBase64(proxy)
 | 
						proxy, err := utils.DecodeBase64(proxy, true)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
 | 
				
			||||||
			Type: ErrInvalidBase64,
 | 
					 | 
				
			||||||
			Raw:  proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	serverInfoAndParams := strings.SplitN(proxy, "/?", 2)
 | 
						serverInfoAndParams := strings.SplitN(proxy, "/?", 2)
 | 
				
			||||||
	parts := strings.Split(serverInfoAndParams[0], ":")
 | 
						if len(serverInfoAndParams) != 2 {
 | 
				
			||||||
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, proxy)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						parts := SplitNRight(serverInfoAndParams[0], ":", 6)
 | 
				
			||||||
 | 
						if len(parts) < 6 {
 | 
				
			||||||
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, proxy)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	server := parts[0]
 | 
						server := parts[0]
 | 
				
			||||||
	protocol := parts[2]
 | 
						protocol := parts[2]
 | 
				
			||||||
	method := parts[3]
 | 
						method := parts[3]
 | 
				
			||||||
	obfs := parts[4]
 | 
						obfs := parts[4]
 | 
				
			||||||
	password, err := DecodeBase64(parts[5])
 | 
						password, err := utils.DecodeBase64(parts[5], true)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
			Message: err.Error(),
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	port, err := ParsePort(parts[1])
 | 
						port, err := ParsePort(parts[1])
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidPort,
 | 
					 | 
				
			||||||
			Message: err.Error(),
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var obfsParam string
 | 
						var obfsParam string
 | 
				
			||||||
@@ -74,29 +71,21 @@ func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	if len(serverInfoAndParams) == 2 {
 | 
						if len(serverInfoAndParams) == 2 {
 | 
				
			||||||
		params, err := url.ParseQuery(serverInfoAndParams[1])
 | 
							params, err := url.ParseQuery(serverInfoAndParams[1])
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return P.Proxy{}, &ParseError{
 | 
								return P.Proxy{}, fmt.Errorf("%w: %s", ErrCannotParseParams, err.Error())
 | 
				
			||||||
				Type:    ErrCannotParseParams,
 | 
					 | 
				
			||||||
				Raw:     proxy,
 | 
					 | 
				
			||||||
				Message: err.Error(),
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if params.Get("obfsparam") != "" {
 | 
							if params.Get("obfsparam") != "" {
 | 
				
			||||||
			obfsParam, err = DecodeBase64(params.Get("obfsparam"))
 | 
								obfsParam, err = utils.DecodeBase64(params.Get("obfsparam"), true)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if params.Get("protoparam") != "" {
 | 
							if params.Get("protoparam") != "" {
 | 
				
			||||||
			protoParam, err = DecodeBase64(params.Get("protoparam"))
 | 
								protoParam, err = utils.DecodeBase64(params.Get("protoparam"), true)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if params.Get("remarks") != "" {
 | 
							if params.Get("remarks") != "" {
 | 
				
			||||||
			remarks, err = DecodeBase64(params.Get("remarks"))
 | 
								remarks, err = utils.DecodeBase64(params.Get("remarks"), true)
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			remarks = server + ":" + strconv.Itoa(port)
 | 
								remarks = server + ":" + strconv.Itoa(port)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return P.Proxy{}, &ParseError{
 | 
								return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
				Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
				Raw:     proxy,
 | 
					 | 
				
			||||||
				Message: err.Error(),
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -112,12 +101,35 @@ func (p *ShadowsocksRParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
			Password:      password,
 | 
								Password:      password,
 | 
				
			||||||
			ObfsParam:     obfsParam,
 | 
								ObfsParam:     obfsParam,
 | 
				
			||||||
			ProtocolParam: protoParam,
 | 
								ProtocolParam: protoParam,
 | 
				
			||||||
 | 
								UDP:           config.UseUDP,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return result, nil
 | 
						return result, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	RegisterParser(&ShadowsocksRParser{})
 | 
						RegisterParser(&ShadowsocksRParser{})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func SplitNRight(s, sep string, n int) []string {
 | 
				
			||||||
 | 
						if n <= 0 {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if n == 1 {
 | 
				
			||||||
 | 
							return []string{s}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						parts := strings.Split(s, sep)
 | 
				
			||||||
 | 
						if len(parts) <= n {
 | 
				
			||||||
 | 
							return parts
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						result := make([]string, n)
 | 
				
			||||||
 | 
						for i, j := len(parts)-1, 0; i >= 0; i, j = i-1, j+1 {
 | 
				
			||||||
 | 
							if j < n-1 {
 | 
				
			||||||
 | 
								result[n-j-1] = parts[len(parts)-j-1]
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								result[0] = strings.Join(parts[:i+1], sep)
 | 
				
			||||||
 | 
								break
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return result
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,7 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	P "github.com/bestnite/sub2clash/model/proxy"
 | 
						P "github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/utils"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SocksParser struct{}
 | 
					type SocksParser struct{}
 | 
				
			||||||
@@ -18,48 +19,33 @@ func (p *SocksParser) SupportMeta() bool {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *SocksParser) GetPrefixes() []string {
 | 
					func (p *SocksParser) GetPrefixes() []string {
 | 
				
			||||||
	return []string{"socks://"}
 | 
						return []string{"socks://", "socks5://"}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *SocksParser) GetType() string {
 | 
					func (p *SocksParser) GetType() string {
 | 
				
			||||||
	return "socks5"
 | 
						return "socks5"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *SocksParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *SocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	link, err := url.Parse(proxy)
 | 
						link, err := url.Parse(proxy)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "url parse error",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	server := link.Hostname()
 | 
						server := link.Hostname()
 | 
				
			||||||
	if server == "" {
 | 
						if server == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server host")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server host",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	portStr := link.Port()
 | 
						portStr := link.Port()
 | 
				
			||||||
	if portStr == "" {
 | 
						if portStr == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server port")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server port",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	port, err := ParsePort(portStr)
 | 
						port, err := ParsePort(portStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
			Type: ErrInvalidPort,
 | 
					 | 
				
			||||||
			Raw:  portStr,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	remarks := link.Fragment
 | 
						remarks := link.Fragment
 | 
				
			||||||
@@ -68,23 +54,26 @@ func (p *SocksParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	remarks = strings.TrimSpace(remarks)
 | 
						remarks = strings.TrimSpace(remarks)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	encodeStr := link.User.Username()
 | 
					 | 
				
			||||||
	var username, password string
 | 
						var username, password string
 | 
				
			||||||
	if encodeStr != "" {
 | 
					
 | 
				
			||||||
		decodeStr, err := DecodeBase64(encodeStr)
 | 
						username = link.User.Username()
 | 
				
			||||||
		splitStr := strings.Split(decodeStr, ":")
 | 
						password, hasPassword := link.User.Password()
 | 
				
			||||||
		if err != nil {
 | 
					
 | 
				
			||||||
			return P.Proxy{}, &ParseError{
 | 
						if !hasPassword && isLikelyBase64(username) {
 | 
				
			||||||
				Type:    ErrInvalidStruct,
 | 
							decodedStr, err := utils.DecodeBase64(username, true)
 | 
				
			||||||
				Message: "url parse error",
 | 
							if err == nil {
 | 
				
			||||||
				Raw:     proxy,
 | 
								usernameAndPassword := strings.SplitN(decodedStr, ":", 2)
 | 
				
			||||||
 | 
								if len(usernameAndPassword) == 2 {
 | 
				
			||||||
 | 
									username = usernameAndPassword[0]
 | 
				
			||||||
 | 
									password = usernameAndPassword[1]
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									username = decodedStr
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		username = splitStr[0]
 | 
					 | 
				
			||||||
		if len(splitStr) == 2 {
 | 
					 | 
				
			||||||
			password = splitStr[1]
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tls, udp := link.Query().Get("tls"), link.Query().Get("udp")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return P.Proxy{
 | 
						return P.Proxy{
 | 
				
			||||||
		Type: p.GetType(),
 | 
							Type: p.GetType(),
 | 
				
			||||||
		Name: remarks,
 | 
							Name: remarks,
 | 
				
			||||||
@@ -93,9 +82,10 @@ func (p *SocksParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
			Port:     port,
 | 
								Port:     port,
 | 
				
			||||||
			UserName: username,
 | 
								UserName: username,
 | 
				
			||||||
			Password: password,
 | 
								Password: password,
 | 
				
			||||||
 | 
								TLS:      tls == "true",
 | 
				
			||||||
 | 
								UDP:      udp == "true",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,45 +26,29 @@ func (p *TrojanParser) GetType() string {
 | 
				
			|||||||
	return "trojan"
 | 
						return "trojan"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *TrojanParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *TrojanParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	link, err := url.Parse(proxy)
 | 
						link, err := url.Parse(proxy)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "url parse error",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	password := link.User.Username()
 | 
						password := link.User.Username()
 | 
				
			||||||
	server := link.Hostname()
 | 
						server := link.Hostname()
 | 
				
			||||||
	if server == "" {
 | 
						if server == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server host")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server host",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	portStr := link.Port()
 | 
						portStr := link.Port()
 | 
				
			||||||
	if portStr == "" {
 | 
						if portStr == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server port")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server port",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	port, err := ParsePort(portStr)
 | 
						port, err := ParsePort(portStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidPort,
 | 
					 | 
				
			||||||
			Message: err.Error(),
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	remarks := link.Fragment
 | 
						remarks := link.Fragment
 | 
				
			||||||
@@ -74,7 +58,17 @@ func (p *TrojanParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	remarks = strings.TrimSpace(remarks)
 | 
						remarks = strings.TrimSpace(remarks)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	query := link.Query()
 | 
						query := link.Query()
 | 
				
			||||||
	network, security, alpnStr, sni, pbk, sid, fp, path, host, serviceName := query.Get("type"), query.Get("security"), query.Get("alpn"), query.Get("sni"), query.Get("pbk"), query.Get("sid"), query.Get("fp"), query.Get("path"), query.Get("host"), query.Get("serviceName")
 | 
						network, security, alpnStr, sni, pbk, sid, fp, path, host, serviceName, udp, insecure := query.Get("type"), query.Get("security"), query.Get("alpn"), query.Get("sni"), query.Get("pbk"), query.Get("sid"), query.Get("fp"), query.Get("path"), query.Get("host"), query.Get("serviceName"), query.Get("udp"), query.Get("allowInsecure")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						insecureBool := insecure == "1"
 | 
				
			||||||
 | 
						result := P.Trojan{
 | 
				
			||||||
 | 
							Server:         server,
 | 
				
			||||||
 | 
							Port:           port,
 | 
				
			||||||
 | 
							Password:       password,
 | 
				
			||||||
 | 
							Network:        network,
 | 
				
			||||||
 | 
							UDP:            udp == "true",
 | 
				
			||||||
 | 
							SkipCertVerify: insecureBool,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var alpn []string
 | 
						var alpn []string
 | 
				
			||||||
	if strings.Contains(alpnStr, ",") {
 | 
						if strings.Contains(alpnStr, ",") {
 | 
				
			||||||
@@ -82,26 +76,23 @@ func (p *TrojanParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		alpn = nil
 | 
							alpn = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if len(alpn) > 0 {
 | 
				
			||||||
	result := P.Trojan{
 | 
							result.ALPN = alpn
 | 
				
			||||||
		Server:   server,
 | 
					 | 
				
			||||||
		Port:     port,
 | 
					 | 
				
			||||||
		Password: password,
 | 
					 | 
				
			||||||
		Network:  network,
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if security == "xtls" || security == "tls" {
 | 
						if fp != "" {
 | 
				
			||||||
		result.ALPN = alpn
 | 
							result.ClientFingerprint = fp
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if sni != "" {
 | 
				
			||||||
		result.SNI = sni
 | 
							result.SNI = sni
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if security == "reality" {
 | 
						if security == "reality" {
 | 
				
			||||||
		result.SNI = sni
 | 
					 | 
				
			||||||
		result.RealityOpts = P.RealityOptions{
 | 
							result.RealityOpts = P.RealityOptions{
 | 
				
			||||||
			PublicKey: pbk,
 | 
								PublicKey: pbk,
 | 
				
			||||||
			ShortID:   sid,
 | 
								ShortID:   sid,
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		result.Fingerprint = fp
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if network == "ws" {
 | 
						if network == "ws" {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,41 +26,29 @@ func (p *VlessParser) GetType() string {
 | 
				
			|||||||
	return "vless"
 | 
						return "vless"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *VlessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	link, err := url.Parse(proxy)
 | 
						link, err := url.Parse(proxy)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "url parse error",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	server := link.Hostname()
 | 
						server := link.Hostname()
 | 
				
			||||||
	if server == "" {
 | 
						if server == "" {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, "missing server host")
 | 
				
			||||||
			Type:    ErrInvalidStruct,
 | 
					 | 
				
			||||||
			Message: "missing server host",
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	portStr := link.Port()
 | 
						portStr := link.Port()
 | 
				
			||||||
	port, err := ParsePort(portStr)
 | 
						port, err := ParsePort(portStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
			Type:    ErrInvalidPort,
 | 
					 | 
				
			||||||
			Message: err.Error(),
 | 
					 | 
				
			||||||
			Raw:     proxy,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	query := link.Query()
 | 
						query := link.Query()
 | 
				
			||||||
	uuid := link.User.Username()
 | 
						uuid := link.User.Username()
 | 
				
			||||||
	flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName, _type := query.Get("flow"), query.Get("security"), query.Get("alpn"), query.Get("sni"), query.Get("allowInsecure"), query.Get("fp"), query.Get("pbk"), query.Get("sid"), query.Get("path"), query.Get("host"), query.Get("serviceName"), query.Get("type")
 | 
						flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName, _type, udp := query.Get("flow"), query.Get("security"), query.Get("alpn"), query.Get("sni"), query.Get("allowInsecure"), query.Get("fp"), query.Get("pbk"), query.Get("sid"), query.Get("path"), query.Get("host"), query.Get("serviceName"), query.Get("type"), query.Get("udp")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	insecureBool := insecure == "1"
 | 
						insecureBool := insecure == "1"
 | 
				
			||||||
	var alpn []string
 | 
						var alpn []string
 | 
				
			||||||
@@ -69,6 +57,7 @@ func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		alpn = nil
 | 
							alpn = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	remarks := link.Fragment
 | 
						remarks := link.Fragment
 | 
				
			||||||
	if remarks == "" {
 | 
						if remarks == "" {
 | 
				
			||||||
		remarks = fmt.Sprintf("%s:%s", server, portStr)
 | 
							remarks = fmt.Sprintf("%s:%s", server, portStr)
 | 
				
			||||||
@@ -80,23 +69,32 @@ func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
		Port:           port,
 | 
							Port:           port,
 | 
				
			||||||
		UUID:           uuid,
 | 
							UUID:           uuid,
 | 
				
			||||||
		Flow:           flow,
 | 
							Flow:           flow,
 | 
				
			||||||
 | 
							UDP:            udp == "true",
 | 
				
			||||||
 | 
							SkipCertVerify: insecureBool,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(alpn) > 0 {
 | 
				
			||||||
 | 
							result.ALPN = alpn
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if fp != "" {
 | 
				
			||||||
 | 
							result.ClientFingerprint = fp
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if sni != "" {
 | 
				
			||||||
 | 
							result.ServerName = sni
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if security == "tls" {
 | 
						if security == "tls" {
 | 
				
			||||||
		result.TLS = true
 | 
							result.TLS = true
 | 
				
			||||||
		result.ALPN = alpn
 | 
					 | 
				
			||||||
		result.SkipCertVerify = insecureBool
 | 
					 | 
				
			||||||
		result.ClientFingerprint = fp
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if security == "reality" {
 | 
						if security == "reality" {
 | 
				
			||||||
		result.TLS = true
 | 
							result.TLS = true
 | 
				
			||||||
		result.ServerName = sni
 | 
					 | 
				
			||||||
		result.RealityOpts = P.RealityOptions{
 | 
							result.RealityOpts = P.RealityOptions{
 | 
				
			||||||
			PublicKey: pbk,
 | 
								PublicKey: pbk,
 | 
				
			||||||
			ShortID:   sid,
 | 
								ShortID:   sid,
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		result.ClientFingerprint = fp
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if _type == "ws" {
 | 
						if _type == "ws" {
 | 
				
			||||||
@@ -125,11 +123,7 @@ func (p *VlessParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		hosts, err := url.QueryUnescape(host)
 | 
							hosts, err := url.QueryUnescape(host)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return P.Proxy{}, &ParseError{
 | 
								return P.Proxy{}, fmt.Errorf("%w: %s", ErrCannotParseParams, err.Error())
 | 
				
			||||||
				Type:    ErrCannotParseParams,
 | 
					 | 
				
			||||||
				Raw:     proxy,
 | 
					 | 
				
			||||||
				Message: err.Error(),
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		result.Network = "http"
 | 
							result.Network = "http"
 | 
				
			||||||
		if hosts != "" {
 | 
							if hosts != "" {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,11 +2,13 @@ package parser
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	P "github.com/bestnite/sub2clash/model/proxy"
 | 
						P "github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/utils"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type VmessJson struct {
 | 
					type VmessJson struct {
 | 
				
			||||||
@@ -45,9 +47,9 @@ func (p *VmessParser) GetType() string {
 | 
				
			|||||||
	return "vmess"
 | 
						return "vmess"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
 | 
					func (p *VmessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
 | 
				
			||||||
	if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
						if !hasPrefix(proxy, p.GetPrefixes()) {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPrefix, proxy)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, prefix := range p.GetPrefixes() {
 | 
						for _, prefix := range p.GetPrefixes() {
 | 
				
			||||||
@@ -56,15 +58,15 @@ func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
			break
 | 
								break
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	base64, err := DecodeBase64(proxy)
 | 
						base64, err := utils.DecodeBase64(proxy, true)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidBase64, Raw: proxy, Message: err.Error()}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var vmess VmessJson
 | 
						var vmess VmessJson
 | 
				
			||||||
	err = json.Unmarshal([]byte(base64), &vmess)
 | 
						err = json.Unmarshal([]byte(base64), &vmess)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return P.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
 | 
							return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var port int
 | 
						var port int
 | 
				
			||||||
@@ -72,11 +74,7 @@ func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	case string:
 | 
						case string:
 | 
				
			||||||
		port, err = ParsePort(vmess.Port.(string))
 | 
							port, err = ParsePort(vmess.Port.(string))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return P.Proxy{}, &ParseError{
 | 
								return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidPort, err.Error())
 | 
				
			||||||
				Type:    ErrInvalidPort,
 | 
					 | 
				
			||||||
				Message: err.Error(),
 | 
					 | 
				
			||||||
				Raw:     proxy,
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case float64:
 | 
						case float64:
 | 
				
			||||||
		port = int(vmess.Port.(float64))
 | 
							port = int(vmess.Port.(float64))
 | 
				
			||||||
@@ -87,7 +85,7 @@ func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
	case string:
 | 
						case string:
 | 
				
			||||||
		aid, err = strconv.Atoi(vmess.Aid.(string))
 | 
							aid, err = strconv.Atoi(vmess.Aid.(string))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return P.Proxy{}, &ParseError{Type: ErrInvalidStruct, Raw: proxy, Message: err.Error()}
 | 
								return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case float64:
 | 
						case float64:
 | 
				
			||||||
		aid = int(vmess.Aid.(float64))
 | 
							aid = int(vmess.Aid.(float64))
 | 
				
			||||||
@@ -102,27 +100,38 @@ func (p *VmessParser) Parse(proxy string) (P.Proxy, error) {
 | 
				
			|||||||
		name = vmess.Ps
 | 
							name = vmess.Ps
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result := P.Vmess{
 | 
					 | 
				
			||||||
		Server:  vmess.Add,
 | 
					 | 
				
			||||||
		Port:    port,
 | 
					 | 
				
			||||||
		UUID:    vmess.Id,
 | 
					 | 
				
			||||||
		AlterID: aid,
 | 
					 | 
				
			||||||
		Cipher:  vmess.Scy,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if vmess.Tls == "tls" {
 | 
					 | 
				
			||||||
	var alpn []string
 | 
						var alpn []string
 | 
				
			||||||
	if strings.Contains(vmess.Alpn, ",") {
 | 
						if strings.Contains(vmess.Alpn, ",") {
 | 
				
			||||||
		alpn = strings.Split(vmess.Alpn, ",")
 | 
							alpn = strings.Split(vmess.Alpn, ",")
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		alpn = nil
 | 
							alpn = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
		result.TLS = true
 | 
					
 | 
				
			||||||
		result.Fingerprint = vmess.Fp
 | 
						result := P.Vmess{
 | 
				
			||||||
 | 
							Server:  vmess.Add,
 | 
				
			||||||
 | 
							Port:    port,
 | 
				
			||||||
 | 
							UUID:    vmess.Id,
 | 
				
			||||||
 | 
							AlterID: aid,
 | 
				
			||||||
 | 
							Cipher:  vmess.Scy,
 | 
				
			||||||
 | 
							UDP:     config.UseUDP,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(alpn) > 0 {
 | 
				
			||||||
		result.ALPN = alpn
 | 
							result.ALPN = alpn
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if vmess.Fp != "" {
 | 
				
			||||||
 | 
							result.ClientFingerprint = vmess.Fp
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if vmess.Sni != "" {
 | 
				
			||||||
		result.ServerName = vmess.Sni
 | 
							result.ServerName = vmess.Sni
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if vmess.Tls == "tls" {
 | 
				
			||||||
 | 
							result.TLS = true
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if vmess.Net == "ws" {
 | 
						if vmess.Net == "ws" {
 | 
				
			||||||
		if vmess.Path == "" {
 | 
							if vmess.Path == "" {
 | 
				
			||||||
			vmess.Path = "/"
 | 
								vmess.Path = "/"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,14 +12,14 @@ import (
 | 
				
			|||||||
	"gopkg.in/yaml.v3"
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func SubHandler(model M.ClashType, template string) func(c *gin.Context) {
 | 
					func ConvertHandler(template string) func(c *gin.Context) {
 | 
				
			||||||
	return func(c *gin.Context) {
 | 
						return func(c *gin.Context) {
 | 
				
			||||||
		query, err := M.ParseSubQuery(c)
 | 
							query, err := M.ParseConvertQuery(c)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			c.String(http.StatusBadRequest, err.Error())
 | 
								c.String(http.StatusBadRequest, err.Error())
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		sub, err := common.BuildSub(model, query, template, config.GlobalConfig.CacheExpire, config.GlobalConfig.RequestRetryTimes)
 | 
							sub, err := common.BuildSub(query.ClashType, query, template, config.GlobalConfig.CacheExpire, config.GlobalConfig.RequestRetryTimes)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			c.String(http.StatusInternalServerError, err.Error())
 | 
								c.String(http.StatusInternalServerError, err.Error())
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
@@ -27,11 +27,10 @@ func SubHandler(model M.ClashType, template string) func(c *gin.Context) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		if len(query.Subs) == 1 {
 | 
							if len(query.Subs) == 1 {
 | 
				
			||||||
			userInfoHeader, err := common.FetchSubscriptionUserInfo(query.Subs[0], "clash", config.GlobalConfig.RequestRetryTimes)
 | 
								userInfoHeader, err := common.FetchSubscriptionUserInfo(query.Subs[0], "clash", config.GlobalConfig.RequestRetryTimes)
 | 
				
			||||||
			if err != nil {
 | 
								if err == nil {
 | 
				
			||||||
				c.String(http.StatusInternalServerError, err.Error())
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
				c.Header("subscription-userinfo", userInfoHeader)
 | 
									c.Header("subscription-userinfo", userInfoHeader)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if query.NodeListMode {
 | 
							if query.NodeListMode {
 | 
				
			||||||
			nodelist := M.NodeList{}
 | 
								nodelist := M.NodeList{}
 | 
				
			||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package handler
 | 
					package handler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@@ -141,7 +142,6 @@ func UpdateLinkHandler(c *gin.Context) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetRawConfHandler(c *gin.Context) {
 | 
					func GetRawConfHandler(c *gin.Context) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	hash := c.Param("hash")
 | 
						hash := c.Param("hash")
 | 
				
			||||||
	password := c.Query("password")
 | 
						password := c.Query("password")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -168,7 +168,19 @@ func GetRawConfHandler(c *gin.Context) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	response, err := http.Get(strings.TrimSuffix(config.GlobalConfig.Address, "/") + "/" + shortLink.Url)
 | 
						scheme := "http"
 | 
				
			||||||
 | 
						if c.Request.TLS != nil {
 | 
				
			||||||
 | 
							scheme = "https"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						host := c.Request.Host
 | 
				
			||||||
 | 
						targetPath := strings.TrimPrefix(shortLink.Url, "/")
 | 
				
			||||||
 | 
						requestURL := fmt.Sprintf("%s://%s/%s", scheme, host, targetPath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						client := &http.Client{
 | 
				
			||||||
 | 
							Timeout: 30 * time.Second, // 30秒超时
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						response, err := client.Get(requestURL)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		respondWithError(c, http.StatusInternalServerError, "请求错误: "+err.Error())
 | 
							respondWithError(c, http.StatusInternalServerError, "请求错误: "+err.Error())
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
@@ -185,7 +197,6 @@ func GetRawConfHandler(c *gin.Context) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetRawConfUriHandler(c *gin.Context) {
 | 
					func GetRawConfUriHandler(c *gin.Context) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	hash := c.Query("hash")
 | 
						hash := c.Query("hash")
 | 
				
			||||||
	password := c.Query("password")
 | 
						password := c.Query("password")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,6 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"github.com/bestnite/sub2clash/config"
 | 
						"github.com/bestnite/sub2clash/config"
 | 
				
			||||||
	"github.com/bestnite/sub2clash/constant"
 | 
						"github.com/bestnite/sub2clash/constant"
 | 
				
			||||||
	"github.com/bestnite/sub2clash/model"
 | 
					 | 
				
			||||||
	"github.com/bestnite/sub2clash/server/handler"
 | 
						"github.com/bestnite/sub2clash/server/handler"
 | 
				
			||||||
	"github.com/bestnite/sub2clash/server/middleware"
 | 
						"github.com/bestnite/sub2clash/server/middleware"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -40,8 +39,7 @@ func SetRoute(r *gin.Engine) {
 | 
				
			|||||||
			)
 | 
								)
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	r.GET("/clash", middleware.ZapLogger(), handler.SubHandler(model.Clash, config.GlobalConfig.ClashTemplate))
 | 
						r.GET("/convert/:config", middleware.ZapLogger(), handler.ConvertHandler(config.GlobalConfig.ClashTemplate))
 | 
				
			||||||
	r.GET("/meta", middleware.ZapLogger(), handler.SubHandler(model.ClashMeta, config.GlobalConfig.MetaTemplate))
 | 
					 | 
				
			||||||
	r.GET("/s/:hash", middleware.ZapLogger(), handler.GetRawConfHandler)
 | 
						r.GET("/s/:hash", middleware.ZapLogger(), handler.GetRawConfHandler)
 | 
				
			||||||
	r.POST("/short", middleware.ZapLogger(), handler.GenerateLinkHandler)
 | 
						r.POST("/short", middleware.ZapLogger(), handler.GenerateLinkHandler)
 | 
				
			||||||
	r.PUT("/short", middleware.ZapLogger(), handler.UpdateLinkHandler)
 | 
						r.PUT("/short", middleware.ZapLogger(), handler.UpdateLinkHandler)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,5 @@
 | 
				
			|||||||
<!DOCTYPE html>
 | 
					<!DOCTYPE html>
 | 
				
			||||||
<html lang="zh-CN">
 | 
					<html lang="zh-CN" data-bs-theme="light">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<head>
 | 
					<head>
 | 
				
			||||||
    <meta charset="UTF-8" />
 | 
					    <meta charset="UTF-8" />
 | 
				
			||||||
@@ -28,10 +28,38 @@
 | 
				
			|||||||
            height: 25px;
 | 
					            height: 25px;
 | 
				
			||||||
            width: 25px;
 | 
					            width: 25px;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* 主题切换按钮样式 */
 | 
				
			||||||
 | 
					        .theme-toggle {
 | 
				
			||||||
 | 
					            position: fixed;
 | 
				
			||||||
 | 
					            top: 20px;
 | 
				
			||||||
 | 
					            right: 20px;
 | 
				
			||||||
 | 
					            z-index: 1000;
 | 
				
			||||||
 | 
					            border: none;
 | 
				
			||||||
 | 
					            border-radius: 50%;
 | 
				
			||||||
 | 
					            width: 50px;
 | 
				
			||||||
 | 
					            height: 50px;
 | 
				
			||||||
 | 
					            font-size: 20px;
 | 
				
			||||||
 | 
					            display: flex;
 | 
				
			||||||
 | 
					            align-items: center;
 | 
				
			||||||
 | 
					            justify-content: center;
 | 
				
			||||||
 | 
					            transition: all 0.3s ease;
 | 
				
			||||||
 | 
					            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        .theme-toggle:hover {
 | 
				
			||||||
 | 
					            transform: scale(1.1);
 | 
				
			||||||
 | 
					            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    </style>
 | 
					    </style>
 | 
				
			||||||
</head>
 | 
					</head>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<body class="bg-light">
 | 
					<body>
 | 
				
			||||||
 | 
					    <!-- 主题切换按钮 -->
 | 
				
			||||||
 | 
					    <button class="theme-toggle btn btn-outline-secondary" onclick="toggleTheme()" title="切换深色/浅色模式">
 | 
				
			||||||
 | 
					        <span id="theme-icon">🌙</span>
 | 
				
			||||||
 | 
					    </button>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <div class="container mt-5">
 | 
					    <div class="container mt-5">
 | 
				
			||||||
        <div class="mb-4">
 | 
					        <div class="mb-4">
 | 
				
			||||||
            <h2>sub2clash</h2>
 | 
					            <h2>sub2clash</h2>
 | 
				
			||||||
@@ -54,8 +82,8 @@
 | 
				
			|||||||
        <div class="form-group mb-3">
 | 
					        <div class="form-group mb-3">
 | 
				
			||||||
            <label for="endpoint">客户端类型:</label>
 | 
					            <label for="endpoint">客户端类型:</label>
 | 
				
			||||||
            <select class="form-control" id="endpoint" name="endpoint">
 | 
					            <select class="form-control" id="endpoint" name="endpoint">
 | 
				
			||||||
                <option value="clash">Clash</option>
 | 
					                <option value="1">Clash</option>
 | 
				
			||||||
                <option value="meta">Clash.Meta</option>
 | 
					                <option value="2" selected>Clash.Meta</option>
 | 
				
			||||||
            </select>
 | 
					            </select>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
        <!-- Template -->
 | 
					        <!-- Template -->
 | 
				
			||||||
@@ -75,9 +103,9 @@
 | 
				
			|||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
        <!-- User Agent -->
 | 
					        <!-- User Agent -->
 | 
				
			||||||
        <div class="form-group mb-3">
 | 
					        <div class="form-group mb-3">
 | 
				
			||||||
            <label for="user-agent">ua标识:</label>
 | 
					            <label for="user-agent">UA 标识:</label>
 | 
				
			||||||
            <textarea class="form-control" id="user-agent" name="user-agent"
 | 
					            <textarea class="form-control" id="user-agent" name="user-agent"
 | 
				
			||||||
                placeholder="用于获取订阅的http请求中的user-agent标识(可选)" rows="3"></textarea>
 | 
					                placeholder="用于获取订阅的 http 请求中的 User-Agent 标识(可选)" rows="3"></textarea>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
        <!-- Refresh -->
 | 
					        <!-- Refresh -->
 | 
				
			||||||
        <div class="form-check mb-3">
 | 
					        <div class="form-check mb-3">
 | 
				
			||||||
@@ -104,6 +132,11 @@
 | 
				
			|||||||
            <input class="form-check-input" id="igcg" name="igcg" type="checkbox" />
 | 
					            <input class="form-check-input" id="igcg" name="igcg" type="checkbox" />
 | 
				
			||||||
            <label class="form-check-label" for="igcg">不输出国家策略组</label>
 | 
					            <label class="form-check-label" for="igcg">不输出国家策略组</label>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 | 
					        <!-- Use UDP -->
 | 
				
			||||||
 | 
					        <div class="form-check mb-3">
 | 
				
			||||||
 | 
					            <input class="form-check-input" id="useUDP" name="useUDP" type="checkbox" />
 | 
				
			||||||
 | 
					            <label class="form-check-label" for="useUDP">使用 UDP</label>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
        <!-- Rule Provider -->
 | 
					        <!-- Rule Provider -->
 | 
				
			||||||
        <div class="form-group mb-3" id="ruleProviderGroup">
 | 
					        <div class="form-group mb-3" id="ruleProviderGroup">
 | 
				
			||||||
            <label>Rule Provider:</label>
 | 
					            <label>Rule Provider:</label>
 | 
				
			||||||
@@ -145,8 +178,9 @@
 | 
				
			|||||||
        <div class="form-group mb-5">
 | 
					        <div class="form-group mb-5">
 | 
				
			||||||
            <label for="apiLink">配置链接:</label>
 | 
					            <label for="apiLink">配置链接:</label>
 | 
				
			||||||
            <div class="input-group mb-2">
 | 
					            <div class="input-group mb-2">
 | 
				
			||||||
                <input class="form-control bg-light" id="apiLink" type="text" placeholder="链接" readonly
 | 
					                <input class="form-control" id="apiLink" type="text" placeholder="链接" readonly
 | 
				
			||||||
                    style="cursor: not-allowed;" />
 | 
					                    style="cursor: not-allowed;" />
 | 
				
			||||||
 | 
					                <button class="btn btn-primary" onclick="generateURL()" type="button">生成配置</button>
 | 
				
			||||||
                <button class="btn btn-primary" onclick="copyToClipboard('apiLink',this)" type="button">
 | 
					                <button class="btn btn-primary" onclick="copyToClipboard('apiLink',this)" type="button">
 | 
				
			||||||
                    复制链接
 | 
					                    复制链接
 | 
				
			||||||
                </button>
 | 
					                </button>
 | 
				
			||||||
@@ -162,7 +196,7 @@
 | 
				
			|||||||
                </button>
 | 
					                </button>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <div class="input-group">
 | 
					            <div class="input-group">
 | 
				
			||||||
                <input class="form-control bg-light" id="apiShortLink" type="text" placeholder="短链接" readonly
 | 
					                <input class="form-control" id="apiShortLink" type="text" placeholder="短链接" readonly
 | 
				
			||||||
                    style="cursor: not-allowed;" />
 | 
					                    style="cursor: not-allowed;" />
 | 
				
			||||||
                <button class="btn btn-primary" onclick="updateShortLink()" type="button">
 | 
					                <button class="btn btn-primary" onclick="updateShortLink()" type="button">
 | 
				
			||||||
                    更新短链
 | 
					                    更新短链
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,9 @@
 | 
				
			|||||||
function setInputReadOnly(input, readonly) {
 | 
					function setInputReadOnly(input, readonly) {
 | 
				
			||||||
  if (readonly) {
 | 
					  if (readonly) {
 | 
				
			||||||
    input.readOnly = true;
 | 
					    input.readOnly = true;
 | 
				
			||||||
    input.classList.add('bg-light');
 | 
					 | 
				
			||||||
    input.style.cursor = 'not-allowed';
 | 
					    input.style.cursor = 'not-allowed';
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    input.readOnly = false;
 | 
					    input.readOnly = false;
 | 
				
			||||||
    input.classList.remove('bg-light');
 | 
					 | 
				
			||||||
    input.style.cursor = 'auto';
 | 
					    input.style.cursor = 'auto';
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -19,6 +17,7 @@ function clearExistingValues() {
 | 
				
			|||||||
  document.getElementById("autoTest").checked = false;
 | 
					  document.getElementById("autoTest").checked = false;
 | 
				
			||||||
  document.getElementById("lazy").checked = false;
 | 
					  document.getElementById("lazy").checked = false;
 | 
				
			||||||
  document.getElementById("igcg").checked = false;
 | 
					  document.getElementById("igcg").checked = false;
 | 
				
			||||||
 | 
					  document.getElementById("useUDP").checked = false;
 | 
				
			||||||
  document.getElementById("template").value = "";
 | 
					  document.getElementById("template").value = "";
 | 
				
			||||||
  document.getElementById("sort").value = "nameasc";
 | 
					  document.getElementById("sort").value = "nameasc";
 | 
				
			||||||
  document.getElementById("remove").value = "";
 | 
					  document.getElementById("remove").value = "";
 | 
				
			||||||
@@ -50,85 +49,57 @@ function clearExistingValues() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function generateURI() {
 | 
					function generateURI() {
 | 
				
			||||||
  const queryParams = [];
 | 
					  const config = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取 API Endpoint
 | 
					  config.clashType = parseInt(document.getElementById("endpoint").value);
 | 
				
			||||||
  const endpoint = document.getElementById("endpoint").value;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取并组合订阅链接
 | 
					 | 
				
			||||||
  let subLines = document
 | 
					  let subLines = document
 | 
				
			||||||
    .getElementById("sub")
 | 
					    .getElementById("sub")
 | 
				
			||||||
    .value.split("\n")
 | 
					    .value.split("\n")
 | 
				
			||||||
    .filter((line) => line.trim() !== "");
 | 
					    .filter((line) => line.trim() !== "");
 | 
				
			||||||
  let noSub = false;
 | 
					 | 
				
			||||||
  // 去除 subLines 中空元素
 | 
					 | 
				
			||||||
  subLines = subLines.map((item) => {
 | 
					 | 
				
			||||||
    if (item !== "") {
 | 
					 | 
				
			||||||
      return item;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
  if (subLines.length > 0) {
 | 
					  if (subLines.length > 0) {
 | 
				
			||||||
    queryParams.push(`sub=${encodeURIComponent(subLines.join(","))}`);
 | 
					    config.subscriptions = subLines;
 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    noSub = true;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取并组合节点分享链接
 | 
					 | 
				
			||||||
  let proxyLines = document
 | 
					  let proxyLines = document
 | 
				
			||||||
    .getElementById("proxy")
 | 
					    .getElementById("proxy")
 | 
				
			||||||
    .value.split("\n")
 | 
					    .value.split("\n")
 | 
				
			||||||
    .filter((line) => line.trim() !== "");
 | 
					    .filter((line) => line.trim() !== "");
 | 
				
			||||||
  let noProxy = false;
 | 
					 | 
				
			||||||
  // 去除 proxyLines 中空元素
 | 
					 | 
				
			||||||
  proxyLines = proxyLines.map((item) => {
 | 
					 | 
				
			||||||
    if (item !== "") {
 | 
					 | 
				
			||||||
      return item;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
  if (proxyLines.length > 0) {
 | 
					  if (proxyLines.length > 0) {
 | 
				
			||||||
    queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(","))}`);
 | 
					    config.proxies = proxyLines;
 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    noProxy = true;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (noSub && noProxy) {
 | 
					
 | 
				
			||||||
    // alert("订阅链接和节点分享链接不能同时为空!");
 | 
					  if (
 | 
				
			||||||
 | 
					    (config.subscriptions === undefined || config.subscriptions.length === 0) &&
 | 
				
			||||||
 | 
					    (config.proxies === undefined || config.proxies.length === 0)
 | 
				
			||||||
 | 
					  ) {
 | 
				
			||||||
    return "";
 | 
					    return "";
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取订阅user-agent标识
 | 
					  config.userAgent = document.getElementById("user-agent").value;
 | 
				
			||||||
  const userAgent = document.getElementById("user-agent").value;
 | 
					 | 
				
			||||||
  queryParams.push(`userAgent=${encodeURIComponent(userAgent)}`);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取复选框的值
 | 
					  config.refresh = document.getElementById("refresh").checked;
 | 
				
			||||||
  const refresh = document.getElementById("refresh").checked;
 | 
					  config.autoTest = document.getElementById("autoTest").checked;
 | 
				
			||||||
  queryParams.push(`refresh=${refresh ? "true" : "false"}`);
 | 
					  config.lazy = document.getElementById("lazy").checked;
 | 
				
			||||||
  const autoTest = document.getElementById("autoTest").checked;
 | 
					  config.nodeList = document.getElementById("nodeList").checked;
 | 
				
			||||||
  queryParams.push(`autoTest=${autoTest ? "true" : "false"}`);
 | 
					  config.ignoreCountryGroup = document.getElementById("igcg").checked;
 | 
				
			||||||
  const lazy = document.getElementById("lazy").checked;
 | 
					  config.useUDP = document.getElementById("useUDP").checked;
 | 
				
			||||||
  queryParams.push(`lazy=${lazy ? "true" : "false"}`);
 | 
					 | 
				
			||||||
  const nodeList = document.getElementById("nodeList").checked;
 | 
					 | 
				
			||||||
  queryParams.push(`nodeList=${nodeList ? "true" : "false"}`);
 | 
					 | 
				
			||||||
  const igcg = document.getElementById("igcg").checked;
 | 
					 | 
				
			||||||
  queryParams.push(`ignoreCountryGroup=${igcg ? "true" : "false"}`);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取模板链接或名称(如果存在)
 | 
					 | 
				
			||||||
  const template = document.getElementById("template").value;
 | 
					  const template = document.getElementById("template").value;
 | 
				
			||||||
  if (template.trim() !== "") {
 | 
					  if (template.trim() !== "") {
 | 
				
			||||||
    queryParams.push(`template=${encodeURIComponent(template)}`);
 | 
					    config.template = template;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取Rule Provider和规则
 | 
					  const ruleProvidersElements = document.getElementsByName("ruleProvider");
 | 
				
			||||||
  const ruleProviders = document.getElementsByName("ruleProvider");
 | 
					  if (ruleProvidersElements.length > 0) {
 | 
				
			||||||
  const rules = document.getElementsByName("rule");
 | 
					    const ruleProviders = [];
 | 
				
			||||||
  let providers = [];
 | 
					    for (let i = 0; i < ruleProvidersElements.length / 5; i++) {
 | 
				
			||||||
  for (let i = 0; i < ruleProviders.length / 5; i++) {
 | 
					 | 
				
			||||||
      let baseIndex = i * 5;
 | 
					      let baseIndex = i * 5;
 | 
				
			||||||
    let behavior = ruleProviders[baseIndex].value;
 | 
					      let behavior = ruleProvidersElements[baseIndex].value;
 | 
				
			||||||
    let url = ruleProviders[baseIndex + 1].value;
 | 
					      let url = ruleProvidersElements[baseIndex + 1].value;
 | 
				
			||||||
    let group = ruleProviders[baseIndex + 2].value;
 | 
					      let group = ruleProvidersElements[baseIndex + 2].value;
 | 
				
			||||||
    let prepend = ruleProviders[baseIndex + 3].value;
 | 
					      let prepend = ruleProvidersElements[baseIndex + 3].value;
 | 
				
			||||||
    let name = ruleProviders[baseIndex + 4].value;
 | 
					      let name = ruleProvidersElements[baseIndex + 4].value;
 | 
				
			||||||
    // 是否存在空值
 | 
					 | 
				
			||||||
      if (
 | 
					      if (
 | 
				
			||||||
        behavior.trim() === "" ||
 | 
					        behavior.trim() === "" ||
 | 
				
			||||||
        url.trim() === "" ||
 | 
					        url.trim() === "" ||
 | 
				
			||||||
@@ -136,53 +107,80 @@ function generateURI() {
 | 
				
			|||||||
        prepend.trim() === "" ||
 | 
					        prepend.trim() === "" ||
 | 
				
			||||||
        name.trim() === ""
 | 
					        name.trim() === ""
 | 
				
			||||||
      ) {
 | 
					      ) {
 | 
				
			||||||
      // alert("Rule Provider 中存在空值,请检查后重试!");
 | 
					 | 
				
			||||||
        return "";
 | 
					        return "";
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    providers.push(`[${behavior},${url},${group},${prepend},${name}]`);
 | 
					      ruleProviders.push({
 | 
				
			||||||
 | 
					        behavior: behavior,
 | 
				
			||||||
 | 
					        url: url,
 | 
				
			||||||
 | 
					        group: group,
 | 
				
			||||||
 | 
					        prepend: prepend.toLowerCase() === "true",
 | 
				
			||||||
 | 
					        name: name,
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (ruleProviders.length > 0) {
 | 
				
			||||||
 | 
					      config.ruleProviders = ruleProviders;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  queryParams.push(`ruleProvider=${encodeURIComponent(providers.join(","))}`);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let ruleList = [];
 | 
					  const rulesElements = document.getElementsByName("rule");
 | 
				
			||||||
  for (let i = 0; i < rules.length / 2; i++) {
 | 
					  if (rulesElements.length > 0) {
 | 
				
			||||||
    if (rules[i * 2].value.trim() !== "") {
 | 
					    const rules = [];
 | 
				
			||||||
      let rule = rules[i * 2].value;
 | 
					    for (let i = 0; i < rulesElements.length / 2; i++) {
 | 
				
			||||||
      let prepend = rules[i * 2 + 1].value;
 | 
					      if (rulesElements[i * 2].value.trim() !== "") {
 | 
				
			||||||
      // 是否存在空值
 | 
					        let rule = rulesElements[i * 2].value;
 | 
				
			||||||
 | 
					        let prepend = rulesElements[i * 2 + 1].value;
 | 
				
			||||||
        if (rule.trim() === "" || prepend.trim() === "") {
 | 
					        if (rule.trim() === "" || prepend.trim() === "") {
 | 
				
			||||||
        // alert("Rule 中存在空值,请检查后重试!");
 | 
					 | 
				
			||||||
          return "";
 | 
					          return "";
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      ruleList.push(`[${rule},${prepend}]`);
 | 
					        rules.push({
 | 
				
			||||||
 | 
					          rule: rule,
 | 
				
			||||||
 | 
					          prepend: prepend.toLowerCase() === "true",
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (rules.length > 0) {
 | 
				
			||||||
 | 
					      config.rules = rules;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  queryParams.push(`rule=${encodeURIComponent(ruleList.join(","))}`);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取排序策略
 | 
					  config.sort = document.getElementById("sort").value;
 | 
				
			||||||
  const sort = document.getElementById("sort").value;
 | 
					 | 
				
			||||||
  queryParams.push(`sort=${sort}`);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取删除节点的正则表达式
 | 
					 | 
				
			||||||
  const remove = document.getElementById("remove").value;
 | 
					  const remove = document.getElementById("remove").value;
 | 
				
			||||||
  if (remove.trim() !== "") {
 | 
					  if (remove.trim() !== "") {
 | 
				
			||||||
    queryParams.push(`remove=${encodeURIComponent(remove)}`);
 | 
					    config.remove = remove;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // 获取替换节点名称的正则表达式
 | 
					  const replacesElements = document.getElementsByName("replace");
 | 
				
			||||||
  let replaceList = [];
 | 
					  if (replacesElements.length > 0) {
 | 
				
			||||||
  const replaces = document.getElementsByName("replace");
 | 
					    const replace = {};
 | 
				
			||||||
  for (let i = 0; i < replaces.length / 2; i++) {
 | 
					    for (let i = 0; i < replacesElements.length / 2; i++) {
 | 
				
			||||||
    let replaceStr = `<${replaces[i * 2].value}>`;
 | 
					      let replaceStr = replacesElements[i * 2].value;
 | 
				
			||||||
    let replaceTo = `<${replaces[i * 2 + 1].value}>`;
 | 
					      let replaceTo = replacesElements[i * 2 + 1].value;
 | 
				
			||||||
      if (replaceStr.trim() === "") {
 | 
					      if (replaceStr.trim() === "") {
 | 
				
			||||||
      // alert("重命名设置中存在空值,请检查后重试!");
 | 
					 | 
				
			||||||
        return "";
 | 
					        return "";
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    replaceList.push(`[${replaceStr},${replaceTo}]`);
 | 
					      replace[replaceStr] = replaceTo;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (Object.keys(replace).length > 0) {
 | 
				
			||||||
 | 
					      config.replace = replace;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  queryParams.push(`replace=${encodeURIComponent(replaceList.join(","))}`);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return `${endpoint}?${queryParams.join("&")}`;
 | 
					  const jsonString = JSON.stringify(config);
 | 
				
			||||||
 | 
					  // 解决 btoa 中文报错,使用 TextEncoder 进行 UTF-8 编码再 base64
 | 
				
			||||||
 | 
					  function base64EncodeUnicode(str) {
 | 
				
			||||||
 | 
					    const bytes = new TextEncoder().encode(str);
 | 
				
			||||||
 | 
					    let binary = '';
 | 
				
			||||||
 | 
					    bytes.forEach((b) => binary += String.fromCharCode(b));
 | 
				
			||||||
 | 
					    return btoa(binary);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  const encoded = base64EncodeUnicode(jsonString);
 | 
				
			||||||
 | 
					  const urlSafeBase64 = encoded
 | 
				
			||||||
 | 
					    .replace(/\+/g, "-")
 | 
				
			||||||
 | 
					    .replace(/\//g, "_")
 | 
				
			||||||
 | 
					    .replace(/=/g, "");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return `convert/${urlSafeBase64}`;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 将输入框中的 URL 解析为参数
 | 
					// 将输入框中的 URL 解析为参数
 | 
				
			||||||
@@ -215,11 +213,11 @@ async function parseInputURL() {
 | 
				
			|||||||
    q.append("password", password);
 | 
					    q.append("password", password);
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const response = await axios.get("./short?" + q.toString());
 | 
					      const response = await axios.get("./short?" + q.toString());
 | 
				
			||||||
      url = new URL(window.location.href + response.data);
 | 
					      url = new URL(response.data, window.location.href);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // 回显配置链接
 | 
					      // 回显配置链接
 | 
				
			||||||
      const apiLinkInput = document.querySelector("#apiLink");
 | 
					      const apiLinkInput = document.querySelector("#apiLink");
 | 
				
			||||||
      apiLinkInput.value = `${window.location.origin}${window.location.pathname}${response.data}`;
 | 
					      apiLinkInput.value = url.href;
 | 
				
			||||||
      setInputReadOnly(apiLinkInput, true);
 | 
					      setInputReadOnly(apiLinkInput, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // 回显短链相关信息
 | 
					      // 回显短链相关信息
 | 
				
			||||||
@@ -247,94 +245,106 @@ async function parseInputURL() {
 | 
				
			|||||||
      alert("获取短链失败,请检查密码!");
 | 
					      alert("获取短链失败,请检查密码!");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  let params = new URLSearchParams(url.search);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // 分配值到对应的输入框
 | 
					 | 
				
			||||||
  const pathSections = url.pathname.split("/");
 | 
					  const pathSections = url.pathname.split("/");
 | 
				
			||||||
  const lastSection = pathSections[pathSections.length - 1];
 | 
					  const convertIndex = pathSections.findIndex((s) => s === "convert");
 | 
				
			||||||
  const clientTypeSelect = document.getElementById("endpoint");
 | 
					
 | 
				
			||||||
  switch (lastSection.toLowerCase()) {
 | 
					  if (convertIndex === -1 || convertIndex + 1 >= pathSections.length) {
 | 
				
			||||||
    case "meta":
 | 
					    alert("无效的配置链接,请确认链接为新版格式。");
 | 
				
			||||||
      clientTypeSelect.value = "meta";
 | 
					    return;
 | 
				
			||||||
      break;
 | 
					  }
 | 
				
			||||||
    case "clash":
 | 
					  const base64Config = pathSections[convertIndex + 1];
 | 
				
			||||||
    default:
 | 
					  let config;
 | 
				
			||||||
      clientTypeSelect.value = "clash";
 | 
					  try {
 | 
				
			||||||
      break;
 | 
					    const regularBase64 = base64Config.replace(/-/g, "+").replace(/_/g, "/");
 | 
				
			||||||
 | 
					    const decodedStr = atob(regularBase64);
 | 
				
			||||||
 | 
					    config = JSON.parse(decodeURIComponent(escape(decodedStr)));
 | 
				
			||||||
 | 
					  } catch (e) {
 | 
				
			||||||
 | 
					    alert("解析配置失败!");
 | 
				
			||||||
 | 
					    console.error(e);
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("sub")) {
 | 
					  document.getElementById("endpoint").value = config.clashType || "1";
 | 
				
			||||||
    document.getElementById("sub").value = decodeURIComponent(params.get("sub"))
 | 
					
 | 
				
			||||||
      .split(",")
 | 
					  if (config.subscriptions) {
 | 
				
			||||||
      .join("\n");
 | 
					    document.getElementById("sub").value = config.subscriptions.join("\n");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("proxy")) {
 | 
					  if (config.proxies) {
 | 
				
			||||||
    document.getElementById("proxy").value = decodeURIComponent(
 | 
					    document.getElementById("proxy").value = config.proxies.join("\n");
 | 
				
			||||||
      params.get("proxy")
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
      .split(",")
 | 
					 | 
				
			||||||
      .join("\n");
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("refresh")) {
 | 
					  if (config.refresh) {
 | 
				
			||||||
    document.getElementById("refresh").checked =
 | 
					    document.getElementById("refresh").checked = config.refresh;
 | 
				
			||||||
      params.get("refresh") === "true";
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("autoTest")) {
 | 
					  if (config.autoTest) {
 | 
				
			||||||
    document.getElementById("autoTest").checked =
 | 
					    document.getElementById("autoTest").checked = config.autoTest;
 | 
				
			||||||
      params.get("autoTest") === "true";
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("lazy")) {
 | 
					  if (config.lazy) {
 | 
				
			||||||
    document.getElementById("lazy").checked = params.get("lazy") === "true";
 | 
					    document.getElementById("lazy").checked = config.lazy;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("template")) {
 | 
					  if (config.template) {
 | 
				
			||||||
    document.getElementById("template").value = decodeURIComponent(
 | 
					    document.getElementById("template").value = config.template;
 | 
				
			||||||
      params.get("template")
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("sort")) {
 | 
					  if (config.sort) {
 | 
				
			||||||
    document.getElementById("sort").value = params.get("sort");
 | 
					    document.getElementById("sort").value = config.sort;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("remove")) {
 | 
					  if (config.remove) {
 | 
				
			||||||
    document.getElementById("remove").value = decodeURIComponent(
 | 
					    document.getElementById("remove").value = config.remove;
 | 
				
			||||||
      params.get("remove")
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("userAgent")) {
 | 
					  if (config.userAgent) {
 | 
				
			||||||
    document.getElementById("user-agent").value = decodeURIComponent(
 | 
					    document.getElementById("user-agent").value = config.userAgent;
 | 
				
			||||||
      params.get("userAgent")
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("ignoreCountryGroup")) {
 | 
					  if (config.ignoreCountryGroup) {
 | 
				
			||||||
    document.getElementById("igcg").checked = 
 | 
					    document.getElementById("igcg").checked = config.ignoreCountryGroup;
 | 
				
			||||||
      params.get("ignoreCountryGroup") === "true";
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("replace")) {
 | 
					  if (config.replace) {
 | 
				
			||||||
    parseAndFillReplaceParams(decodeURIComponent(params.get("replace")));
 | 
					    const replaceGroup = document.getElementById("replaceGroup");
 | 
				
			||||||
 | 
					    for (const original in config.replace) {
 | 
				
			||||||
 | 
					      const div = createReplace();
 | 
				
			||||||
 | 
					      div.children[0].value = original;
 | 
				
			||||||
 | 
					      div.children[1].value = config.replace[original];
 | 
				
			||||||
 | 
					      replaceGroup.appendChild(div);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("ruleProvider")) {
 | 
					  if (config.ruleProviders) {
 | 
				
			||||||
    parseAndFillRuleProviderParams(
 | 
					    const ruleProviderGroup = document.getElementById("ruleProviderGroup");
 | 
				
			||||||
      decodeURIComponent(params.get("ruleProvider"))
 | 
					    for (const p of config.ruleProviders) {
 | 
				
			||||||
    );
 | 
					      const div = createRuleProvider();
 | 
				
			||||||
 | 
					      div.children[0].value = p.behavior;
 | 
				
			||||||
 | 
					      div.children[1].value = p.url;
 | 
				
			||||||
 | 
					      div.children[2].value = p.group;
 | 
				
			||||||
 | 
					      div.children[3].value = p.prepend;
 | 
				
			||||||
 | 
					      div.children[4].value = p.name;
 | 
				
			||||||
 | 
					      ruleProviderGroup.appendChild(div);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("rule")) {
 | 
					  if (config.rules) {
 | 
				
			||||||
    parseAndFillRuleParams(decodeURIComponent(params.get("rule")));
 | 
					    const ruleGroup = document.getElementById("ruleGroup");
 | 
				
			||||||
 | 
					    for (const r of config.rules) {
 | 
				
			||||||
 | 
					      const div = createRule();
 | 
				
			||||||
 | 
					      div.children[0].value = r.rule;
 | 
				
			||||||
 | 
					      div.children[1].value = r.prepend;
 | 
				
			||||||
 | 
					      ruleGroup.appendChild(div);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (params.has("nodeList")) {
 | 
					  if (config.nodeList) {
 | 
				
			||||||
    document.getElementById("nodeList").checked =
 | 
					    document.getElementById("nodeList").checked = config.nodeList;
 | 
				
			||||||
      params.get("nodeList") === "true";
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (config.useUDP) {
 | 
				
			||||||
 | 
					    document.getElementById("useUDP").checked = config.useUDP;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -346,51 +356,6 @@ function clearInputGroup(groupId) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function parseAndFillReplaceParams(replaceParams) {
 | 
					 | 
				
			||||||
  const replaceGroup = document.getElementById("replaceGroup");
 | 
					 | 
				
			||||||
  let matches;
 | 
					 | 
				
			||||||
  const regex = /\[(<.*?>),(<.*?>)\]/g;
 | 
					 | 
				
			||||||
  const str = decodeURIComponent(replaceParams);
 | 
					 | 
				
			||||||
  while ((matches = regex.exec(str)) !== null) {
 | 
					 | 
				
			||||||
    const div = createReplace();
 | 
					 | 
				
			||||||
    const original = matches[1].slice(1, -1); // Remove < and >
 | 
					 | 
				
			||||||
    const replacement = matches[2].slice(1, -1); // Remove < and >
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    div.children[0].value = original;
 | 
					 | 
				
			||||||
    div.children[1].value = replacement;
 | 
					 | 
				
			||||||
    replaceGroup.appendChild(div);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function parseAndFillRuleProviderParams(ruleProviderParams) {
 | 
					 | 
				
			||||||
  const ruleProviderGroup = document.getElementById("ruleProviderGroup");
 | 
					 | 
				
			||||||
  let matches;
 | 
					 | 
				
			||||||
  const regex = /\[(.*?),(.*?),(.*?),(.*?),(.*?)\]/g;
 | 
					 | 
				
			||||||
  const str = decodeURIComponent(ruleProviderParams);
 | 
					 | 
				
			||||||
  while ((matches = regex.exec(str)) !== null) {
 | 
					 | 
				
			||||||
    const div = createRuleProvider();
 | 
					 | 
				
			||||||
    div.children[0].value = matches[1];
 | 
					 | 
				
			||||||
    div.children[1].value = matches[2];
 | 
					 | 
				
			||||||
    div.children[2].value = matches[3];
 | 
					 | 
				
			||||||
    div.children[3].value = matches[4];
 | 
					 | 
				
			||||||
    div.children[4].value = matches[5];
 | 
					 | 
				
			||||||
    ruleProviderGroup.appendChild(div);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function parseAndFillRuleParams(ruleParams) {
 | 
					 | 
				
			||||||
  const ruleGroup = document.getElementById("ruleGroup");
 | 
					 | 
				
			||||||
  let matches;
 | 
					 | 
				
			||||||
  const regex = /\[(.*?),(.*?)\]/g;
 | 
					 | 
				
			||||||
  const str = decodeURIComponent(ruleParams);
 | 
					 | 
				
			||||||
  while ((matches = regex.exec(str)) !== null) {
 | 
					 | 
				
			||||||
    const div = createRule();
 | 
					 | 
				
			||||||
    div.children[0].value = matches[1];
 | 
					 | 
				
			||||||
    div.children[1].value = matches[2];
 | 
					 | 
				
			||||||
    ruleGroup.appendChild(div);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function copyToClipboard(elem, e) {
 | 
					async function copyToClipboard(elem, e) {
 | 
				
			||||||
  const apiLinkInput = document.querySelector(`#${elem}`).value;
 | 
					  const apiLinkInput = document.querySelector(`#${elem}`).value;
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
@@ -580,4 +545,53 @@ function updateShortLink() {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 主题切换功能
 | 
				
			||||||
 | 
					function initTheme() {
 | 
				
			||||||
 | 
					  const html = document.querySelector('html');
 | 
				
			||||||
 | 
					  const themeIcon = document.getElementById('theme-icon');
 | 
				
			||||||
 | 
					  let theme;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // 从localStorage获取用户偏好的主题
 | 
				
			||||||
 | 
					  const savedTheme = localStorage.getItem('theme');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (savedTheme) {
 | 
				
			||||||
 | 
					    // 如果用户之前设置过主题,使用保存的主题
 | 
				
			||||||
 | 
					    theme = savedTheme;
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    // 如果没有设置过,检测系统主题偏好
 | 
				
			||||||
 | 
					    const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
 | 
				
			||||||
 | 
					    theme = prefersDark ? 'dark' : 'light';
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // 设置主题
 | 
				
			||||||
 | 
					  html.setAttribute('data-bs-theme', theme);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // 更新图标
 | 
				
			||||||
 | 
					  if (theme === 'dark') {
 | 
				
			||||||
 | 
					    themeIcon.textContent = '☀️';
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    themeIcon.textContent = '🌙';
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function toggleTheme() {
 | 
				
			||||||
 | 
					  const html = document.querySelector('html');
 | 
				
			||||||
 | 
					  const currentTheme = html.getAttribute('data-bs-theme');
 | 
				
			||||||
 | 
					  // 切换主题
 | 
				
			||||||
 | 
					  const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
 | 
				
			||||||
 | 
					  html.setAttribute('data-bs-theme', newTheme);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // 更新图标
 | 
				
			||||||
 | 
					  if (newTheme === 'dark') {
 | 
				
			||||||
 | 
					    themeIcon.textContent = '☀️';
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    themeIcon.textContent = '🌙';
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // 保存用户偏好到localStorage
 | 
				
			||||||
 | 
					  localStorage.setItem('theme', newTheme);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
listenInput();
 | 
					listenInput();
 | 
				
			||||||
 | 
					initTheme();
 | 
				
			||||||
							
								
								
									
										219
									
								
								test/parser/anytls_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										219
									
								
								test/parser/anytls_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,219 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://password123@127.0.0.1:8080#Anytls%20Proxy"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "anytls",
 | 
				
			||||||
 | 
							Name: "Anytls Proxy",
 | 
				
			||||||
 | 
							Anytls: proxy.Anytls{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Basic_WithSNI(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://password123@proxy.example.com:443?sni=proxy.example.com#Anytls%20SNI"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "anytls",
 | 
				
			||||||
 | 
							Name: "Anytls SNI",
 | 
				
			||||||
 | 
							Anytls: proxy.Anytls{
 | 
				
			||||||
 | 
								Server:         "proxy.example.com",
 | 
				
			||||||
 | 
								Port:           443,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SNI:            "proxy.example.com",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Basic_WithInsecure(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://password123@proxy.example.com:443?insecure=1&sni=proxy.example.com#Anytls%20Insecure"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "anytls",
 | 
				
			||||||
 | 
							Name: "Anytls Insecure",
 | 
				
			||||||
 | 
							Anytls: proxy.Anytls{
 | 
				
			||||||
 | 
								Server:         "proxy.example.com",
 | 
				
			||||||
 | 
								Port:           443,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SNI:            "proxy.example.com",
 | 
				
			||||||
 | 
								SkipCertVerify: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Basic_IPv6Address(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://password123@[2001:db8::1]:8080#Anytls%20IPv6"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "anytls",
 | 
				
			||||||
 | 
							Name: "Anytls IPv6",
 | 
				
			||||||
 | 
							Anytls: proxy.Anytls{
 | 
				
			||||||
 | 
								Server:         "2001:db8::1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Basic_ComplexPassword(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://ComplexPassword!%40%23%24@proxy.example.com:8443?sni=example.com&insecure=1#Anytls%20Full"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "anytls",
 | 
				
			||||||
 | 
							Name: "Anytls Full",
 | 
				
			||||||
 | 
							Anytls: proxy.Anytls{
 | 
				
			||||||
 | 
								Server:         "proxy.example.com",
 | 
				
			||||||
 | 
								Port:           8443,
 | 
				
			||||||
 | 
								Password:       "ComplexPassword!@#$",
 | 
				
			||||||
 | 
								SNI:            "example.com",
 | 
				
			||||||
 | 
								SkipCertVerify: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Basic_NoPassword(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://@127.0.0.1:8080#No%20Password"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "anytls",
 | 
				
			||||||
 | 
							Name: "No Password",
 | 
				
			||||||
 | 
							Anytls: proxy.Anytls{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Basic_UsernameOnly(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://username@127.0.0.1:8080#Username%20Only"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "anytls",
 | 
				
			||||||
 | 
							Name: "Username Only",
 | 
				
			||||||
 | 
							Anytls: proxy.Anytls{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "username",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Error_MissingServer(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://password123@:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Error_MissingPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://password123@127.0.0.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Error_InvalidPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anytls://password123@127.0.0.1:99999"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAnytls_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.AnytlsParser{}
 | 
				
			||||||
 | 
						input := "anyssl://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										198
									
								
								test/parser/hysteria2_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										198
									
								
								test/parser/hysteria2_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,198 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://password123@127.0.0.1:8080#Hysteria2%20Proxy"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria2",
 | 
				
			||||||
 | 
							Name: "Hysteria2 Proxy",
 | 
				
			||||||
 | 
							Hysteria2: proxy.Hysteria2{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Basic_AltPrefix(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hy2://password123@proxy.example.com:443?insecure=1&sni=proxy.example.com#Hysteria2%20Alt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria2",
 | 
				
			||||||
 | 
							Name: "Hysteria2 Alt",
 | 
				
			||||||
 | 
							Hysteria2: proxy.Hysteria2{
 | 
				
			||||||
 | 
								Server:         "proxy.example.com",
 | 
				
			||||||
 | 
								Port:           443,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SNI:            "proxy.example.com",
 | 
				
			||||||
 | 
								SkipCertVerify: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Basic_WithObfs(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://password123@127.0.0.1:8080?obfs=salamander&obfs-password=obfs123#Hysteria2%20Obfs"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria2",
 | 
				
			||||||
 | 
							Name: "Hysteria2 Obfs",
 | 
				
			||||||
 | 
							Hysteria2: proxy.Hysteria2{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								Obfs:           "salamander",
 | 
				
			||||||
 | 
								ObfsPassword:   "obfs123",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Basic_IPv6Address(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://password123@[2001:db8::1]:8080#Hysteria2%20IPv6"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria2",
 | 
				
			||||||
 | 
							Name: "Hysteria2 IPv6",
 | 
				
			||||||
 | 
							Hysteria2: proxy.Hysteria2{
 | 
				
			||||||
 | 
								Server:         "2001:db8::1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Basic_FullConfig(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://password123@proxy.example.com:443?insecure=1&sni=proxy.example.com&obfs=salamander&obfs-password=obfs123#Hysteria2%20Full"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria2",
 | 
				
			||||||
 | 
							Name: "Hysteria2 Full",
 | 
				
			||||||
 | 
							Hysteria2: proxy.Hysteria2{
 | 
				
			||||||
 | 
								Server:         "proxy.example.com",
 | 
				
			||||||
 | 
								Port:           443,
 | 
				
			||||||
 | 
								Password:       "password123",
 | 
				
			||||||
 | 
								SNI:            "proxy.example.com",
 | 
				
			||||||
 | 
								Obfs:           "salamander",
 | 
				
			||||||
 | 
								ObfsPassword:   "obfs123",
 | 
				
			||||||
 | 
								SkipCertVerify: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Basic_NoPassword(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://@127.0.0.1:8080#No%20Password"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria2",
 | 
				
			||||||
 | 
							Name: "No Password",
 | 
				
			||||||
 | 
							Hysteria2: proxy.Hysteria2{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Password:       "",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Error_MissingServer(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://password123@:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Error_MissingPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://password123@127.0.0.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Error_InvalidPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria2://password123@127.0.0.1:99999"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria2_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.Hysteria2Parser{}
 | 
				
			||||||
 | 
						input := "hysteria://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										183
									
								
								test/parser/hysteria_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										183
									
								
								test/parser/hysteria_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,183 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://127.0.0.1:8080?protocol=udp&auth=password123&upmbps=100&downmbps=100#Hysteria%20Proxy"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria",
 | 
				
			||||||
 | 
							Name: "Hysteria Proxy",
 | 
				
			||||||
 | 
							Hysteria: proxy.Hysteria{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Protocol:       "udp",
 | 
				
			||||||
 | 
								Auth:           "password123",
 | 
				
			||||||
 | 
								Up:             "100",
 | 
				
			||||||
 | 
								Down:           "100",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Basic_WithAuthString(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://proxy.example.com:443?protocol=wechat-video&auth-str=myauth&upmbps=50&downmbps=200&insecure=true#Hysteria%20Auth"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria",
 | 
				
			||||||
 | 
							Name: "Hysteria Auth",
 | 
				
			||||||
 | 
							Hysteria: proxy.Hysteria{
 | 
				
			||||||
 | 
								Server:         "proxy.example.com",
 | 
				
			||||||
 | 
								Port:           443,
 | 
				
			||||||
 | 
								Protocol:       "wechat-video",
 | 
				
			||||||
 | 
								AuthString:     "myauth",
 | 
				
			||||||
 | 
								Up:             "50",
 | 
				
			||||||
 | 
								Down:           "200",
 | 
				
			||||||
 | 
								SkipCertVerify: true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Basic_WithObfs(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://127.0.0.1:8080?auth=password123&upmbps=100&downmbps=100&obfs=xplus&alpn=h3#Hysteria%20Obfs"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria",
 | 
				
			||||||
 | 
							Name: "Hysteria Obfs",
 | 
				
			||||||
 | 
							Hysteria: proxy.Hysteria{
 | 
				
			||||||
 | 
								Server:         "127.0.0.1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Auth:           "password123",
 | 
				
			||||||
 | 
								Up:             "100",
 | 
				
			||||||
 | 
								Down:           "100",
 | 
				
			||||||
 | 
								Obfs:           "xplus",
 | 
				
			||||||
 | 
								ALPN:           []string{"h3"},
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Basic_IPv6Address(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://[2001:db8::1]:8080?auth=password123&upmbps=100&downmbps=100#Hysteria%20IPv6"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria",
 | 
				
			||||||
 | 
							Name: "Hysteria IPv6",
 | 
				
			||||||
 | 
							Hysteria: proxy.Hysteria{
 | 
				
			||||||
 | 
								Server:         "2001:db8::1",
 | 
				
			||||||
 | 
								Port:           8080,
 | 
				
			||||||
 | 
								Auth:           "password123",
 | 
				
			||||||
 | 
								Up:             "100",
 | 
				
			||||||
 | 
								Down:           "100",
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Basic_MultiALPN(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://proxy.example.com:443?auth=password123&upmbps=100&downmbps=100&alpn=h3,h2,http/1.1#Hysteria%20Multi%20ALPN"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "hysteria",
 | 
				
			||||||
 | 
							Name: "Hysteria Multi ALPN",
 | 
				
			||||||
 | 
							Hysteria: proxy.Hysteria{
 | 
				
			||||||
 | 
								Server:         "proxy.example.com",
 | 
				
			||||||
 | 
								Port:           443,
 | 
				
			||||||
 | 
								Auth:           "password123",
 | 
				
			||||||
 | 
								Up:             "100",
 | 
				
			||||||
 | 
								Down:           "100",
 | 
				
			||||||
 | 
								ALPN:           []string{"h3", "h2", "http/1.1"},
 | 
				
			||||||
 | 
								SkipCertVerify: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Error_MissingServer(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://:8080?auth=password123"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Error_MissingPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://127.0.0.1?auth=password123"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Error_InvalidPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria://127.0.0.1:99999?auth=password123"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHysteria_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.HysteriaParser{}
 | 
				
			||||||
 | 
						input := "hysteria2://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										184
									
								
								test/parser/shadowsocks_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										184
									
								
								test/parser/shadowsocks_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,184 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@127.0.0.1:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ss",
 | 
				
			||||||
 | 
							Name: "127.0.0.1:8080",
 | 
				
			||||||
 | 
							ShadowSocks: proxy.ShadowSocks{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     8080,
 | 
				
			||||||
 | 
								Cipher:   "aes-256-gcm",
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Basic_IPv6Address(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@[2001:db8::1]:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ss",
 | 
				
			||||||
 | 
							Name: "2001:db8::1:8080",
 | 
				
			||||||
 | 
							ShadowSocks: proxy.ShadowSocks{
 | 
				
			||||||
 | 
								Server:   "2001:db8::1",
 | 
				
			||||||
 | 
								Port:     8080,
 | 
				
			||||||
 | 
								Cipher:   "aes-256-gcm",
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Basic_WithRemark(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@proxy.example.com:8080#My%20SS%20Proxy"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ss",
 | 
				
			||||||
 | 
							Name: "My SS Proxy",
 | 
				
			||||||
 | 
							ShadowSocks: proxy.ShadowSocks{
 | 
				
			||||||
 | 
								Server:   "proxy.example.com",
 | 
				
			||||||
 | 
								Port:     8080,
 | 
				
			||||||
 | 
								Cipher:   "aes-256-gcm",
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Advanced_Base64FullEncoded(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmRAbG9jYWxob3N0OjgwODA=#Local%20SS"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ss",
 | 
				
			||||||
 | 
							Name: "Local SS",
 | 
				
			||||||
 | 
							ShadowSocks: proxy.ShadowSocks{
 | 
				
			||||||
 | 
								Server:   "localhost",
 | 
				
			||||||
 | 
								Port:     8080,
 | 
				
			||||||
 | 
								Cipher:   "aes-256-gcm",
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Advanced_PlainUserPassword(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://aes-256-gcm:password@192.168.1.1:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ss",
 | 
				
			||||||
 | 
							Name: "192.168.1.1:8080",
 | 
				
			||||||
 | 
							ShadowSocks: proxy.ShadowSocks{
 | 
				
			||||||
 | 
								Server:   "192.168.1.1",
 | 
				
			||||||
 | 
								Port:     8080,
 | 
				
			||||||
 | 
								Cipher:   "aes-256-gcm",
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Advanced_ChaCha20Cipher(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://chacha20-poly1305:mypassword@server.com:443#ChaCha20"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ss",
 | 
				
			||||||
 | 
							Name: "ChaCha20",
 | 
				
			||||||
 | 
							ShadowSocks: proxy.ShadowSocks{
 | 
				
			||||||
 | 
								Server:   "server.com",
 | 
				
			||||||
 | 
								Port:     443,
 | 
				
			||||||
 | 
								Cipher:   "chacha20-poly1305",
 | 
				
			||||||
 | 
								Password: "mypassword",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 错误处理测试
 | 
				
			||||||
 | 
					func TestShadowsocks_Error_MissingServer(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if !errors.Is(err, parser.ErrInvalidStruct) {
 | 
				
			||||||
 | 
							t.Errorf("Error is not expected: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Error_MissingPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ=@127.0.0.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if !errors.Is(err, parser.ErrInvalidStruct) {
 | 
				
			||||||
 | 
							t.Errorf("Error is not expected: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocks_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksParser{}
 | 
				
			||||||
 | 
						input := "http://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if !errors.Is(err, parser.ErrInvalidPrefix) {
 | 
				
			||||||
 | 
							t.Errorf("Error is not expected: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										112
									
								
								test/parser/shadowsocksr_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								test/parser/shadowsocksr_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocksR_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksRParser{}
 | 
				
			||||||
 | 
						input := "ssr://MTI3LjAuMC4xOjQ0MzpvcmlnaW46YWVzLTE5Mi1jZmI6cGxhaW46TVRJek1USXovP2dyb3VwPVpHVm1ZWFZzZEEmcmVtYXJrcz1TRUZJUVE"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ssr",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							ShadowSocksR: proxy.ShadowSocksR{
 | 
				
			||||||
 | 
								Server:        "127.0.0.1",
 | 
				
			||||||
 | 
								Port:          443,
 | 
				
			||||||
 | 
								Cipher:        "aes-192-cfb",
 | 
				
			||||||
 | 
								Password:      "123123",
 | 
				
			||||||
 | 
								ObfsParam:     "",
 | 
				
			||||||
 | 
								Obfs:          "plain",
 | 
				
			||||||
 | 
								Protocol:      "origin",
 | 
				
			||||||
 | 
								ProtocolParam: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocksR_Basic_WithParams(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksRParser{}
 | 
				
			||||||
 | 
						input := "ssr://MTI3LjAuMC4xOjQ0MzpvcmlnaW46YWVzLTE5Mi1jZmI6dGxzMS4wX3Nlc3Npb25fYXV0aDpNVEl6TVRJei8/b2Jmc3BhcmFtPWIySm1jeTF3WVhKaGJXVjBaWEkmcHJvdG9wYXJhbT1jSEp2ZEc5allXd3RjR0Z5WVcxbGRHVnkmZ3JvdXA9WkdWbVlYVnNkQSZyZW1hcmtzPVNFRklRUQ"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ssr",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							ShadowSocksR: proxy.ShadowSocksR{
 | 
				
			||||||
 | 
								Server:        "127.0.0.1",
 | 
				
			||||||
 | 
								Port:          443,
 | 
				
			||||||
 | 
								Cipher:        "aes-192-cfb",
 | 
				
			||||||
 | 
								Password:      "123123",
 | 
				
			||||||
 | 
								ObfsParam:     "obfs-parameter",
 | 
				
			||||||
 | 
								Obfs:          "tls1.0_session_auth",
 | 
				
			||||||
 | 
								Protocol:      "origin",
 | 
				
			||||||
 | 
								ProtocolParam: "protocal-parameter",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocksR_Basic_IPv6Address(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksRParser{}
 | 
				
			||||||
 | 
						input := "ssr://WzIwMDE6MGRiODo4NWEzOjAwMDA6MDAwMDo4YTJlOjAzNzA6NzMzNF06NDQzOm9yaWdpbjphZXMtMTkyLWNmYjpwbGFpbjpNVEl6TVRJei8/Z3JvdXA9WkdWbVlYVnNkQSZyZW1hcmtzPVNFRklRUQ"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "ssr",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							ShadowSocksR: proxy.ShadowSocksR{
 | 
				
			||||||
 | 
								Server:        "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
 | 
				
			||||||
 | 
								Port:          443,
 | 
				
			||||||
 | 
								Cipher:        "aes-192-cfb",
 | 
				
			||||||
 | 
								Password:      "123123",
 | 
				
			||||||
 | 
								ObfsParam:     "",
 | 
				
			||||||
 | 
								Obfs:          "plain",
 | 
				
			||||||
 | 
								Protocol:      "origin",
 | 
				
			||||||
 | 
								ProtocolParam: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocksR_Error_InvalidBase64(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksRParser{}
 | 
				
			||||||
 | 
						input := "ssr://invalid_base64"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestShadowsocksR_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.ShadowsocksRParser{}
 | 
				
			||||||
 | 
						input := "ss://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										168
									
								
								test/parser/socks_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										168
									
								
								test/parser/socks_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,168 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://user:pass@127.0.0.1:1080#SOCKS%20Proxy"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "socks5",
 | 
				
			||||||
 | 
							Name: "SOCKS Proxy",
 | 
				
			||||||
 | 
							Socks: proxy.Socks{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     1080,
 | 
				
			||||||
 | 
								UserName: "user",
 | 
				
			||||||
 | 
								Password: "pass",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Basic_NoAuth(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://127.0.0.1:1080#SOCKS%20No%20Auth"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "socks5",
 | 
				
			||||||
 | 
							Name: "SOCKS No Auth",
 | 
				
			||||||
 | 
							Socks: proxy.Socks{
 | 
				
			||||||
 | 
								Server: "127.0.0.1",
 | 
				
			||||||
 | 
								Port:   1080,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Basic_IPv6Address(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://user:pass@[2001:db8::1]:1080#SOCKS%20IPv6"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "socks5",
 | 
				
			||||||
 | 
							Name: "SOCKS IPv6",
 | 
				
			||||||
 | 
							Socks: proxy.Socks{
 | 
				
			||||||
 | 
								Server:   "2001:db8::1",
 | 
				
			||||||
 | 
								Port:     1080,
 | 
				
			||||||
 | 
								UserName: "user",
 | 
				
			||||||
 | 
								Password: "pass",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Basic_WithTLS(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://user:pass@127.0.0.1:1080?tls=true&sni=example.com#SOCKS%20TLS"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "socks5",
 | 
				
			||||||
 | 
							Name: "SOCKS TLS",
 | 
				
			||||||
 | 
							Socks: proxy.Socks{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     1080,
 | 
				
			||||||
 | 
								UserName: "user",
 | 
				
			||||||
 | 
								Password: "pass",
 | 
				
			||||||
 | 
								TLS:      true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Basic_WithUDP(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://user:pass@127.0.0.1:1080?udp=true#SOCKS%20UDP"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "socks5",
 | 
				
			||||||
 | 
							Name: "SOCKS UDP",
 | 
				
			||||||
 | 
							Socks: proxy.Socks{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     1080,
 | 
				
			||||||
 | 
								UserName: "user",
 | 
				
			||||||
 | 
								Password: "pass",
 | 
				
			||||||
 | 
								UDP:      true,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Error_MissingServer(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://user:pass@:1080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Error_MissingPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://user:pass@127.0.0.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Error_InvalidPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "socks://user:pass@127.0.0.1:99999"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSocks_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.SocksParser{}
 | 
				
			||||||
 | 
						input := "ss://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										182
									
								
								test/parser/trojan_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										182
									
								
								test/parser/trojan_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,182 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@127.0.0.1:443#Trojan%20Proxy"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "trojan",
 | 
				
			||||||
 | 
							Name: "Trojan Proxy",
 | 
				
			||||||
 | 
							Trojan: proxy.Trojan{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     443,
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Basic_WithTLS(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@127.0.0.1:443?security=tls&sni=example.com&alpn=h2,http/1.1#Trojan%20TLS"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "trojan",
 | 
				
			||||||
 | 
							Name: "Trojan TLS",
 | 
				
			||||||
 | 
							Trojan: proxy.Trojan{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     443,
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
								ALPN:     []string{"h2", "http/1.1"},
 | 
				
			||||||
 | 
								SNI:      "example.com",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Basic_WithReality(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@127.0.0.1:443?security=reality&sni=example.com&pbk=publickey123&sid=shortid123&fp=chrome#Trojan%20Reality"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "trojan",
 | 
				
			||||||
 | 
							Name: "Trojan Reality",
 | 
				
			||||||
 | 
							Trojan: proxy.Trojan{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     443,
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
								SNI:      "example.com",
 | 
				
			||||||
 | 
								RealityOpts: proxy.RealityOptions{
 | 
				
			||||||
 | 
									PublicKey: "publickey123",
 | 
				
			||||||
 | 
									ShortID:   "shortid123",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								Fingerprint: "chrome",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Basic_WithWebSocket(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@127.0.0.1:443?type=ws&path=/ws&host=example.com#Trojan%20WS"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "trojan",
 | 
				
			||||||
 | 
							Name: "Trojan WS",
 | 
				
			||||||
 | 
							Trojan: proxy.Trojan{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     443,
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
								Network:  "ws",
 | 
				
			||||||
 | 
								WSOpts: proxy.WSOptions{
 | 
				
			||||||
 | 
									Path: "/ws",
 | 
				
			||||||
 | 
									Headers: map[string]string{
 | 
				
			||||||
 | 
										"Host": "example.com",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Basic_WithGrpc(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@127.0.0.1:443?type=grpc&serviceName=grpc_service#Trojan%20gRPC"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "trojan",
 | 
				
			||||||
 | 
							Name: "Trojan gRPC",
 | 
				
			||||||
 | 
							Trojan: proxy.Trojan{
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     443,
 | 
				
			||||||
 | 
								Password: "password",
 | 
				
			||||||
 | 
								Network:  "grpc",
 | 
				
			||||||
 | 
								GrpcOpts: proxy.GrpcOptions{
 | 
				
			||||||
 | 
									GrpcServiceName: "grpc_service",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Error_MissingServer(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@:443"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Error_MissingPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@127.0.0.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Error_InvalidPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "trojan://password@127.0.0.1:99999"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTrojan_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.TrojanParser{}
 | 
				
			||||||
 | 
						input := "ss://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										24
									
								
								test/parser/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								test/parser/utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"reflect"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func validateResult(t *testing.T, expected proxy.Proxy, result proxy.Proxy) {
 | 
				
			||||||
 | 
						t.Helper()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if result.Type != expected.Type {
 | 
				
			||||||
 | 
							t.Errorf("Type mismatch: expected %s, got %s", expected.Type, result.Type)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !reflect.DeepEqual(expected, result) {
 | 
				
			||||||
 | 
							expectedYaml, _ := yaml.Marshal(expected)
 | 
				
			||||||
 | 
							resultYaml, _ := yaml.Marshal(result)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							t.Errorf("Structure mismatch: \nexpected:\n %s\ngot:\n %s", string(expectedYaml), string(resultYaml))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										214
									
								
								test/parser/vless_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										214
									
								
								test/parser/vless_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,214 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:8080#VLESS%20Proxy"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vless",
 | 
				
			||||||
 | 
							Name: "VLESS Proxy",
 | 
				
			||||||
 | 
							Vless: proxy.Vless{
 | 
				
			||||||
 | 
								Server: "127.0.0.1",
 | 
				
			||||||
 | 
								Port:   8080,
 | 
				
			||||||
 | 
								UUID:   "b831b0c4-33b7-4873-9834-28d66d87d4ce",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Basic_WithTLS(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?security=tls&sni=example.com&alpn=h2,http/1.1#VLESS%20TLS"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vless",
 | 
				
			||||||
 | 
							Name: "VLESS TLS",
 | 
				
			||||||
 | 
							Vless: proxy.Vless{
 | 
				
			||||||
 | 
								Server:     "127.0.0.1",
 | 
				
			||||||
 | 
								Port:       443,
 | 
				
			||||||
 | 
								UUID:       "b831b0c4-33b7-4873-9834-28d66d87d4ce",
 | 
				
			||||||
 | 
								TLS:        true,
 | 
				
			||||||
 | 
								ALPN:       []string{"h2", "http/1.1"},
 | 
				
			||||||
 | 
								ServerName: "example.com",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Basic_WithReality(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?security=reality&sni=example.com&pbk=publickey123&sid=shortid123&fp=chrome#VLESS%20Reality"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vless",
 | 
				
			||||||
 | 
							Name: "VLESS Reality",
 | 
				
			||||||
 | 
							Vless: proxy.Vless{
 | 
				
			||||||
 | 
								Server:     "127.0.0.1",
 | 
				
			||||||
 | 
								Port:       443,
 | 
				
			||||||
 | 
								UUID:       "b831b0c4-33b7-4873-9834-28d66d87d4ce",
 | 
				
			||||||
 | 
								TLS:        true,
 | 
				
			||||||
 | 
								ServerName: "example.com",
 | 
				
			||||||
 | 
								RealityOpts: proxy.RealityOptions{
 | 
				
			||||||
 | 
									PublicKey: "publickey123",
 | 
				
			||||||
 | 
									ShortID:   "shortid123",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								Fingerprint: "chrome",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Basic_WithWebSocket(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?type=ws&path=/ws&host=example.com#VLESS%20WS"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vless",
 | 
				
			||||||
 | 
							Name: "VLESS WS",
 | 
				
			||||||
 | 
							Vless: proxy.Vless{
 | 
				
			||||||
 | 
								Server:  "127.0.0.1",
 | 
				
			||||||
 | 
								Port:    443,
 | 
				
			||||||
 | 
								UUID:    "b831b0c4-33b7-4873-9834-28d66d87d4ce",
 | 
				
			||||||
 | 
								Network: "ws",
 | 
				
			||||||
 | 
								WSOpts: proxy.WSOptions{
 | 
				
			||||||
 | 
									Path: "/ws",
 | 
				
			||||||
 | 
									Headers: map[string]string{
 | 
				
			||||||
 | 
										"Host": "example.com",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Basic_WithGrpc(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?type=grpc&serviceName=grpc_service#VLESS%20gRPC"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vless",
 | 
				
			||||||
 | 
							Name: "VLESS gRPC",
 | 
				
			||||||
 | 
							Vless: proxy.Vless{
 | 
				
			||||||
 | 
								Server:  "127.0.0.1",
 | 
				
			||||||
 | 
								Port:    443,
 | 
				
			||||||
 | 
								UUID:    "b831b0c4-33b7-4873-9834-28d66d87d4ce",
 | 
				
			||||||
 | 
								Network: "grpc",
 | 
				
			||||||
 | 
								GrpcOpts: proxy.GrpcOptions{
 | 
				
			||||||
 | 
									GrpcServiceName: "grpc_service",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Basic_WithHTTP(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:443?type=http&path=/path1,/path2&host=host1.com,host2.com#VLESS%20HTTP"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vless",
 | 
				
			||||||
 | 
							Name: "VLESS HTTP",
 | 
				
			||||||
 | 
							Vless: proxy.Vless{
 | 
				
			||||||
 | 
								Server:  "127.0.0.1",
 | 
				
			||||||
 | 
								Port:    443,
 | 
				
			||||||
 | 
								UUID:    "b831b0c4-33b7-4873-9834-28d66d87d4ce",
 | 
				
			||||||
 | 
								Network: "http",
 | 
				
			||||||
 | 
								HTTPOpts: proxy.HTTPOptions{
 | 
				
			||||||
 | 
									Path: []string{"/path1", "/path2"},
 | 
				
			||||||
 | 
									Headers: map[string][]string{
 | 
				
			||||||
 | 
										"host": {"host1.com", "host2.com"},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Error_MissingServer(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Error_MissingPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Error_InvalidPort(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "vless://b831b0c4-33b7-4873-9834-28d66d87d4ce@127.0.0.1:99999"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVless_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VlessParser{}
 | 
				
			||||||
 | 
						input := "ss://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										232
									
								
								test/parser/vmess_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										232
									
								
								test/parser/vmess_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,232 @@
 | 
				
			|||||||
 | 
					package test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/model/proxy"
 | 
				
			||||||
 | 
						"github.com/bestnite/sub2clash/parser"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Basic_SimpleLink(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJ0bHMiOiJ0bHMiLCJ0eXBlIjoibm9uZSIsInYiOiIyIn0="
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vmess",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							Vmess: proxy.Vmess{
 | 
				
			||||||
 | 
								UUID:    "12345678-9012-3456-7890-123456789012",
 | 
				
			||||||
 | 
								AlterID: 0,
 | 
				
			||||||
 | 
								Cipher:  "auto",
 | 
				
			||||||
 | 
								Server:  "127.0.0.1",
 | 
				
			||||||
 | 
								Port:    443,
 | 
				
			||||||
 | 
								TLS:     true,
 | 
				
			||||||
 | 
								Network: "ws",
 | 
				
			||||||
 | 
								WSOpts: proxy.WSOptions{
 | 
				
			||||||
 | 
									Path: "/",
 | 
				
			||||||
 | 
									Headers: map[string]string{
 | 
				
			||||||
 | 
										"Host": "127.0.0.1",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Basic_WithPath(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBhdGgiOiIvd3MiLCJwb3J0IjoiNDQzIiwicHMiOiJIQUNLIiwidGxzIjoidGxzIiwidHlwZSI6Im5vbmUiLCJ2IjoiMiJ9"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vmess",
 | 
				
			||||||
 | 
							Name: "HACK",
 | 
				
			||||||
 | 
							Vmess: proxy.Vmess{
 | 
				
			||||||
 | 
								UUID:    "12345678-9012-3456-7890-123456789012",
 | 
				
			||||||
 | 
								AlterID: 0,
 | 
				
			||||||
 | 
								Cipher:  "auto",
 | 
				
			||||||
 | 
								Server:  "127.0.0.1",
 | 
				
			||||||
 | 
								Port:    443,
 | 
				
			||||||
 | 
								TLS:     true,
 | 
				
			||||||
 | 
								Network: "ws",
 | 
				
			||||||
 | 
								WSOpts: proxy.WSOptions{
 | 
				
			||||||
 | 
									Path: "/ws",
 | 
				
			||||||
 | 
									Headers: map[string]string{
 | 
				
			||||||
 | 
										"Host": "127.0.0.1",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Basic_WithHost(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaG9zdCI6ImV4YW1wbGUuY29tIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJ0bHMiOiJ0bHMiLCJ0eXBlIjoibm9uZSIsInYiOiIyIn0="
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vmess",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							Vmess: proxy.Vmess{
 | 
				
			||||||
 | 
								UUID:    "12345678-9012-3456-7890-123456789012",
 | 
				
			||||||
 | 
								AlterID: 0,
 | 
				
			||||||
 | 
								Cipher:  "auto",
 | 
				
			||||||
 | 
								Server:  "127.0.0.1",
 | 
				
			||||||
 | 
								Port:    443,
 | 
				
			||||||
 | 
								TLS:     true,
 | 
				
			||||||
 | 
								Network: "ws",
 | 
				
			||||||
 | 
								WSOpts: proxy.WSOptions{
 | 
				
			||||||
 | 
									Path: "/",
 | 
				
			||||||
 | 
									Headers: map[string]string{
 | 
				
			||||||
 | 
										"Host": "example.com",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Basic_WithSNI(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJzbmkiOiJleGFtcGxlLmNvbSIsInRscyI6InRscyIsInR5cGUiOiJub25lIiwidiI6IjIifQ=="
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vmess",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							Vmess: proxy.Vmess{
 | 
				
			||||||
 | 
								UUID:       "12345678-9012-3456-7890-123456789012",
 | 
				
			||||||
 | 
								AlterID:    0,
 | 
				
			||||||
 | 
								Cipher:     "auto",
 | 
				
			||||||
 | 
								Server:     "127.0.0.1",
 | 
				
			||||||
 | 
								Port:       443,
 | 
				
			||||||
 | 
								TLS:        true,
 | 
				
			||||||
 | 
								Network:    "ws",
 | 
				
			||||||
 | 
								ServerName: "example.com",
 | 
				
			||||||
 | 
								WSOpts: proxy.WSOptions{
 | 
				
			||||||
 | 
									Path: "/",
 | 
				
			||||||
 | 
									Headers: map[string]string{
 | 
				
			||||||
 | 
										"Host": "127.0.0.1",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Basic_WithAlterID(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIxIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJ3cyIsInBvcnQiOiI0NDMiLCJwcyI6IkhBSEEiLCJ0bHMiOiJ0bHMiLCJ0eXBlIjoibm9uZSIsInYiOiIyIn0="
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vmess",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							Vmess: proxy.Vmess{
 | 
				
			||||||
 | 
								UUID:    "12345678-9012-3456-7890-123456789012",
 | 
				
			||||||
 | 
								AlterID: 1,
 | 
				
			||||||
 | 
								Cipher:  "auto",
 | 
				
			||||||
 | 
								Server:  "127.0.0.1",
 | 
				
			||||||
 | 
								Port:    443,
 | 
				
			||||||
 | 
								TLS:     true,
 | 
				
			||||||
 | 
								Network: "ws",
 | 
				
			||||||
 | 
								WSOpts: proxy.WSOptions{
 | 
				
			||||||
 | 
									Path: "/",
 | 
				
			||||||
 | 
									Headers: map[string]string{
 | 
				
			||||||
 | 
										"Host": "127.0.0.1",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Basic_GRPC(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJhaWQiOiIwIiwiaWQiOiIxMjM0NTY3OC05MDEyLTM0NTYtNzg5MC0xMjM0NTY3ODkwMTIiLCJuZXQiOiJncnBjIiwicG9ydCI6IjQ0MyIsInBzIjoiSEFIQSIsInRscyI6InRscyIsInR5cGUiOiJub25lIiwidiI6IjIifQ=="
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						expected := proxy.Proxy{
 | 
				
			||||||
 | 
							Type: "vmess",
 | 
				
			||||||
 | 
							Name: "HAHA",
 | 
				
			||||||
 | 
							Vmess: proxy.Vmess{
 | 
				
			||||||
 | 
								UUID:     "12345678-9012-3456-7890-123456789012",
 | 
				
			||||||
 | 
								AlterID:  0,
 | 
				
			||||||
 | 
								Cipher:   "auto",
 | 
				
			||||||
 | 
								Server:   "127.0.0.1",
 | 
				
			||||||
 | 
								Port:     443,
 | 
				
			||||||
 | 
								TLS:      true,
 | 
				
			||||||
 | 
								Network:  "grpc",
 | 
				
			||||||
 | 
								GrpcOpts: proxy.GrpcOptions{},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Errorf("Unexpected error: %v", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						validateResult(t, expected, result)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Error_InvalidBase64(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://invalid_base64"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Error_InvalidJSON(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "vmess://eyJpbnZhbGlkIjoianNvbn0="
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestVmess_Error_InvalidProtocol(t *testing.T) {
 | 
				
			||||||
 | 
						p := &parser.VmessParser{}
 | 
				
			||||||
 | 
						input := "ss://example.com:8080"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := p.Parse(parser.ParseConfig{UseUDP: false}, input)
 | 
				
			||||||
 | 
						if err == nil {
 | 
				
			||||||
 | 
							t.Errorf("Expected error but got none")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										31
									
								
								utils/base64.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								utils/base64.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/base64"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func DecodeBase64(s string, urlSafe bool) (string, error) {
 | 
				
			||||||
 | 
						s = strings.TrimSpace(s)
 | 
				
			||||||
 | 
						if len(s)%4 != 0 {
 | 
				
			||||||
 | 
							s += strings.Repeat("=", 4-len(s)%4)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var decodeStr []byte
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
						if urlSafe {
 | 
				
			||||||
 | 
							decodeStr, err = base64.URLEncoding.DecodeString(s)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							decodeStr, err = base64.StdEncoding.DecodeString(s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return "", err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return string(decodeStr), nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func EncodeBase64(s string, urlSafe bool) string {
 | 
				
			||||||
 | 
						if urlSafe {
 | 
				
			||||||
 | 
							return base64.URLEncoding.EncodeToString([]byte(s))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return base64.StdEncoding.EncodeToString([]byte(s))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user