mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-07-05 04:12:34 +08:00
Compare commits
10 Commits
v0.0.3
...
v0.0.5-fix
Author | SHA1 | Date | |
---|---|---|---|
159a3562d4 | |||
34b85c8d63 | |||
3546396e3d | |||
be9df16b61 | |||
67e4121fa6 | |||
c1e9099156 | |||
429788a19f | |||
2339b7d256 | |||
06c9858866 | |||
8d06ab3175 |
6
.github/workflows/docker.yml
vendored
6
.github/workflows/docker.yml
vendored
@ -47,6 +47,9 @@ jobs:
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
build-args: |
|
||||
dev=true
|
||||
version=${{ github.sha }}
|
||||
push: true
|
||||
tags: ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }}
|
||||
|
||||
@ -56,6 +59,9 @@ jobs:
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
build-args: |
|
||||
dev=false
|
||||
version=${{ steps.set_tag.outputs.tag }}
|
||||
push: true
|
||||
tags: |
|
||||
ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }}
|
||||
|
46
.github/workflows/go.yml
vendored
46
.github/workflows/go.yml
vendored
@ -23,12 +23,10 @@ jobs:
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
LDFLAGS="-s -w"
|
||||
LDFLAGS="-s -w -X config.Version=${{ github.ref_name }}"
|
||||
|
||||
# Linux
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags="$LDFLAGS" -o output/sub2clash-linux-386 main.go
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$LDFLAGS" -o output/sub2clash-linux-amd64 main.go
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags="$LDFLAGS" -o output/sub2clash-linux-arm main.go
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="$LDFLAGS" -o output/sub2clash-linux-arm64 main.go
|
||||
|
||||
# Darwin
|
||||
@ -36,9 +34,7 @@ jobs:
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="$LDFLAGS" -o output/sub2clash-darwin-arm64 main.go
|
||||
|
||||
# Windows
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="$LDFLAGS" -o output/sub2clash-windows-386.exe main.go
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="$LDFLAGS" -o output/sub2clash-windows-amd64.exe main.go
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=arm go build -ldflags="$LDFLAGS" -o output/sub2clash-windows-arm.exe main.go
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -ldflags="$LDFLAGS" -o output/sub2clash-windows-arm64.exe main.go
|
||||
|
||||
- name: Create Release
|
||||
@ -52,16 +48,6 @@ jobs:
|
||||
draft: false
|
||||
prerelease: false
|
||||
|
||||
- name: Upload Release Asset (Linux 386)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./output/sub2clash-linux-386
|
||||
asset_name: sub2clash-linux-386
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
- name: Upload Release Asset (Linux amd64)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
@ -72,16 +58,6 @@ jobs:
|
||||
asset_name: sub2clash-linux-amd64
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
- name: Upload Release Asset (Linux arm)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./output/sub2clash-linux-arm
|
||||
asset_name: sub2clash-linux-arm
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
- name: Upload Release Asset (Linux arm64)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
@ -112,16 +88,6 @@ jobs:
|
||||
asset_name: sub2clash-darwin-arm64
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
- name: Upload Release Asset (Windows 386)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./output/sub2clash-windows-386.exe
|
||||
asset_name: sub2clash-windows-386.exe
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
- name: Upload Release Asset (Windows amd64)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
@ -132,16 +98,6 @@ jobs:
|
||||
asset_name: sub2clash-windows-amd64.exe
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
- name: Upload Release Asset (Windows arm)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./output/sub2clash-windows-arm.exe
|
||||
asset_name: sub2clash-windows-arm.exe
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
- name: Upload Release Asset (Windows arm64)
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@ dist
|
||||
subs
|
||||
test
|
||||
logs
|
||||
sub2clash.db
|
||||
sub2clash.db
|
||||
.env
|
||||
|
@ -10,10 +10,8 @@ builds:
|
||||
- darwin
|
||||
goarch:
|
||||
- amd64
|
||||
- arm
|
||||
- arm64
|
||||
- 386
|
||||
ldflags:
|
||||
- -s -w
|
||||
- -s -w -X sub2clash/config.Version={{ .Version }}
|
||||
no_unique_dist_dir: true
|
||||
binary: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
|
36
API_README.md
Normal file
36
API_README.md
Normal file
@ -0,0 +1,36 @@
|
||||
# `/clash`, `/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 | 否 | - | 通过正则表达式删除节点 |
|
||||
|
||||
# `/short`
|
||||
|
||||
获取短链,Content-Type 为 `application/json`
|
||||
具体参考使用可以参考 [api\templates\index.html](./api/templates/index.html)
|
||||
|
||||
| Body 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
||||
|----------|--------|------|-----|------------------|
|
||||
| url | string | 是 | - | 需要转换的 Query 参数部分 |
|
||||
| password | string | 否 | - | 短链密码 |
|
||||
|
||||
# `/s/:hash`
|
||||
|
||||
短链跳转
|
||||
`hash` 为动态路由参数,可以通过 `/short` 接口获取
|
||||
|
||||
| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|
||||
|----------|--------|------|-----|------|
|
||||
| password | string | 否 | - | 短链密码 |
|
@ -9,8 +9,12 @@ WORKDIR /app
|
||||
COPY . .
|
||||
RUN go mod download
|
||||
|
||||
# 获取参数
|
||||
ARG version
|
||||
ARG dev
|
||||
|
||||
# 使用 -ldflags 参数进行编译
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o sub2clash main.go
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X sub2clash/config.Version=${version} -X sub2clash/config.Dev=${dev}" -o sub2clash main.go
|
||||
|
||||
FROM alpine:latest
|
||||
|
||||
|
25
README.md
25
README.md
@ -35,26 +35,21 @@
|
||||
| REQUEST_MAX_FILE_SIZE | Get 请求订阅文件最大大小(byte) | `1048576` |
|
||||
| CACHE_EXPIRE | 订阅缓存时间(秒) | `300` |
|
||||
| LOG_LEVEL | 日志等级,可选值 `debug`,`info`,`warn`,`error` | `info` |
|
||||
| SHORT_LINK_LENGTH | 短链长度 | `6` |
|
||||
|
||||
### API
|
||||
|
||||
#### `/clash`, `/meta`
|
||||
[API文档](./API_README.md)
|
||||
|
||||
获取 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` |
|
||||
可以通过变量自定义模板中的策略组代理节点
|
||||
解释的不太清楚,可以参考下方默认模板
|
||||
|
||||
## 默认模板
|
||||
- `<all>` 为添加所有节点
|
||||
- `<countries>` 为添加所有国家策略组
|
||||
|
||||
#### 默认模板
|
||||
|
||||
- [Clash](./templates/template_clash.yaml)
|
||||
- [Clash.Meta](./templates/template_meta.yaml)
|
||||
@ -62,5 +57,3 @@
|
||||
## 已知问题
|
||||
|
||||
[代理链接解析](./parser)还没有经过严格测试,可能会出现解析错误的情况,如果出现问题请提交 issue
|
||||
|
||||
## TODO
|
||||
|
@ -4,10 +4,14 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v3"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2clash/logger"
|
||||
"sub2clash/model"
|
||||
"sub2clash/parser"
|
||||
"sub2clash/utils"
|
||||
@ -30,51 +34,150 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
|
||||
if err != nil {
|
||||
templateBytes, err = utils.LoadTemplate(template)
|
||||
if err != nil {
|
||||
logger.Logger.Debug(
|
||||
"load template failed", zap.String("template", template), zap.Error(err),
|
||||
)
|
||||
return nil, errors.New("加载模板失败: " + err.Error())
|
||||
}
|
||||
} else {
|
||||
templateBytes, err = utils.LoadSubscription(template, query.Refresh)
|
||||
if err != nil {
|
||||
logger.Logger.Debug(
|
||||
"load template failed", zap.String("template", template), zap.Error(err),
|
||||
)
|
||||
return nil, errors.New("加载模板失败: " + err.Error())
|
||||
}
|
||||
}
|
||||
// 解析模板
|
||||
err = yaml.Unmarshal(templateBytes, &temp)
|
||||
if err != nil {
|
||||
logger.Logger.Debug("parse template failed", zap.Error(err))
|
||||
return nil, errors.New("解析模板失败: " + err.Error())
|
||||
}
|
||||
var proxyList []model.Proxy
|
||||
// 加载订阅
|
||||
for i := range query.Subs {
|
||||
data, err := utils.LoadSubscription(query.Subs[i], query.Refresh)
|
||||
if err != nil {
|
||||
logger.Logger.Debug(
|
||||
"load subscription failed", zap.String("url", query.Subs[i]), zap.Error(err),
|
||||
)
|
||||
return nil, errors.New("加载订阅失败: " + err.Error())
|
||||
}
|
||||
// 解析订阅
|
||||
var proxyList []model.Proxy
|
||||
err = yaml.Unmarshal(data, &sub)
|
||||
if err != nil {
|
||||
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|http|https)://")
|
||||
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|vless)://")
|
||||
if reg.Match(data) {
|
||||
proxyList = utils.ParseProxy(strings.Split(string(data), "\n")...)
|
||||
p := utils.ParseProxy(strings.Split(string(data), "\n")...)
|
||||
proxyList = append(proxyList, p...)
|
||||
} else {
|
||||
// 如果无法直接解析,尝试Base64解码
|
||||
base64, err := parser.DecodeBase64(string(data))
|
||||
if err != nil {
|
||||
logger.Logger.Debug(
|
||||
"parse subscription failed", zap.String("url", query.Subs[i]),
|
||||
zap.String("data", string(data)),
|
||||
zap.Error(err),
|
||||
)
|
||||
return nil, errors.New("加载订阅失败: " + err.Error())
|
||||
}
|
||||
proxyList = utils.ParseProxy(strings.Split(base64, "\n")...)
|
||||
p := utils.ParseProxy(strings.Split(base64, "\n")...)
|
||||
proxyList = append(proxyList, p...)
|
||||
}
|
||||
} else {
|
||||
proxyList = sub.Proxies
|
||||
proxyList = append(proxyList, sub.Proxies...)
|
||||
}
|
||||
utils.AddProxy(sub, query.AutoTest, query.Lazy, query.Sort, clashType, proxyList...)
|
||||
}
|
||||
// 处理自定义代理
|
||||
utils.AddProxy(
|
||||
sub, query.AutoTest, query.Lazy, query.Sort, clashType,
|
||||
utils.ParseProxy(query.Proxies...)...,
|
||||
)
|
||||
MergeSubAndTemplate(temp, sub)
|
||||
// 添加自定义节点
|
||||
if len(query.Proxies) != 0 {
|
||||
proxyList = append(proxyList, utils.ParseProxy(query.Proxies...)...)
|
||||
}
|
||||
// 去掉配置相同的节点
|
||||
proxies := make(map[string]*model.Proxy)
|
||||
newProxies := make([]model.Proxy, 0, len(proxyList))
|
||||
for i := range proxyList {
|
||||
key := proxyList[i].Server + ":" + strconv.Itoa(proxyList[i].Port) + ":" + proxyList[i].Type
|
||||
if _, exist := proxies[key]; !exist {
|
||||
proxies[key] = &proxyList[i]
|
||||
newProxies = append(newProxies, proxyList[i])
|
||||
}
|
||||
}
|
||||
proxyList = newProxies
|
||||
// 删除节点
|
||||
if strings.TrimSpace(query.Remove) != "" {
|
||||
newProxyList := make([]model.Proxy, 0, len(proxyList))
|
||||
for i := range proxyList {
|
||||
removeReg, err := regexp.Compile(query.Remove)
|
||||
if err != nil {
|
||||
logger.Logger.Debug("remove regexp compile failed", zap.Error(err))
|
||||
return nil, errors.New("remove 参数非法: " + err.Error())
|
||||
}
|
||||
// 删除匹配到的节点
|
||||
if removeReg.MatchString(proxyList[i].Name) {
|
||||
continue // 如果匹配到要删除的元素,跳过该元素,不添加到新切片中
|
||||
}
|
||||
newProxyList = append(newProxyList, proxyList[i]) // 将要保留的元素添加到新切片中
|
||||
}
|
||||
proxyList = newProxyList
|
||||
}
|
||||
// 重命名
|
||||
if len(query.ReplaceKeys) != 0 {
|
||||
// 创建重命名正则表达式
|
||||
replaceRegs := make([]*regexp.Regexp, 0, len(query.ReplaceKeys))
|
||||
for _, v := range query.ReplaceKeys {
|
||||
replaceReg, err := regexp.Compile(v)
|
||||
if err != nil {
|
||||
logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
|
||||
return nil, errors.New("replace 参数非法: " + err.Error())
|
||||
}
|
||||
replaceRegs = append(replaceRegs, replaceReg)
|
||||
}
|
||||
for i := range proxyList {
|
||||
// 重命名匹配到的节点
|
||||
for j, v := range replaceRegs {
|
||||
if err != nil {
|
||||
logger.Logger.Debug("replace regexp compile failed", zap.Error(err))
|
||||
return nil, errors.New("replaceName 参数非法: " + err.Error())
|
||||
}
|
||||
if v.MatchString(proxyList[i].Name) {
|
||||
proxyList[i].Name = v.ReplaceAllString(
|
||||
proxyList[i].Name, query.ReplaceTo[j],
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 重名检测
|
||||
names := make(map[string]int)
|
||||
for i := range proxyList {
|
||||
if _, exist := names[proxyList[i].Name]; exist {
|
||||
proxyList[i].Name = proxyList[i].Name + " " + strconv.Itoa(names[proxyList[i].Name])
|
||||
}
|
||||
names[proxyList[i].Name] = names[proxyList[i].Name] + 1
|
||||
}
|
||||
// trim
|
||||
for i := range proxyList {
|
||||
proxyList[i].Name = strings.TrimSpace(proxyList[i].Name)
|
||||
}
|
||||
// 将新增节点都添加到临时变量 t 中,防止策略组排序错乱
|
||||
var t = &model.Subscription{}
|
||||
utils.AddProxy(t, query.AutoTest, query.Lazy, clashType, proxyList...)
|
||||
// 排序策略组
|
||||
switch query.Sort {
|
||||
case "sizeasc":
|
||||
sort.Sort(model.ProxyGroupsSortBySize(t.ProxyGroups))
|
||||
case "sizedesc":
|
||||
sort.Sort(sort.Reverse(model.ProxyGroupsSortBySize(t.ProxyGroups)))
|
||||
case "nameasc":
|
||||
sort.Sort(model.ProxyGroupsSortByName(t.ProxyGroups))
|
||||
case "namedesc":
|
||||
sort.Sort(sort.Reverse(model.ProxyGroupsSortByName(t.ProxyGroups)))
|
||||
default:
|
||||
sort.Sort(model.ProxyGroupsSortByName(t.ProxyGroups))
|
||||
}
|
||||
// 合并新节点和模板
|
||||
MergeSubAndTemplate(temp, t)
|
||||
// 处理自定义规则
|
||||
for _, v := range query.Rules {
|
||||
if v.Prepend {
|
||||
@ -118,28 +221,28 @@ func MergeSubAndTemplate(temp *model.Subscription, sub *model.Subscription) {
|
||||
)
|
||||
}
|
||||
}
|
||||
var proxyNames []string
|
||||
for _, proxy := range sub.Proxies {
|
||||
proxyNames = append(proxyNames, proxy.Name)
|
||||
}
|
||||
// 将订阅中的节点添加到模板中
|
||||
temp.Proxies = append(temp.Proxies, sub.Proxies...)
|
||||
// 将订阅中的策略组添加到模板中
|
||||
skipGroups := []string{"全球直连", "广告拦截", "手动切换"}
|
||||
for i := range temp.ProxyGroups {
|
||||
skip := false
|
||||
for _, v := range skipGroups {
|
||||
if strings.Contains(temp.ProxyGroups[i].Name, v) {
|
||||
if v == "手动切换" {
|
||||
proxies := make([]string, 0, len(sub.Proxies))
|
||||
for _, p := range sub.Proxies {
|
||||
proxies = append(proxies, p.Name)
|
||||
}
|
||||
temp.ProxyGroups[i].Proxies = proxies
|
||||
}
|
||||
skip = true
|
||||
continue
|
||||
if temp.ProxyGroups[i].IsCountryGrop {
|
||||
continue
|
||||
}
|
||||
newProxies := make([]string, 0, len(temp.ProxyGroups[i].Proxies))
|
||||
for j := range temp.ProxyGroups[i].Proxies {
|
||||
if temp.ProxyGroups[i].Proxies[j] == "<all>" {
|
||||
newProxies = append(newProxies, proxyNames...)
|
||||
} else if temp.ProxyGroups[i].Proxies[j] == "<countries>" {
|
||||
newProxies = append(newProxies, countryGroupNames...)
|
||||
} else {
|
||||
newProxies = append(newProxies, temp.ProxyGroups[i].Proxies[j])
|
||||
}
|
||||
}
|
||||
if !skip {
|
||||
temp.ProxyGroups[i].Proxies = append(temp.ProxyGroups[i].Proxies, countryGroupNames...)
|
||||
}
|
||||
temp.ProxyGroups[i].Proxies = newProxies
|
||||
}
|
||||
temp.ProxyGroups = append(temp.ProxyGroups, sub.ProxyGroups...)
|
||||
}
|
||||
|
@ -1,12 +1,18 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2clash/config"
|
||||
"sub2clash/logger"
|
||||
"sub2clash/model"
|
||||
"sub2clash/utils"
|
||||
"sub2clash/utils/database"
|
||||
"sub2clash/validator"
|
||||
"time"
|
||||
@ -18,36 +24,85 @@ func ShortLinkGenHandler(c *gin.Context) {
|
||||
if err := c.ShouldBind(¶ms); err != nil {
|
||||
c.String(400, "参数错误: "+err.Error())
|
||||
}
|
||||
// 生成短链接
|
||||
//hash := utils.RandomString(6)
|
||||
shortLink := sha256.Sum224([]byte(params.Url))
|
||||
hash := hex.EncodeToString(shortLink[:])
|
||||
// 存入数据库
|
||||
database.DB.FirstOrCreate(
|
||||
if strings.TrimSpace(params.Url) == "" {
|
||||
c.String(400, "参数错误")
|
||||
return
|
||||
}
|
||||
// 生成hash
|
||||
hash := utils.RandomString(config.Default.ShortLinkLength)
|
||||
var item model.ShortLink
|
||||
result := database.FindShortLinkByUrl(params.Url, &item)
|
||||
if result.Error == nil {
|
||||
if item.Password != params.Password {
|
||||
item.Password = params.Password
|
||||
database.SaveShortLink(&item)
|
||||
c.String(200, item.Hash+"?password="+params.Password)
|
||||
} else {
|
||||
c.String(200, item.Hash)
|
||||
}
|
||||
return
|
||||
} else {
|
||||
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
c.String(500, "数据库错误: "+result.Error.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
// 如果记录存在则重新生成hash,直到记录不存在
|
||||
result = database.FindShortLinkByHash(hash, &item)
|
||||
for result.Error == nil {
|
||||
hash = utils.RandomString(config.Default.ShortLinkLength)
|
||||
result = database.FindShortLinkByHash(hash, &item)
|
||||
}
|
||||
// 创建记录
|
||||
database.FirstOrCreateShortLink(
|
||||
&model.ShortLink{
|
||||
Hash: hash,
|
||||
Url: params.Url,
|
||||
LastRequestTime: -1,
|
||||
Password: params.Password,
|
||||
},
|
||||
)
|
||||
// 返回短链接
|
||||
if params.Password != "" {
|
||||
hash += "?password=" + params.Password
|
||||
}
|
||||
c.String(200, hash)
|
||||
}
|
||||
|
||||
func ShortLinkGetHandler(c *gin.Context) {
|
||||
// 获取动态路由
|
||||
hash := c.Param("hash")
|
||||
// 查询数据库
|
||||
var shortLink model.ShortLink
|
||||
result := database.DB.Where("hash = ?", hash).First(&shortLink)
|
||||
// 更新最后访问时间
|
||||
shortLink.LastRequestTime = time.Now().Unix()
|
||||
database.DB.Save(&shortLink)
|
||||
// 重定向
|
||||
if result.Error != nil {
|
||||
c.String(404, "未找到短链接")
|
||||
password := c.Query("password")
|
||||
if strings.TrimSpace(hash) == "" {
|
||||
c.String(400, "参数错误")
|
||||
return
|
||||
}
|
||||
uri := config.Default.BasePath + shortLink.Url
|
||||
c.Redirect(http.StatusTemporaryRedirect, uri)
|
||||
// 查询数据库
|
||||
var shortLink model.ShortLink
|
||||
result := database.FindShortLinkByHash(hash, &shortLink)
|
||||
// 重定向
|
||||
if result.Error != nil {
|
||||
c.String(404, "未找到短链接或密码错误")
|
||||
return
|
||||
}
|
||||
if shortLink.Password != "" && shortLink.Password != password {
|
||||
c.String(404, "未找到短链接或密码错误")
|
||||
return
|
||||
}
|
||||
// 更新最后访问时间
|
||||
shortLink.LastRequestTime = time.Now().Unix()
|
||||
database.SaveShortLink(&shortLink)
|
||||
get, err := utils.Get("http://localhost:" + strconv.Itoa(config.Default.Port) + "/" + shortLink.Url)
|
||||
if err != nil {
|
||||
logger.Logger.Debug("get short link data failed", zap.Error(err))
|
||||
c.String(500, "请求错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
all, err := io.ReadAll(get.Body)
|
||||
if err != nil {
|
||||
logger.Logger.Debug("read short link data failed", zap.Error(err))
|
||||
c.String(500, "读取错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, string(all))
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"html/template"
|
||||
"sub2clash/api/controller"
|
||||
"sub2clash/config"
|
||||
"sub2clash/middleware"
|
||||
)
|
||||
|
||||
@ -17,7 +18,11 @@ func SetRoute(r *gin.Engine) {
|
||||
r.SetHTMLTemplate(template.Must(template.New("").ParseFS(templates, "templates/*")))
|
||||
r.GET(
|
||||
"/", func(c *gin.Context) {
|
||||
c.HTML(200, "index.html", nil)
|
||||
c.HTML(
|
||||
200, "index.html", gin.H{
|
||||
"Version": config.Version,
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
r.GET(
|
||||
|
@ -1,163 +1,270 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
|
||||
<title>sub2clash</title>
|
||||
|
||||
<!-- Bootstrap CSS -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
|
||||
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
|
||||
<link
|
||||
crossorigin="anonymous"
|
||||
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
|
||||
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
|
||||
rel="stylesheet"
|
||||
/>
|
||||
|
||||
<!-- Bootstrap JS -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
|
||||
crossorigin="anonymous"></script>
|
||||
<script
|
||||
crossorigin="anonymous"
|
||||
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
|
||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
|
||||
></script>
|
||||
|
||||
<!-- Axios -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
|
||||
|
||||
<style>
|
||||
.container {
|
||||
max-width: 800px;
|
||||
}
|
||||
.container {
|
||||
max-width: 800px;
|
||||
}
|
||||
|
||||
.btn-xs {
|
||||
padding: 2px 2px; /* 调整内边距以减小按钮大小 */
|
||||
font-size: 10px; /* 设置字体大小 */
|
||||
line-height: 1.2; /* 调整行高 */
|
||||
border-radius: 3px; /* 可选的边框半径调整 */
|
||||
height: 25px;
|
||||
width: 25px;
|
||||
}
|
||||
.btn-xs {
|
||||
padding: 2px 2px; /* 调整内边距以减小按钮大小 */
|
||||
font-size: 10px; /* 设置字体大小 */
|
||||
line-height: 1.2; /* 调整行高 */
|
||||
border-radius: 3px; /* 可选的边框半径调整 */
|
||||
height: 25px;
|
||||
width: 25px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
</head>
|
||||
|
||||
<body class="bg-light">
|
||||
|
||||
<div class="container mt-5">
|
||||
<div class="mb-4">
|
||||
<body class="bg-light">
|
||||
<div class="container mt-5">
|
||||
<div class="mb-4">
|
||||
<h2>sub2clash</h2>
|
||||
<span class="text-muted fst-italic">通用订阅链接转 Clash(Meta) 配置工具 <a
|
||||
href="https://github.com/nitezs/sub2clash#clash-meta" target="_blank">使用文档</a></span>
|
||||
</div>
|
||||
|
||||
<form id="apiForm">
|
||||
<span class="text-muted fst-italic"
|
||||
>通用订阅链接转 Clash(Meta) 配置工具
|
||||
<a
|
||||
href="https://github.com/nitezs/sub2clash#clash-meta"
|
||||
target="_blank"
|
||||
>使用文档</a
|
||||
></span
|
||||
>
|
||||
</div>
|
||||
|
||||
<form id="apiForm">
|
||||
<!-- API Endpoint -->
|
||||
<div class="form-group mb-3">
|
||||
<label for="endpoint">客户端类型:</label>
|
||||
<select class="form-control" id="endpoint" name="endpoint">
|
||||
<option value="clash">Clash</option>
|
||||
<option value="meta">Clash.Meta</option>
|
||||
</select>
|
||||
<label for="endpoint">客户端类型:</label>
|
||||
<select class="form-control" id="endpoint" name="endpoint">
|
||||
<option value="clash">Clash</option>
|
||||
<option value="meta">Clash.Meta</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Subscription Link -->
|
||||
<div class="form-group mb-3">
|
||||
<label for="sub">订阅链接:</label>
|
||||
<textarea class="form-control" id="sub" name="sub" rows="5" placeholder="每行输入一个订阅链接"></textarea>
|
||||
<label for="sub">订阅链接:</label>
|
||||
<textarea
|
||||
class="form-control"
|
||||
id="sub"
|
||||
name="sub"
|
||||
placeholder="每行输入一个订阅链接"
|
||||
rows="5"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<!-- Proxy Link -->
|
||||
<div class="form-group mb-3">
|
||||
<label for="proxy">节点分享链接:</label>
|
||||
<textarea class="form-control" id="proxy" name="proxy" rows="5"
|
||||
placeholder="每行输入一个节点分享链接"></textarea>
|
||||
<label for="proxy">节点分享链接:</label>
|
||||
<textarea
|
||||
class="form-control"
|
||||
id="proxy"
|
||||
name="proxy"
|
||||
placeholder="每行输入一个节点分享链接"
|
||||
rows="5"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<!-- Refresh -->
|
||||
<div class="form-check mb-3">
|
||||
<input type="checkbox" class="form-check-input" id="refresh" name="refresh">
|
||||
<label class="form-check-label" for="refresh">强制刷新配置</label>
|
||||
<input
|
||||
class="form-check-input"
|
||||
id="refresh"
|
||||
name="refresh"
|
||||
type="checkbox"
|
||||
/>
|
||||
<label class="form-check-label" for="refresh">强制刷新配置</label>
|
||||
</div>
|
||||
|
||||
<!-- Template -->
|
||||
<div class="form-group mb-3">
|
||||
<label for="template">模板链接或名称(可选):</label>
|
||||
<input type="text" class="form-control" id="template" name="template"
|
||||
placeholder="输入外部模板链接或内部模板名称">
|
||||
<label for="template">模板链接或名称(可选):</label>
|
||||
<input
|
||||
class="form-control"
|
||||
id="template"
|
||||
name="template"
|
||||
placeholder="输入外部模板链接或内部模板名称"
|
||||
type="text"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Rule Provider -->
|
||||
<div class="form-group mb-3" id="ruleProviderGroup">
|
||||
<label>Rule Provider:</label>
|
||||
<button type="button" class="btn btn-primary mb-1 btn-xs" onclick="addRuleProvider()">+</button>
|
||||
<label>Rule Provider:</label>
|
||||
<button
|
||||
class="btn btn-primary mb-1 btn-xs"
|
||||
onclick="addRuleProvider()"
|
||||
type="button"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Rule -->
|
||||
<div class="form-group mb-3" id="ruleGroup">
|
||||
<label>规则:</label>
|
||||
<button type="button" class="btn btn-primary mb-1 btn-xs" onclick="addRule()">+</button>
|
||||
<label>规则:</label>
|
||||
<button
|
||||
class="btn btn-primary mb-1 btn-xs"
|
||||
onclick="addRule()"
|
||||
type="button"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Auto Test -->
|
||||
<div class="form-check mb-3">
|
||||
<input type="checkbox" class="form-check-input" id="autoTest" name="autoTest">
|
||||
<label class="form-check-label" for="autoTest">指定国家策略组是否自动测速</label>
|
||||
<input
|
||||
class="form-check-input"
|
||||
id="autoTest"
|
||||
name="autoTest"
|
||||
type="checkbox"
|
||||
/>
|
||||
<label class="form-check-label" for="autoTest"
|
||||
>国家策略组自动测速</label
|
||||
>
|
||||
</div>
|
||||
|
||||
<!-- Lazy -->
|
||||
<div class="form-check mb-3">
|
||||
<input type="checkbox" class="form-check-input" id="lazy" name="lazy">
|
||||
<label class="form-check-label" for="lazy">自动测速是否启用 lazy</label>
|
||||
<input
|
||||
class="form-check-input"
|
||||
id="lazy"
|
||||
name="lazy"
|
||||
type="checkbox"
|
||||
/>
|
||||
<label class="form-check-label" for="lazy">自动测速启用 lazy</label>
|
||||
</div>
|
||||
|
||||
<!-- Sort -->
|
||||
<div class="form-group mb-3">
|
||||
<label for="sort">国家策略组排序策略:</label>
|
||||
<select class="form-control" id="sort" name="sort">
|
||||
<option value="nameasc">名称(升序)</option>
|
||||
<option value="namedesc">名称(降序)</option>
|
||||
<option value="sizeasc">节点数量(升序)</option>
|
||||
<option value="sizedesc">节点数量(降序)</option>
|
||||
</select>
|
||||
<label for="sort">国家策略组排序策略:</label>
|
||||
<select class="form-control" id="sort" name="sort">
|
||||
<option value="nameasc">名称(升序)</option>
|
||||
<option value="namedesc">名称(降序)</option>
|
||||
<option value="sizeasc">节点数量(升序)</option>
|
||||
<option value="sizedesc">节点数量(降序)</option>
|
||||
</select>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<!-- Display the API Link -->
|
||||
<div class="form-group mb-5">
|
||||
<!-- Remove -->
|
||||
<div class="form-group mb-3">
|
||||
<label for="remove">删除节点:</label>
|
||||
<input
|
||||
class="form-control"
|
||||
type="text"
|
||||
name="remove"
|
||||
id="remove"
|
||||
placeholder="正则表达式"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Rename -->
|
||||
<div class="form-group mb-3" id="replaceGroup">
|
||||
<label>节点名称替换:</label>
|
||||
<button
|
||||
class="btn btn-primary mb-1 btn-xs"
|
||||
onclick="addReplace()"
|
||||
type="button"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<!-- Display the API Link -->
|
||||
<div class="form-group mb-5">
|
||||
<label for="apiLink">配置链接:</label>
|
||||
<div class="input-group mb-2">
|
||||
<input type="text" class="form-control" id="apiLink" readonly>
|
||||
<button class="btn btn-primary" type="button" onclick="generateURL()">生成链接</button>
|
||||
<button class="btn btn-primary" type="button" onclick="copyToClipboard('apiLink',this)">复制链接</button>
|
||||
<input class="form-control" id="apiLink" readonly type="text" />
|
||||
<button class="btn btn-primary" onclick="generateURL()" type="button">
|
||||
生成链接
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-primary"
|
||||
onclick="copyToClipboard('apiLink',this)"
|
||||
type="button"
|
||||
>
|
||||
复制链接
|
||||
</button>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control" id="apiShortLink" readonly>
|
||||
<button class="btn btn-primary" type="button" onclick="generateShortLink()">生成短链</button>
|
||||
<button class="btn btn-primary" type="button" onclick="copyToClipboard('apiShortLink',this)">复制短链
|
||||
</button>
|
||||
<input class="form-control" id="apiShortLink" readonly type="text" />
|
||||
<input
|
||||
class="form-control"
|
||||
id="password"
|
||||
type="text"
|
||||
placeholder="密码"
|
||||
/>
|
||||
<button
|
||||
class="btn btn-primary"
|
||||
onclick="generateShortLink()"
|
||||
type="button"
|
||||
>
|
||||
生成短链
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-primary"
|
||||
onclick="copyToClipboard('apiShortLink',this)"
|
||||
type="button"
|
||||
>
|
||||
复制短链
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- footer-->
|
||||
<footer>
|
||||
<p class="text-center">
|
||||
Powered by
|
||||
<a class="link-primary" href="https://github.com/nitezs/sub2clash"
|
||||
>sub2clash</a
|
||||
>
|
||||
</p>
|
||||
<p class="text-center">Version {{.Version}}</p>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
<!-- footer-->
|
||||
<footer>
|
||||
<p class="text-center">Powered by <a class="link-primary"
|
||||
href="https://github.com/nitezs/sub2clash">sub2clash</a></p>
|
||||
</footer>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function copyToClipboard(elem, e) {
|
||||
<script>
|
||||
async function copyToClipboard(elem, e) {
|
||||
const apiLinkInput = document.querySelector(`#${elem}`).value;
|
||||
try {
|
||||
await navigator.clipboard.writeText(apiLinkInput);
|
||||
let text = e.textContent;
|
||||
e.addEventListener("mouseout", function () {
|
||||
e.textContent = text;
|
||||
});
|
||||
e.textContent = "复制成功";
|
||||
await navigator.clipboard.writeText(apiLinkInput);
|
||||
let text = e.textContent;
|
||||
e.addEventListener("mouseout", function () {
|
||||
e.textContent = text;
|
||||
});
|
||||
e.textContent = "复制成功";
|
||||
} catch (err) {
|
||||
console.error('复制到剪贴板失败:', err);
|
||||
console.error("复制到剪贴板失败:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createRuleProvider() {
|
||||
const div = document.createElement('div');
|
||||
div.classList.add('input-group', 'mb-2');
|
||||
function createRuleProvider() {
|
||||
const div = document.createElement("div");
|
||||
div.classList.add("input-group", "mb-2");
|
||||
div.innerHTML = `
|
||||
<input type="text" class="form-control" name="ruleProvider" placeholder="Behavior">
|
||||
<input type="text" class="form-control" name="ruleProvider" placeholder="Url">
|
||||
@ -167,11 +274,22 @@
|
||||
<button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button>
|
||||
`;
|
||||
return div;
|
||||
}
|
||||
}
|
||||
|
||||
function createRule() {
|
||||
const div = document.createElement('div');
|
||||
div.classList.add('input-group', 'mb-2');
|
||||
function createReplace() {
|
||||
const div = document.createElement("div");
|
||||
div.classList.add("input-group", "mb-2");
|
||||
div.innerHTML = `
|
||||
<input type="text" class="form-control" name="replace" placeholder="原字符串(正则表达式)">
|
||||
<input type="text" class="form-control" name="replace" placeholder="替换为(可为空)">
|
||||
<button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button>
|
||||
`;
|
||||
return div;
|
||||
}
|
||||
|
||||
function createRule() {
|
||||
const div = document.createElement("div");
|
||||
div.classList.add("input-group", "mb-2");
|
||||
div.innerHTML = `
|
||||
<input type="text" class="form-control" name="rule" placeholder="Rule">
|
||||
<input type="text" class="form-control" name="rule" placeholder="Prepend">
|
||||
@ -179,73 +297,84 @@
|
||||
<button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button>
|
||||
`;
|
||||
return div;
|
||||
}
|
||||
}
|
||||
|
||||
function addRuleProvider() {
|
||||
function addRuleProvider() {
|
||||
const div = createRuleProvider();
|
||||
document.getElementById('ruleProviderGroup').appendChild(div);
|
||||
}
|
||||
document.getElementById("ruleProviderGroup").appendChild(div);
|
||||
}
|
||||
|
||||
function addRule() {
|
||||
function addRule() {
|
||||
const div = createRule();
|
||||
document.getElementById('ruleGroup').appendChild(div);
|
||||
}
|
||||
document.getElementById("ruleGroup").appendChild(div);
|
||||
}
|
||||
|
||||
function removeElement(button) {
|
||||
function addReplace() {
|
||||
const div = createReplace();
|
||||
document.getElementById("replaceGroup").appendChild(div);
|
||||
}
|
||||
|
||||
function removeElement(button) {
|
||||
button.parentElement.remove();
|
||||
}
|
||||
}
|
||||
|
||||
function generateURI() {
|
||||
function generateURI() {
|
||||
const queryParams = [];
|
||||
|
||||
// 获取 API Endpoint
|
||||
const endpoint = document.getElementById("endpoint").value;
|
||||
|
||||
// 获取并组合订阅链接
|
||||
let subLines = document.getElementById("sub").value.split('\n').filter(line => line.trim() !== "");
|
||||
let noSub = false
|
||||
let subLines = document
|
||||
.getElementById("sub")
|
||||
.value.split("\n")
|
||||
.filter((line) => line.trim() !== "");
|
||||
let noSub = false;
|
||||
// 去除 subLines 中空元素
|
||||
subLines = subLines.map((item) => {
|
||||
if (item !== "") {
|
||||
return item;
|
||||
}
|
||||
if (item !== "") {
|
||||
return item;
|
||||
}
|
||||
});
|
||||
if (subLines.length > 0) {
|
||||
queryParams.push(`sub=${encodeURIComponent(subLines.join(','))}`);
|
||||
queryParams.push(`sub=${encodeURIComponent(subLines.join(","))}`);
|
||||
} else {
|
||||
noSub = true
|
||||
noSub = true;
|
||||
}
|
||||
|
||||
// 获取并组合节点分享链接
|
||||
let proxyLines = document.getElementById("proxy").value.split('\n').filter(line => line.trim() !== "");
|
||||
let noProxy = false
|
||||
let proxyLines = document
|
||||
.getElementById("proxy")
|
||||
.value.split("\n")
|
||||
.filter((line) => line.trim() !== "");
|
||||
let noProxy = false;
|
||||
// 去除 proxyLines 中空元素
|
||||
proxyLines = proxyLines.map((item) => {
|
||||
if (item !== "") {
|
||||
return item;
|
||||
}
|
||||
if (item !== "") {
|
||||
return item;
|
||||
}
|
||||
});
|
||||
if (proxyLines.length > 0) {
|
||||
queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(','))}`);
|
||||
queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(","))}`);
|
||||
} else {
|
||||
noProxy = true
|
||||
noProxy = true;
|
||||
}
|
||||
if (noSub && noProxy) {
|
||||
alert("订阅链接和节点分享链接不能同时为空!")
|
||||
return
|
||||
alert("订阅链接和节点分享链接不能同时为空!");
|
||||
return "";
|
||||
}
|
||||
// 获取复选框的值
|
||||
const refresh = document.getElementById("refresh").checked;
|
||||
queryParams.push(`refresh=${refresh ? 'true' : 'false'}`);
|
||||
queryParams.push(`refresh=${refresh ? "true" : "false"}`);
|
||||
const autoTest = document.getElementById("autoTest").checked;
|
||||
queryParams.push(`autoTest=${autoTest ? 'true' : 'false'}`);
|
||||
queryParams.push(`autoTest=${autoTest ? "true" : "false"}`);
|
||||
const lazy = document.getElementById("lazy").checked;
|
||||
queryParams.push(`lazy=${lazy ? 'true' : 'false'}`);
|
||||
queryParams.push(`lazy=${lazy ? "true" : "false"}`);
|
||||
|
||||
// 获取模板链接或名称(如果存在)
|
||||
const template = document.getElementById("template").value;
|
||||
if (template.trim() !== "") {
|
||||
queryParams.push(`template=${encodeURIComponent(template)}`);
|
||||
queryParams.push(`template=${encodeURIComponent(template)}`);
|
||||
}
|
||||
|
||||
// 获取Rule Provider和规则
|
||||
@ -253,65 +382,115 @@
|
||||
const rules = document.getElementsByName("rule");
|
||||
let providers = [];
|
||||
for (let i = 0; i < ruleProviders.length / 5; i++) {
|
||||
let baseIndex = i * 5;
|
||||
let behavior = ruleProviders[baseIndex].value;
|
||||
let url = ruleProviders[baseIndex + 1].value;
|
||||
let group = ruleProviders[baseIndex + 2].value;
|
||||
let prepend = ruleProviders[baseIndex + 3].value;
|
||||
let name = ruleProviders[baseIndex + 4].value;
|
||||
// 是否存在空值
|
||||
if (behavior.trim() === "" || url.trim() === "" || group.trim() === "" || prepend.trim() === "" || name.trim() === "") {
|
||||
alert("Rule Provider 中存在空值,请检查后重试!");
|
||||
return;
|
||||
}
|
||||
providers.push(`[${behavior},${url},${group},${prepend},${name}]`);
|
||||
let baseIndex = i * 5;
|
||||
let behavior = ruleProviders[baseIndex].value;
|
||||
let url = ruleProviders[baseIndex + 1].value;
|
||||
let group = ruleProviders[baseIndex + 2].value;
|
||||
let prepend = ruleProviders[baseIndex + 3].value;
|
||||
let name = ruleProviders[baseIndex + 4].value;
|
||||
// 是否存在空值
|
||||
if (
|
||||
behavior.trim() === "" ||
|
||||
url.trim() === "" ||
|
||||
group.trim() === "" ||
|
||||
prepend.trim() === "" ||
|
||||
name.trim() === ""
|
||||
) {
|
||||
alert("Rule Provider 中存在空值,请检查后重试!");
|
||||
return "";
|
||||
}
|
||||
providers.push(`[${behavior},${url},${group},${prepend},${name}]`);
|
||||
}
|
||||
queryParams.push(`ruleProvider=${encodeURIComponent(providers.join(','))}`);
|
||||
queryParams.push(
|
||||
`ruleProvider=${encodeURIComponent(providers.join(","))}`,
|
||||
);
|
||||
|
||||
let ruleList = [];
|
||||
for (let i = 0; i < rules.length / 3; i++) {
|
||||
if (rules[i * 3].value.trim() !== "") {
|
||||
let rule = rules[i * 3].value;
|
||||
let prepend = rules[i * 3 + 1].value;
|
||||
let group = rules[i * 3 + 2].value;
|
||||
// 是否存在空值
|
||||
if (rule.trim() === "" || prepend.trim() === "" || group.trim() === "") {
|
||||
alert("Rule 中存在空值,请检查后重试!");
|
||||
return;
|
||||
}
|
||||
ruleList.push(`[${rule},${prepend},${group}]`);
|
||||
if (rules[i * 3].value.trim() !== "") {
|
||||
let rule = rules[i * 3].value;
|
||||
let prepend = rules[i * 3 + 1].value;
|
||||
let group = rules[i * 3 + 2].value;
|
||||
// 是否存在空值
|
||||
if (
|
||||
rule.trim() === "" ||
|
||||
prepend.trim() === "" ||
|
||||
group.trim() === ""
|
||||
) {
|
||||
alert("Rule 中存在空值,请检查后重试!");
|
||||
return "";
|
||||
}
|
||||
ruleList.push(`[${rule},${prepend},${group}]`);
|
||||
}
|
||||
}
|
||||
queryParams.push(`rule=${encodeURIComponent(ruleList.join(','))}`);
|
||||
queryParams.push(`rule=${encodeURIComponent(ruleList.join(","))}`);
|
||||
|
||||
// 获取排序策略
|
||||
const sort = document.getElementById("sort").value;
|
||||
queryParams.push(`sort=${sort}`);
|
||||
return `${endpoint}?${queryParams.join('&')}`;
|
||||
}
|
||||
|
||||
function generateURL() {
|
||||
// 获取删除节点的正则表达式
|
||||
const remove = document.getElementById("remove").value;
|
||||
if (remove.trim() !== "") {
|
||||
queryParams.push(`remove=${encodeURIComponent(remove)}`);
|
||||
}
|
||||
|
||||
// 获取替换节点名称的正则表达式
|
||||
let replaceList = [];
|
||||
const replaces = document.getElementsByName("replace");
|
||||
for (let i = 0; i < replaces.length / 2; i++) {
|
||||
let replaceStr = `<${replaces[i * 2].value}>`;
|
||||
let replaceTo = `<${replaces[i * 2 + 1].value}>`;
|
||||
if (replaceStr.trim() === "") {
|
||||
alert("重命名设置中存在空值,请检查后重试!");
|
||||
return "";
|
||||
}
|
||||
replaceList.push(`[${replaceStr},${replaceTo}]`);
|
||||
}
|
||||
queryParams.push(
|
||||
`replace=${encodeURIComponent(replaceList.join(","))}`,
|
||||
);
|
||||
|
||||
return `${endpoint}?${queryParams.join("&")}`;
|
||||
}
|
||||
|
||||
function generateURL() {
|
||||
const apiLink = document.getElementById("apiLink");
|
||||
apiLink.value = `${window.location.origin}${window.location.pathname}${generateURI()}`;
|
||||
}
|
||||
let uri = generateURI();
|
||||
if (uri === "") {
|
||||
return;
|
||||
}
|
||||
apiLink.value = `${window.location.origin}${window.location.pathname}${uri}`;
|
||||
}
|
||||
|
||||
function generateShortLink() {
|
||||
function generateShortLink() {
|
||||
const apiShortLink = document.getElementById("apiShortLink");
|
||||
|
||||
axios.post("./short", {
|
||||
"url": generateURI()
|
||||
}, {
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
}).then((response) => {
|
||||
const password = document.getElementById("password");
|
||||
let uri = generateURI();
|
||||
if (uri === "") {
|
||||
return;
|
||||
}
|
||||
axios
|
||||
.post(
|
||||
"./short",
|
||||
{
|
||||
url: uri,
|
||||
password: password.value.trim(),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
)
|
||||
.then((response) => {
|
||||
apiShortLink.value = `${window.location.origin}${window.location.pathname}s/${response.data}`;
|
||||
}).catch((error) => {
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
alert("生成短链失败,请重试!");
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/joho/godotenv"
|
||||
"os"
|
||||
"strconv"
|
||||
@ -15,11 +16,14 @@ type Config struct {
|
||||
CacheExpire int64
|
||||
LogLevel string
|
||||
BasePath string
|
||||
ShortLinkLength int
|
||||
}
|
||||
|
||||
var Default *Config
|
||||
var Version string
|
||||
var Dev string
|
||||
|
||||
func init() {
|
||||
func LoadConfig() error {
|
||||
Default = &Config{
|
||||
MetaTemplate: "template_meta.yaml",
|
||||
ClashTemplate: "template_clash.yaml",
|
||||
@ -29,12 +33,13 @@ func init() {
|
||||
CacheExpire: 60 * 5,
|
||||
LogLevel: "info",
|
||||
BasePath: "/",
|
||||
ShortLinkLength: 6,
|
||||
}
|
||||
_ = godotenv.Load()
|
||||
if os.Getenv("PORT") != "" {
|
||||
atoi, err := strconv.Atoi(os.Getenv("PORT"))
|
||||
if err != nil {
|
||||
panic("PORT invalid")
|
||||
return errors.New("PORT invalid")
|
||||
}
|
||||
Default.Port = atoi
|
||||
}
|
||||
@ -47,21 +52,21 @@ func init() {
|
||||
if os.Getenv("REQUEST_RETRY_TIMES") != "" {
|
||||
atoi, err := strconv.Atoi(os.Getenv("REQUEST_RETRY_TIMES"))
|
||||
if err != nil {
|
||||
panic("REQUEST_RETRY_TIMES invalid")
|
||||
return errors.New("REQUEST_RETRY_TIMES invalid")
|
||||
}
|
||||
Default.RequestRetryTimes = atoi
|
||||
}
|
||||
if os.Getenv("REQUEST_MAX_FILE_SIZE") != "" {
|
||||
atoi, err := strconv.Atoi(os.Getenv("REQUEST_MAX_FILE_SIZE"))
|
||||
if err != nil {
|
||||
panic("REQUEST_MAX_FILE_SIZE invalid")
|
||||
return errors.New("REQUEST_MAX_FILE_SIZE invalid")
|
||||
}
|
||||
Default.RequestMaxFileSize = int64(atoi)
|
||||
}
|
||||
if os.Getenv("CACHE_EXPIRE") != "" {
|
||||
atoi, err := strconv.Atoi(os.Getenv("CACHE_EXPIRE"))
|
||||
if err != nil {
|
||||
panic("CACHE_EXPIRE invalid")
|
||||
return errors.New("CACHE_EXPIRE invalid")
|
||||
}
|
||||
Default.CacheExpire = int64(atoi)
|
||||
}
|
||||
@ -74,4 +79,12 @@ func init() {
|
||||
Default.BasePath += "/"
|
||||
}
|
||||
}
|
||||
if os.Getenv("SHORT_LINK_LENGTH") != "" {
|
||||
atoi, err := strconv.Atoi(os.Getenv("SHORT_LINK_LENGTH"))
|
||||
if err != nil {
|
||||
return errors.New("SHORT_LINK_LENGTH invalid")
|
||||
}
|
||||
Default.ShortLinkLength = atoi
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -9,7 +9,8 @@ services:
|
||||
- "8011:8011"
|
||||
volumes:
|
||||
- ./logs:/app/logs
|
||||
# - ./templates:/app/templates
|
||||
- ./templates:/app/templates
|
||||
- ./data:/app/data
|
||||
# environment:
|
||||
# - PORT=8011
|
||||
# - META_TEMPLATE=template_meta.yaml
|
||||
|
@ -4,18 +4,18 @@ import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"path/filepath"
|
||||
"sub2clash/config"
|
||||
"sub2clash/utils"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Logger *zap.Logger
|
||||
lock sync.Mutex
|
||||
Logger *zap.Logger
|
||||
lock sync.Mutex
|
||||
logLevel string
|
||||
)
|
||||
|
||||
func init() {
|
||||
func InitLogger(level string) {
|
||||
logLevel = level
|
||||
buildLogger()
|
||||
go rotateLogs()
|
||||
}
|
||||
@ -24,7 +24,7 @@ func buildLogger() {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
var level zapcore.Level
|
||||
switch config.Default.LogLevel {
|
||||
switch logLevel {
|
||||
case "error":
|
||||
level = zap.ErrorLevel
|
||||
case "debug":
|
||||
@ -36,10 +36,6 @@ func buildLogger() {
|
||||
default:
|
||||
level = zap.InfoLevel
|
||||
}
|
||||
err := utils.MKDir("logs")
|
||||
if err != nil {
|
||||
panic("创建日志失败" + err.Error())
|
||||
}
|
||||
zapConfig := zap.NewProductionConfig()
|
||||
zapConfig.Encoding = "console"
|
||||
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
@ -47,9 +43,10 @@ func buildLogger() {
|
||||
zapConfig.OutputPaths = []string{"stdout", getLogFileName("info")}
|
||||
zapConfig.ErrorOutputPaths = []string{"stderr", getLogFileName("error")}
|
||||
zapConfig.Level = zap.NewAtomicLevelAt(level)
|
||||
var err error
|
||||
Logger, err = zapConfig.Build()
|
||||
if err != nil {
|
||||
panic("创建日志失败" + err.Error())
|
||||
panic("log failed" + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
76
main.go
76
main.go
@ -5,8 +5,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sub2clash/api"
|
||||
"sub2clash/config"
|
||||
@ -21,43 +19,45 @@ var templateMeta string
|
||||
//go:embed templates/template_clash.yaml
|
||||
var templateClash string
|
||||
|
||||
func writeTemplate(path string, template string) error {
|
||||
tPath := filepath.Join(
|
||||
"templates", path,
|
||||
)
|
||||
if _, err := os.Stat(tPath); os.IsNotExist(err) {
|
||||
file, err := os.Create(tPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
_ = file.Close()
|
||||
}(file)
|
||||
_, err = file.WriteString(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := utils.MKDir("subs"); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := utils.MKDir("templates"); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := writeTemplate(config.Default.MetaTemplate, templateMeta); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := writeTemplate(config.Default.ClashTemplate, templateClash); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
err := database.ConnectDB()
|
||||
var err error
|
||||
// 创建文件夹
|
||||
err = utils.MkEssentialDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
logger.Logger.Panic("create essential dir failed", zap.Error(err))
|
||||
}
|
||||
// 加载配置
|
||||
err = config.LoadConfig()
|
||||
// 初始化日志
|
||||
logger.InitLogger(config.Default.LogLevel)
|
||||
if err != nil {
|
||||
logger.Logger.Panic("load config failed", zap.Error(err))
|
||||
}
|
||||
// 检查更新
|
||||
if config.Dev != "true" {
|
||||
go func() {
|
||||
update, newVersion, err := utils.CheckUpdate()
|
||||
if err != nil {
|
||||
logger.Logger.Warn("check update failed", zap.Error(err))
|
||||
}
|
||||
if update {
|
||||
logger.Logger.Info("new version is available", zap.String("version", newVersion))
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
logger.Logger.Info("running in dev mode")
|
||||
}
|
||||
// 写入默认模板
|
||||
err = utils.WriteDefalutTemplate(templateMeta, templateClash)
|
||||
if err != nil {
|
||||
logger.Logger.Panic("write default template failed", zap.Error(err))
|
||||
}
|
||||
// 连接数据库
|
||||
err = database.ConnectDB()
|
||||
if err != nil {
|
||||
logger.Logger.Panic("database connect failed", zap.Error(err))
|
||||
}
|
||||
logger.Logger.Info("database connect success")
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -69,10 +69,10 @@ func main() {
|
||||
r := gin.Default()
|
||||
// 设置路由
|
||||
api.SetRoute(r)
|
||||
logger.Logger.Info("Server is running at http://localhost:" + strconv.Itoa(config.Default.Port))
|
||||
logger.Logger.Info("server is running at http://localhost:" + strconv.Itoa(config.Default.Port))
|
||||
err := r.Run(":" + strconv.Itoa(config.Default.Port))
|
||||
if err != nil {
|
||||
logger.Logger.Error("Server run error", zap.Error(err))
|
||||
logger.Logger.Error("server running failed", zap.Error(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
12
model/github.go
Normal file
12
model/github.go
Normal file
@ -0,0 +1,12 @@
|
||||
package model
|
||||
|
||||
type Tags []struct {
|
||||
Name string `json:"name"`
|
||||
ZipballUrl string `json:"zipball_url"`
|
||||
TarballUrl string `json:"tarball_url"`
|
||||
Commit struct {
|
||||
Sha string `json:"sha"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
NodeId string `json:"node_id"`
|
||||
}
|
@ -39,7 +39,6 @@ func (p ProxyGroupsSortByName) Less(i, j int) bool {
|
||||
bestMatch, _, _ := matcher.Match(language.Make("zh"))
|
||||
// 使用最佳匹配的语言进行排序
|
||||
c := collate.New(bestMatch)
|
||||
|
||||
return c.CompareString(p[i].Name, p[j].Name) < 0
|
||||
}
|
||||
|
||||
|
@ -3,5 +3,6 @@ package model
|
||||
type ShortLink struct {
|
||||
Hash string `gorm:"primary_key"`
|
||||
Url string
|
||||
Password string
|
||||
LastRequestTime int64
|
||||
}
|
||||
|
@ -12,12 +12,12 @@ import (
|
||||
func ParseSS(proxy string) (model.Proxy, error) {
|
||||
// 判断是否以 ss:// 开头
|
||||
if !strings.HasPrefix(proxy, "ss://") {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 ss Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
||||
}
|
||||
// 分割
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
|
||||
if len(parts) != 2 {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 ss Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
||||
}
|
||||
if !strings.Contains(parts[0], ":") {
|
||||
// 解码
|
||||
@ -29,13 +29,13 @@ func ParseSS(proxy string) (model.Proxy, error) {
|
||||
}
|
||||
credentials := strings.SplitN(parts[0], ":", 2)
|
||||
if len(credentials) != 2 {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 ss 凭证")
|
||||
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
||||
}
|
||||
// 分割
|
||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
||||
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 ss 服务器和端口")
|
||||
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
||||
}
|
||||
// 转换端口字符串为数字
|
||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
func ParseShadowsocksR(proxy string) (model.Proxy, error) {
|
||||
// 判断是否以 ssr:// 开头
|
||||
if !strings.HasPrefix(proxy, "ssr://") {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 ssr Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid ssr Url")
|
||||
}
|
||||
var err error
|
||||
proxy = strings.TrimPrefix(proxy, "ssr://")
|
||||
|
@ -11,12 +11,12 @@ import (
|
||||
func ParseTrojan(proxy string) (model.Proxy, error) {
|
||||
// 判断是否以 trojan:// 开头
|
||||
if !strings.HasPrefix(proxy, "trojan://") {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 trojan Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid trojan Url")
|
||||
}
|
||||
// 分割
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "trojan://"), "@", 2)
|
||||
if len(parts) != 2 {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 trojan Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid trojan Url")
|
||||
}
|
||||
// 分割
|
||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
||||
@ -27,7 +27,7 @@ func ParseTrojan(proxy string) (model.Proxy, error) {
|
||||
return model.Proxy{}, err
|
||||
}
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 trojan 服务器和端口")
|
||||
return model.Proxy{}, fmt.Errorf("invalid trojan")
|
||||
}
|
||||
// 处理端口
|
||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
||||
|
@ -11,12 +11,12 @@ import (
|
||||
func ParseVless(proxy string) (model.Proxy, error) {
|
||||
// 判断是否以 vless:// 开头
|
||||
if !strings.HasPrefix(proxy, "vless://") {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 vless Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid vless Url")
|
||||
}
|
||||
// 分割
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
|
||||
if len(parts) != 2 {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 vless Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid vless Url")
|
||||
}
|
||||
// 分割
|
||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
||||
@ -27,7 +27,7 @@ func ParseVless(proxy string) (model.Proxy, error) {
|
||||
return model.Proxy{}, err
|
||||
}
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 vless 服务器和端口")
|
||||
return model.Proxy{}, fmt.Errorf("invalid vless")
|
||||
}
|
||||
// 处理端口
|
||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
func ParseVmess(proxy string) (model.Proxy, error) {
|
||||
// 判断是否以 vmess:// 开头
|
||||
if !strings.HasPrefix(proxy, "vmess://") {
|
||||
return model.Proxy{}, fmt.Errorf("无效的 vmess Url")
|
||||
return model.Proxy{}, fmt.Errorf("invalid vmess Url")
|
||||
}
|
||||
// 解码
|
||||
base64, err := DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))
|
||||
|
@ -8,88 +8,104 @@ proxy-groups:
|
||||
- name: 节点选择
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 手动切换
|
||||
type: select
|
||||
proxies:
|
||||
- <all>
|
||||
- name: 电报消息
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: OpenAi
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 油管视频
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 巴哈姆特
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 哔哩哔哩
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 全球直连
|
||||
- name: 国外媒体
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 国内媒体
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 手动切换
|
||||
- name: 谷歌FCM
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- name: 微软云盘
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- name: 微软服务
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- name: 苹果服务
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- name: 游戏平台
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- 手动切换
|
||||
- name: 网易音乐
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- name: 全球直连
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- name: 广告拦截
|
||||
@ -105,6 +121,7 @@ proxy-groups:
|
||||
- name: 漏网之鱼
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 节点选择
|
||||
- DIRECT
|
||||
- 手动切换
|
||||
@ -9579,4 +9596,4 @@ rules:
|
||||
- PROCESS-NAME,Weiyun.exe,全球直连
|
||||
- PROCESS-NAME,baidunetdisk.exe,全球直连
|
||||
- GEOIP,CN,全球直连
|
||||
- MATCH,漏网之鱼
|
||||
- MATCH,漏网之鱼
|
||||
|
@ -8,40 +8,90 @@ proxy-groups:
|
||||
- name: 节点选择
|
||||
type: select
|
||||
proxies:
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 手动切换
|
||||
type: select
|
||||
proxies:
|
||||
- name: 微软服务
|
||||
- <all>
|
||||
- name: 游戏平台(中国)
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 游戏平台
|
||||
- name: 游戏平台(全球)
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 巴哈姆特
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 哔哩哔哩
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 全球直连
|
||||
- name: Telegram
|
||||
type: select
|
||||
proxies:
|
||||
- DIRECT
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: OpenAI
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: Youtube
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: Microsoft
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: Onedrive
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: Apple
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: Netflix
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
- name: 广告拦截
|
||||
type: select
|
||||
proxies:
|
||||
@ -51,17 +101,25 @@ proxy-groups:
|
||||
type: select
|
||||
proxies:
|
||||
- 节点选择
|
||||
- <countries>
|
||||
- 手动切换
|
||||
- DIRECT
|
||||
rules:
|
||||
- GEOSITE,private,全球直连,no-resolve
|
||||
- GEOIP,private,全球直连
|
||||
- GEOSITE,private,DIRECT,no-resolve
|
||||
- GEOIP,private,DIRECT
|
||||
- GEOSITE,category-ads-all,广告拦截
|
||||
- GEOSITE,microsoft,微软服务
|
||||
- GEOSITE,microsoft,Microsoft
|
||||
- GEOSITE,apple,Apple
|
||||
- GEOSITE,netflix,Netflix
|
||||
- GEOSITE,onedrive,Onedrive
|
||||
- GEOSITE,youtube,Youtube
|
||||
- GEOSITE,telegram,Telegram
|
||||
- GEOSITE,openai,OpenAI
|
||||
- GEOSITE,bilibili,哔哩哔哩
|
||||
- GEOSITE,bahamut,巴哈姆特
|
||||
- GEOSITE,category-games,游戏平台
|
||||
- GEOSITE,category-games@cn,游戏平台(中国)
|
||||
- GEOSITE,category-games,游戏平台(全球)
|
||||
- GEOSITE,geolocation-!cn,节点选择
|
||||
- GEOSITE,CN,全球直连
|
||||
- GEOIP,CN,全球直连
|
||||
- MATCH,漏网之鱼
|
||||
- GEOSITE,CN,DIRECT
|
||||
- GEOIP,CN,DIRECT
|
||||
- MATCH,漏网之鱼
|
||||
|
33
utils/check_update.go
Normal file
33
utils/check_update.go
Normal file
@ -0,0 +1,33 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"sub2clash/config"
|
||||
"sub2clash/model"
|
||||
)
|
||||
|
||||
func CheckUpdate() (bool, string, error) {
|
||||
get, err := Get("https://api.github.com/repos/nitezs/sub2clash/tags")
|
||||
if err != nil {
|
||||
return false, "", errors.New("get version info failed" + err.Error())
|
||||
|
||||
}
|
||||
var version model.Tags
|
||||
all, err := io.ReadAll(get.Body)
|
||||
if err != nil {
|
||||
return false, "", errors.New("get version info failed" + err.Error())
|
||||
|
||||
}
|
||||
err = json.Unmarshal(all, &version)
|
||||
if err != nil {
|
||||
return false, "", errors.New("get version info failed" + err.Error())
|
||||
|
||||
}
|
||||
if version[0].Name == config.Version {
|
||||
return false, "", nil
|
||||
} else {
|
||||
return true, version[0].Name, nil
|
||||
}
|
||||
}
|
@ -2,17 +2,29 @@ package database
|
||||
|
||||
import (
|
||||
"github.com/glebarez/sqlite"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"path/filepath"
|
||||
"sub2clash/logger"
|
||||
"sub2clash/model"
|
||||
"sub2clash/utils"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func ConnectDB() error {
|
||||
// 用上面的数据库连接初始化 gorm
|
||||
db, err := gorm.Open(sqlite.Open("sub2clash.db"), &gorm.Config{})
|
||||
err := utils.MKDir("data")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
db, err := gorm.Open(
|
||||
sqlite.Open(filepath.Join("data", "sub2clash.db")), &gorm.Config{
|
||||
Logger: nil,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@ -24,3 +36,23 @@ func ConnectDB() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindShortLinkByUrl(url string, shortLink *model.ShortLink) *gorm.DB {
|
||||
logger.Logger.Debug("find short link by url", zap.String("url", url))
|
||||
return DB.Where("url = ?", url).First(&shortLink)
|
||||
}
|
||||
|
||||
func FindShortLinkByHash(hash string, shortLink *model.ShortLink) *gorm.DB {
|
||||
logger.Logger.Debug("find short link by hash", zap.String("hash", hash))
|
||||
return DB.Where("hash = ?", hash).First(&shortLink)
|
||||
}
|
||||
|
||||
func SaveShortLink(shortLink *model.ShortLink) {
|
||||
logger.Logger.Debug("save short link", zap.String("hash", shortLink.Hash))
|
||||
DB.Save(shortLink)
|
||||
}
|
||||
|
||||
func FirstOrCreateShortLink(shortLink *model.ShortLink) {
|
||||
logger.Logger.Debug("first or create short link", zap.String("hash", shortLink.Hash))
|
||||
DB.FirstOrCreate(shortLink)
|
||||
}
|
||||
|
10
utils/get.go
10
utils/get.go
@ -12,7 +12,15 @@ func Get(url string) (resp *http.Response, err error) {
|
||||
haveTried := 0
|
||||
retryDelay := time.Second // 延迟1秒再重试
|
||||
for haveTried < retryTimes {
|
||||
get, err := http.Get(url)
|
||||
client := &http.Client{}
|
||||
//client.Timeout = time.Second * 10
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
haveTried++
|
||||
time.Sleep(retryDelay)
|
||||
continue
|
||||
}
|
||||
get, err := client.Do(req)
|
||||
if err != nil {
|
||||
haveTried++
|
||||
time.Sleep(retryDelay)
|
||||
|
30
utils/mkdir.go
Normal file
30
utils/mkdir.go
Normal file
@ -0,0 +1,30 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
func MKDir(dir string) error {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MkEssentialDir() error {
|
||||
if err := MKDir("subs"); err != nil {
|
||||
return errors.New("create subs dir failed" + err.Error())
|
||||
}
|
||||
if err := MKDir("templates"); err != nil {
|
||||
return errors.New("create templates dir failed" + err.Error())
|
||||
}
|
||||
if err := MKDir("logs"); err != nil {
|
||||
return errors.New("create logs dir failed" + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
16
utils/os.go
16
utils/os.go
@ -1,16 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func MKDir(dir string) error {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"sub2clash/model"
|
||||
"sub2clash/parser"
|
||||
@ -30,12 +29,9 @@ func GetContryName(proxy model.Proxy) string {
|
||||
|
||||
func AddProxy(
|
||||
sub *model.Subscription, autotest bool,
|
||||
lazy bool, sortStrategy string,
|
||||
clashType model.ClashType, proxies ...model.Proxy,
|
||||
lazy bool, clashType model.ClashType, proxies ...model.Proxy,
|
||||
) {
|
||||
newCountryGroupNames := make([]string, 0)
|
||||
proxyTypes := model.GetSupportProxyTypes(clashType)
|
||||
|
||||
// 添加节点
|
||||
for _, proxy := range proxies {
|
||||
if !proxyTypes[proxy.Type] {
|
||||
@ -82,29 +78,8 @@ func AddProxy(
|
||||
}
|
||||
}
|
||||
sub.ProxyGroups = append(sub.ProxyGroups, newGroup)
|
||||
newCountryGroupNames = append(newCountryGroupNames, countryName)
|
||||
}
|
||||
}
|
||||
// 统计国家策略组数量
|
||||
countryGroupCount := 0
|
||||
for i := range sub.ProxyGroups {
|
||||
if sub.ProxyGroups[i].IsCountryGrop {
|
||||
countryGroupCount++
|
||||
}
|
||||
}
|
||||
// 对国家策略组进行排序
|
||||
switch sortStrategy {
|
||||
case "sizeasc":
|
||||
sort.Sort(model.ProxyGroupsSortBySize(sub.ProxyGroups[:countryGroupCount]))
|
||||
case "sizedesc":
|
||||
sort.Sort(sort.Reverse(model.ProxyGroupsSortBySize(sub.ProxyGroups[:countryGroupCount])))
|
||||
case "nameasc":
|
||||
sort.Sort(model.ProxyGroupsSortByName(sub.ProxyGroups[:countryGroupCount]))
|
||||
case "namedesc":
|
||||
sort.Sort(sort.Reverse(model.ProxyGroupsSortByName(sub.ProxyGroups[:countryGroupCount])))
|
||||
default:
|
||||
sort.Sort(model.ProxyGroupsSortByName(sub.ProxyGroups[:countryGroupCount]))
|
||||
}
|
||||
}
|
||||
|
||||
func ParseProxy(proxies ...string) []model.Proxy {
|
||||
|
12
utils/sub.go
12
utils/sub.go
@ -35,7 +35,9 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
_ = file.Close()
|
||||
if file != nil {
|
||||
_ = file.Close()
|
||||
}
|
||||
}(file)
|
||||
fileLock.RLock()
|
||||
defer fileLock.RUnlock()
|
||||
@ -56,7 +58,9 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
if Body != nil {
|
||||
_ = Body.Close()
|
||||
}
|
||||
}(resp.Body)
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
@ -67,7 +71,9 @@ func FetchSubscriptionFromAPI(url string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
_ = file.Close()
|
||||
if file != nil {
|
||||
_ = file.Close()
|
||||
}
|
||||
}(file)
|
||||
fileLock.Lock()
|
||||
defer fileLock.Unlock()
|
||||
|
@ -17,7 +17,9 @@ func LoadTemplate(template string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
_ = file.Close()
|
||||
if file != nil {
|
||||
_ = file.Close()
|
||||
}
|
||||
}(file)
|
||||
result, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
|
39
utils/write_default_template.go
Normal file
39
utils/write_default_template.go
Normal file
@ -0,0 +1,39 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sub2clash/config"
|
||||
)
|
||||
|
||||
func writeTemplate(path string, template string) error {
|
||||
tPath := filepath.Join(
|
||||
"templates", path,
|
||||
)
|
||||
if _, err := os.Stat(tPath); os.IsNotExist(err) {
|
||||
file, err := os.Create(tPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
if file != nil {
|
||||
_ = file.Close()
|
||||
}
|
||||
}(file)
|
||||
_, err = file.WriteString(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteDefalutTemplate(templateMeta string, templateClash string) error {
|
||||
if err := writeTemplate(config.Default.MetaTemplate, templateMeta); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeTemplate(config.Default.ClashTemplate, templateClash); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
package validator
|
||||
|
||||
type ShortLinkGenValidator struct {
|
||||
Url string `form:"url" binding:"required"`
|
||||
Url string `form:"url" binding:"required"`
|
||||
Password string `form:"password"`
|
||||
}
|
||||
|
||||
type ShortLinkGetValidator struct {
|
||||
Hash string `form:"hash" binding:"required"`
|
||||
Hash string `form:"hash" binding:"required"`
|
||||
Password string `form:"password"`
|
||||
}
|
||||
|
@ -25,6 +25,10 @@ type SubValidator struct {
|
||||
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:""`
|
||||
}
|
||||
|
||||
type RuleProviderStruct struct {
|
||||
@ -135,5 +139,17 @@ func ParseQuery(c *gin.Context) (SubValidator, error) {
|
||||
} 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 SubValidator{}, errors.New("参数错误: replace 格式错误")
|
||||
}
|
||||
query.ReplaceKeys = append(query.ReplaceKeys, replaces[i][1])
|
||||
query.ReplaceTo = append(query.ReplaceTo, replaces[i][2])
|
||||
}
|
||||
}
|
||||
return query, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user