init
This commit is contained in:
62
core/crontab.go
Normal file
62
core/crontab.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robfig/cron"
|
||||
)
|
||||
|
||||
var CronInfo CronServiceInfo
|
||||
|
||||
type CronServiceInfo struct {
|
||||
Cron []string
|
||||
Func []func()
|
||||
}
|
||||
|
||||
func (c *CronServiceInfo) Register(cron string, f func()) {
|
||||
c.Cron = append(c.Cron, cron)
|
||||
c.Func = append(c.Func, f)
|
||||
}
|
||||
|
||||
func CronRun() {
|
||||
c := cron.New()
|
||||
for k, v := range CronInfo.Cron {
|
||||
err := c.AddFunc(v, wrap(CronInfo.Func[k]))
|
||||
if err != nil {
|
||||
fmt.Println("定时任务添加失败")
|
||||
}
|
||||
}
|
||||
c.Start()
|
||||
}
|
||||
|
||||
func wrap(f func()) func() {
|
||||
return func() {
|
||||
//defer CronRecovery(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name())
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
func CronRegister(rule string, f func()) {
|
||||
CronInfo.Register(rule, f)
|
||||
}
|
||||
|
||||
//func CronRecovery(name string){
|
||||
// if err := recover(); err != nil {
|
||||
// var code int
|
||||
// var msg interface{}
|
||||
// if h, ok := err.(*Exception); ok {
|
||||
// code = h.Code
|
||||
// msg = h.Msg
|
||||
// } else if _, ok = err.(error); ok {
|
||||
// msg = "未知错误"
|
||||
// code = UNKNOW_ERROR
|
||||
// } else {
|
||||
// msg = "服务器错误"
|
||||
// code = SERVER_ERROR
|
||||
// }
|
||||
// Log.WithFields(map[string]interface{}{
|
||||
// "code":code,
|
||||
// "model":"task",
|
||||
// "func":name,
|
||||
// }).Error(msg)
|
||||
// }
|
||||
//}
|
||||
48
core/file.go
Normal file
48
core/file.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func IsFileExist(filename string) bool {
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func EnsureFileExists(filename string) (bool, error) {
|
||||
// 检查文件是否存在
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
// 文件不存在,创建文件
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer file.Close()
|
||||
return true, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
func WriteFile(filename, content string) (bool, error) {
|
||||
// 打开文件以覆盖写入数据
|
||||
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer file.Close()
|
||||
// 将数据写入文件
|
||||
_, err = file.WriteString(content)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, err
|
||||
}
|
||||
|
||||
func ReadFile(filename string) (string, error) {
|
||||
// 读取整个文件内容
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(content), nil
|
||||
}
|
||||
92
core/fmm.go
Normal file
92
core/fmm.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UpdateFmmIpv6M3u() {
|
||||
filename := "./ipv6.m3u"
|
||||
// 确保文件存在
|
||||
if exists, err := EnsureFileExists(filename); err != nil {
|
||||
fmt.Printf("检查或创建文件ipv6.m3u失败:%s\n", err.Error())
|
||||
return
|
||||
} else if !exists {
|
||||
fmt.Printf("缓存文件ipv6.m3u不存在\n")
|
||||
return
|
||||
}
|
||||
|
||||
URL := "https://mirror.ghproxy.com/https://raw.githubusercontent.com/fanmingming/live/main/tv/m3u/ipv6.m3u"
|
||||
statusCode, responseBody, err := FetchURL(URL, 10*time.Second)
|
||||
if err != nil {
|
||||
fmt.Printf("HTTP请求失败:%s\n", err.Error())
|
||||
return
|
||||
}
|
||||
if statusCode != http.StatusOK {
|
||||
fmt.Printf("HTTP状态码为:%d\n", statusCode)
|
||||
return
|
||||
}
|
||||
|
||||
fileContent, err := ReadFile(filename)
|
||||
if err != nil {
|
||||
fmt.Printf("读取本地文件ipv6.m3u失败:%s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if Md5(fileContent) != Md5(responseBody) {
|
||||
file, err := WriteFile(filename, responseBody)
|
||||
if err != nil {
|
||||
fmt.Printf("写入文件ipv6.m3u失败:%s\n", err.Error())
|
||||
return
|
||||
}
|
||||
if !file {
|
||||
fmt.Printf("写入文件ipv6.m3u失败\n")
|
||||
return
|
||||
}
|
||||
fmt.Printf("ipv6.m3u本地更新云端数据成功\n")
|
||||
} else {
|
||||
fmt.Printf("ipv6.m3u本地与云端数据一致无需同步\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func UpdateFmmIpv6ToTxtM3u() {
|
||||
ipv6TxtFilename := "./ipv6-txt.m3u"
|
||||
if exists, err := EnsureFileExists(ipv6TxtFilename); err != nil {
|
||||
fmt.Printf("检查或创建ipv6-txt.m3u文件失败:%s\n", err.Error())
|
||||
return
|
||||
} else if !exists {
|
||||
fmt.Printf("缓存文件ipv6-txt.m3u不存在\n")
|
||||
return
|
||||
}
|
||||
URL := "https://fanmingming.com/txt?url=http://82.156.99.49:6677"
|
||||
statusCode, responseBody, err := FetchURL(URL+"/ipv6-iptv.m3u", 10*time.Second)
|
||||
if err != nil {
|
||||
fmt.Printf("HTTP请求失败:%s\n", err.Error())
|
||||
return
|
||||
}
|
||||
if statusCode != http.StatusOK {
|
||||
fmt.Printf("HTTP状态码为:%d\n", statusCode)
|
||||
return
|
||||
}
|
||||
fileContent, err := ReadFile(ipv6TxtFilename)
|
||||
if err != nil {
|
||||
fmt.Printf("读取本地文件ipv6-txt.m3u失败:%s\n", err.Error())
|
||||
return
|
||||
}
|
||||
if Md5(fileContent) != Md5(responseBody) {
|
||||
file, err := WriteFile(ipv6TxtFilename, responseBody)
|
||||
if err != nil {
|
||||
fmt.Printf("写入文件ipv6-txt.m3u失败:%s\n", err.Error())
|
||||
return
|
||||
}
|
||||
if !file {
|
||||
fmt.Printf("写入文件ipv6-txt.m3u失败\n")
|
||||
return
|
||||
}
|
||||
fmt.Printf("ipv6-txt.m3u本地更新云端数据成功\n")
|
||||
} else {
|
||||
fmt.Printf("ipv6-txt.m3u本地与云端数据一致无需同步\n")
|
||||
}
|
||||
}
|
||||
43
core/gin_handler.go
Normal file
43
core/gin_handler.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func HandlerIpv6M3u(c *gin.Context) {
|
||||
filename := "./ipv6.m3u"
|
||||
// 检查缓存文件是否存在
|
||||
if exists := IsFileExist(filename); !exists {
|
||||
c.JSON(200, gin.H{"code": -1, "msg": "缓存文件不存在"})
|
||||
return
|
||||
}
|
||||
content, err := ReadFile(filename)
|
||||
if err != nil {
|
||||
fmt.Printf("读取缓存文件ipv6.m3u失败:%s\n", err.Error())
|
||||
c.JSON(200, gin.H{"code": -1, "msg": "读取缓存文件失败"})
|
||||
return
|
||||
}
|
||||
// 返回响应内容
|
||||
c.Header("Content-Type", "text/plain; charset=utf-8")
|
||||
c.String(200, content)
|
||||
}
|
||||
|
||||
func HandlerDiypIpv6IptvM3u(c *gin.Context) {
|
||||
filename := "./ipv6-txt.m3u"
|
||||
// 检查缓存文件是否存在
|
||||
if exists := IsFileExist(filename); !exists {
|
||||
c.JSON(200, gin.H{"code": -1, "msg": "缓存文件不存在"})
|
||||
return
|
||||
}
|
||||
content, err := ReadFile(filename)
|
||||
if err != nil {
|
||||
fmt.Printf("读取缓存文件ipv6-txt.m3u失败:%s\n", err.Error())
|
||||
c.JSON(200, gin.H{"code": -1, "msg": "读取缓存文件失败"})
|
||||
return
|
||||
}
|
||||
// 返回响应内容
|
||||
c.Header("Content-Type", "text/plain; charset=utf-8")
|
||||
c.String(200, content)
|
||||
|
||||
}
|
||||
35
core/http.go
Normal file
35
core/http.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FetchURL 执行 HTTP GET 请求并返回响应状态码和响应体
|
||||
func FetchURL(url string, timeout time.Duration) (int, string, error) {
|
||||
// 创建自定义 HTTP 客户端
|
||||
client := &http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
// 创建请求
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("failed to make GET request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应内容
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp.StatusCode, "", fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
return resp.StatusCode, string(body), nil
|
||||
}
|
||||
12
core/md5.go
Normal file
12
core/md5.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func Md5(str string) string {
|
||||
_md5 := md5.New()
|
||||
_md5.Write([]byte(str))
|
||||
return hex.EncodeToString(_md5.Sum([]byte("")))
|
||||
}
|
||||
Reference in New Issue
Block a user