Skip to content

Commit

Permalink
Merge pull request #14 from nitezs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
nitezs authored Feb 19, 2024
2 parents 50877b1 + f51958b commit 3309a0c
Show file tree
Hide file tree
Showing 28 changed files with 10,516 additions and 10,350 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ REQUEST_RETRY_TIMES=3
REQUEST_MAX_FILE_SIZE=1048576
CACHE_EXPIRE=300
LOG_LEVEL=info
BASE_PATH=/
BASE_PATH=/
26 changes: 13 additions & 13 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
# hooks:
# - go mod tidy
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
ldflags:
- -s -w -X sub2clash/config.Version={{ .Version }}
no_unique_dist_dir: true
binary: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
ldflags:
- -s -w -X sub2clash/config.Version={{ .Version }}
no_unique_dist_dir: true
binary: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"configurations": [
{
"name": "Debug",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/main.go",
"output": "${workspaceFolder}/dist/main.exe",
"buildFlags": "-ldflags '-X sub2clash/config.Dev=true -X sub2clash/config.Version=dev'"
}
]
}
28 changes: 14 additions & 14 deletions API_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

获取 Clash/Clash.Meta 配置链接

| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|--------------|--------|-------------------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| sub | string | sub/proxy 至少有一项存在 | - | 订阅链接,可以在链接结尾加上`#名称`,来给订阅中的节点加上统一前缀(可以输入多个,用 `,` 分隔) |
| proxy | string | sub/proxy 至少有一项存在 | - | 节点分享链接(可以输入多个,用 `,` 分隔) |
| refresh | bool || `false` | 强制刷新配置(默认缓存 5 分钟) |
| template | string || - | 外部模板链接或内部模板名称 |
| ruleProvider | string || - | 格式 `[Behavior,Url,Group,Prepend,Name],[Behavior,Url,Group,Prepend,Name]...`,其中 `Group` 是该规则集使用的策略组名,`Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到MATCH规则之前|
| rule | string || - | 格式 `[Rule,Prepend],[Rule,Prepend]...`,其中 `Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到MATCH规则之前|
| autoTest | bool || `false` | 国家策略组是否自动测速 |
| lazy | bool || `false` | 自动测速是否启用 lazy |
| sort | string || `nameasc` | 国家策略组排序策略,可选值 `nameasc``namedesc``sizeasc``sizedesc` |
| replace | string || - | 通过正则表达式重命名节点,格式 `[<ReplaceKey>,<ReplaceTo>],[<ReplaceKey>,<ReplaceTo>]...` |
| remove | string || - | 通过正则表达式删除节点 |
| nodeList | bool || `false` | 只输出节点 | |
| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
|--------------|--------|-------------------|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| sub | string | sub/proxy 至少有一项存在 | - | 订阅链接,可以在链接结尾加上`#名称`,来给订阅中的节点加上统一前缀(可以输入多个,用 `,` 分隔) |
| proxy | string | sub/proxy 至少有一项存在 | - | 节点分享链接(可以输入多个,用 `,` 分隔) |
| refresh | bool || `false` | 强制刷新配置(默认缓存 5 分钟) |
| template | string || - | 外部模板链接或内部模板名称 |
| ruleProvider | string || - | 格式 `[Behavior,Url,Group,Prepend,Name],[Behavior,Url,Group,Prepend,Name]...`,其中 `Group` 是该规则集使用的策略组名,`Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到 MATCH 规则之前|
| rule | string || - | 格式 `[Rule,Prepend],[Rule,Prepend]...`,其中 `Prepend` 为 bool 类型,如果为 `true` 规则将被添加到规则列表顶部,否则添加到规则列表底部(会调整到 MATCH 规则之前|
| autoTest | bool || `false` | 国家策略组是否自动测速 |
| lazy | bool || `false` | 自动测速是否启用 lazy |
| sort | string || `nameasc` | 国家策略组排序策略,可选值 `nameasc``namedesc``sizeasc``sizedesc` |
| replace | string || - | 通过正则表达式重命名节点,格式 `[<ReplaceKey>,<ReplaceTo>],[<ReplaceKey>,<ReplaceTo>]...` |
| remove | string || - | 通过正则表达式删除节点 |
| nodeList | bool || `false` | 只输出节点 |

# `/short`

Expand Down
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# sub2clash

将订阅链接转换为 Clash、Clash.Meta 配置
将订阅链接转换为 Clash、Clash.Meta 配置
[预览](https://www.nite07.com/sub)

## 特性

- 开箱即用的规则、策略组配置
- 自动根据节点名称按国家划分策略组
- 支持多订阅合并
- 支持添加自定义 Rule Provider、Rule
- 支持多种协议
- Shadowsocks
- ShadowsocksR
- Vmess
- Vless
- Vless (Clash.Meta)
- Trojan
- Hysteria2 (Clash.Meta)

## 使用

Expand All @@ -25,20 +28,20 @@

可以通过编辑 .env 文件来修改默认配置,docker 直接添加环境变量

| 变量名 | 说明 | 默认值 |
|-----------------------|-----------------------------------------------------------|-----------------------|
| PORT | 端口 | `8011` |
| META_TEMPLATE | meta 模板文件名 | `template_meta.yaml` |
| CLASH_TEMPLATE | clash 模板文件名 | `template_clash.yaml` |
| REQUEST_RETRY_TIMES | Get 请求重试次数 | `3` |
| REQUEST_MAX_FILE_SIZE | Get 请求订阅文件最大大小(byte) | `1048576` |
| CACHE_EXPIRE | 订阅缓存时间(秒) | `300` |
| LOG_LEVEL | 日志等级,可选值 `debug`,`info`,`warn`,`error` | `info` |
| SHORT_LINK_LENGTH | 短链长度 | `6` |
| 变量名 | 说明 | 默认值 |
|-----------------------|----------------------------------------|-----------------------|
| PORT | 端口 | `8011` |
| META_TEMPLATE | 默认 meta 模板文件名 | `template_meta.yaml` |
| CLASH_TEMPLATE | 默认 clash 模板文件名 | `template_clash.yaml` |
| REQUEST_RETRY_TIMES | Get 请求重试次数 | `3` |
| REQUEST_MAX_FILE_SIZE | Get 请求订阅文件最大大小(byte) | `1048576` |
| CACHE_EXPIRE | 订阅缓存时间(秒) | `300` |
| LOG_LEVEL | 日志等级,可选值 `debug`,`info`,`warn`,`error` | `info` |
| SHORT_LINK_LENGTH | 短链长度 | `6` |

### API
### API

[API文档](./API_README.md)
[API 文档](./API_README.md)

### 模板

Expand Down
5 changes: 3 additions & 2 deletions api/controller/clash.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package controller

import (
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
"net/http"
"sub2clash/config"
"sub2clash/model"
"sub2clash/validator"

"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
)

func SubmodHandler(c *gin.Context) {
Expand Down
18 changes: 11 additions & 7 deletions api/controller/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"net/url"
"regexp"
"sort"
Expand All @@ -16,6 +14,9 @@ import (
"sub2clash/parser"
"sub2clash/utils"
"sub2clash/validator"

"go.uber.org/zap"
"gopkg.in/yaml.v3"
)

func BuildSub(clashType model.ClashType, query validator.SubValidator, template string) (
Expand All @@ -30,17 +31,20 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
if query.Template != "" {
template = query.Template
}
_, err = url.ParseRequestURI(template)
if err != nil {
templateBytes, err = utils.LoadTemplate(template)
if strings.HasPrefix(template, "http") {
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())
}
} else {
templateBytes, err = utils.LoadSubscription(template, query.Refresh)
unescape, err := url.QueryUnescape(template)
if err != nil {
return nil, errors.New("加载模板失败: " + err.Error())
}
templateBytes, err = utils.LoadTemplate(unescape)
if err != nil {
logger.Logger.Debug(
"load template failed", zap.String("template", template), zap.Error(err),
Expand Down Expand Up @@ -72,7 +76,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template
err = yaml.Unmarshal(data, &sub)
newProxies := make([]model.Proxy, 0)
if err != nil {
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|vless)://")
reg, _ := regexp.Compile("(ssr|ss|vmess|trojan|vless|hysteria)://")
if reg.Match(data) {
p := utils.ParseProxy(strings.Split(string(data), "\n")...)
newProxies = p
Expand Down
5 changes: 3 additions & 2 deletions api/controller/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package controller

import (
_ "embed"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
"net/http"
"sub2clash/config"
"sub2clash/model"
"sub2clash/validator"

"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
)

func SubHandler(c *gin.Context) {
Expand Down
7 changes: 4 additions & 3 deletions api/controller/short_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package controller

import (
"errors"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm"
"io"
"net/http"
"strconv"
Expand All @@ -16,6 +13,10 @@ import (
"sub2clash/utils/database"
"sub2clash/validator"
"time"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm"
)

func ShortLinkGenHandler(c *gin.Context) {
Expand Down
3 changes: 2 additions & 1 deletion api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package api

import (
"embed"
"github.com/gin-gonic/gin"
"html/template"
"log"
"net/http"
"sub2clash/api/controller"
"sub2clash/config"
"sub2clash/middleware"

"github.com/gin-gonic/gin"
)

//go:embed static
Expand Down
Loading

0 comments on commit 3309a0c

Please sign in to comment.