Skip to content

Commit

Permalink
feat: 解析短链
Browse files Browse the repository at this point in the history
  • Loading branch information
nitezs committed Mar 13, 2024
1 parent 867da56 commit 1c5e17a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 23 deletions.
25 changes: 24 additions & 1 deletion api/handler/short_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,30 @@ func ShortLinkGenHandler(c *gin.Context) {
c.String(200, hash)
}

func ShortLinkGetHandler(c *gin.Context) {
func ShortLinkGetUrlHandler(c *gin.Context) {
var params validator.ShortLinkGetValidator
if err := c.ShouldBindQuery(&params); err != nil {
c.String(400, "参数错误: "+err.Error())
return
}
if strings.TrimSpace(params.Hash) == "" {
c.String(400, "参数错误")
return
}
var shortLink model.ShortLink
result := database.FindShortLinkByHash(params.Hash, &shortLink)
if result.Error != nil {
c.String(404, "未找到短链接")
return
}
if shortLink.Password != "" && shortLink.Password != params.Password {
c.String(403, "密码错误")
return
}
c.String(200, shortLink.Url)
}

func ShortLinkGetConfigHandler(c *gin.Context) {
// 获取动态路由
hash := c.Param("hash")
password := c.Query("password")
Expand Down
14 changes: 9 additions & 5 deletions api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ func SetRoute(r *gin.Engine) {
handler.SubHandler(c)
},
)
r.POST(
"/short", func(c *gin.Context) {
handler.ShortLinkGenHandler(c)
r.GET(
"/s/:hash", func(c *gin.Context) {
handler.ShortLinkGetConfigHandler(c)
},
)
r.GET(
"/s/:hash", func(c *gin.Context) {
handler.ShortLinkGetHandler(c)
"/short", func(c *gin.Context) {
handler.ShortLinkGetUrlHandler(c)
})
r.POST(
"/short", func(c *gin.Context) {
handler.ShortLinkGenHandler(c)
},
)
r.PUT(
Expand Down
12 changes: 5 additions & 7 deletions api/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Axios -->
<script src="https://cdn.jsdelivr.net/npm/axios@latest/dist/axios.min.js"></script>
<script src="./static/index.js"></script>
<style>
.container {
max-width: 800px;
Expand Down Expand Up @@ -48,6 +47,7 @@ <h2>sub2clash</h2>
<label for="apiLink">解析链接:</label>
<div class="input-group mb-2">
<input class="form-control" id="urlInput" type="text" placeholder="通过生成的链接重新填写下方设置" />
<input class="form-control" id="urlshortLinkPasswdInput" type="text" placeholder="短链密码" />
<button class="btn btn-primary" onclick="parseInputURL()" type="button">
解析
</button>
Expand Down Expand Up @@ -142,16 +142,13 @@ <h2>sub2clash</h2>
<div class="form-group mb-5">
<label for="apiLink">配置链接:</label>
<div class="input-group mb-2">
<input class="form-control" id="apiLink" type="text" />
<button class="btn btn-primary" onclick="generateURL()" type="button">
生成链接
</button>
<input class="form-control" id="apiLink" type="text" placeholder="链接" />
<button class="btn btn-primary" onclick="copyToClipboard('apiLink',this)" type="button">
复制链接
</button>
</div>
<div class="input-group">
<input class="form-control" id="apiShortLink" type="text" />
<input class="form-control" id="apiShortLink" type="text" placeholder="链接" />
<input class="form-control" id="password" type="text" placeholder="密码" />
<button class="btn btn-primary" onclick="generateShortLink()" type="button">
生成短链
Expand All @@ -164,6 +161,7 @@ <h2>sub2clash</h2>
</button>
</div>
</div>

<!-- footer-->
<footer>
<p class="text-center">
Expand All @@ -174,5 +172,5 @@ <h2>sub2clash</h2>
</footer>
</div>
</body>

<script src="./static/index.js"></script>
</html>
59 changes: 49 additions & 10 deletions api/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function generateURI() {
noProxy = true;
}
if (noSub && noProxy) {
alert("订阅链接和节点分享链接不能同时为空!");
// alert("订阅链接和节点分享链接不能同时为空!");
return "";
}
// 获取复选框的值
Expand Down Expand Up @@ -103,7 +103,7 @@ function generateURI() {
prepend.trim() === "" ||
name.trim() === ""
) {
alert("Rule Provider 中存在空值,请检查后重试!");
// alert("Rule Provider 中存在空值,请检查后重试!");
return "";
}
providers.push(`[${behavior},${url},${group},${prepend},${name}]`);
Expand All @@ -118,7 +118,7 @@ function generateURI() {
let group = rules[i * 3 + 2].value;
// 是否存在空值
if (rule.trim() === "" || prepend.trim() === "" || group.trim() === "") {
alert("Rule 中存在空值,请检查后重试!");
// alert("Rule 中存在空值,请检查后重试!");
return "";
}
ruleList.push(`[${rule},${prepend},${group}]`);
Expand All @@ -143,7 +143,7 @@ function generateURI() {
let replaceStr = `<${replaces[i * 2].value}>`;
let replaceTo = `<${replaces[i * 2 + 1].value}>`;
if (replaceStr.trim() === "") {
alert("重命名设置中存在空值,请检查后重试!");
// alert("重命名设置中存在空值,请检查后重试!");
return "";
}
replaceList.push(`[${replaceStr},${replaceTo}]`);
Expand All @@ -154,9 +154,12 @@ function generateURI() {
}

// 将输入框中的 URL 解析为参数
function parseInputURL() {
async function parseInputURL() {
// 获取输入框中的 URL
const inputURL = document.getElementById("urlInput").value;
const urlshortLinkPasswdInput = document.getElementById(
"urlshortLinkPasswdInput"
).value;

if (!inputURL) {
alert("请输入有效的链接!");
Expand All @@ -170,13 +173,23 @@ function parseInputURL() {
alert("无效的链接!");
return;
}

if (url.pathname.includes("/s/")) {
let hash = url.pathname.substring(url.pathname.lastIndexOf("/s/") + 3);
let q = new URLSearchParams();
q.append("hash", hash);
q.append("password", urlshortLinkPasswdInput);
try {
const response = await axios.get("./short?" + q.toString());
url = new URL(window.location.href + response.data);
} catch (error) {
console.log(error);
alert("获取短链失败,请检查密码!");
}
}
let params = new URLSearchParams(url.search);
// 清除现有的输入框值
clearExistingValues();

// 获取查询参数
const params = new URLSearchParams(url.search);

// 分配值到对应的输入框
const pathSections = url.pathname.split("/");
const lastSection = pathSections[pathSections.length - 1];
Expand Down Expand Up @@ -360,19 +373,43 @@ function createRule() {
return div;
}

function listenInput() {
let selectElements = document.querySelectorAll("select");
let inputElements = document.querySelectorAll("input");
let textAreaElements = document.querySelectorAll("textarea");
inputElements.forEach(function (element) {
element.addEventListener("input", function () {
generateURL();
});
});
textAreaElements.forEach(function (element) {
element.addEventListener("input", function () {
generateURL();
});
});
selectElements.forEach(function (element) {
element.addEventListener("change", function () {
generateURL();
});
});
}

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

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

function addReplace() {
const div = createReplace();
document.getElementById("replaceGroup").appendChild(div);
listenInput();
}

function removeElement(button) {
Expand Down Expand Up @@ -423,7 +460,7 @@ function updateShortLink() {
let hash = apiShortLink.value;
if (hash.startsWith("http")) {
let u = new URL(hash);
hash = u.pathname.replace("/s/", "");
hash = u.pathname.substring(u.pathname.lastIndexOf("/s/") + 3);
}
if (password.value.trim() === "") {
alert("请输入密码!");
Expand Down Expand Up @@ -455,3 +492,5 @@ function updateShortLink() {
alert(error.response.data);
});
}

listenInput();

0 comments on commit 1c5e17a

Please sign in to comment.