Skip to content

Commit

Permalink
feat: 增加短链生成
Browse files Browse the repository at this point in the history
  • Loading branch information
nitezs committed Sep 17, 2023
1 parent 38dbea4 commit 6b08b2c
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ dist
subs
test
logs
dist/
sub2clash.db
6 changes: 3 additions & 3 deletions api/controller/default.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controller

import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"errors"
"gopkg.in/yaml.v3"
Expand All @@ -14,7 +14,7 @@ import (
"sub2clash/validator"
)

func BuildSub(clashType model.ClashType, query validator.SubQuery, template string) (
func BuildSub(clashType model.ClashType, query validator.SubValidator, template string) (
*model.Subscription, error,
) {
// 定义变量
Expand Down Expand Up @@ -85,7 +85,7 @@ func BuildSub(clashType model.ClashType, query validator.SubQuery, template stri
}
// 处理自定义 ruleProvider
for _, v := range query.RuleProviders {
hash := md5.Sum([]byte(v.Url))
hash := sha256.Sum224([]byte(v.Url))
name := hex.EncodeToString(hash[:])
provider := model.RuleProvider{
Type: "http",
Expand Down
50 changes: 50 additions & 0 deletions api/controller/short_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controller

import (
"crypto/sha256"
"encoding/hex"
"github.com/gin-gonic/gin"
"sub2clash/model"
"sub2clash/utils/database"
"sub2clash/validator"
"time"
)

func ShortLinkGenHandler(c *gin.Context) {
// 从请求中获取参数
var params validator.ShortLinkGenValidator
if err := c.ShouldBind(&params); err != nil {
c.String(400, "参数错误: "+err.Error())
}
// 生成短链接
//hash := utils.RandomString(6)
shortLink := sha256.Sum224([]byte(params.Url))
hash := hex.EncodeToString(shortLink[:])
// 存入数据库
database.DB.FirstOrCreate(
&model.ShortLink{
Hash: hash,
Url: params.Url,
LastRequestTime: -1,
},
)
// 返回短链接
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, "未找到短链接")
return
}
c.Redirect(302, "/"+shortLink.Url)
}
10 changes: 10 additions & 0 deletions api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ func SetRoute(r *gin.Engine) {
controller.SubHandler(c)
},
)
r.POST(
"/short", func(c *gin.Context) {
controller.ShortLinkGenHandler(c)
},
)
r.GET(
"/s/:hash", func(c *gin.Context) {
controller.ShortLinkGetHandler(c)
},
)
}
66 changes: 52 additions & 14 deletions api/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>

<!-- Axios -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<style>
.container {
max-width: 800px;
Expand Down Expand Up @@ -111,17 +114,21 @@ <h2>sub2clash</h2>
<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">
<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="copyToClipboard()">复制链接</button>
<button class="btn btn-primary" type="button" onclick="generateURL()">生成链接</button>
<button class="btn btn-primary" type="button" onclick="copyToClipboard('apiLink',this)">复制链接</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>
</div>
</div>

Expand All @@ -134,12 +141,15 @@ <h2>sub2clash</h2>
</div>

<script>
async function copyToClipboard() {
const apiLinkInput = document.getElementById("apiLink").value;

async function copyToClipboard(elem, e) {
const apiLinkInput = document.querySelector(`#${elem}`).value;
try {
await navigator.clipboard.writeText(apiLinkInput);
alert("API链接已复制到剪贴板!");
let text = e.textContent;
e.addEventListener("mouseout", function () {
e.textContent = text;
});
e.textContent = "复制成功";
} catch (err) {
console.error('复制到剪贴板失败:', err);
}
Expand Down Expand Up @@ -185,14 +195,15 @@ <h2>sub2clash</h2>
button.parentElement.remove();
}

function generateAPIRequest() {
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
// 去除 subLines 中空元素
subLines = subLines.map((item) => {
if (item !== "") {
Expand All @@ -201,10 +212,13 @@ <h2>sub2clash</h2>
});
if (subLines.length > 0) {
queryParams.push(`sub=${encodeURIComponent(subLines.join(','))}`);
} else {
noSub = true
}

// 获取并组合节点分享链接
let proxyLines = document.getElementById("proxy").value.split('\n').filter(line => line.trim() !== "");
let noProxy = false
// 去除 proxyLines 中空元素
proxyLines = proxyLines.map((item) => {
if (item !== "") {
Expand All @@ -213,8 +227,13 @@ <h2>sub2clash</h2>
});
if (proxyLines.length > 0) {
queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(','))}`);
} else {
noProxy = true
}
if (noSub && noProxy) {
alert("订阅链接和节点分享链接不能同时为空!")
return
}

// 获取复选框的值
const refresh = document.getElementById("refresh").checked;
queryParams.push(`refresh=${refresh ? 'true' : 'false'}`);
Expand Down Expand Up @@ -268,11 +287,30 @@ <h2>sub2clash</h2>
// 获取排序策略
const sort = document.getElementById("sort").value;
queryParams.push(`sort=${sort}`);
return `${endpoint}?${queryParams.join('&')}`;
}

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

function generateShortLink() {
const apiShortLink = document.getElementById("apiShortLink");

axios.post("/short", {
"url": generateURI()
}, {
headers: {
"Content-Type": "application/json"
}
}).then((response) => {
apiShortLink.value = `${window.location.origin}${window.location.pathname}s/${response.data}`;
}).catch((error) => {
console.log(error);
alert("生成短链失败,请重试!");
})
}
</script>

</body>
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.4 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
Expand All @@ -35,4 +38,6 @@ require (
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gorm.io/driver/sqlite v1.5.3 // indirect
gorm.io/gorm v1.25.4 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand All @@ -45,6 +49,8 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -98,5 +104,9 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.3 h1:7/0dUgX28KAcopdfbRWWl68Rflh6osa4rDh+m51KL2g=
gorm.io/driver/sqlite v1.5.3/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sub2clash/config"
"sub2clash/logger"
"sub2clash/utils"
"sub2clash/utils/database"
)

//go:embed templates/template_meta.yaml
Expand Down Expand Up @@ -53,6 +54,10 @@ func init() {
if err := writeTemplate(config.Default.ClashTemplate, templateClash); err != nil {
os.Exit(1)
}
err := database.ConnectDB()
if err != nil {
panic(err)
}
}

func main() {
Expand Down
7 changes: 7 additions & 0 deletions model/short_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type ShortLink struct {
Hash string `gorm:"primary_key"`
Url string
LastRequestTime int64
}
22 changes: 22 additions & 0 deletions utils/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package database

import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"sub2clash/model"
)

var DB *gorm.DB

func ConnectDB() error {
db, err := gorm.Open(sqlite.Open("sub2clash.db"), &gorm.Config{})
if err != nil {
return err
}
DB = db
err = db.AutoMigrate(&model.ShortLink{})
if err != nil {
return err
}
return nil
}
13 changes: 13 additions & 0 deletions utils/random_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import "math/rand"

func RandomString(length int) string {
// 生成随机字符串
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var result []byte
for i := 0; i < length; i++ {
result = append(result, charset[rand.Intn(len(charset))])
}
return string(result)
}
6 changes: 3 additions & 3 deletions utils/sub.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package utils

import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
Expand All @@ -19,7 +19,7 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
if refresh {
return FetchSubscriptionFromAPI(url)
}
hash := md5.Sum([]byte(url))
hash := sha256.Sum224([]byte(url))
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
stat, err := os.Stat(fileName)
if err != nil {
Expand Down Expand Up @@ -49,7 +49,7 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
}

func FetchSubscriptionFromAPI(url string) ([]byte, error) {
hash := md5.Sum([]byte(url))
hash := sha256.Sum224([]byte(url))
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
resp, err := Get(url)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions validator/short_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package validator

type ShortLinkGenValidator struct {
Url string `form:"url" binding:"required"`
}

type ShortLinkGetValidator struct {
Hash string `form:"hash" binding:"required"`
}
Loading

0 comments on commit 6b08b2c

Please sign in to comment.