mirror of
https://github.com/ayflying/p2p.git
synced 2026-03-05 01:39:23 +00:00
增加上传与接收文件
This commit is contained in:
15
api/system/system.go
Normal file
15
api/system/system.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// =================================================================================
|
||||||
|
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||||
|
// =================================================================================
|
||||||
|
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/ayflying/p2p/api/system/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ISystemV1 interface {
|
||||||
|
Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)
|
||||||
|
}
|
||||||
11
api/system/v1/update.go
Normal file
11
api/system/v1/update.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package v1
|
||||||
|
|
||||||
|
import "github.com/gogf/gf/v2/frame/g"
|
||||||
|
|
||||||
|
type UpdateReq struct {
|
||||||
|
g.Meta `path:"/system/update" tags:"system" method:"get" sm:"更新服务端"`
|
||||||
|
Url string `json:"url" dc:"更新地址"`
|
||||||
|
Version string `json:"version" dc:"当前版本"`
|
||||||
|
}
|
||||||
|
type UpdateRes struct {
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ gfcli:
|
|||||||
arch: "amd64"
|
arch: "amd64"
|
||||||
system: "windows,linux"
|
system: "windows,linux"
|
||||||
mod: "none"
|
mod: "none"
|
||||||
packSrc: "resource,manifest"
|
packSrc: "hack,resource,manifest"
|
||||||
version: "v1.0.0"
|
version: "v1.0.0"
|
||||||
path: "./bin"
|
path: "./bin"
|
||||||
# extra: -trimpath -ldflags="-s -w -H=windowsgui"
|
# extra: -trimpath -ldflags="-s -w -H=windowsgui"
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ayflying/p2p/internal/consts"
|
||||||
"github.com/ayflying/p2p/internal/controller/p2p"
|
"github.com/ayflying/p2p/internal/controller/p2p"
|
||||||
|
"github.com/ayflying/p2p/internal/controller/system"
|
||||||
"github.com/ayflying/p2p/internal/service"
|
"github.com/ayflying/p2p/internal/service"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
@@ -22,14 +24,16 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
s = g.Server()
|
|
||||||
|
|
||||||
Main = gcmd.Command{
|
Main = gcmd.Command{
|
||||||
Name: "main",
|
Name: "main",
|
||||||
Usage: "main",
|
Usage: "main",
|
||||||
Brief: "start http server",
|
Brief: "start http server",
|
||||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||||
g.Log().Debug(ctx, "开始执行main")
|
g.Log().Debug(ctx, "开始执行main")
|
||||||
|
Version, err := g.Cfg("hack").Get(ctx, "gfcli.build.version")
|
||||||
|
g.Log().Debugf(ctx, "当前启动的版本为:%v", Version)
|
||||||
|
|
||||||
|
s := g.Server(consts.Name)
|
||||||
|
|
||||||
parser, err = gcmd.Parse(g.MapStrBool{
|
parser, err = gcmd.Parse(g.MapStrBool{
|
||||||
"p,port": true,
|
"p,port": true,
|
||||||
@@ -49,6 +53,7 @@ var (
|
|||||||
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
||||||
group.Bind(
|
group.Bind(
|
||||||
p2p.NewV1(),
|
p2p.NewV1(),
|
||||||
|
system.NewV1(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -79,6 +84,9 @@ var (
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 启动系统托盘
|
||||||
|
service.OS().Load(consts.Name, consts.Name+"服务端", "manifest/images/favicon.ico")
|
||||||
|
|
||||||
s.Run()
|
s.Run()
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ayflying/p2p/internal/consts"
|
||||||
"github.com/ayflying/p2p/internal/service"
|
"github.com/ayflying/p2p/internal/service"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gcmd"
|
"github.com/gogf/gf/v2/os/gcmd"
|
||||||
@@ -25,6 +26,7 @@ var (
|
|||||||
// Func 为命令的执行函数,接收上下文和参数解析器
|
// Func 为命令的执行函数,接收上下文和参数解析器
|
||||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||||
g.Log().Debug(ctx, "开始执行dht")
|
g.Log().Debug(ctx, "开始执行dht")
|
||||||
|
s := g.Server(consts.Name)
|
||||||
|
|
||||||
parser, err = gcmd.Parse(g.MapStrBool{
|
parser, err = gcmd.Parse(g.MapStrBool{
|
||||||
"p,port": true,
|
"p,port": true,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
systemV1 "github.com/ayflying/p2p/api/system/v1"
|
||||||
"github.com/ayflying/p2p/internal/service"
|
"github.com/ayflying/p2p/internal/service"
|
||||||
"github.com/gogf/gf/v2/crypto/gsha1"
|
"github.com/gogf/gf/v2/crypto/gsha1"
|
||||||
"github.com/gogf/gf/v2/encoding/gcompress"
|
"github.com/gogf/gf/v2/encoding/gcompress"
|
||||||
@@ -45,8 +46,8 @@ var (
|
|||||||
var filePath = path.Join(pathMain, version, platform, name)
|
var filePath = path.Join(pathMain, version, platform, name)
|
||||||
dirList, _ := gfile.ScanDir(path.Join(pathMain, version), "*", false)
|
dirList, _ := gfile.ScanDir(path.Join(pathMain, version), "*", false)
|
||||||
for _, v := range dirList {
|
for _, v := range dirList {
|
||||||
updateFilename := gfile.Name(v)
|
updatePlatform := gfile.Name(v)
|
||||||
updateFilePath := path.Join("server_update", name, version, updateFilename)
|
updateFilePath := path.Join("server_update", name, version, updatePlatform)
|
||||||
|
|
||||||
var obj bytes.Buffer
|
var obj bytes.Buffer
|
||||||
g.Log().Debugf(ctx, "读取目录成功:%v", v)
|
g.Log().Debugf(ctx, "读取目录成功:%v", v)
|
||||||
@@ -54,14 +55,14 @@ var (
|
|||||||
g.Log().Debugf(ctx, "判断当前文件是否存在:%v", fileMian)
|
g.Log().Debugf(ctx, "判断当前文件是否存在:%v", fileMian)
|
||||||
if gfile.IsFile(fileMian) {
|
if gfile.IsFile(fileMian) {
|
||||||
// 写入文件哈希
|
// 写入文件哈希
|
||||||
versionFile[updateFilePath+".gz"] = gsha1.MustEncryptFile(fileMian)
|
versionFile[updatePlatform] = gsha1.MustEncryptFile(fileMian)
|
||||||
err = gcompress.GzipPathWriter(fileMian, &obj)
|
err = gcompress.GzipPathWriter(fileMian, &obj)
|
||||||
service.S3().PutObject(ctx, &obj, updateFilePath+".gz")
|
service.S3().PutObject(ctx, &obj, updateFilePath+".gz")
|
||||||
g.Log().Debugf(ctx, "成功上传文件到:%v", updateFilePath+".gz")
|
g.Log().Debugf(ctx, "成功上传文件到:%v", updateFilePath+".gz")
|
||||||
}
|
}
|
||||||
if gfile.IsFile(fileMian + ".exe") {
|
if gfile.IsFile(fileMian + ".exe") {
|
||||||
// 写入文件哈希
|
// 写入文件哈希
|
||||||
versionFile[updateFilePath+".gz"] = gsha1.MustEncryptFile(fileMian + ".exe")
|
versionFile[updatePlatform] = gsha1.MustEncryptFile(fileMian + ".exe")
|
||||||
err = gcompress.GzipPathWriter(fileMian+".exe", &obj)
|
err = gcompress.GzipPathWriter(fileMian+".exe", &obj)
|
||||||
service.S3().PutObject(ctx, &obj, updateFilePath+".gz")
|
service.S3().PutObject(ctx, &obj, updateFilePath+".gz")
|
||||||
g.Log().Debugf(ctx, "成功上传文件到:%v", updateFilePath+".gz")
|
g.Log().Debugf(ctx, "成功上传文件到:%v", updateFilePath+".gz")
|
||||||
@@ -69,13 +70,38 @@ var (
|
|||||||
|
|
||||||
// 写入文件版本文件
|
// 写入文件版本文件
|
||||||
fileByte := gjson.MustEncode(versionFile)
|
fileByte := gjson.MustEncode(versionFile)
|
||||||
service.S3().PutObject(ctx, bytes.NewReader(fileByte), path.Join("server_update", name, version, "version.json"))
|
service.S3().PutObject(ctx, bytes.NewReader(fileByte), path.Join("server_update", name, "version.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Error(ctx, err)
|
g.Log().Error(ctx, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Log().Debugf(ctx, "当前获取到的地址为:%v", filePath)
|
g.Log().Debugf(ctx, "当前获取到的地址为:%v", filePath)
|
||||||
|
|
||||||
|
versionUrl := service.S3().GetCdnUrl(path.Join("server_update", name, "version.json"))
|
||||||
|
listVar := g.Cfg().MustGet(ctx, "p2p.list")
|
||||||
|
var p2pItem []struct {
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
SSL bool `json:"ssl"`
|
||||||
|
Ws string `json:"ws"`
|
||||||
|
}
|
||||||
|
listVar.Scan(&p2pItem)
|
||||||
|
for _, v := range p2pItem {
|
||||||
|
|
||||||
|
url := "http"
|
||||||
|
if v.SSL == true {
|
||||||
|
url = "https"
|
||||||
|
}
|
||||||
|
url = fmt.Sprintf("%s://%s:%d/system/update", url, v.Host, v.Port)
|
||||||
|
|
||||||
|
g.Log().Debugf(ctx, "开始上传到服务器:%v,file=%v", url, versionUrl)
|
||||||
|
_, err := g.Client().Get(ctx, url, systemV1.UpdateReq{
|
||||||
|
Url: versionUrl,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Error(ctx, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}}
|
}}
|
||||||
)
|
)
|
||||||
|
|||||||
5
internal/controller/system/system.go
Normal file
5
internal/controller/system/system.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
// =================================================================================
|
||||||
|
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
|
||||||
|
// =================================================================================
|
||||||
|
|
||||||
|
package system
|
||||||
15
internal/controller/system/system_new.go
Normal file
15
internal/controller/system/system_new.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// =================================================================================
|
||||||
|
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
|
||||||
|
// =================================================================================
|
||||||
|
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ayflying/p2p/api/system"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ControllerV1 struct{}
|
||||||
|
|
||||||
|
func NewV1() system.ISystemV1 {
|
||||||
|
return &ControllerV1{}
|
||||||
|
}
|
||||||
74
internal/controller/system/system_v1_update.go
Normal file
74
internal/controller/system/system_v1_update.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ayflying/p2p/api/system/v1"
|
||||||
|
"github.com/gogf/gf/v2/crypto/gsha1"
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/os/gcmd"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
|
||||||
|
|
||||||
|
getRunFile := gcmd.GetArg(0).String()
|
||||||
|
|
||||||
|
fileSha, err := gsha1.EncryptFile(getRunFile)
|
||||||
|
g.Dump(fileSha)
|
||||||
|
g.Dump(getRunFile)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Println("5秒后开始重启...")
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
|
if err = restartSelf(); err != nil {
|
||||||
|
log.Fatalf("重启失败:%v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// restartSelf 实现 Windows 平台下的程序自重启
|
||||||
|
func restartSelf() error {
|
||||||
|
// 1. 获取当前程序的绝对路径
|
||||||
|
exePath, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// 处理路径中的符号链接(确保路径正确)
|
||||||
|
exePath, err = filepath.EvalSymlinks(exePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 获取命令行参数(os.Args[0] 是程序名,实际参数从 os.Args[1:] 开始)
|
||||||
|
args := os.Args[1:]
|
||||||
|
|
||||||
|
// 3. 构建新进程命令(路径为当前程序,参数为原参数)
|
||||||
|
cmd := exec.Command(exePath, args...)
|
||||||
|
// 设置新进程的工作目录与当前进程一致
|
||||||
|
cmd.Dir, err = os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新进程的输出继承当前进程的标准输出(可选,根据需求调整)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
|
||||||
|
// 4. 启动新进程(非阻塞,Start() 后立即返回)
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 新进程启动成功后,退出当前进程
|
||||||
|
os.Exit(0)
|
||||||
|
return nil // 理论上不会执行到这里
|
||||||
|
}
|
||||||
@@ -242,6 +242,12 @@ func (s *sS3) GetPath(url string) (filePath string) {
|
|||||||
return url[len(get+bucketName)+1:]
|
return url[len(get+bucketName)+1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCdnUrl 通过文件名,获取直连地址
|
||||||
|
func (s *sS3) GetCdnUrl(file string) string {
|
||||||
|
urlStr, _ := url.JoinPath(s.cfg.Url, file)
|
||||||
|
return urlStr
|
||||||
|
}
|
||||||
|
|
||||||
// CopyObject 在指定存储桶内复制文件
|
// CopyObject 在指定存储桶内复制文件
|
||||||
// bucketName 存储桶名称
|
// bucketName 存储桶名称
|
||||||
// dstStr 目标文件路径
|
// dstStr 目标文件路径
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ type (
|
|||||||
GetUrl(filePath string, defaultFile ...string) (url string)
|
GetUrl(filePath string, defaultFile ...string) (url string)
|
||||||
// GetPath 从文件访问 URL 中提取文件路径
|
// GetPath 从文件访问 URL 中提取文件路径
|
||||||
GetPath(url string) (filePath string)
|
GetPath(url string) (filePath string)
|
||||||
|
// GetCdnUrl 通过文件名,获取直连地址
|
||||||
|
GetCdnUrl(file string) string
|
||||||
// CopyObject 在指定存储桶内复制文件
|
// CopyObject 在指定存储桶内复制文件
|
||||||
// bucketName 存储桶名称
|
// bucketName 存储桶名称
|
||||||
// dstStr 目标文件路径
|
// dstStr 目标文件路径
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -1,10 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ayflying/p2p/internal/consts"
|
|
||||||
_ "github.com/ayflying/p2p/internal/logic"
|
_ "github.com/ayflying/p2p/internal/logic"
|
||||||
_ "github.com/ayflying/p2p/internal/packed"
|
_ "github.com/ayflying/p2p/internal/packed"
|
||||||
"github.com/ayflying/p2p/internal/service"
|
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
|
|
||||||
//步骤1:加载驱动
|
//步骤1:加载驱动
|
||||||
@@ -19,9 +17,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 启动系统托盘
|
|
||||||
service.OS().Load(consts.Name, consts.Name+"服务端", "manifest/images/favicon.ico")
|
|
||||||
|
|
||||||
if ok := gfile.Exists("runtime"); !ok {
|
if ok := gfile.Exists("runtime"); !ok {
|
||||||
gfile.Mkdir("runtime")
|
gfile.Mkdir("runtime")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user