Skip to content

Commit

Permalink
feat: Web UI
Browse files Browse the repository at this point in the history
update: 校验Rule-Provider是否有重名
modify: 修改meta默认模板
modify: 根据Clash筛选返回配置中的节点类型
  • Loading branch information
nitezs committed Sep 17, 2023
1 parent 9185216 commit d38d5bc
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
| 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` | 指定国家策略组是否自动测速 |
| autoTest | bool || `false` | 国家策略组是否自动测速 |
| lazy | bool || `false` | 自动测速是否启用 lazy |
| sort | string || `nameasc` | 国家策略组排序策略,可选值 `nameasc``namedesc``sizeasc``sizedesc` |

Expand All @@ -64,4 +64,4 @@

## TODO

- [ ] 可视化面板
- [x] 可视化面板
12 changes: 12 additions & 0 deletions api/route.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package api

import (
"embed"
"github.com/gin-gonic/gin"
"html/template"
"sub2clash/api/controller"
"sub2clash/middleware"
)

//go:embed templates/*
var templates embed.FS

func SetRoute(r *gin.Engine) {
r.Use(middleware.ZapLogger())
// 使用内嵌的模板文件
r.SetHTMLTemplate(template.Must(template.New("").ParseFS(templates, "templates/*")))
r.GET(
"/", func(c *gin.Context) {
c.HTML(200, "index.html", nil)
},
)
r.GET(
"/clash", func(c *gin.Context) {
controller.SubmodHandler(c)
Expand Down
279 changes: 279 additions & 0 deletions api/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
<!DOCTYPE html>
<html lang="zh-CN">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>sub2clash</title>

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>

<style>
.container {
max-width: 800px;
}

.btn-xs {
padding: 2px 2px; /* 调整内边距以减小按钮大小 */
font-size: 10px; /* 设置字体大小 */
line-height: 1.2; /* 调整行高 */
border-radius: 3px; /* 可选的边框半径调整 */
height: 25px;
width: 25px;
}
</style>
</head>

<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">

<!-- 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>
</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>
</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>
</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>
</div>

<!-- Template -->
<div class="form-group mb-3">
<label for="template">模板链接或名称(可选):</label>
<input type="text" class="form-control" id="template" name="template"
placeholder="输入外部模板链接或内部模板名称">
</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>
</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>
</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>
</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>
</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>
</div>

<!-- Click to Get API Link -->
<button type="button" class="btn btn-primary mb-3" onclick="generateAPIRequest()">获取API链接</button>
</form>

<!-- Display the API Link -->
<div class="form-group mb-5">
<label for="apiLink">API 链接:</label>
<div class="input-group">
<input type="text" class="form-control" id="apiLink" readonly>
<button class="btn btn-primary" type="button" onclick="copyToClipboard()">复制链接</button>
</div>
</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() {
const apiLinkInput = document.getElementById("apiLink").value;

try {
await navigator.clipboard.writeText(apiLinkInput);
alert("API链接已复制到剪贴板!");
} catch (err) {
console.error('复制到剪贴板失败:', err);
}
}

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">
<input type="text" class="form-control" name="ruleProvider" placeholder="Group">
<input type="text" class="form-control" name="ruleProvider" placeholder="Prepend">
<input type="text" class="form-control" name="ruleProvider" placeholder="Name">
<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">
<input type="text" class="form-control" name="rule" placeholder="Group">
<button type="button" class="btn btn-danger" onclick="removeElement(this)">删除</button>
`;
return div;
}

function addRuleProvider() {
const div = createRuleProvider();
document.getElementById('ruleProviderGroup').appendChild(div);
}

function addRule() {
const div = createRule();
document.getElementById('ruleGroup').appendChild(div);
}

function removeElement(button) {
button.parentElement.remove();
}

function generateAPIRequest() {
const queryParams = [];

// 获取 API Endpoint
const endpoint = document.getElementById("endpoint").value;

// 获取并组合订阅链接
let subLines = document.getElementById("sub").value.split('\n').filter(line => line.trim() !== "");
// 去除 subLines 中空元素
subLines = subLines.map((item) => {
if (item !== "") {
return item;
}
});
if (subLines.length > 0) {
queryParams.push(`sub=${encodeURIComponent(subLines.join(','))}`);
}

// 获取并组合节点分享链接
let proxyLines = document.getElementById("proxy").value.split('\n').filter(line => line.trim() !== "");
// 去除 proxyLines 中空元素
proxyLines = proxyLines.map((item) => {
if (item !== "") {
return item;
}
});
if (proxyLines.length > 0) {
queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(','))}`);
}

// 获取复选框的值
const refresh = document.getElementById("refresh").checked;
queryParams.push(`refresh=${refresh ? 'true' : 'false'}`);
const autoTest = document.getElementById("autoTest").checked;
queryParams.push(`autoTest=${autoTest ? 'true' : 'false'}`);
const lazy = document.getElementById("lazy").checked;
queryParams.push(`lazy=${lazy ? 'true' : 'false'}`);

// 获取模板链接或名称(如果存在)
const template = document.getElementById("template").value;
if (template.trim() !== "") {
queryParams.push(`template=${encodeURIComponent(template)}`);
}

// 获取Rule Provider和规则
const ruleProviders = document.getElementsByName("ruleProvider");
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}]`);
}
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}]`);
}
}
queryParams.push(`rule=${encodeURIComponent(ruleList.join(','))}`);

// 获取排序策略
const sort = document.getElementById("sort").value;
queryParams.push(`sort=${sort}`);

// 组合最终的API链接
document.getElementById("apiLink").value = `${window.location.origin}${window.location.pathname}${endpoint}?${queryParams.join('&')}`;
}

</script>

</body>
</html>
14 changes: 10 additions & 4 deletions templates/template_meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ proxy-groups:
- name: 手动切换
type: select
proxies:
- name: 微软服务
type: select
proxies:
- 节点选择
- 手动切换
- DIRECT
- name: 游戏平台
type: select
proxies:
Expand Down Expand Up @@ -48,14 +54,14 @@ proxy-groups:
- 手动切换
- DIRECT
rules:
- GEOSITE,private,全球直连
- GEOSITE,private,全球直连,no-resolve
- GEOIP,private,全球直连
- GEOSITE,category-ads-all,广告拦截
- GEOSITE,CN,全球直连
- GEOIP,CN,全球直连
- GEOSITE,biliintl,哔哩哔哩
- GEOSITE,microsoft,微软服务
- GEOSITE,bilibili,哔哩哔哩
- GEOSITE,bahamut,巴哈姆特
- GEOSITE,category-games,游戏平台
- GEOSITE,geolocation-!cn,节点选择
- GEOSITE,CN,全球直连
- GEOIP,CN,全球直连
- MATCH,漏网之鱼
2 changes: 1 addition & 1 deletion utils/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// LoadTemplate 加载模板
// template 模板文件名
// templates 模板文件名
func LoadTemplate(template string) ([]byte, error) {
tPath := filepath.Join("templates", template)
if _, err := os.Stat(tPath); err == nil {
Expand Down
10 changes: 9 additions & 1 deletion validator/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type SubQuery struct {
Proxy string `form:"proxy" binding:""`
Proxies []string `form:"-" binding:""`
Refresh bool `form:"refresh,default=false" binding:""`
Template string `form:"template" binding:""`
Template string `form:"templates" binding:""`
RuleProvider string `form:"ruleProvider" binding:""`
RuleProviders []RuleProviderStruct `form:"-" binding:""`
Rule string `form:"rule" binding:""`
Expand Down Expand Up @@ -101,6 +101,14 @@ func ParseQuery(c *gin.Context) (SubQuery, error) {
},
)
}
// 校验 Rule-Provider 是否有重名
names := make(map[string]bool)
for _, ruleProvider := range query.RuleProviders {
if _, ok := names[ruleProvider.Name]; ok {
return SubQuery{}, errors.New("参数错误: Rule-Provider 名称重复")
}
names[ruleProvider.Name] = true
}
} else {
query.RuleProviders = nil
}
Expand Down

0 comments on commit d38d5bc

Please sign in to comment.