This commit is contained in:
mubai
2023-12-23 22:32:52 +08:00
parent d85dbe915c
commit b48e53a637
151 changed files with 12451 additions and 1382 deletions

View File

@@ -2,8 +2,10 @@ package util
import (
"bufio"
"errors"
"os"
"path/filepath"
"server/config"
)
/*
@@ -11,11 +13,16 @@ import (
*/
// SaveOnlineFile 保存网络文件, 提供下载url和保存路径, 返回保存后的文件访问url相对路径
func SaveOnlineFile(url, dir string) (err error) {
func SaveOnlineFile(url, dir string) (path string, err error) {
// 请求获取文件内容
r := &RequestInfo{Uri: url}
ApiGet(r)
// 创建保存文件的目录
// 如果请求结果为空则直接跳过当前图片的同步, 等待后续触发时重试
if len(r.Resp) <= 0 {
err = errors.New("SyncPicture Failed: response is empty")
return
}
// 成功拿到图片数据 则创建保存文件的目录
if _, err = os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
@@ -29,11 +36,18 @@ func SaveOnlineFile(url, dir string) (err error) {
return
}
defer file.Close()
//_, _ = file.Write(r.Resp)
// 将文件内容写入到file
writer := bufio.NewWriter(file)
_, err = writer.Write(r.Resp)
err = writer.Flush()
return
return filepath.Base(fileName), err
}
func CreateBaseDir() error {
// 如果不存在指定目录则创建该目录
if _, err := os.Stat(config.FilmPictureUploadDir); os.IsNotExist(err) {
return os.MkdirAll(config.FilmPictureUploadDir, os.ModePerm)
}
return nil
}

View File

@@ -36,7 +36,7 @@ func CreateClient() *colly.Collector {
c := colly.NewCollector()
// 设置请求使用clash的socks5代理
setProxy(c)
//setProxy(c)
// 设置代理信息
//if proxy, err := proxy.RoundRobinProxySwitcher("127.0.0.1:7890"); err != nil {
@@ -81,8 +81,8 @@ func ApiGet(r *RequestInfo) {
//extensions.Referer(Client)
// 请求成功后的响应
Client.OnResponse(func(response *colly.Response) {
// 将响应结构封装到 RequestInfo.Resp中
if len(response.Body) > 0 {
if (response.StatusCode == 200 || (response.StatusCode >= 300 && response.StatusCode <= 399)) && len(response.Body) > 0 {
// 将响应结构封装到 RequestInfo.Resp中
r.Resp = response.Body
} else {
r.Resp = []byte{}
@@ -99,6 +99,24 @@ func ApiGet(r *RequestInfo) {
}
}
// ApiTest 处理API请求后的数据, 主测试
func ApiTest(r *RequestInfo) error {
// 请求成功后的响应
Client.OnResponse(func(response *colly.Response) {
// 判断请求状态
if (response.StatusCode == 200 || (response.StatusCode >= 300 && response.StatusCode <= 399)) && len(response.Body) > 0 {
// 将响应结构封装到 RequestInfo.Resp中
r.Resp = response.Body
} else {
r.Resp = []byte{}
}
})
// 执行请求返回错误结果
err := Client.Visit(fmt.Sprintf("%s?%s", r.Uri, r.Params.Encode()))
log.Println(err)
return err
}
// 本地代理测试
func setProxy(c *colly.Collector) {
proxyUrl, _ := url.Parse("socks5://127.0.0.1:7890")

View File

@@ -0,0 +1,126 @@
package util
import (
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
"log"
"net/url"
"regexp"
)
// GenerateUUID 生成UUID
func GenerateUUID() (uuid string) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
uuid = fmt.Sprintf("%X-%X-%X-%X-%X",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
// RandomString 生成指定长度两倍的随机字符串
func RandomString(length int) (uuid string) {
b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
uuid = fmt.Sprintf("%x", b)
return
}
// GenerateSalt 生成 length为16的随机字符串
func GenerateSalt() (uuid string) {
b := make([]byte, 8)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
uuid = fmt.Sprintf("%X", b)
return
}
// PasswordEncrypt 密码加密 , (password+salt) md5 * 3
func PasswordEncrypt(password, salt string) string {
b := []byte(fmt.Sprint(password, salt)) // 将字符串转换为字节切片
var r [16]byte
for i := 0; i < 3; i++ {
r = md5.Sum(b) // 调用md5.Sum()函数进行加密
b = []byte(hex.EncodeToString(r[:]))
}
return hex.EncodeToString(r[:])
}
// ParsePriKeyBytes 解析私钥
func ParsePriKeyBytes(buf []byte) (*rsa.PrivateKey, error) {
p := &pem.Block{}
p, buf = pem.Decode(buf)
if p == nil {
return nil, errors.New("private key parse error")
}
return x509.ParsePKCS1PrivateKey(p.Bytes)
}
// ParsePubKeyBytes 解析公钥
func ParsePubKeyBytes(buf []byte) (*rsa.PublicKey, error) {
p, _ := pem.Decode(buf)
if p == nil {
return nil, errors.New("parse publicKey content nil")
}
pubKey, err := x509.ParsePKCS1PublicKey(p.Bytes)
if err != nil {
return nil, errors.New("x509.ParsePKCS1PublicKey error")
}
return pubKey, nil
}
// ValidDomain 域名校验(http://example.xxx)
func ValidDomain(s string) bool {
return regexp.MustCompile(`^(http|https)://[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?$`).MatchString(s)
}
// ValidIPHost 校验是否符合http|https//ip 格式
func ValidIPHost(s string) bool {
return regexp.MustCompile(`^(http|https)://(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:[0-9]{1,5})?$`).MatchString(s)
}
// ValidURL 校验http链接是否是符合规范的URL
func ValidURL(s string) bool {
_, err := url.ParseRequestURI(s)
if err != nil {
return false
}
return true
}
func ValidPwd(s string) error {
if len(s) < 8 || len(s) > 12 {
return fmt.Errorf("密码长度不符合规范, 必须为8-10位")
}
// 分别校验数字 大小写字母和特殊字符
num := `[0-9]{1}`
l := `[a-z]{1}`
u := `[A-Z]{1}`
symbol := `[!@#~$%^&*()+|_]{1}`
if b, err := regexp.MatchString(num, s); !b || err != nil {
return errors.New("密码必须包含数字 ")
}
if b, err := regexp.MatchString(l, s); !b || err != nil {
return errors.New("密码必须包含小写字母")
}
if b, err := regexp.MatchString(u, s); !b || err != nil {
return errors.New("密码必须包含大写字母")
}
if b, err := regexp.MatchString(symbol, s); !b || err != nil {
return errors.New("密码必须包含特殊字")
}
return nil
}