mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-11-03 20:30:35 +00:00
Compare commits
7 Commits
v0.0.13-be
...
v1.0.3-rc
| Author | SHA1 | Date | |
|---|---|---|---|
|
1e8a79c2d2
|
|||
|
be656cca57
|
|||
|
23a85f573b
|
|||
|
fce75baed4
|
|||
| f5686561f9 | |||
| 83c24170a0 | |||
| 80d91efca4 |
4
.github/workflows/docker.yml
vendored
4
.github/workflows/docker.yml
vendored
@@ -45,7 +45,7 @@ jobs:
|
|||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
build-args: |
|
build-args: |
|
||||||
"version=${{ github.ref_name }}"
|
"version=${{ github.ref_name }}"
|
||||||
@@ -53,3 +53,5 @@ jobs:
|
|||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
||||||
|
file: Containerfile
|
||||||
|
|
||||||
|
|||||||
11
.github/workflows/release.yml
vendored
11
.github/workflows/release.yml
vendored
@@ -13,13 +13,18 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v5
|
||||||
|
|
||||||
|
- name: Install UPX
|
||||||
|
uses: crazy-max/ghaction-upx@v3
|
||||||
|
with:
|
||||||
|
install-only: true
|
||||||
|
|
||||||
- name: Run GoReleaser
|
- name: Run GoReleaser
|
||||||
uses: goreleaser/goreleaser-action@v5
|
uses: goreleaser/goreleaser-action@v6
|
||||||
with:
|
with:
|
||||||
distribution: goreleaser
|
distribution: goreleaser
|
||||||
version: latest
|
version: latest
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
version: 2
|
||||||
project_name: sub2clash
|
project_name: sub2clash
|
||||||
builds:
|
builds:
|
||||||
- env:
|
- env:
|
||||||
@@ -9,19 +10,15 @@ builds:
|
|||||||
goarch:
|
goarch:
|
||||||
- amd64
|
- amd64
|
||||||
- arm64
|
- arm64
|
||||||
- arm
|
|
||||||
- "386"
|
- "386"
|
||||||
goarm:
|
|
||||||
- "6"
|
|
||||||
- "7"
|
|
||||||
ldflags:
|
ldflags:
|
||||||
- -s -w -X sub2clash/constant.Version={{ .Version }}
|
- -s -w -X github.com/bestnite/sub2clash/constant.Version={{ .Version }}
|
||||||
flags:
|
flags:
|
||||||
- -trimpath
|
- -trimpath
|
||||||
archives:
|
archives:
|
||||||
- format: tar.gz
|
- formats: ["tar.gz"]
|
||||||
format_overrides:
|
format_overrides:
|
||||||
- format: zip
|
- formats: ["zip"]
|
||||||
goos: windows
|
goos: windows
|
||||||
wrap_in_directory: true
|
wrap_in_directory: true
|
||||||
files:
|
files:
|
||||||
@@ -30,3 +27,19 @@ archives:
|
|||||||
- templates
|
- templates
|
||||||
release:
|
release:
|
||||||
draft: true
|
draft: true
|
||||||
|
upx:
|
||||||
|
- enabled: true
|
||||||
|
compress: best
|
||||||
|
nfpms:
|
||||||
|
- id: sub2clash
|
||||||
|
homepage: https://github.com/bestnite/sub2clash
|
||||||
|
maintainer: Nite <admin@nite07.com>
|
||||||
|
license: "MIT"
|
||||||
|
formats:
|
||||||
|
- apk
|
||||||
|
- deb
|
||||||
|
- rpm
|
||||||
|
- termux.deb
|
||||||
|
- archlinux
|
||||||
|
provides:
|
||||||
|
- sub2clash
|
||||||
|
|||||||
47
API.md
47
API.md
@@ -1,47 +0,0 @@
|
|||||||
# `GET /clash`, `GET /meta`
|
|
||||||
|
|
||||||
获取 Clash/Clash.Meta 配置链接
|
|
||||||
|
|
||||||
| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
|
||||||
| ------------ | ------ | ------------------------ | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
||||||
| sub | string | sub/proxy 至少有一项存在 | - | 订阅链接,可以在链接结尾加上`#名称`,来给订阅中的节点加上统一前缀(可以输入多个,用 `,` 分隔) |
|
|
||||||
| proxy | string | sub/proxy 至少有一项存在 | - | 节点分享链接(可以输入多个,用 `,` 分隔) |
|
|
||||||
| refresh | bool | 否 | `false` | 强制刷新配置(默认缓存 5 分钟) |
|
|
||||||
| template | string | 否 | - | 外部模板链接或内部模板名称 |
|
|
||||||
| ruleProvider | string | 否 | - | 格式 `[Behavior,Url,Group,Prepend,Name],[Behavior,Url,Group,Prepend,Name]...`,其中 `Group` 是该规则集使用的策略组名,`Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到 MATCH 规则之前) |
|
|
||||||
| rule | string | 否 | - | 格式 `[Rule,Prepend],[Rule,Prepend]...`,其中 `Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到 MATCH 规则之前) |
|
|
||||||
| autoTest | bool | 否 | `false` | 国家策略组是否自动测速 |
|
|
||||||
| lazy | bool | 否 | `false` | 自动测速是否启用 lazy |
|
|
||||||
| sort | string | 否 | `nameasc` | 国家策略组排序策略,可选值 `nameasc`、`namedesc`、`sizeasc`、`sizedesc` |
|
|
||||||
| replace | string | 否 | - | 通过正则表达式重命名节点,格式 `[<ReplaceKey>,<ReplaceTo>],[<ReplaceKey>,<ReplaceTo>]...` |
|
|
||||||
| remove | string | 否 | - | 通过正则表达式删除节点 |
|
|
||||||
| nodeList | bool | 否 | `false` | 只输出节点 |
|
|
||||||
|
|
||||||
# `POST /short`
|
|
||||||
|
|
||||||
获取短链,Content-Type 为 `application/json`
|
|
||||||
具体参考使用可以参考 [api\templates\index.html](api/static/index.html)
|
|
||||||
|
|
||||||
| Body 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
|
||||||
| --------- | ------ | -------- | ------ | ------------------------- |
|
|
||||||
| url | string | 是 | - | 需要转换的 Query 参数部分 |
|
|
||||||
| password | string | 否 | - | 短链密码 |
|
|
||||||
|
|
||||||
# `GET /s/:hash`
|
|
||||||
|
|
||||||
短链跳转
|
|
||||||
`hash` 为动态路由参数,可以通过 `/short` 接口获取
|
|
||||||
|
|
||||||
| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
|
||||||
| ---------- | ------ | -------- | ------ | -------- |
|
|
||||||
| password | string | 否 | - | 短链密码 |
|
|
||||||
|
|
||||||
# `PUT /short`
|
|
||||||
|
|
||||||
更新短链,Content-Type 为 `application/json`
|
|
||||||
|
|
||||||
| Body 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
|
||||||
| --------- | ------ | -------- | ------ | ------------------------- |
|
|
||||||
| url | string | 是 | - | 需要转换的 Query 参数部分 |
|
|
||||||
| password | string | 否 | - | 短链密码 |
|
|
||||||
| hash | string | 是 | - | 短链 hash |
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.21-alpine as builder
|
FROM golang:1.25 as builder
|
||||||
LABEL authors="nite07"
|
LABEL authors="nite07"
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
75
README.md
75
README.md
@@ -59,7 +59,80 @@
|
|||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
[API 文档](./API.md)
|
#### `GET /convert/:config`
|
||||||
|
|
||||||
|
获取 Clash/Clash.Meta 配置链接
|
||||||
|
|
||||||
|
| Path 参数 | 类型 | 说明 |
|
||||||
|
| --------- | ------ | ---------------------------------------------- |
|
||||||
|
| config | string | Base64 URL Safe 编码后的 JSON 字符串,格式如下 |
|
||||||
|
|
||||||
|
##### `config` JSON 结构
|
||||||
|
|
||||||
|
| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
||||||
|
| ------------------ | ----------------- | ------------------------ | --------- | -------------------------------------------------------------------------------------------------------- |
|
||||||
|
| clashType | int | 是 | 1 | 配置文件类型 (1: Clash, 2: Clash.Meta) |
|
||||||
|
| subscriptions | []string | sub/proxy 至少有一项存在 | - | 订阅链接(v2ray 或 clash 格式),可以在链接结尾加上`#名称`,来给订阅中的节点加上统一前缀(可以输入多个) |
|
||||||
|
| proxies | []string | sub/proxy 至少有一项存在 | - | 节点分享链接(可以输入多个) |
|
||||||
|
| refresh | bool | 否 | `false` | 强制刷新配置(默认缓存 5 分钟) |
|
||||||
|
| template | string | 否 | - | 外部模板链接或内部模板名称 |
|
||||||
|
| ruleProviders | []RuleProvider | 否 | - | 规则 |
|
||||||
|
| rules | []Rule | 否 | - | 规则 |
|
||||||
|
| autoTest | bool | 否 | `false` | 国家策略组是否自动测速 |
|
||||||
|
| lazy | bool | 否 | `false` | 自动测速是否启用 lazy |
|
||||||
|
| sort | string | 否 | `nameasc` | 国家策略组排序策略,可选值 `nameasc`、`namedesc`、`sizeasc`、`sizedesc` |
|
||||||
|
| replace | map[string]string | 否 | - | 通过正则表达式重命名节点 |
|
||||||
|
| remove | string | 否 | - | 通过正则表达式删除节点 |
|
||||||
|
| 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`
|
||||||
|
|
||||||
|
获取短链,Content-Type 为 `application/json`
|
||||||
|
具体参考使用可以参考 [api\templates\index.html](api/static/index.html)
|
||||||
|
|
||||||
|
| Body 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
||||||
|
| --------- | ------ | -------- | ------ | ------------------------- |
|
||||||
|
| url | string | 是 | - | 需要转换的 Query 参数部分 |
|
||||||
|
| password | string | 否 | - | 短链密码 |
|
||||||
|
|
||||||
|
#### `GET /s/:hash`
|
||||||
|
|
||||||
|
短链跳转
|
||||||
|
`hash` 为动态路由参数,可以通过 `/short` 接口获取
|
||||||
|
|
||||||
|
| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
||||||
|
| ---------- | ------ | -------- | ------ | -------- |
|
||||||
|
| password | string | 否 | - | 短链密码 |
|
||||||
|
|
||||||
|
#### `PUT /short`
|
||||||
|
|
||||||
|
更新短链,Content-Type 为 `application/json`
|
||||||
|
|
||||||
|
| Body 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
||||||
|
| --------- | ------ | -------- | ------ | ------------------------- |
|
||||||
|
| url | string | 是 | - | 需要转换的 Query 参数部分 |
|
||||||
|
| password | string | 否 | - | 短链密码 |
|
||||||
|
| hash | string | 是 | - | 短链 hash |
|
||||||
|
|
||||||
### 模板
|
### 模板
|
||||||
|
|
||||||
|
|||||||
@@ -133,8 +133,8 @@ func NewTemplateLoadError(template string, cause error) *CommonError {
|
|||||||
return NewError(ErrTemplateLoad, fmt.Sprintf("failed to load template: %s", template), cause)
|
return NewError(ErrTemplateLoad, fmt.Sprintf("failed to load template: %s", template), cause)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTemplateParseError(cause error) *CommonError {
|
func NewTemplateParseError(data []byte, cause error) *CommonError {
|
||||||
return NewError(ErrTemplateParse, "failed to parse template", cause)
|
return NewError(ErrTemplateParse, fmt.Sprintf("failed to parse template: %s", data), cause)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscription errors
|
// Subscription errors
|
||||||
@@ -142,8 +142,8 @@ func NewSubscriptionLoadError(url string, cause error) *CommonError {
|
|||||||
return NewError(ErrSubscriptionLoad, fmt.Sprintf("failed to load subscription: %s", url), cause)
|
return NewError(ErrSubscriptionLoad, fmt.Sprintf("failed to load subscription: %s", url), cause)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriptionParseError(cause error) *CommonError {
|
func NewSubscriptionParseError(data []byte, cause error) *CommonError {
|
||||||
return NewError(ErrSubscriptionParse, "failed to parse subscription", cause)
|
return NewError(ErrSubscriptionParse, fmt.Sprintf("failed to parse subscription: %s", string(data)), cause)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regex errors
|
// Regex errors
|
||||||
|
|||||||
@@ -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{}
|
||||||
@@ -128,22 +129,22 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
err = yaml.Unmarshal(templateBytes, &temp)
|
err = yaml.Unmarshal(templateBytes, &temp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Debug("parse template failed", zap.Error(err))
|
logger.Logger.Debug("parse template failed", zap.Error(err))
|
||||||
return nil, NewTemplateParseError(err)
|
return nil, NewTemplateParseError(templateBytes, err)
|
||||||
}
|
}
|
||||||
var proxyList []P.Proxy
|
var proxyList []P.Proxy
|
||||||
|
|
||||||
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
|
||||||
@@ -160,14 +161,14 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
}
|
}
|
||||||
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]),
|
||||||
zap.String("data", string(data)),
|
zap.String("data", string(data)),
|
||||||
zap.Error(err),
|
zap.Error(err),
|
||||||
)
|
)
|
||||||
return nil, NewSubscriptionParseError(err)
|
return nil, NewSubscriptionParseError(data, err)
|
||||||
}
|
}
|
||||||
p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, strings.Split(base64, "\n")...)
|
p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, strings.Split(base64, "\n")...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,7 +187,7 @@ 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 {
|
||||||
p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, query.Proxies...)
|
p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, query.Proxies...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -236,22 +237,17 @@ func BuildSub(clashType model.ClashType, query model.SubConfig, template string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 替换
|
// 替换
|
||||||
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 {
|
||||||
}
|
if replaceReg.MatchString(proxyList[i].Name) {
|
||||||
for i := range proxyList {
|
proxyList[i].Name = replaceReg.ReplaceAllString(
|
||||||
|
proxyList[i].Name, v,
|
||||||
for j, v := range replaceRegs {
|
|
||||||
if v.MatchString(proxyList[i].Name) {
|
|
||||||
proxyList[i].Name = v.ReplaceAllString(
|
|
||||||
proxyList[i].Name, query.ReplaceTo[j],
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -276,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))
|
||||||
|
|||||||
@@ -3,11 +3,27 @@ package common
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadTemplate(templatePath string) ([]byte, error) {
|
const templatesDir = "templates"
|
||||||
if _, err := os.Stat(templatePath); err == nil {
|
|
||||||
file, err := os.Open(templatePath)
|
// LoadTemplate 只读取运行目录下的 templates 目录,防止其他文件内容泄漏
|
||||||
|
func LoadTemplate(templateName string) ([]byte, error) {
|
||||||
|
// 清理路径,防止目录遍历攻击
|
||||||
|
cleanTemplateName := filepath.Clean(templateName)
|
||||||
|
|
||||||
|
// 检查是否尝试访问父目录
|
||||||
|
if strings.HasPrefix(cleanTemplateName, "..") || strings.Contains(cleanTemplateName, string(filepath.Separator)+".."+string(filepath.Separator)) {
|
||||||
|
return nil, NewFileNotFoundError(templateName) // 拒绝包含父目录的路径
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建完整路径,确保只从 templates 目录读取
|
||||||
|
fullPath := filepath.Join(templatesDir, cleanTemplateName)
|
||||||
|
|
||||||
|
if _, err := os.Stat(fullPath); err == nil {
|
||||||
|
file, err := os.Open(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -22,5 +38,5 @@ func LoadTemplate(templatePath string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
return nil, NewFileNotFoundError(templatePath)
|
return nil, NewFileNotFoundError(templateName)
|
||||||
}
|
}
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -1,8 +1,6 @@
|
|||||||
module github.com/bestnite/sub2clash
|
module github.com/bestnite/sub2clash
|
||||||
|
|
||||||
go 1.21.0
|
go 1.25
|
||||||
|
|
||||||
toolchain go1.24.3
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.10.1
|
github.com/gin-gonic/gin v1.10.1
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -2,17 +2,17 @@ package proxy
|
|||||||
|
|
||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/anytls.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/anytls.go
|
||||||
type Anytls struct {
|
type Anytls struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port"`
|
Port IntOrString `yaml:"port"`
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
ALPN []string `yaml:"alpn,omitempty"`
|
ALPN []string `yaml:"alpn,omitempty"`
|
||||||
SNI string `yaml:"sni,omitempty"`
|
SNI string `yaml:"sni,omitempty"`
|
||||||
ECHOpts ECHOptions `yaml:"ech-opts,omitempty"`
|
ECHOpts ECHOptions `yaml:"ech-opts,omitempty"`
|
||||||
ClientFingerprint string `yaml:"client-fingerprint,omitempty"`
|
ClientFingerprint string `yaml:"client-fingerprint,omitempty"`
|
||||||
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `yaml:"fingerprint,omitempty"`
|
Fingerprint string `yaml:"fingerprint,omitempty"`
|
||||||
UDP bool `yaml:"udp,omitempty"`
|
UDP bool `yaml:"udp,omitempty"`
|
||||||
IdleSessionCheckInterval int `yaml:"idle-session-check-interval,omitempty"`
|
IdleSessionCheckInterval int `yaml:"idle-session-check-interval,omitempty"`
|
||||||
IdleSessionTimeout int `yaml:"idle-session-timeout,omitempty"`
|
IdleSessionTimeout int `yaml:"idle-session-timeout,omitempty"`
|
||||||
MinIdleSession int `yaml:"min-idle-session,omitempty"`
|
MinIdleSession int `yaml:"min-idle-session,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,28 +2,28 @@ package proxy
|
|||||||
|
|
||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria.go
|
||||||
type Hysteria struct {
|
type Hysteria struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port,omitempty"`
|
Port IntOrString `yaml:"port,omitempty"`
|
||||||
Ports string `yaml:"ports,omitempty"`
|
Ports string `yaml:"ports,omitempty"`
|
||||||
Protocol string `yaml:"protocol,omitempty"`
|
Protocol string `yaml:"protocol,omitempty"`
|
||||||
ObfsProtocol string `yaml:"obfs-protocol,omitempty"` // compatible with Stash
|
ObfsProtocol string `yaml:"obfs-protocol,omitempty"` // compatible with Stash
|
||||||
Up string `yaml:"up"`
|
Up string `yaml:"up"`
|
||||||
UpSpeed int `yaml:"up-speed,omitempty"` // compatible with Stash
|
UpSpeed int `yaml:"up-speed,omitempty"` // compatible with Stash
|
||||||
Down string `yaml:"down"`
|
Down string `yaml:"down"`
|
||||||
DownSpeed int `yaml:"down-speed,omitempty"` // compatible with Stash
|
DownSpeed int `yaml:"down-speed,omitempty"` // compatible with Stash
|
||||||
Auth string `yaml:"auth,omitempty"`
|
Auth string `yaml:"auth,omitempty"`
|
||||||
AuthString string `yaml:"auth-str,omitempty"`
|
AuthString string `yaml:"auth-str,omitempty"`
|
||||||
Obfs string `yaml:"obfs,omitempty"`
|
Obfs string `yaml:"obfs,omitempty"`
|
||||||
SNI string `yaml:"sni,omitempty"`
|
SNI string `yaml:"sni,omitempty"`
|
||||||
ECHOpts ECHOptions `yaml:"ech-opts,omitempty"`
|
ECHOpts ECHOptions `yaml:"ech-opts,omitempty"`
|
||||||
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `yaml:"fingerprint,omitempty"`
|
Fingerprint string `yaml:"fingerprint,omitempty"`
|
||||||
ALPN []string `yaml:"alpn,omitempty"`
|
ALPN []string `yaml:"alpn,omitempty"`
|
||||||
CustomCA string `yaml:"ca,omitempty"`
|
CustomCA string `yaml:"ca,omitempty"`
|
||||||
CustomCAString string `yaml:"ca-str,omitempty"`
|
CustomCAString string `yaml:"ca-str,omitempty"`
|
||||||
ReceiveWindowConn int `yaml:"recv-window-conn,omitempty"`
|
ReceiveWindowConn int `yaml:"recv-window-conn,omitempty"`
|
||||||
ReceiveWindow int `yaml:"recv-window,omitempty"`
|
ReceiveWindow int `yaml:"recv-window,omitempty"`
|
||||||
DisableMTUDiscovery bool `yaml:"disable-mtu-discovery,omitempty"`
|
DisableMTUDiscovery bool `yaml:"disable-mtu-discovery,omitempty"`
|
||||||
FastOpen bool `yaml:"fast-open,omitempty"`
|
FastOpen bool `yaml:"fast-open,omitempty"`
|
||||||
HopInterval int `yaml:"hop-interval,omitempty"`
|
HopInterval int `yaml:"hop-interval,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,24 +2,24 @@ package proxy
|
|||||||
|
|
||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria2.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/hysteria2.go
|
||||||
type Hysteria2 struct {
|
type Hysteria2 struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port,omitempty"`
|
Port IntOrString `yaml:"port,omitempty"`
|
||||||
Ports string `yaml:"ports,omitempty"`
|
Ports string `yaml:"ports,omitempty"`
|
||||||
HopInterval int `yaml:"hop-interval,omitempty"`
|
HopInterval int `yaml:"hop-interval,omitempty"`
|
||||||
Up string `yaml:"up,omitempty"`
|
Up string `yaml:"up,omitempty"`
|
||||||
Down string `yaml:"down,omitempty"`
|
Down string `yaml:"down,omitempty"`
|
||||||
Password string `yaml:"password,omitempty"`
|
Password string `yaml:"password,omitempty"`
|
||||||
Obfs string `yaml:"obfs,omitempty"`
|
Obfs string `yaml:"obfs,omitempty"`
|
||||||
ObfsPassword string `yaml:"obfs-password,omitempty"`
|
ObfsPassword string `yaml:"obfs-password,omitempty"`
|
||||||
SNI string `yaml:"sni,omitempty"`
|
SNI string `yaml:"sni,omitempty"`
|
||||||
ECHOpts ECHOptions `yaml:"ech-opts,omitempty"`
|
ECHOpts ECHOptions `yaml:"ech-opts,omitempty"`
|
||||||
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `yaml:"fingerprint,omitempty"`
|
Fingerprint string `yaml:"fingerprint,omitempty"`
|
||||||
ALPN []string `yaml:"alpn,omitempty"`
|
ALPN []string `yaml:"alpn,omitempty"`
|
||||||
CustomCA string `yaml:"ca,omitempty"`
|
CustomCA string `yaml:"ca,omitempty"`
|
||||||
CustomCAString string `yaml:"ca-str,omitempty"`
|
CustomCAString string `yaml:"ca-str,omitempty"`
|
||||||
CWND int `yaml:"cwnd,omitempty"`
|
CWND int `yaml:"cwnd,omitempty"`
|
||||||
UdpMTU int `yaml:"udp-mtu,omitempty"`
|
UdpMTU int `yaml:"udp-mtu,omitempty"`
|
||||||
|
|
||||||
// quic-go special config
|
// quic-go special config
|
||||||
InitialStreamReceiveWindow uint64 `yaml:"initial-stream-receive-window,omitempty"`
|
InitialStreamReceiveWindow uint64 `yaml:"initial-stream-receive-window,omitempty"`
|
||||||
|
|||||||
@@ -2,10 +2,31 @@ package proxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type IntOrString int
|
||||||
|
|
||||||
|
func (i *IntOrString) UnmarshalYAML(value *yaml.Node) error {
|
||||||
|
intVal := 0
|
||||||
|
err := yaml.Unmarshal([]byte(value.Value), &intVal)
|
||||||
|
if err == nil {
|
||||||
|
*i = IntOrString(intVal)
|
||||||
|
}
|
||||||
|
strVal := ""
|
||||||
|
err = yaml.Unmarshal([]byte(value.Value), &strVal)
|
||||||
|
if err == nil {
|
||||||
|
_int, err := strconv.ParseInt(strVal, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
*i = IntOrString(_int)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type HTTPOptions struct {
|
type HTTPOptions struct {
|
||||||
Method string `yaml:"method,omitempty"`
|
Method string `yaml:"method,omitempty"`
|
||||||
Path []string `yaml:"path,omitempty"`
|
Path []string `yaml:"path,omitempty"`
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package proxy
|
|||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocks.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocks.go
|
||||||
type ShadowSocks struct {
|
type ShadowSocks struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port"`
|
Port IntOrString `yaml:"port"`
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
Cipher string `yaml:"cipher"`
|
Cipher string `yaml:"cipher"`
|
||||||
UDP bool `yaml:"udp,omitempty"`
|
UDP bool `yaml:"udp,omitempty"`
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ package proxy
|
|||||||
|
|
||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocksr.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/shadowsocksr.go
|
||||||
type ShadowSocksR struct {
|
type ShadowSocksR struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port"`
|
Port IntOrString `yaml:"port"`
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
Cipher string `yaml:"cipher"`
|
Cipher string `yaml:"cipher"`
|
||||||
Obfs string `yaml:"obfs"`
|
Obfs string `yaml:"obfs"`
|
||||||
ObfsParam string `yaml:"obfs-param,omitempty"`
|
ObfsParam string `yaml:"obfs-param,omitempty"`
|
||||||
Protocol string `yaml:"protocol"`
|
Protocol string `yaml:"protocol"`
|
||||||
ProtocolParam string `yaml:"protocol-param,omitempty"`
|
ProtocolParam string `yaml:"protocol-param,omitempty"`
|
||||||
UDP bool `yaml:"udp,omitempty"`
|
UDP bool `yaml:"udp,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package proxy
|
|||||||
|
|
||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/socks5.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/socks5.go
|
||||||
type Socks struct {
|
type Socks struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port"`
|
Port IntOrString `yaml:"port"`
|
||||||
UserName string `yaml:"username,omitempty"`
|
UserName string `yaml:"username,omitempty"`
|
||||||
Password string `yaml:"password,omitempty"`
|
Password string `yaml:"password,omitempty"`
|
||||||
TLS bool `yaml:"tls,omitempty"`
|
TLS bool `yaml:"tls,omitempty"`
|
||||||
UDP bool `yaml:"udp,omitempty"`
|
UDP bool `yaml:"udp,omitempty"`
|
||||||
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `yaml:"fingerprint,omitempty"`
|
Fingerprint string `yaml:"fingerprint,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package proxy
|
|||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/trojan.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/trojan.go
|
||||||
type Trojan struct {
|
type Trojan struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port"`
|
Port IntOrString `yaml:"port"`
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
ALPN []string `yaml:"alpn,omitempty"`
|
ALPN []string `yaml:"alpn,omitempty"`
|
||||||
SNI string `yaml:"sni,omitempty"`
|
SNI string `yaml:"sni,omitempty"`
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package proxy
|
|||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vless.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vless.go
|
||||||
type Vless struct {
|
type Vless struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port"`
|
Port IntOrString `yaml:"port"`
|
||||||
UUID string `yaml:"uuid"`
|
UUID string `yaml:"uuid"`
|
||||||
Flow string `yaml:"flow,omitempty"`
|
Flow string `yaml:"flow,omitempty"`
|
||||||
TLS bool `yaml:"tls,omitempty"`
|
TLS bool `yaml:"tls,omitempty"`
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package proxy
|
|||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vmess.go
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/adapter/outbound/vmess.go
|
||||||
type Vmess struct {
|
type Vmess struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Port int `yaml:"port"`
|
Port IntOrString `yaml:"port"`
|
||||||
UUID string `yaml:"uuid"`
|
UUID string `yaml:"uuid"`
|
||||||
AlterID int `yaml:"alterId"`
|
AlterID IntOrString `yaml:"alterId"`
|
||||||
Cipher string `yaml:"cipher"`
|
Cipher string `yaml:"cipher"`
|
||||||
UDP bool `yaml:"udp,omitempty"`
|
UDP bool `yaml:"udp,omitempty"`
|
||||||
Network string `yaml:"network,omitempty"`
|
Network string `yaml:"network,omitempty"`
|
||||||
|
|||||||
@@ -1,159 +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:""`
|
|
||||||
UseUDP bool `form:"useUDP,default=false" 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
|
|
||||||
}
|
|
||||||
@@ -14,51 +14,51 @@ type NodeList struct {
|
|||||||
|
|
||||||
// https://github.com/MetaCubeX/mihomo/blob/Meta/config/config.go RawConfig
|
// https://github.com/MetaCubeX/mihomo/blob/Meta/config/config.go RawConfig
|
||||||
type Subscription struct {
|
type Subscription struct {
|
||||||
Port int `yaml:"port,omitempty" json:"port"`
|
Port proxy.IntOrString `yaml:"port,omitempty" json:"port"`
|
||||||
SocksPort int `yaml:"socks-port,omitempty" json:"socks-port"`
|
SocksPort proxy.IntOrString `yaml:"socks-port,omitempty" json:"socks-port"`
|
||||||
RedirPort int `yaml:"redir-port,omitempty" json:"redir-port"`
|
RedirPort proxy.IntOrString `yaml:"redir-port,omitempty" json:"redir-port"`
|
||||||
TProxyPort int `yaml:"tproxy-port,omitempty" json:"tproxy-port"`
|
TProxyPort proxy.IntOrString `yaml:"tproxy-port,omitempty" json:"tproxy-port"`
|
||||||
MixedPort int `yaml:"mixed-port,omitempty" json:"mixed-port"`
|
MixedPort proxy.IntOrString `yaml:"mixed-port,omitempty" json:"mixed-port"`
|
||||||
ShadowSocksConfig string `yaml:"ss-config,omitempty" json:"ss-config"`
|
ShadowSocksConfig string `yaml:"ss-config,omitempty" json:"ss-config"`
|
||||||
VmessConfig string `yaml:"vmess-config,omitempty" json:"vmess-config"`
|
VmessConfig string `yaml:"vmess-config,omitempty" json:"vmess-config"`
|
||||||
InboundTfo bool `yaml:"inbound-tfo,omitempty" json:"inbound-tfo"`
|
InboundTfo bool `yaml:"inbound-tfo,omitempty" json:"inbound-tfo"`
|
||||||
InboundMPTCP bool `yaml:"inbound-mptcp,omitempty" json:"inbound-mptcp"`
|
InboundMPTCP bool `yaml:"inbound-mptcp,omitempty" json:"inbound-mptcp"`
|
||||||
Authentication []string `yaml:"authentication,omitempty" json:"authentication"`
|
Authentication []string `yaml:"authentication,omitempty" json:"authentication"`
|
||||||
SkipAuthPrefixes []netip.Prefix `yaml:"skip-auth-prefixes,omitempty" json:"skip-auth-prefixes"`
|
SkipAuthPrefixes []netip.Prefix `yaml:"skip-auth-prefixes,omitempty" json:"skip-auth-prefixes"`
|
||||||
LanAllowedIPs []netip.Prefix `yaml:"lan-allowed-ips,omitempty" json:"lan-allowed-ips"`
|
LanAllowedIPs []netip.Prefix `yaml:"lan-allowed-ips,omitempty" json:"lan-allowed-ips"`
|
||||||
LanDisAllowedIPs []netip.Prefix `yaml:"lan-disallowed-ips,omitempty" json:"lan-disallowed-ips"`
|
LanDisAllowedIPs []netip.Prefix `yaml:"lan-disallowed-ips,omitempty" json:"lan-disallowed-ips"`
|
||||||
AllowLan bool `yaml:"allow-lan,omitempty" json:"allow-lan"`
|
AllowLan bool `yaml:"allow-lan,omitempty" json:"allow-lan"`
|
||||||
BindAddress string `yaml:"bind-address,omitempty" json:"bind-address"`
|
BindAddress string `yaml:"bind-address,omitempty" json:"bind-address"`
|
||||||
Mode string `yaml:"mode,omitempty" json:"mode"`
|
Mode string `yaml:"mode,omitempty" json:"mode"`
|
||||||
UnifiedDelay bool `yaml:"unified-delay,omitempty" json:"unified-delay"`
|
UnifiedDelay bool `yaml:"unified-delay,omitempty" json:"unified-delay"`
|
||||||
LogLevel string `yaml:"log-level,omitempty" json:"log-level"`
|
LogLevel string `yaml:"log-level,omitempty" json:"log-level"`
|
||||||
IPv6 bool `yaml:"ipv6,omitempty" json:"ipv6"`
|
IPv6 bool `yaml:"ipv6,omitempty" json:"ipv6"`
|
||||||
ExternalController string `yaml:"external-controller,omitempty" json:"external-controller"`
|
ExternalController string `yaml:"external-controller,omitempty" json:"external-controller"`
|
||||||
ExternalControllerPipe string `yaml:"external-controller-pipe,omitempty" json:"external-controller-pipe"`
|
ExternalControllerPipe string `yaml:"external-controller-pipe,omitempty" json:"external-controller-pipe"`
|
||||||
ExternalControllerUnix string `yaml:"external-controller-unix,omitempty" json:"external-controller-unix"`
|
ExternalControllerUnix string `yaml:"external-controller-unix,omitempty" json:"external-controller-unix"`
|
||||||
ExternalControllerTLS string `yaml:"external-controller-tls,omitempty" json:"external-controller-tls"`
|
ExternalControllerTLS string `yaml:"external-controller-tls,omitempty" json:"external-controller-tls"`
|
||||||
ExternalControllerCors C.RawCors `yaml:"external-controller-cors,omitempty" json:"external-controller-cors"`
|
ExternalControllerCors C.RawCors `yaml:"external-controller-cors,omitempty" json:"external-controller-cors"`
|
||||||
ExternalUI string `yaml:"external-ui,omitempty" json:"external-ui"`
|
ExternalUI string `yaml:"external-ui,omitempty" json:"external-ui"`
|
||||||
ExternalUIURL string `yaml:"external-ui-url,omitempty" json:"external-ui-url"`
|
ExternalUIURL string `yaml:"external-ui-url,omitempty" json:"external-ui-url"`
|
||||||
ExternalUIName string `yaml:"external-ui-name,omitempty" json:"external-ui-name"`
|
ExternalUIName string `yaml:"external-ui-name,omitempty" json:"external-ui-name"`
|
||||||
ExternalDohServer string `yaml:"external-doh-server,omitempty" json:"external-doh-server"`
|
ExternalDohServer string `yaml:"external-doh-server,omitempty" json:"external-doh-server"`
|
||||||
Secret string `yaml:"secret,omitempty" json:"secret"`
|
Secret string `yaml:"secret,omitempty" json:"secret"`
|
||||||
Interface string `yaml:"interface-name,omitempty" json:"interface-name"`
|
Interface string `yaml:"interface-name,omitempty" json:"interface-name"`
|
||||||
RoutingMark int `yaml:"routing-mark,omitempty" json:"routing-mark"`
|
RoutingMark int `yaml:"routing-mark,omitempty" json:"routing-mark"`
|
||||||
Tunnels []LC.Tunnel `yaml:"tunnels,omitempty" json:"tunnels"`
|
Tunnels []LC.Tunnel `yaml:"tunnels,omitempty" json:"tunnels"`
|
||||||
GeoAutoUpdate bool `yaml:"geo-auto-update,omitempty" json:"geo-auto-update"`
|
GeoAutoUpdate bool `yaml:"geo-auto-update,omitempty" json:"geo-auto-update"`
|
||||||
GeoUpdateInterval int `yaml:"geo-update-interval,omitempty" json:"geo-update-interval"`
|
GeoUpdateInterval int `yaml:"geo-update-interval,omitempty" json:"geo-update-interval"`
|
||||||
GeodataMode bool `yaml:"geodata-mode,omitempty" json:"geodata-mode"`
|
GeodataMode bool `yaml:"geodata-mode,omitempty" json:"geodata-mode"`
|
||||||
GeodataLoader string `yaml:"geodata-loader,omitempty" json:"geodata-loader"`
|
GeodataLoader string `yaml:"geodata-loader,omitempty" json:"geodata-loader"`
|
||||||
GeositeMatcher string `yaml:"geosite-matcher,omitempty" json:"geosite-matcher"`
|
GeositeMatcher string `yaml:"geosite-matcher,omitempty" json:"geosite-matcher"`
|
||||||
TCPConcurrent bool `yaml:"tcp-concurrent,omitempty" json:"tcp-concurrent"`
|
TCPConcurrent bool `yaml:"tcp-concurrent,omitempty" json:"tcp-concurrent"`
|
||||||
FindProcessMode string `yaml:"find-process-mode,omitempty" json:"find-process-mode"`
|
FindProcessMode string `yaml:"find-process-mode,omitempty" json:"find-process-mode"`
|
||||||
GlobalClientFingerprint string `yaml:"global-client-fingerprint,omitempty" json:"global-client-fingerprint"`
|
GlobalClientFingerprint string `yaml:"global-client-fingerprint,omitempty" json:"global-client-fingerprint"`
|
||||||
GlobalUA string `yaml:"global-ua,omitempty" json:"global-ua"`
|
GlobalUA string `yaml:"global-ua,omitempty" json:"global-ua"`
|
||||||
ETagSupport bool `yaml:"etag-support,omitempty" json:"etag-support"`
|
ETagSupport bool `yaml:"etag-support,omitempty" json:"etag-support"`
|
||||||
KeepAliveIdle int `yaml:"keep-alive-idle,omitempty" json:"keep-alive-idle"`
|
KeepAliveIdle int `yaml:"keep-alive-idle,omitempty" json:"keep-alive-idle"`
|
||||||
KeepAliveInterval int `yaml:"keep-alive-interval,omitempty" json:"keep-alive-interval"`
|
KeepAliveInterval int `yaml:"keep-alive-interval,omitempty" json:"keep-alive-interval"`
|
||||||
DisableKeepAlive bool `yaml:"disable-keep-alive,omitempty" json:"disable-keep-alive"`
|
DisableKeepAlive bool `yaml:"disable-keep-alive,omitempty" json:"disable-keep-alive"`
|
||||||
|
|
||||||
ProxyProvider map[string]map[string]any `yaml:"proxy-providers,omitempty" json:"proxy-providers"`
|
ProxyProvider map[string]map[string]any `yaml:"proxy-providers,omitempty" json:"proxy-providers"`
|
||||||
RuleProvider map[string]RuleProvider `yaml:"rule-providers,omitempty" json:"rule-providers"`
|
RuleProvider map[string]RuleProvider `yaml:"rule-providers,omitempty" json:"rule-providers"`
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (p *AnytlsParser) Parse(config ParseConfig, proxy string) (P.Proxy, error)
|
|||||||
Name: remarks,
|
Name: remarks,
|
||||||
Anytls: P.Anytls{
|
Anytls: P.Anytls{
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
Password: password,
|
Password: password,
|
||||||
SNI: sni,
|
SNI: sni,
|
||||||
SkipCertVerify: insecureBool,
|
SkipCertVerify: insecureBool,
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
P "github.com/bestnite/sub2clash/model/proxy"
|
P "github.com/bestnite/sub2clash/model/proxy"
|
||||||
|
"github.com/bestnite/sub2clash/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func hasPrefix(proxy string, prefixes []string) bool {
|
func hasPrefix(proxy string, prefixes []string) bool {
|
||||||
@@ -49,7 +49,7 @@ func isLikelyBase64(s string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
decoded, err := DecodeBase64(s)
|
decoded, err := utils.DecodeBase64(s, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -60,23 +60,6 @@ func isLikelyBase64(s string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeBase64(s string) (string, error) {
|
|
||||||
s = strings.TrimSpace(s)
|
|
||||||
|
|
||||||
if strings.Contains(s, "-") || strings.Contains(s, "_") {
|
|
||||||
s = strings.ReplaceAll(s, "-", "+")
|
|
||||||
s = strings.ReplaceAll(s, "_", "/")
|
|
||||||
}
|
|
||||||
if len(s)%4 != 0 {
|
|
||||||
s += strings.Repeat("=", 4-len(s)%4)
|
|
||||||
}
|
|
||||||
decodeStr, err := base64.StdEncoding.DecodeString(s)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(decodeStr), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ParseConfig struct {
|
type ParseConfig struct {
|
||||||
UseUDP bool
|
UseUDP bool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func (p *HysteriaParser) Parse(config ParseConfig, proxy string) (P.Proxy, error
|
|||||||
Name: remarks,
|
Name: remarks,
|
||||||
Hysteria: P.Hysteria{
|
Hysteria: P.Hysteria{
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
Up: upmbps,
|
Up: upmbps,
|
||||||
Down: downmbps,
|
Down: downmbps,
|
||||||
Auth: auth,
|
Auth: auth,
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func (p *Hysteria2Parser) Parse(config ParseConfig, proxy string) (P.Proxy, erro
|
|||||||
Name: remarks,
|
Name: remarks,
|
||||||
Hysteria2: P.Hysteria2{
|
Hysteria2: P.Hysteria2{
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
Password: password,
|
Password: password,
|
||||||
Obfs: obfs,
|
Obfs: obfs,
|
||||||
ObfsPassword: obfsPassword,
|
ObfsPassword: obfsPassword,
|
||||||
|
|||||||
@@ -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协议解析器
|
||||||
@@ -43,7 +44,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
d, err := DecodeBase64(s[0])
|
d, err := utils.DecodeBase64(s[0], true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
||||||
}
|
}
|
||||||
@@ -76,7 +77,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
|||||||
password, hasPassword := link.User.Password()
|
password, hasPassword := link.User.Password()
|
||||||
|
|
||||||
if !hasPassword && isLikelyBase64(method) {
|
if !hasPassword && isLikelyBase64(method) {
|
||||||
decodedStr, err := DecodeBase64(method)
|
decodedStr, err := utils.DecodeBase64(method, true)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
methodAndPass := strings.SplitN(decodedStr, ":", 2)
|
methodAndPass := strings.SplitN(decodedStr, ":", 2)
|
||||||
if len(methodAndPass) == 2 {
|
if len(methodAndPass) == 2 {
|
||||||
@@ -88,7 +89,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if password != "" && 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{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
||||||
}
|
}
|
||||||
@@ -107,7 +108,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
|||||||
Cipher: method,
|
Cipher: method,
|
||||||
Password: password,
|
Password: password,
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
UDP: config.UseUDP,
|
UDP: config.UseUDP,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"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{}
|
||||||
@@ -39,7 +40,7 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
proxy, err := DecodeBase64(proxy)
|
proxy, err := utils.DecodeBase64(proxy, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
|
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
|
||||||
}
|
}
|
||||||
@@ -55,7 +56,7 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
|||||||
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{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
||||||
}
|
}
|
||||||
@@ -73,13 +74,13 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
|||||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrCannotParseParams, err.Error())
|
return P.Proxy{}, fmt.Errorf("%w: %s", ErrCannotParseParams, 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)
|
||||||
}
|
}
|
||||||
@@ -93,7 +94,7 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
|||||||
Name: remarks,
|
Name: remarks,
|
||||||
ShadowSocksR: P.ShadowSocksR{
|
ShadowSocksR: P.ShadowSocksR{
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
Protocol: protocol,
|
Protocol: protocol,
|
||||||
Cipher: method,
|
Cipher: method,
|
||||||
Obfs: obfs,
|
Obfs: obfs,
|
||||||
|
|||||||
@@ -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{}
|
||||||
@@ -59,7 +60,7 @@ func (p *SocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
|||||||
password, hasPassword := link.User.Password()
|
password, hasPassword := link.User.Password()
|
||||||
|
|
||||||
if !hasPassword && isLikelyBase64(username) {
|
if !hasPassword && isLikelyBase64(username) {
|
||||||
decodedStr, err := DecodeBase64(username)
|
decodedStr, err := utils.DecodeBase64(username, true)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
usernameAndPassword := strings.SplitN(decodedStr, ":", 2)
|
usernameAndPassword := strings.SplitN(decodedStr, ":", 2)
|
||||||
if len(usernameAndPassword) == 2 {
|
if len(usernameAndPassword) == 2 {
|
||||||
@@ -78,7 +79,7 @@ func (p *SocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
|||||||
Name: remarks,
|
Name: remarks,
|
||||||
Socks: P.Socks{
|
Socks: P.Socks{
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
UserName: username,
|
UserName: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
TLS: tls == "true",
|
TLS: tls == "true",
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ func (p *TrojanParser) Parse(config ParseConfig, proxy string) (P.Proxy, error)
|
|||||||
insecureBool := insecure == "1"
|
insecureBool := insecure == "1"
|
||||||
result := P.Trojan{
|
result := P.Trojan{
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
Password: password,
|
Password: password,
|
||||||
Network: network,
|
Network: network,
|
||||||
UDP: udp == "true",
|
UDP: udp == "true",
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ func (p *VlessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
result := P.Vless{
|
result := P.Vless{
|
||||||
Server: server,
|
Server: server,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
UUID: uuid,
|
UUID: uuid,
|
||||||
Flow: flow,
|
Flow: flow,
|
||||||
UDP: udp == "true",
|
UDP: udp == "true",
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"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 {
|
||||||
@@ -57,7 +58,7 @@ func (p *VmessParser) Parse(config ParseConfig, 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{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
|
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
|
||||||
}
|
}
|
||||||
@@ -108,9 +109,9 @@ func (p *VmessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
|||||||
|
|
||||||
result := P.Vmess{
|
result := P.Vmess{
|
||||||
Server: vmess.Add,
|
Server: vmess.Add,
|
||||||
Port: port,
|
Port: P.IntOrString(port),
|
||||||
UUID: vmess.Id,
|
UUID: vmess.Id,
|
||||||
AlterID: aid,
|
AlterID: P.IntOrString(aid),
|
||||||
Cipher: vmess.Scy,
|
Cipher: vmess.Scy,
|
||||||
UDP: config.UseUDP,
|
UDP: config.UseUDP,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -142,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")
|
||||||
|
|
||||||
@@ -176,11 +175,11 @@ func GetRawConfHandler(c *gin.Context) {
|
|||||||
host := c.Request.Host
|
host := c.Request.Host
|
||||||
targetPath := strings.TrimPrefix(shortLink.Url, "/")
|
targetPath := strings.TrimPrefix(shortLink.Url, "/")
|
||||||
requestURL := fmt.Sprintf("%s://%s/%s", scheme, host, targetPath)
|
requestURL := fmt.Sprintf("%s://%s/%s", scheme, host, targetPath)
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 30 * time.Second, // 30秒超时
|
Timeout: 30 * time.Second, // 30秒超时
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := client.Get(requestURL)
|
response, err := client.Get(requestURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(c, http.StatusInternalServerError, "请求错误: "+err.Error())
|
respondWithError(c, http.StatusInternalServerError, "请求错误: "+err.Error())
|
||||||
@@ -198,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)
|
||||||
|
|||||||
@@ -82,14 +82,14 @@
|
|||||||
<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" selected>Clash.Meta</option>
|
<option value="2" selected>Clash.Meta</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<!-- Template -->
|
<!-- Template -->
|
||||||
<div class="form-group mb-3">
|
<div class="form-group mb-3">
|
||||||
<label for="template">模板链接或名称:</label>
|
<label for="template">模板链接或名称:</label>
|
||||||
<input class="form-control" id="template" name="template" placeholder="输入外部模板链接或内部模板名称(可选)" type="text" />
|
<input class="form-control" id="template" name="template" placeholder="输入模板链接(可选)" type="text" />
|
||||||
</div>
|
</div>
|
||||||
<!-- Subscription Link -->
|
<!-- Subscription Link -->
|
||||||
<div class="form-group mb-3">
|
<div class="form-group mb-3">
|
||||||
@@ -180,6 +180,7 @@
|
|||||||
<div class="input-group mb-2">
|
<div class="input-group mb-2">
|
||||||
<input class="form-control" 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>
|
||||||
|
|||||||
@@ -49,141 +49,138 @@ 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 useUDP = document.getElementById("useUDP").checked;
|
|
||||||
queryParams.push(`useUDP=${useUDP ? "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 = ruleProvidersElements[baseIndex].value;
|
||||||
let behavior = ruleProviders[baseIndex].value;
|
let url = ruleProvidersElements[baseIndex + 1].value;
|
||||||
let url = ruleProviders[baseIndex + 1].value;
|
let group = ruleProvidersElements[baseIndex + 2].value;
|
||||||
let group = ruleProviders[baseIndex + 2].value;
|
let prepend = ruleProvidersElements[baseIndex + 3].value;
|
||||||
let prepend = ruleProviders[baseIndex + 3].value;
|
let name = ruleProvidersElements[baseIndex + 4].value;
|
||||||
let name = ruleProviders[baseIndex + 4].value;
|
if (
|
||||||
// 是否存在空值
|
behavior.trim() === "" ||
|
||||||
if (
|
url.trim() === "" ||
|
||||||
behavior.trim() === "" ||
|
group.trim() === "" ||
|
||||||
url.trim() === "" ||
|
prepend.trim() === "" ||
|
||||||
group.trim() === "" ||
|
name.trim() === ""
|
||||||
prepend.trim() === "" ||
|
) {
|
||||||
name.trim() === ""
|
|
||||||
) {
|
|
||||||
// alert("Rule Provider 中存在空值,请检查后重试!");
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
providers.push(`[${behavior},${url},${group},${prepend},${name}]`);
|
|
||||||
}
|
|
||||||
queryParams.push(`ruleProvider=${encodeURIComponent(providers.join(","))}`);
|
|
||||||
|
|
||||||
let ruleList = [];
|
|
||||||
for (let i = 0; i < rules.length / 2; i++) {
|
|
||||||
if (rules[i * 2].value.trim() !== "") {
|
|
||||||
let rule = rules[i * 2].value;
|
|
||||||
let prepend = rules[i * 2 + 1].value;
|
|
||||||
// 是否存在空值
|
|
||||||
if (rule.trim() === "" || prepend.trim() === "") {
|
|
||||||
// alert("Rule 中存在空值,请检查后重试!");
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
ruleList.push(`[${rule},${prepend}]`);
|
ruleProviders.push({
|
||||||
|
behavior: behavior,
|
||||||
|
url: url,
|
||||||
|
group: group,
|
||||||
|
prepend: prepend.toLowerCase() === "true",
|
||||||
|
name: name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (ruleProviders.length > 0) {
|
||||||
|
config.ruleProviders = ruleProviders;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
queryParams.push(`rule=${encodeURIComponent(ruleList.join(","))}`);
|
|
||||||
|
|
||||||
// 获取排序策略
|
const rulesElements = document.getElementsByName("rule");
|
||||||
const sort = document.getElementById("sort").value;
|
if (rulesElements.length > 0) {
|
||||||
queryParams.push(`sort=${sort}`);
|
const rules = [];
|
||||||
|
for (let i = 0; i < rulesElements.length / 2; i++) {
|
||||||
|
if (rulesElements[i * 2].value.trim() !== "") {
|
||||||
|
let rule = rulesElements[i * 2].value;
|
||||||
|
let prepend = rulesElements[i * 2 + 1].value;
|
||||||
|
if (rule.trim() === "" || prepend.trim() === "") {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
rules.push({
|
||||||
|
rule: rule,
|
||||||
|
prepend: prepend.toLowerCase() === "true",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (rules.length > 0) {
|
||||||
|
config.rules = rules;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config.sort = document.getElementById("sort").value;
|
||||||
|
|
||||||
// 获取删除节点的正则表达式
|
|
||||||
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 "";
|
}
|
||||||
|
replace[replaceStr] = replaceTo;
|
||||||
|
}
|
||||||
|
if (Object.keys(replace).length > 0) {
|
||||||
|
config.replace = replace;
|
||||||
}
|
}
|
||||||
replaceList.push(`[${replaceStr},${replaceTo}]`);
|
|
||||||
}
|
}
|
||||||
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 解析为参数
|
||||||
@@ -216,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);
|
||||||
|
|
||||||
// 回显短链相关信息
|
// 回显短链相关信息
|
||||||
@@ -248,99 +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 (params.has("useUDP")) {
|
if (config.useUDP) {
|
||||||
document.getElementById("useUDP").checked =
|
document.getElementById("useUDP").checked = config.useUDP;
|
||||||
params.get("useUDP") === "true";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,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 {
|
||||||
|
|||||||
33
test/yaml_test.go
Normal file
33
test/yaml_test.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/bestnite/sub2clash/model/proxy"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testStruct struct {
|
||||||
|
A proxy.IntOrString `yaml:"a"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshal(t *testing.T) {
|
||||||
|
yamlData1 := `a: 123`
|
||||||
|
res := testStruct{}
|
||||||
|
err := yaml.Unmarshal([]byte(yamlData1), &res)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal yaml: %v", err)
|
||||||
|
}
|
||||||
|
if res.A != 123 {
|
||||||
|
t.Errorf("expected 123, but got %v", res.A)
|
||||||
|
}
|
||||||
|
|
||||||
|
yamlData2 := `a: "123"`
|
||||||
|
err = yaml.Unmarshal([]byte(yamlData2), &res)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal yaml: %v", err)
|
||||||
|
}
|
||||||
|
if res.A != 123 {
|
||||||
|
t.Errorf("expected 123, but got %v", res.A)
|
||||||
|
}
|
||||||
|
}
|
||||||
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