mirror of
https://github.com/ayflying/p2p.git
synced 2026-03-05 01:39:23 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 63f5bef038 | |||
| f90cf8b855 | |||
| a0f367efeb | |||
| ceb44e936f | |||
| 49c84b886b | |||
| 607af816c4 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -16,7 +16,6 @@ manifest/output/
|
||||
temp/
|
||||
temp.yaml
|
||||
bin
|
||||
**/config/config.yaml
|
||||
v1.0.0/
|
||||
config/local.yaml
|
||||
main.exe~
|
||||
|
||||
@@ -456,7 +456,7 @@ func (s *sP2P) receiveGatewayMessages(ctx context.Context) {
|
||||
|
||||
g.Log().Info(ctx, "文件接收完成")
|
||||
// 开始覆盖文件与重启
|
||||
err = service.System().Update(ctx)
|
||||
err = service.System().Update(ctx, "")
|
||||
|
||||
//// 调用不同系统的更新服务
|
||||
//service.OS().Update(msgData.Version, msgData.Server)
|
||||
|
||||
@@ -188,9 +188,10 @@ func (s *sP2P) removeDuplicates(strs []string) []string {
|
||||
return result
|
||||
}
|
||||
|
||||
const privKeyPath = "runtime/p2p.key"
|
||||
|
||||
// 生成固定密钥(核心:通过固定种子生成相同密钥)
|
||||
func (s *sP2P) generateFixedKey() (crypto.PrivKey, error) {
|
||||
privKeyPath := "runtime/message.key"
|
||||
if ok := gfile.Exists(privKeyPath); ok {
|
||||
// 从文件读取密钥
|
||||
keyBytes := gfile.GetBytes(privKeyPath)
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ayflying/p2p/internal/service"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcron"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
@@ -17,10 +20,21 @@ func init() {
|
||||
|
||||
getDev, _ := g.Cfg().GetWithEnv(gctx.New(), "dev")
|
||||
if !getDev.Bool() {
|
||||
service.System().CheckUpdate()
|
||||
// 每天0点检查更新
|
||||
gcron.Add(gctx.New(), "0 0 0 * * *", func(ctx context.Context) {
|
||||
err := service.System().CheckUpdate()
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "检查更新失败:%v", err)
|
||||
}
|
||||
})
|
||||
|
||||
err := service.System().CheckUpdate()
|
||||
if err != nil {
|
||||
g.Log().Errorf(gctx.New(), "检查更新失败:%v", err)
|
||||
}
|
||||
} else {
|
||||
g.Log().Debugf(gctx.New(), "开发模式,不检查更新")
|
||||
}
|
||||
}
|
||||
|
||||
func (system *sSystem) Init() {}
|
||||
func (s *sSystem) Init() {}
|
||||
|
||||
@@ -17,12 +17,15 @@ import (
|
||||
"github.com/ayflying/p2p/internal/service"
|
||||
"github.com/gogf/gf/v2/encoding/gcompress"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
// 本地版本号(建议从编译参数注入,如 -ldflags "-X main.version=v0.1.3")
|
||||
const versionFile = "version.txt"
|
||||
|
||||
var localVersion = "v0.0.0"
|
||||
|
||||
// 对应 GitHub API 响应的核心字段(按需精简)
|
||||
@@ -48,15 +51,30 @@ type GitHubRelease struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
func (s *sSystem) Update(ctx context.Context) (err error) {
|
||||
func (s *sSystem) Update(ctx context.Context, gzFile string) (err error) {
|
||||
//拼接操作系统和架构(格式:OS_ARCH)
|
||||
platform := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
|
||||
|
||||
runFile := gcmd.GetArg(0).String()
|
||||
oldFile, err := service.System().RenameRunningFile(runFile)
|
||||
g.Log().Debugf(ctx, "执行文件改名为%v", oldFile)
|
||||
gz := path.Join("download", platform+".gz")
|
||||
err = gcompress.UnGzipFile(gz, runFile)
|
||||
if gzFile == "" {
|
||||
gzFile = path.Join("download", platform+".gz")
|
||||
}
|
||||
//结束后删除压缩包
|
||||
defer gfile.RemoveFile(gzFile)
|
||||
|
||||
ext := gfile.Ext(gzFile)
|
||||
if ext == ".zip" {
|
||||
g.Log().Debugf(ctx, "zip解压%v到%v", gzFile, gfile.Dir(runFile))
|
||||
err = gcompress.UnZipFile(gzFile, gfile.Dir(runFile))
|
||||
} else {
|
||||
g.Log().Debugf(ctx, "gzip解压%v到%v", gzFile, runFile)
|
||||
err = gcompress.UnGzipFile(gzFile, runFile)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Println("5秒后开始重启...")
|
||||
@@ -71,6 +89,16 @@ func (s *sSystem) Update(ctx context.Context) (err error) {
|
||||
|
||||
// RestartSelf 实现 Windows 平台下的程序自重启
|
||||
func (s *sSystem) RestartSelf() error {
|
||||
ctx := gctx.New()
|
||||
// 判断是否为linux平台
|
||||
if runtime.GOOS == "linux" {
|
||||
err := ghttp.RestartAllServer(ctx, os.Args[0])
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "重启失败:%v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 1. 获取当前程序的绝对路径
|
||||
exePath, err := os.Executable()
|
||||
if err != nil {
|
||||
@@ -166,7 +194,7 @@ func (s *sSystem) getLatestVersion() (string, []*Assets, error) {
|
||||
return release.TagName, release.Assets, nil
|
||||
}
|
||||
|
||||
func (s *sSystem) CheckUpdate() {
|
||||
func (s *sSystem) CheckUpdate() (err error) {
|
||||
ctx := gctx.New()
|
||||
latestVersion, assets, err := s.getLatestVersion()
|
||||
if err != nil {
|
||||
@@ -174,7 +202,7 @@ func (s *sSystem) CheckUpdate() {
|
||||
return
|
||||
}
|
||||
|
||||
localVersion = gfile.GetContents("download/version.txt")
|
||||
localVersion = gfile.GetContents(versionFile)
|
||||
|
||||
if s.isNewVersion(localVersion, latestVersion) {
|
||||
g.Log().Printf(ctx, "发现新版本:%s(当前版本:%s)", latestVersion, localVersion)
|
||||
@@ -186,19 +214,25 @@ func (s *sSystem) CheckUpdate() {
|
||||
if strings.Contains(fmt.Sprintf("_%s.", asset.Name), platform) {
|
||||
fmt.Printf("- %s\n", asset.BrowserDownloadUrl)
|
||||
|
||||
// 下载更新文件
|
||||
fileDownload, err2 := g.Client().Get(ctx, asset.BrowserDownloadUrl)
|
||||
if err2 != nil {
|
||||
return
|
||||
}
|
||||
//filename := gfile.Name()
|
||||
err = gfile.PutBytes(path.Join("download", asset.Name), fileDownload.ReadAll())
|
||||
updateFile := path.Join("download", asset.Name)
|
||||
err = gfile.PutBytes(updateFile, fileDownload.ReadAll())
|
||||
|
||||
err = s.Update(ctx, updateFile)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 保存最新版本号到文件
|
||||
gfile.PutContents("download/version.txt", latestVersion)
|
||||
gfile.PutContents(versionFile, latestVersion)
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("当前已是最新版本:%s\n", localVersion)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ import (
|
||||
type (
|
||||
ISystem interface {
|
||||
Init()
|
||||
Update(ctx context.Context) (err error)
|
||||
Update(ctx context.Context, gzFile string) (err error)
|
||||
// RestartSelf 实现 Windows 平台下的程序自重启
|
||||
RestartSelf() error
|
||||
// RenameRunningFile 重命名正在运行的程序文件(如 message.exe → message.exe~)
|
||||
RenameRunningFile(exePath string) (string, error)
|
||||
CheckUpdate()
|
||||
CheckUpdate() (err error)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
46
manifest/config/config.yaml
Normal file
46
manifest/config/config.yaml
Normal file
@@ -0,0 +1,46 @@
|
||||
module:
|
||||
server: true
|
||||
client: true
|
||||
|
||||
|
||||
|
||||
# https://goframe.org/docs/web/server-config-file-template
|
||||
server:
|
||||
address: "51888"
|
||||
# openapiPath: "/api.json"
|
||||
# swaggerPath: "/swagger"
|
||||
dumpRouterMap: false
|
||||
graceful: true
|
||||
|
||||
# https://goframe.org/docs/core/glog-config
|
||||
logger:
|
||||
level : "all"
|
||||
stdout: true
|
||||
|
||||
# https://goframe.org/docs/core/gdb-config-file
|
||||
database:
|
||||
default:
|
||||
link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
|
||||
redis:
|
||||
default:
|
||||
address: "ay.cname.com:6379"
|
||||
db: 15
|
||||
pass: "12345678"
|
||||
cache:
|
||||
address: "ay.cname.com:6379"
|
||||
db: 15
|
||||
pass: "12345678"
|
||||
|
||||
p2p:
|
||||
list:
|
||||
# - host: "192.168.50.173"
|
||||
# port: 51888
|
||||
# ssl: false
|
||||
# ws: ws
|
||||
- host: "ay.cname.com"
|
||||
port: 51888
|
||||
ssl: false
|
||||
ws: ws
|
||||
|
||||
|
||||
Reference in New Issue
Block a user