feat: 增加短链生成

This commit is contained in:
2023-09-17 15:52:37 +08:00
parent 38dbea4a2a
commit 6b08b2cb86
14 changed files with 206 additions and 34 deletions

View 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
View 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)
}

View File

@ -1,7 +1,7 @@
package utils
import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
@ -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 {
@ -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 {