mirror of
				https://github.com/bestnite/sub2clash.git
				synced 2025-11-03 20:30:35 +00:00 
			
		
		
		
	feat: 增加短链生成
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -3,4 +3,4 @@ dist
 | 
				
			|||||||
subs
 | 
					subs
 | 
				
			||||||
test
 | 
					test
 | 
				
			||||||
logs
 | 
					logs
 | 
				
			||||||
dist/
 | 
					sub2clash.db
 | 
				
			||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
package controller
 | 
					package controller
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"crypto/md5"
 | 
						"crypto/sha256"
 | 
				
			||||||
	"encoding/hex"
 | 
						"encoding/hex"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"gopkg.in/yaml.v3"
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
@@ -14,7 +14,7 @@ import (
 | 
				
			|||||||
	"sub2clash/validator"
 | 
						"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,
 | 
						*model.Subscription, error,
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
	// 定义变量
 | 
						// 定义变量
 | 
				
			||||||
@@ -85,7 +85,7 @@ func BuildSub(clashType model.ClashType, query validator.SubQuery, template stri
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	// 处理自定义 ruleProvider
 | 
						// 处理自定义 ruleProvider
 | 
				
			||||||
	for _, v := range query.RuleProviders {
 | 
						for _, v := range query.RuleProviders {
 | 
				
			||||||
		hash := md5.Sum([]byte(v.Url))
 | 
							hash := sha256.Sum224([]byte(v.Url))
 | 
				
			||||||
		name := hex.EncodeToString(hash[:])
 | 
							name := hex.EncodeToString(hash[:])
 | 
				
			||||||
		provider := model.RuleProvider{
 | 
							provider := model.RuleProvider{
 | 
				
			||||||
			Type:     "http",
 | 
								Type:     "http",
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										50
									
								
								api/controller/short_link.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								api/controller/short_link.go
									
									
									
									
									
										Normal file
									
								
							@@ -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(¶ms); 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
									
								
								api/route.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								api/route.go
									
									
									
									
									
								
							@@ -30,4 +30,14 @@ func SetRoute(r *gin.Engine) {
 | 
				
			|||||||
			controller.SubHandler(c)
 | 
								controller.SubHandler(c)
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
						r.POST(
 | 
				
			||||||
 | 
							"/short", func(c *gin.Context) {
 | 
				
			||||||
 | 
								controller.ShortLinkGenHandler(c)
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						r.GET(
 | 
				
			||||||
 | 
							"/s/:hash", func(c *gin.Context) {
 | 
				
			||||||
 | 
								controller.ShortLinkGetHandler(c)
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,9 @@
 | 
				
			|||||||
            integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
 | 
					            integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
 | 
				
			||||||
            crossorigin="anonymous"></script>
 | 
					            crossorigin="anonymous"></script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <!-- Axios -->
 | 
				
			||||||
 | 
					    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <style>
 | 
					    <style>
 | 
				
			||||||
        .container {
 | 
					        .container {
 | 
				
			||||||
            max-width: 800px;
 | 
					            max-width: 800px;
 | 
				
			||||||
@@ -111,17 +114,21 @@
 | 
				
			|||||||
                <option value="sizedesc">节点数量(降序)</option>
 | 
					                <option value="sizedesc">节点数量(降序)</option>
 | 
				
			||||||
            </select>
 | 
					            </select>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 | 
					 | 
				
			||||||
        <!-- Click to Get API Link -->
 | 
					 | 
				
			||||||
        <button type="button" class="btn btn-primary mb-3" onclick="generateAPIRequest()">获取API链接</button>
 | 
					 | 
				
			||||||
    </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">API 链接:</label>
 | 
					        <label for="apiLink">配置链接:</label>
 | 
				
			||||||
        <div class="input-group">
 | 
					        <div class="input-group mb-2">
 | 
				
			||||||
            <input type="text" class="form-control" id="apiLink" readonly>
 | 
					            <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>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -134,12 +141,15 @@
 | 
				
			|||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script>
 | 
					<script>
 | 
				
			||||||
    async function copyToClipboard() {
 | 
					    async function copyToClipboard(elem, e) {
 | 
				
			||||||
        const apiLinkInput = document.getElementById("apiLink").value;
 | 
					        const apiLinkInput = document.querySelector(`#${elem}`).value;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            await navigator.clipboard.writeText(apiLinkInput);
 | 
					            await navigator.clipboard.writeText(apiLinkInput);
 | 
				
			||||||
            alert("API链接已复制到剪贴板!");
 | 
					            let text = e.textContent;
 | 
				
			||||||
 | 
					            e.addEventListener("mouseout", function () {
 | 
				
			||||||
 | 
					                e.textContent = text;
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					            e.textContent = "复制成功";
 | 
				
			||||||
        } catch (err) {
 | 
					        } catch (err) {
 | 
				
			||||||
            console.error('复制到剪贴板失败:', err);
 | 
					            console.error('复制到剪贴板失败:', err);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -185,7 +195,7 @@
 | 
				
			|||||||
        button.parentElement.remove();
 | 
					        button.parentElement.remove();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function generateAPIRequest() {
 | 
					    function generateURI() {
 | 
				
			||||||
        const queryParams = [];
 | 
					        const queryParams = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 获取 API Endpoint
 | 
					        // 获取 API Endpoint
 | 
				
			||||||
@@ -193,6 +203,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // 获取并组合订阅链接
 | 
					        // 获取并组合订阅链接
 | 
				
			||||||
        let subLines = document.getElementById("sub").value.split('\n').filter(line => line.trim() !== "");
 | 
					        let subLines = document.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 !== "") {
 | 
				
			||||||
@@ -201,10 +212,13 @@
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
        if (subLines.length > 0) {
 | 
					        if (subLines.length > 0) {
 | 
				
			||||||
            queryParams.push(`sub=${encodeURIComponent(subLines.join(','))}`);
 | 
					            queryParams.push(`sub=${encodeURIComponent(subLines.join(','))}`);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            noSub = true
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 获取并组合节点分享链接
 | 
					        // 获取并组合节点分享链接
 | 
				
			||||||
        let proxyLines = document.getElementById("proxy").value.split('\n').filter(line => line.trim() !== "");
 | 
					        let proxyLines = document.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 !== "") {
 | 
				
			||||||
@@ -213,8 +227,13 @@
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
        if (proxyLines.length > 0) {
 | 
					        if (proxyLines.length > 0) {
 | 
				
			||||||
            queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(','))}`);
 | 
					            queryParams.push(`proxy=${encodeURIComponent(proxyLines.join(','))}`);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            noProxy = true
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (noSub && noProxy) {
 | 
				
			||||||
 | 
					            alert("订阅链接和节点分享链接不能同时为空!")
 | 
				
			||||||
 | 
					            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'}`);
 | 
				
			||||||
@@ -268,11 +287,30 @@
 | 
				
			|||||||
        // 获取排序策略
 | 
					        // 获取排序策略
 | 
				
			||||||
        const sort = document.getElementById("sort").value;
 | 
					        const sort = document.getElementById("sort").value;
 | 
				
			||||||
        queryParams.push(`sort=${sort}`);
 | 
					        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>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								go.mod
									
									
									
									
									
								
							@@ -20,10 +20,13 @@ require (
 | 
				
			|||||||
	github.com/go-playground/universal-translator v0.18.1 // indirect
 | 
						github.com/go-playground/universal-translator v0.18.1 // indirect
 | 
				
			||||||
	github.com/go-playground/validator/v10 v10.15.4 // indirect
 | 
						github.com/go-playground/validator/v10 v10.15.4 // indirect
 | 
				
			||||||
	github.com/goccy/go-json v0.10.2 // 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/json-iterator/go v1.1.12 // indirect
 | 
				
			||||||
	github.com/klauspost/cpuid/v2 v2.2.5 // indirect
 | 
						github.com/klauspost/cpuid/v2 v2.2.5 // indirect
 | 
				
			||||||
	github.com/leodido/go-urn v1.2.4 // indirect
 | 
						github.com/leodido/go-urn v1.2.4 // indirect
 | 
				
			||||||
	github.com/mattn/go-isatty v0.0.19 // 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/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 | 
				
			||||||
	github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
						github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
				
			||||||
	github.com/pelletier/go-toml/v2 v2.1.0 // indirect
 | 
						github.com/pelletier/go-toml/v2 v2.1.0 // indirect
 | 
				
			||||||
@@ -35,4 +38,6 @@ require (
 | 
				
			|||||||
	golang.org/x/net v0.15.0 // indirect
 | 
						golang.org/x/net v0.15.0 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.12.0 // indirect
 | 
						golang.org/x/sys v0.12.0 // indirect
 | 
				
			||||||
	google.golang.org/protobuf v1.31.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
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								go.sum
									
									
									
									
									
								
							@@ -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 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
 | 
				
			||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 | 
					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/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 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
 | 
				
			||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 | 
					github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 | 
				
			||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 | 
					github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 | 
				
			||||||
@@ -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/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 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
 | 
				
			||||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
 | 
					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-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 h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
 | 
				
			||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 | 
					github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 | 
				
			||||||
@@ -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.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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
					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=
 | 
					nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
 | 
				
			||||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
 | 
					rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.go
									
									
									
									
									
								
							@@ -12,6 +12,7 @@ import (
 | 
				
			|||||||
	"sub2clash/config"
 | 
						"sub2clash/config"
 | 
				
			||||||
	"sub2clash/logger"
 | 
						"sub2clash/logger"
 | 
				
			||||||
	"sub2clash/utils"
 | 
						"sub2clash/utils"
 | 
				
			||||||
 | 
						"sub2clash/utils/database"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:embed templates/template_meta.yaml
 | 
					//go:embed templates/template_meta.yaml
 | 
				
			||||||
@@ -53,6 +54,10 @@ func init() {
 | 
				
			|||||||
	if err := writeTemplate(config.Default.ClashTemplate, templateClash); err != nil {
 | 
						if err := writeTemplate(config.Default.ClashTemplate, templateClash); err != nil {
 | 
				
			||||||
		os.Exit(1)
 | 
							os.Exit(1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						err := database.ConnectDB()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										7
									
								
								model/short_link.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								model/short_link.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					package model
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ShortLink struct {
 | 
				
			||||||
 | 
						Hash            string `gorm:"primary_key"`
 | 
				
			||||||
 | 
						Url             string
 | 
				
			||||||
 | 
						LastRequestTime int64
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										22
									
								
								utils/database/database.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								utils/database/database.go
									
									
									
									
									
										Normal file
									
								
							@@ -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
									
								
								utils/random_string.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								utils/random_string.go
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
package utils
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"crypto/md5"
 | 
						"crypto/sha256"
 | 
				
			||||||
	"encoding/hex"
 | 
						"encoding/hex"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
@@ -19,7 +19,7 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
 | 
				
			|||||||
	if refresh {
 | 
						if refresh {
 | 
				
			||||||
		return FetchSubscriptionFromAPI(url)
 | 
							return FetchSubscriptionFromAPI(url)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	hash := md5.Sum([]byte(url))
 | 
						hash := sha256.Sum224([]byte(url))
 | 
				
			||||||
	fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
 | 
						fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
 | 
				
			||||||
	stat, err := os.Stat(fileName)
 | 
						stat, err := os.Stat(fileName)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -49,7 +49,7 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func FetchSubscriptionFromAPI(url string) ([]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[:]))
 | 
						fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
 | 
				
			||||||
	resp, err := Get(url)
 | 
						resp, err := Get(url)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										9
									
								
								validator/short_link.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								validator/short_link.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					package validator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ShortLinkGenValidator struct {
 | 
				
			||||||
 | 
						Url string `form:"url" binding:"required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ShortLinkGetValidator struct {
 | 
				
			||||||
 | 
						Hash string `form:"hash" binding:"required"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
package validator
 | 
					package validator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"crypto/md5"
 | 
						"crypto/sha256"
 | 
				
			||||||
	"encoding/hex"
 | 
						"encoding/hex"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
@@ -11,7 +11,7 @@ import (
 | 
				
			|||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type SubQuery struct {
 | 
					type SubValidator struct {
 | 
				
			||||||
	Sub           string               `form:"sub" binding:""`
 | 
						Sub           string               `form:"sub" binding:""`
 | 
				
			||||||
	Subs          []string             `form:"-" binding:""`
 | 
						Subs          []string             `form:"-" binding:""`
 | 
				
			||||||
	Proxy         string               `form:"proxy" binding:""`
 | 
						Proxy         string               `form:"proxy" binding:""`
 | 
				
			||||||
@@ -40,19 +40,22 @@ type RuleStruct struct {
 | 
				
			|||||||
	Prepend bool
 | 
						Prepend bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ParseQuery(c *gin.Context) (SubQuery, error) {
 | 
					func ParseQuery(c *gin.Context) (SubValidator, error) {
 | 
				
			||||||
	var query SubQuery
 | 
						var query SubValidator
 | 
				
			||||||
	if err := c.ShouldBind(&query); err != nil {
 | 
						if err := c.ShouldBind(&query); err != nil {
 | 
				
			||||||
		return SubQuery{}, errors.New("参数错误: " + err.Error())
 | 
							return SubValidator{}, errors.New("参数错误: " + err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if query.Sub == "" && query.Proxy == "" {
 | 
						if query.Sub == "" && query.Proxy == "" {
 | 
				
			||||||
		return SubQuery{}, errors.New("参数错误: sub 和 proxy 不能同时为空")
 | 
							return SubValidator{}, errors.New("参数错误: sub 和 proxy 不能同时为空")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if query.Sub != "" {
 | 
						if query.Sub != "" {
 | 
				
			||||||
		query.Subs = strings.Split(query.Sub, ",")
 | 
							query.Subs = strings.Split(query.Sub, ",")
 | 
				
			||||||
		for i := range query.Subs {
 | 
							for i := range query.Subs {
 | 
				
			||||||
 | 
								if !strings.HasPrefix(query.Subs[i], "http") {
 | 
				
			||||||
 | 
									return SubValidator{}, errors.New("参数错误: sub 格式错误")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			if _, err := url.ParseRequestURI(query.Subs[i]); err != nil {
 | 
								if _, err := url.ParseRequestURI(query.Subs[i]); err != nil {
 | 
				
			||||||
				return SubQuery{}, errors.New("参数错误: " + err.Error())
 | 
									return SubValidator{}, errors.New("参数错误: " + err.Error())
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
@@ -67,7 +70,7 @@ func ParseQuery(c *gin.Context) (SubQuery, error) {
 | 
				
			|||||||
		uri, err := url.ParseRequestURI(query.Template)
 | 
							uri, err := url.ParseRequestURI(query.Template)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			if strings.Contains(query.Template, string(os.PathSeparator)) {
 | 
								if strings.Contains(query.Template, string(os.PathSeparator)) {
 | 
				
			||||||
				return SubQuery{}, err
 | 
									return SubValidator{}, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		query.Template = uri.String()
 | 
							query.Template = uri.String()
 | 
				
			||||||
@@ -79,16 +82,16 @@ func ParseQuery(c *gin.Context) (SubQuery, error) {
 | 
				
			|||||||
			length := len(ruleProviders)
 | 
								length := len(ruleProviders)
 | 
				
			||||||
			parts := strings.Split(ruleProviders[length-i-1][1], ",")
 | 
								parts := strings.Split(ruleProviders[length-i-1][1], ",")
 | 
				
			||||||
			if len(parts) < 4 {
 | 
								if len(parts) < 4 {
 | 
				
			||||||
				return SubQuery{}, errors.New("参数错误: ruleProvider 格式错误")
 | 
									return SubValidator{}, errors.New("参数错误: ruleProvider 格式错误")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			u := parts[1]
 | 
								u := parts[1]
 | 
				
			||||||
			uri, err := url.ParseRequestURI(u)
 | 
								uri, err := url.ParseRequestURI(u)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return SubQuery{}, errors.New("参数错误: " + err.Error())
 | 
									return SubValidator{}, errors.New("参数错误: " + err.Error())
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			u = uri.String()
 | 
								u = uri.String()
 | 
				
			||||||
			if len(parts) == 4 {
 | 
								if len(parts) == 4 {
 | 
				
			||||||
				hash := md5.Sum([]byte(u))
 | 
									hash := sha256.Sum224([]byte(u))
 | 
				
			||||||
				parts = append(parts, hex.EncodeToString(hash[:]))
 | 
									parts = append(parts, hex.EncodeToString(hash[:]))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			query.RuleProviders = append(
 | 
								query.RuleProviders = append(
 | 
				
			||||||
@@ -105,7 +108,7 @@ func ParseQuery(c *gin.Context) (SubQuery, error) {
 | 
				
			|||||||
		names := make(map[string]bool)
 | 
							names := make(map[string]bool)
 | 
				
			||||||
		for _, ruleProvider := range query.RuleProviders {
 | 
							for _, ruleProvider := range query.RuleProviders {
 | 
				
			||||||
			if _, ok := names[ruleProvider.Name]; ok {
 | 
								if _, ok := names[ruleProvider.Name]; ok {
 | 
				
			||||||
				return SubQuery{}, errors.New("参数错误: Rule-Provider 名称重复")
 | 
									return SubValidator{}, errors.New("参数错误: Rule-Provider 名称重复")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			names[ruleProvider.Name] = true
 | 
								names[ruleProvider.Name] = true
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user