1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 15:14:43 -05:00
fix: 修复当订阅链接有多个 clash 配置时丢失节点的问题
update: 增加检测更新
modify: 修改数据库路径
modify: 修改短链生成逻辑
modify: 统一输出信息
This commit is contained in:
Nite07 2023-09-21 09:08:02 +08:00 committed by GitHub
parent f166c6a54a
commit 8d06ab3175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 588 additions and 349 deletions

View File

@ -47,6 +47,9 @@ jobs:
with: with:
context: . context: .
file: ./Dockerfile file: ./Dockerfile
build-args: |
dev=true
version=${{ github.sha }}
push: true push: true
tags: ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }} tags: ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }}
@ -56,6 +59,9 @@ jobs:
with: with:
context: . context: .
file: ./Dockerfile file: ./Dockerfile
build-args: |
dev=false
version=${{ steps.set_tag.outputs.tag }}
push: true push: true
tags: | tags: |
ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }} ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }}

View File

@ -23,12 +23,10 @@ jobs:
- name: Build - name: Build
run: | run: |
LDFLAGS="-s -w" LDFLAGS="-s -w -X config.Version=${{ github.ref_name }}"
# Linux # 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=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 CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="$LDFLAGS" -o output/sub2clash-linux-arm64 main.go
# Darwin # Darwin
@ -36,9 +34,7 @@ jobs:
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="$LDFLAGS" -o output/sub2clash-darwin-arm64 main.go CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="$LDFLAGS" -o output/sub2clash-darwin-arm64 main.go
# Windows # 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=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 CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -ldflags="$LDFLAGS" -o output/sub2clash-windows-arm64.exe main.go
- name: Create Release - name: Create Release
@ -52,16 +48,6 @@ jobs:
draft: false draft: false
prerelease: 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) - name: Upload Release Asset (Linux amd64)
uses: actions/upload-release-asset@v1 uses: actions/upload-release-asset@v1
env: env:
@ -72,16 +58,6 @@ jobs:
asset_name: sub2clash-linux-amd64 asset_name: sub2clash-linux-amd64
asset_content_type: application/octet-stream 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) - name: Upload Release Asset (Linux arm64)
uses: actions/upload-release-asset@v1 uses: actions/upload-release-asset@v1
env: env:
@ -112,16 +88,6 @@ jobs:
asset_name: sub2clash-darwin-arm64 asset_name: sub2clash-darwin-arm64
asset_content_type: application/octet-stream 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) - name: Upload Release Asset (Windows amd64)
uses: actions/upload-release-asset@v1 uses: actions/upload-release-asset@v1
env: env:
@ -132,16 +98,6 @@ jobs:
asset_name: sub2clash-windows-amd64.exe asset_name: sub2clash-windows-amd64.exe
asset_content_type: application/octet-stream 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) - name: Upload Release Asset (Windows arm64)
uses: actions/upload-release-asset@v1 uses: actions/upload-release-asset@v1
env: env:

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ dist
subs subs
test test
logs logs
sub2clash.db sub2clash.db
.env

View File

@ -10,10 +10,8 @@ builds:
- darwin - darwin
goarch: goarch:
- amd64 - amd64
- arm
- arm64 - arm64
- 386
ldflags: ldflags:
- -s -w - -s -w -X sub2clash/config.Version={{ .Version }}
no_unique_dist_dir: true no_unique_dist_dir: true
binary: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}" binary: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"

View File

@ -9,8 +9,12 @@ WORKDIR /app
COPY . . COPY . .
RUN go mod download RUN go mod download
# 获取参数
ARG version
ARG dev
# 使用 -ldflags 参数进行编译 # 使用 -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 FROM alpine:latest

View File

@ -35,6 +35,7 @@
| REQUEST_MAX_FILE_SIZE | Get 请求订阅文件最大大小byte | `1048576` | | REQUEST_MAX_FILE_SIZE | Get 请求订阅文件最大大小byte | `1048576` |
| CACHE_EXPIRE | 订阅缓存时间(秒) | `300` | | CACHE_EXPIRE | 订阅缓存时间(秒) | `300` |
| LOG_LEVEL | 日志等级,可选值 `debug`,`info`,`warn`,`error` | `info` | | LOG_LEVEL | 日志等级,可选值 `debug`,`info`,`warn`,`error` | `info` |
| SHORT_LINK_LENGTH | 短链长度 | `6` |
### API ### API

View File

@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"net/url" "net/url"
"regexp" "regexp"
"sort"
"strings" "strings"
"sub2clash/model" "sub2clash/model"
"sub2clash/parser" "sub2clash/parser"
@ -43,6 +44,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
if err != nil { if err != nil {
return nil, errors.New("解析模板失败: " + err.Error()) return nil, errors.New("解析模板失败: " + err.Error())
} }
var proxyList []model.Proxy
// 加载订阅 // 加载订阅
for i := range query.Subs { for i := range query.Subs {
data, err := utils.LoadSubscription(query.Subs[i], query.Refresh) data, err := utils.LoadSubscription(query.Subs[i], query.Refresh)
@ -50,31 +52,49 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
return nil, errors.New("加载订阅失败: " + err.Error()) return nil, errors.New("加载订阅失败: " + err.Error())
} }
// 解析订阅 // 解析订阅
var proxyList []model.Proxy
err = yaml.Unmarshal(data, &sub) err = yaml.Unmarshal(data, &sub)
if err != nil { if err != nil {
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|http|https)://") reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|http|https)://")
if reg.Match(data) { 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 { } else {
// 如果无法直接解析尝试Base64解码 // 如果无法直接解析尝试Base64解码
base64, err := parser.DecodeBase64(string(data)) base64, err := parser.DecodeBase64(string(data))
if err != nil { if err != nil {
return nil, errors.New("加载订阅失败: " + err.Error()) 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 { } else {
proxyList = sub.Proxies proxyList = append(proxyList, sub.Proxies...)
} }
utils.AddProxy(sub, query.AutoTest, query.Lazy, query.Sort, clashType, proxyList...)
} }
// 将新增节点都添加到临时变量 t 中,防止策略组排序错乱
var t = &model.Subscription{}
utils.AddProxy(t, query.AutoTest, query.Lazy, clashType, proxyList...)
// 处理自定义代理 // 处理自定义代理
utils.AddProxy( utils.AddProxy(
sub, query.AutoTest, query.Lazy, query.Sort, clashType, t, query.AutoTest, query.Lazy, clashType,
utils.ParseProxy(query.Proxies...)..., utils.ParseProxy(query.Proxies...)...,
) )
MergeSubAndTemplate(temp, sub) // 排序策略组
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 { for _, v := range query.Rules {
if v.Prepend { if v.Prepend {

View File

@ -1,12 +1,14 @@
package controller package controller
import ( import (
"crypto/sha256" "errors"
"encoding/hex"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm"
"net/http" "net/http"
"strings"
"sub2clash/config" "sub2clash/config"
"sub2clash/model" "sub2clash/model"
"sub2clash/utils"
"sub2clash/utils/database" "sub2clash/utils/database"
"sub2clash/validator" "sub2clash/validator"
"time" "time"
@ -18,12 +20,32 @@ func ShortLinkGenHandler(c *gin.Context) {
if err := c.ShouldBind(&params); err != nil { if err := c.ShouldBind(&params); err != nil {
c.String(400, "参数错误: "+err.Error()) c.String(400, "参数错误: "+err.Error())
} }
// 生成短链接 if strings.TrimSpace(params.Url) == "" {
//hash := utils.RandomString(6) c.String(400, "参数错误")
shortLink := sha256.Sum224([]byte(params.Url)) return
hash := hex.EncodeToString(shortLink[:]) }
// 生成hash
hash := utils.RandomString(config.Default.ShortLinkLength)
// 存入数据库 // 存入数据库
database.DB.FirstOrCreate( var item model.ShortLink
result := database.FindShortLinkByUrl(params.Url, &item)
if result.Error == nil {
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{ &model.ShortLink{
Hash: hash, Hash: hash,
Url: params.Url, Url: params.Url,
@ -37,17 +59,21 @@ func ShortLinkGenHandler(c *gin.Context) {
func ShortLinkGetHandler(c *gin.Context) { func ShortLinkGetHandler(c *gin.Context) {
// 获取动态路由 // 获取动态路由
hash := c.Param("hash") hash := c.Param("hash")
if strings.TrimSpace(hash) == "" {
c.String(400, "参数错误")
return
}
// 查询数据库 // 查询数据库
var shortLink model.ShortLink var shortLink model.ShortLink
result := database.DB.Where("hash = ?", hash).First(&shortLink) result := database.FindShortLinkByHash(hash, &shortLink)
// 更新最后访问时间
shortLink.LastRequestTime = time.Now().Unix()
database.DB.Save(&shortLink)
// 重定向 // 重定向
if result.Error != nil { if result.Error != nil {
c.String(404, "未找到短链接") c.String(404, "未找到短链接")
return return
} }
// 更新最后访问时间
shortLink.LastRequestTime = time.Now().Unix()
database.SaveShortLink(&shortLink)
uri := config.Default.BasePath + shortLink.Url uri := config.Default.BasePath + shortLink.Url
c.Redirect(http.StatusTemporaryRedirect, uri) c.Redirect(http.StatusTemporaryRedirect, uri)
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"html/template" "html/template"
"sub2clash/api/controller" "sub2clash/api/controller"
"sub2clash/config"
"sub2clash/middleware" "sub2clash/middleware"
) )
@ -17,7 +18,11 @@ func SetRoute(r *gin.Engine) {
r.SetHTMLTemplate(template.Must(template.New("").ParseFS(templates, "templates/*"))) r.SetHTMLTemplate(template.Must(template.New("").ParseFS(templates, "templates/*")))
r.GET( r.GET(
"/", func(c *gin.Context) { "/", func(c *gin.Context) {
c.HTML(200, "index.html", nil) c.HTML(
200, "index.html", gin.H{
"Version": config.Version,
},
)
}, },
) )
r.GET( r.GET(

View File

@ -1,163 +1,240 @@
<!DOCTYPE html> <!doctype html>
<html lang="zh-CN"> <html lang="zh-CN">
<head>
<head> <meta charset="UTF-8" />
<meta charset="UTF-8"> <meta content="width=device-width, initial-scale=1.0" name="viewport" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>sub2clash</title> <title>sub2clash</title>
<!-- Bootstrap CSS --> <!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" <link
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"> 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 --> <!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" <script
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"
crossorigin="anonymous"></script> integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
></script>
<!-- Axios --> <!-- Axios -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<style> <style>
.container { .container {
max-width: 800px; max-width: 800px;
} }
.btn-xs { .btn-xs {
padding: 2px 2px; /* 调整内边距以减小按钮大小 */ padding: 2px 2px; /* 调整内边距以减小按钮大小 */
font-size: 10px; /* 设置字体大小 */ font-size: 10px; /* 设置字体大小 */
line-height: 1.2; /* 调整行高 */ line-height: 1.2; /* 调整行高 */
border-radius: 3px; /* 可选的边框半径调整 */ border-radius: 3px; /* 可选的边框半径调整 */
height: 25px; height: 25px;
width: 25px; width: 25px;
} }
</style> </style>
</head> </head>
<body class="bg-light"> <body class="bg-light">
<div class="container mt-5">
<div class="container mt-5"> <div class="mb-4">
<div class="mb-4">
<h2>sub2clash</h2> <h2>sub2clash</h2>
<span class="text-muted fst-italic">通用订阅链接转 Clash(Meta) 配置工具 <a <span class="text-muted fst-italic"
href="https://github.com/nitezs/sub2clash#clash-meta" target="_blank">使用文档</a></span> >通用订阅链接转 Clash(Meta) 配置工具
</div> <a
href="https://github.com/nitezs/sub2clash#clash-meta"
<form id="apiForm"> target="_blank"
>使用文档</a
></span
>
</div>
<form id="apiForm">
<!-- API Endpoint --> <!-- API Endpoint -->
<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="clash">Clash</option>
<option value="meta">Clash.Meta</option> <option value="meta">Clash.Meta</option>
</select> </select>
</div> </div>
<!-- Subscription Link --> <!-- Subscription Link -->
<div class="form-group mb-3"> <div class="form-group mb-3">
<label for="sub">订阅链接:</label> <label for="sub">订阅链接:</label>
<textarea class="form-control" id="sub" name="sub" rows="5" placeholder="每行输入一个订阅链接"></textarea> <textarea
class="form-control"
id="sub"
name="sub"
placeholder="每行输入一个订阅链接"
rows="5"
></textarea>
</div> </div>
<!-- Proxy Link --> <!-- Proxy Link -->
<div class="form-group mb-3"> <div class="form-group mb-3">
<label for="proxy">节点分享链接:</label> <label for="proxy">节点分享链接:</label>
<textarea class="form-control" id="proxy" name="proxy" rows="5" <textarea
placeholder="每行输入一个节点分享链接"></textarea> class="form-control"
id="proxy"
name="proxy"
placeholder="每行输入一个节点分享链接"
rows="5"
></textarea>
</div> </div>
<!-- Refresh --> <!-- Refresh -->
<div class="form-check mb-3"> <div class="form-check mb-3">
<input type="checkbox" class="form-check-input" id="refresh" name="refresh"> <input
<label class="form-check-label" for="refresh">强制刷新配置</label> class="form-check-input"
id="refresh"
name="refresh"
type="checkbox"
/>
<label class="form-check-label" for="refresh">强制刷新配置</label>
</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 type="text" class="form-control" id="template" name="template" <input
placeholder="输入外部模板链接或内部模板名称"> class="form-control"
id="template"
name="template"
placeholder="输入外部模板链接或内部模板名称"
type="text"
/>
</div> </div>
<!-- Rule Provider --> <!-- Rule Provider -->
<div class="form-group mb-3" id="ruleProviderGroup"> <div class="form-group mb-3" id="ruleProviderGroup">
<label>Rule Provider:</label> <label>Rule Provider:</label>
<button type="button" class="btn btn-primary mb-1 btn-xs" onclick="addRuleProvider()">+</button> <button
class="btn btn-primary mb-1 btn-xs"
onclick="addRuleProvider()"
type="button"
>
+
</button>
</div> </div>
<!-- Rule --> <!-- Rule -->
<div class="form-group mb-3" id="ruleGroup"> <div class="form-group mb-3" id="ruleGroup">
<label>规则:</label> <label>规则:</label>
<button type="button" class="btn btn-primary mb-1 btn-xs" onclick="addRule()">+</button> <button
class="btn btn-primary mb-1 btn-xs"
onclick="addRule()"
type="button"
>
+
</button>
</div> </div>
<!-- Auto Test --> <!-- Auto Test -->
<div class="form-check mb-3"> <div class="form-check mb-3">
<input type="checkbox" class="form-check-input" id="autoTest" name="autoTest"> <input
<label class="form-check-label" for="autoTest">指定国家策略组是否自动测速</label> class="form-check-input"
id="autoTest"
name="autoTest"
type="checkbox"
/>
<label class="form-check-label" for="autoTest"
>国家策略组自动测速</label
>
</div> </div>
<!-- Lazy --> <!-- Lazy -->
<div class="form-check mb-3"> <div class="form-check mb-3">
<input type="checkbox" class="form-check-input" id="lazy" name="lazy"> <input
<label class="form-check-label" for="lazy">自动测速是否启用 lazy</label> class="form-check-input"
id="lazy"
name="lazy"
type="checkbox"
/>
<label class="form-check-label" for="lazy">自动测速启用 lazy</label>
</div> </div>
<!-- Sort --> <!-- Sort -->
<div class="form-group mb-3"> <div class="form-group mb-3">
<label for="sort">国家策略组排序策略:</label> <label for="sort">国家策略组排序策略:</label>
<select class="form-control" id="sort" name="sort"> <select class="form-control" id="sort" name="sort">
<option value="nameasc">名称(升序)</option> <option value="nameasc">名称(升序)</option>
<option value="namedesc">名称(降序)</option> <option value="namedesc">名称(降序)</option>
<option value="sizeasc">节点数量(升序)</option> <option value="sizeasc">节点数量(升序)</option>
<option value="sizedesc">节点数量(降序)</option> <option value="sizedesc">节点数量(降序)</option>
</select> </select>
</div> </div>
</form> </form>
<!-- Display the API Link --> <!-- Display the API Link -->
<div class="form-group mb-5"> <div class="form-group mb-5">
<label for="apiLink">配置链接:</label> <label for="apiLink">配置链接:</label>
<div class="input-group mb-2"> <div class="input-group mb-2">
<input type="text" class="form-control" id="apiLink" readonly> <input class="form-control" id="apiLink" readonly type="text" />
<button class="btn btn-primary" type="button" onclick="generateURL()">生成链接</button> <button class="btn btn-primary" onclick="generateURL()" type="button">
<button class="btn btn-primary" type="button" onclick="copyToClipboard('apiLink',this)">复制链接</button> 生成链接
</button>
<button
class="btn btn-primary"
onclick="copyToClipboard('apiLink',this)"
type="button"
>
复制链接
</button>
</div> </div>
<div class="input-group"> <div class="input-group">
<input type="text" class="form-control" id="apiShortLink" readonly> <input class="form-control" id="apiShortLink" readonly type="text" />
<button class="btn btn-primary" type="button" onclick="generateShortLink()">生成短链</button> <button
<button class="btn btn-primary" type="button" onclick="copyToClipboard('apiShortLink',this)">复制短链 class="btn btn-primary"
</button> onclick="generateShortLink()"
type="button"
>
生成短链
</button>
<button
class="btn btn-primary"
onclick="copyToClipboard('apiShortLink',this)"
type="button"
>
复制短链
</button>
</div> </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> </div>
<!-- footer--> <script>
<footer> async function copyToClipboard(elem, e) {
<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) {
const apiLinkInput = document.querySelector(`#${elem}`).value; const apiLinkInput = document.querySelector(`#${elem}`).value;
try { try {
await navigator.clipboard.writeText(apiLinkInput); await navigator.clipboard.writeText(apiLinkInput);
let text = e.textContent; let text = e.textContent;
e.addEventListener("mouseout", function () { e.addEventListener("mouseout", function () {
e.textContent = text; e.textContent = text;
}); });
e.textContent = "复制成功"; e.textContent = "复制成功";
} catch (err) { } catch (err) {
console.error('复制到剪贴板失败:', err); console.error("复制到剪贴板失败:", err);
} }
} }
function createRuleProvider() { function createRuleProvider() {
const div = document.createElement('div'); const div = document.createElement("div");
div.classList.add('input-group', 'mb-2'); div.classList.add("input-group", "mb-2");
div.innerHTML = ` div.innerHTML = `
<input type="text" class="form-control" name="ruleProvider" placeholder="Behavior"> <input type="text" class="form-control" name="ruleProvider" placeholder="Behavior">
<input type="text" class="form-control" name="ruleProvider" placeholder="Url"> <input type="text" class="form-control" name="ruleProvider" placeholder="Url">
@ -167,11 +244,11 @@
<button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button> <button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button>
`; `;
return div; return div;
} }
function createRule() { function createRule() {
const div = document.createElement('div'); const div = document.createElement("div");
div.classList.add('input-group', 'mb-2'); div.classList.add("input-group", "mb-2");
div.innerHTML = ` div.innerHTML = `
<input type="text" class="form-control" name="rule" placeholder="Rule"> <input type="text" class="form-control" name="rule" placeholder="Rule">
<input type="text" class="form-control" name="rule" placeholder="Prepend"> <input type="text" class="form-control" name="rule" placeholder="Prepend">
@ -179,73 +256,79 @@
<button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button> <button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button>
`; `;
return div; return div;
} }
function addRuleProvider() { function addRuleProvider() {
const div = createRuleProvider(); const div = createRuleProvider();
document.getElementById('ruleProviderGroup').appendChild(div); document.getElementById("ruleProviderGroup").appendChild(div);
} }
function addRule() { function addRule() {
const div = createRule(); const div = createRule();
document.getElementById('ruleGroup').appendChild(div); document.getElementById("ruleGroup").appendChild(div);
} }
function removeElement(button) { function removeElement(button) {
button.parentElement.remove(); button.parentElement.remove();
} }
function generateURI() { function generateURI() {
const queryParams = []; const queryParams = [];
// 获取 API Endpoint // 获取 API Endpoint
const endpoint = document.getElementById("endpoint").value; const endpoint = document.getElementById("endpoint").value;
// 获取并组合订阅链接 // 获取并组合订阅链接
let subLines = document.getElementById("sub").value.split('\n').filter(line => line.trim() !== ""); let subLines = document
let noSub = false .getElementById("sub")
.value.split("\n")
.filter((line) => line.trim() !== "");
let noSub = false;
// 去除 subLines 中空元素 // 去除 subLines 中空元素
subLines = subLines.map((item) => { subLines = subLines.map((item) => {
if (item !== "") { if (item !== "") {
return item; return item;
} }
}); });
if (subLines.length > 0) { if (subLines.length > 0) {
queryParams.push(`sub=${encodeURIComponent(subLines.join(','))}`); queryParams.push(`sub=${encodeURIComponent(subLines.join(","))}`);
} else { } else {
noSub = true noSub = true;
} }
// 获取并组合节点分享链接 // 获取并组合节点分享链接
let proxyLines = document.getElementById("proxy").value.split('\n').filter(line => line.trim() !== ""); let proxyLines = document
let noProxy = false .getElementById("proxy")
.value.split("\n")
.filter((line) => line.trim() !== "");
let noProxy = false;
// 去除 proxyLines 中空元素 // 去除 proxyLines 中空元素
proxyLines = proxyLines.map((item) => { proxyLines = proxyLines.map((item) => {
if (item !== "") { if (item !== "") {
return item; return item;
} }
}); });
if (proxyLines.length > 0) { if (proxyLines.length > 0) {
queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(','))}`); queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(","))}`);
} else { } else {
noProxy = true noProxy = true;
} }
if (noSub && noProxy) { if (noSub && noProxy) {
alert("订阅链接和节点分享链接不能同时为空!") alert("订阅链接和节点分享链接不能同时为空!");
return return "";
} }
// 获取复选框的值 // 获取复选框的值
const refresh = document.getElementById("refresh").checked; const refresh = document.getElementById("refresh").checked;
queryParams.push(`refresh=${refresh ? 'true' : 'false'}`); queryParams.push(`refresh=${refresh ? "true" : "false"}`);
const autoTest = document.getElementById("autoTest").checked; const autoTest = document.getElementById("autoTest").checked;
queryParams.push(`autoTest=${autoTest ? 'true' : 'false'}`); queryParams.push(`autoTest=${autoTest ? "true" : "false"}`);
const lazy = document.getElementById("lazy").checked; const lazy = document.getElementById("lazy").checked;
queryParams.push(`lazy=${lazy ? 'true' : 'false'}`); queryParams.push(`lazy=${lazy ? "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)}`); queryParams.push(`template=${encodeURIComponent(template)}`);
} }
// 获取Rule Provider和规则 // 获取Rule Provider和规则
@ -253,65 +336,90 @@
const rules = document.getElementsByName("rule"); const rules = document.getElementsByName("rule");
let providers = []; let providers = [];
for (let i = 0; i < ruleProviders.length / 5; i++) { for (let i = 0; i < ruleProviders.length / 5; i++) {
let baseIndex = i * 5; let baseIndex = i * 5;
let behavior = ruleProviders[baseIndex].value; let behavior = ruleProviders[baseIndex].value;
let url = ruleProviders[baseIndex + 1].value; let url = ruleProviders[baseIndex + 1].value;
let group = ruleProviders[baseIndex + 2].value; let group = ruleProviders[baseIndex + 2].value;
let prepend = ruleProviders[baseIndex + 3].value; let prepend = ruleProviders[baseIndex + 3].value;
let name = ruleProviders[baseIndex + 4].value; let name = ruleProviders[baseIndex + 4].value;
// 是否存在空值 // 是否存在空值
if (behavior.trim() === "" || url.trim() === "" || group.trim() === "" || prepend.trim() === "" || name.trim() === "") { if (
alert("Rule Provider 中存在空值,请检查后重试!"); behavior.trim() === "" ||
return; url.trim() === "" ||
} group.trim() === "" ||
providers.push(`[${behavior},${url},${group},${prepend},${name}]`); 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 = []; let ruleList = [];
for (let i = 0; i < rules.length / 3; i++) { for (let i = 0; i < rules.length / 3; i++) {
if (rules[i * 3].value.trim() !== "") { if (rules[i * 3].value.trim() !== "") {
let rule = rules[i * 3].value; let rule = rules[i * 3].value;
let prepend = rules[i * 3 + 1].value; let prepend = rules[i * 3 + 1].value;
let group = rules[i * 3 + 2].value; let group = rules[i * 3 + 2].value;
// 是否存在空值 // 是否存在空值
if (rule.trim() === "" || prepend.trim() === "" || group.trim() === "") { if (
alert("Rule 中存在空值,请检查后重试!"); rule.trim() === "" ||
return; prepend.trim() === "" ||
} group.trim() === ""
ruleList.push(`[${rule},${prepend},${group}]`); ) {
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; const sort = document.getElementById("sort").value;
queryParams.push(`sort=${sort}`); queryParams.push(`sort=${sort}`);
return `${endpoint}?${queryParams.join('&')}`; return `${endpoint}?${queryParams.join("&")}`;
} }
function generateURL() { function generateURL() {
const apiLink = document.getElementById("apiLink"); 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"); const apiShortLink = document.getElementById("apiShortLink");
let uri = generateURI();
axios.post("./short", { if (uri === "") {
"url": generateURI() return;
}, { }
headers: { axios
"Content-Type": "application/json" .post(
} "./short",
}).then((response) => { {
url: uri,
},
{
headers: {
"Content-Type": "application/json",
},
},
)
.then((response) => {
apiShortLink.value = `${window.location.origin}${window.location.pathname}s/${response.data}`; apiShortLink.value = `${window.location.origin}${window.location.pathname}s/${response.data}`;
}).catch((error) => { })
.catch((error) => {
console.log(error); console.log(error);
alert("生成短链失败,请重试!"); alert("生成短链失败,请重试!");
}) });
} }
</script> </script>
</body>
</body>
</html> </html>

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"errors"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"os" "os"
"strconv" "strconv"
@ -15,11 +16,14 @@ type Config struct {
CacheExpire int64 CacheExpire int64
LogLevel string LogLevel string
BasePath string BasePath string
ShortLinkLength int
} }
var Default *Config var Default *Config
var Version string
var Dev string
func init() { func LoadConfig() error {
Default = &Config{ Default = &Config{
MetaTemplate: "template_meta.yaml", MetaTemplate: "template_meta.yaml",
ClashTemplate: "template_clash.yaml", ClashTemplate: "template_clash.yaml",
@ -29,12 +33,13 @@ func init() {
CacheExpire: 60 * 5, CacheExpire: 60 * 5,
LogLevel: "info", LogLevel: "info",
BasePath: "/", BasePath: "/",
ShortLinkLength: 6,
} }
_ = godotenv.Load() _ = godotenv.Load()
if os.Getenv("PORT") != "" { if os.Getenv("PORT") != "" {
atoi, err := strconv.Atoi(os.Getenv("PORT")) atoi, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil { if err != nil {
panic("PORT invalid") return errors.New("PORT invalid")
} }
Default.Port = atoi Default.Port = atoi
} }
@ -47,21 +52,21 @@ func init() {
if os.Getenv("REQUEST_RETRY_TIMES") != "" { if os.Getenv("REQUEST_RETRY_TIMES") != "" {
atoi, err := strconv.Atoi(os.Getenv("REQUEST_RETRY_TIMES")) atoi, err := strconv.Atoi(os.Getenv("REQUEST_RETRY_TIMES"))
if err != nil { if err != nil {
panic("REQUEST_RETRY_TIMES invalid") return errors.New("REQUEST_RETRY_TIMES invalid")
} }
Default.RequestRetryTimes = atoi Default.RequestRetryTimes = atoi
} }
if os.Getenv("REQUEST_MAX_FILE_SIZE") != "" { if os.Getenv("REQUEST_MAX_FILE_SIZE") != "" {
atoi, err := strconv.Atoi(os.Getenv("REQUEST_MAX_FILE_SIZE")) atoi, err := strconv.Atoi(os.Getenv("REQUEST_MAX_FILE_SIZE"))
if err != nil { if err != nil {
panic("REQUEST_MAX_FILE_SIZE invalid") return errors.New("REQUEST_MAX_FILE_SIZE invalid")
} }
Default.RequestMaxFileSize = int64(atoi) Default.RequestMaxFileSize = int64(atoi)
} }
if os.Getenv("CACHE_EXPIRE") != "" { if os.Getenv("CACHE_EXPIRE") != "" {
atoi, err := strconv.Atoi(os.Getenv("CACHE_EXPIRE")) atoi, err := strconv.Atoi(os.Getenv("CACHE_EXPIRE"))
if err != nil { if err != nil {
panic("CACHE_EXPIRE invalid") return errors.New("CACHE_EXPIRE invalid")
} }
Default.CacheExpire = int64(atoi) Default.CacheExpire = int64(atoi)
} }
@ -74,4 +79,12 @@ func init() {
Default.BasePath += "/" 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
} }

View File

@ -9,7 +9,8 @@ services:
- "8011:8011" - "8011:8011"
volumes: volumes:
- ./logs:/app/logs - ./logs:/app/logs
# - ./templates:/app/templates - ./templates:/app/templates
- ./data:/app/data
# environment: # environment:
# - PORT=8011 # - PORT=8011
# - META_TEMPLATE=template_meta.yaml # - META_TEMPLATE=template_meta.yaml

View File

@ -4,18 +4,18 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"path/filepath" "path/filepath"
"sub2clash/config"
"sub2clash/utils"
"sync" "sync"
"time" "time"
) )
var ( var (
Logger *zap.Logger Logger *zap.Logger
lock sync.Mutex lock sync.Mutex
logLevel string
) )
func init() { func InitLogger(level string) {
logLevel = level
buildLogger() buildLogger()
go rotateLogs() go rotateLogs()
} }
@ -24,7 +24,7 @@ func buildLogger() {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
var level zapcore.Level var level zapcore.Level
switch config.Default.LogLevel { switch logLevel {
case "error": case "error":
level = zap.ErrorLevel level = zap.ErrorLevel
case "debug": case "debug":
@ -36,10 +36,6 @@ func buildLogger() {
default: default:
level = zap.InfoLevel level = zap.InfoLevel
} }
err := utils.MKDir("logs")
if err != nil {
panic("创建日志失败" + err.Error())
}
zapConfig := zap.NewProductionConfig() zapConfig := zap.NewProductionConfig()
zapConfig.Encoding = "console" zapConfig.Encoding = "console"
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
@ -47,9 +43,10 @@ func buildLogger() {
zapConfig.OutputPaths = []string{"stdout", getLogFileName("info")} zapConfig.OutputPaths = []string{"stdout", getLogFileName("info")}
zapConfig.ErrorOutputPaths = []string{"stderr", getLogFileName("error")} zapConfig.ErrorOutputPaths = []string{"stderr", getLogFileName("error")}
zapConfig.Level = zap.NewAtomicLevelAt(level) zapConfig.Level = zap.NewAtomicLevelAt(level)
var err error
Logger, err = zapConfig.Build() Logger, err = zapConfig.Build()
if err != nil { if err != nil {
panic("创建日志失败" + err.Error()) panic("log failed" + err.Error())
} }
} }

75
main.go
View File

@ -5,8 +5,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap" "go.uber.org/zap"
"io" "io"
"os"
"path/filepath"
"strconv" "strconv"
"sub2clash/api" "sub2clash/api"
"sub2clash/config" "sub2clash/config"
@ -21,43 +19,44 @@ var templateMeta string
//go:embed templates/template_clash.yaml //go:embed templates/template_clash.yaml
var templateClash string 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() { func init() {
if err := utils.MKDir("subs"); err != nil { // 加载配置
os.Exit(1) err := config.LoadConfig()
} // 初始化日志
if err := utils.MKDir("templates"); err != nil { logger.InitLogger(config.Default.LogLevel)
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()
if err != nil { if err != nil {
panic(err) 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.MkEssentialDir()
if err != nil {
logger.Logger.Panic("create essential dir failed", zap.Error(err))
}
// 写入默认模板
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() { func main() {
@ -69,10 +68,10 @@ func main() {
r := gin.Default() r := gin.Default()
// 设置路由 // 设置路由
api.SetRoute(r) 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)) err := r.Run(":" + strconv.Itoa(config.Default.Port))
if err != nil { if err != nil {
logger.Logger.Error("Server run error", zap.Error(err)) logger.Logger.Error("server running failed", zap.Error(err))
return return
} }
} }

12
model/github.go Normal file
View 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"`
}

View File

@ -39,7 +39,6 @@ func (p ProxyGroupsSortByName) Less(i, j int) bool {
bestMatch, _, _ := matcher.Match(language.Make("zh")) bestMatch, _, _ := matcher.Match(language.Make("zh"))
// 使用最佳匹配的语言进行排序 // 使用最佳匹配的语言进行排序
c := collate.New(bestMatch) c := collate.New(bestMatch)
return c.CompareString(p[i].Name, p[j].Name) < 0 return c.CompareString(p[i].Name, p[j].Name) < 0
} }

View File

@ -12,12 +12,12 @@ import (
func ParseSS(proxy string) (model.Proxy, error) { func ParseSS(proxy string) (model.Proxy, error) {
// 判断是否以 ss:// 开头 // 判断是否以 ss:// 开头
if !strings.HasPrefix(proxy, "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) parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
if len(parts) != 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], ":") { if !strings.Contains(parts[0], ":") {
// 解码 // 解码
@ -29,13 +29,13 @@ func ParseSS(proxy string) (model.Proxy, error) {
} }
credentials := strings.SplitN(parts[0], ":", 2) credentials := strings.SplitN(parts[0], ":", 2)
if len(credentials) != 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) serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPort := strings.SplitN(serverInfo[0], ":", 2) serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
if len(serverAndPort) != 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])) port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))

View File

@ -11,7 +11,7 @@ import (
func ParseShadowsocksR(proxy string) (model.Proxy, error) { func ParseShadowsocksR(proxy string) (model.Proxy, error) {
// 判断是否以 ssr:// 开头 // 判断是否以 ssr:// 开头
if !strings.HasPrefix(proxy, "ssr://") { if !strings.HasPrefix(proxy, "ssr://") {
return model.Proxy{}, fmt.Errorf("无效的 ssr Url") return model.Proxy{}, fmt.Errorf("invalid ssr Url")
} }
var err error var err error
proxy = strings.TrimPrefix(proxy, "ssr://") proxy = strings.TrimPrefix(proxy, "ssr://")

View File

@ -11,12 +11,12 @@ import (
func ParseTrojan(proxy string) (model.Proxy, error) { func ParseTrojan(proxy string) (model.Proxy, error) {
// 判断是否以 trojan:// 开头 // 判断是否以 trojan:// 开头
if !strings.HasPrefix(proxy, "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) parts := strings.SplitN(strings.TrimPrefix(proxy, "trojan://"), "@", 2)
if len(parts) != 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) serverInfo := strings.SplitN(parts[1], "#", 2)
@ -27,7 +27,7 @@ func ParseTrojan(proxy string) (model.Proxy, error) {
return model.Proxy{}, err return model.Proxy{}, err
} }
if len(serverAndPort) != 2 { 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])) port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))

View File

@ -11,12 +11,12 @@ import (
func ParseVless(proxy string) (model.Proxy, error) { func ParseVless(proxy string) (model.Proxy, error) {
// 判断是否以 vless:// 开头 // 判断是否以 vless:// 开头
if !strings.HasPrefix(proxy, "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) parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
if len(parts) != 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) serverInfo := strings.SplitN(parts[1], "#", 2)
@ -27,7 +27,7 @@ func ParseVless(proxy string) (model.Proxy, error) {
return model.Proxy{}, err return model.Proxy{}, err
} }
if len(serverAndPort) != 2 { 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])) port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))

View File

@ -12,7 +12,7 @@ import (
func ParseVmess(proxy string) (model.Proxy, error) { func ParseVmess(proxy string) (model.Proxy, error) {
// 判断是否以 vmess:// 开头 // 判断是否以 vmess:// 开头
if !strings.HasPrefix(proxy, "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://")) base64, err := DecodeBase64(strings.TrimPrefix(proxy, "vmess://"))

33
utils/check_update.go Normal file
View 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
}
}

View File

@ -2,17 +2,29 @@ package database
import ( import (
"github.com/glebarez/sqlite" "github.com/glebarez/sqlite"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
"path/filepath"
"sub2clash/logger"
"sub2clash/model" "sub2clash/model"
"sub2clash/utils"
) )
var DB *gorm.DB var DB *gorm.DB
func ConnectDB() error { func ConnectDB() error {
// 用上面的数据库连接初始化 gorm // 用上面的数据库连接初始化 gorm
db, err := gorm.Open(sqlite.Open("sub2clash.db"), &gorm.Config{}) err := utils.MKDir("data")
if err != nil { 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 { if err != nil {
return err return err
@ -24,3 +36,23 @@ func ConnectDB() error {
} }
return nil 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)
}

30
utils/mkdir.go Normal file
View 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
}

View File

@ -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
}

View File

@ -1,7 +1,6 @@
package utils package utils
import ( import (
"sort"
"strings" "strings"
"sub2clash/model" "sub2clash/model"
"sub2clash/parser" "sub2clash/parser"
@ -30,12 +29,10 @@ func GetContryName(proxy model.Proxy) string {
func AddProxy( func AddProxy(
sub *model.Subscription, autotest bool, sub *model.Subscription, autotest bool,
lazy bool, sortStrategy string, lazy bool, clashType model.ClashType, proxies ...model.Proxy,
clashType model.ClashType, proxies ...model.Proxy,
) { ) {
newCountryGroupNames := make([]string, 0) newCountryGroupNames := make([]string, 0)
proxyTypes := model.GetSupportProxyTypes(clashType) proxyTypes := model.GetSupportProxyTypes(clashType)
// 添加节点 // 添加节点
for _, proxy := range proxies { for _, proxy := range proxies {
if !proxyTypes[proxy.Type] { if !proxyTypes[proxy.Type] {
@ -85,26 +82,6 @@ func AddProxy(
newCountryGroupNames = append(newCountryGroupNames, countryName) 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 { func ParseProxy(proxies ...string) []model.Proxy {

View File

@ -0,0 +1,37 @@
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) {
_ = 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
}