mirror of
https://github.com/ayflying/p2p.git
synced 2026-03-05 01:39:23 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 49c84b886b | |||
| 607af816c4 |
@@ -456,7 +456,7 @@ func (s *sP2P) receiveGatewayMessages(ctx context.Context) {
|
|||||||
|
|
||||||
g.Log().Info(ctx, "文件接收完成")
|
g.Log().Info(ctx, "文件接收完成")
|
||||||
// 开始覆盖文件与重启
|
// 开始覆盖文件与重启
|
||||||
err = service.System().Update(ctx)
|
err = service.System().Update(ctx, "")
|
||||||
|
|
||||||
//// 调用不同系统的更新服务
|
//// 调用不同系统的更新服务
|
||||||
//service.OS().Update(msgData.Version, msgData.Server)
|
//service.OS().Update(msgData.Version, msgData.Server)
|
||||||
|
|||||||
@@ -188,9 +188,10 @@ func (s *sP2P) removeDuplicates(strs []string) []string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const privKeyPath = "runtime/p2p.key"
|
||||||
|
|
||||||
// 生成固定密钥(核心:通过固定种子生成相同密钥)
|
// 生成固定密钥(核心:通过固定种子生成相同密钥)
|
||||||
func (s *sP2P) generateFixedKey() (crypto.PrivKey, error) {
|
func (s *sP2P) generateFixedKey() (crypto.PrivKey, error) {
|
||||||
privKeyPath := "runtime/message.key"
|
|
||||||
if ok := gfile.Exists(privKeyPath); ok {
|
if ok := gfile.Exists(privKeyPath); ok {
|
||||||
// 从文件读取密钥
|
// 从文件读取密钥
|
||||||
keyBytes := gfile.GetBytes(privKeyPath)
|
keyBytes := gfile.GetBytes(privKeyPath)
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ func init() {
|
|||||||
|
|
||||||
getDev, _ := g.Cfg().GetWithEnv(gctx.New(), "dev")
|
getDev, _ := g.Cfg().GetWithEnv(gctx.New(), "dev")
|
||||||
if !getDev.Bool() {
|
if !getDev.Bool() {
|
||||||
service.System().CheckUpdate()
|
err := service.System().CheckUpdate()
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Errorf(gctx.New(), "检查更新失败:%v", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
g.Log().Debugf(gctx.New(), "开发模式,不检查更新")
|
g.Log().Debugf(gctx.New(), "开发模式,不检查更新")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,15 +48,28 @@ type GitHubRelease struct {
|
|||||||
Body string `json:"body"`
|
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)
|
//拼接操作系统和架构(格式:OS_ARCH)
|
||||||
platform := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
|
platform := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
|
||||||
|
|
||||||
runFile := gcmd.GetArg(0).String()
|
runFile := gcmd.GetArg(0).String()
|
||||||
oldFile, err := service.System().RenameRunningFile(runFile)
|
oldFile, err := service.System().RenameRunningFile(runFile)
|
||||||
g.Log().Debugf(ctx, "执行文件改名为%v", oldFile)
|
g.Log().Debugf(ctx, "执行文件改名为%v", oldFile)
|
||||||
gz := path.Join("download", platform+".gz")
|
if gzFile == "" {
|
||||||
err = gcompress.UnGzipFile(gz, runFile)
|
gzFile = path.Join("download", platform+".gz")
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
go func() {
|
||||||
log.Println("5秒后开始重启...")
|
log.Println("5秒后开始重启...")
|
||||||
@@ -166,7 +179,7 @@ func (s *sSystem) getLatestVersion() (string, []*Assets, error) {
|
|||||||
return release.TagName, release.Assets, nil
|
return release.TagName, release.Assets, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sSystem) CheckUpdate() {
|
func (s *sSystem) CheckUpdate() (err error) {
|
||||||
ctx := gctx.New()
|
ctx := gctx.New()
|
||||||
latestVersion, assets, err := s.getLatestVersion()
|
latestVersion, assets, err := s.getLatestVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,13 +199,18 @@ func (s *sSystem) CheckUpdate() {
|
|||||||
if strings.Contains(fmt.Sprintf("_%s.", asset.Name), platform) {
|
if strings.Contains(fmt.Sprintf("_%s.", asset.Name), platform) {
|
||||||
fmt.Printf("- %s\n", asset.BrowserDownloadUrl)
|
fmt.Printf("- %s\n", asset.BrowserDownloadUrl)
|
||||||
|
|
||||||
|
// 下载更新文件
|
||||||
fileDownload, err2 := g.Client().Get(ctx, asset.BrowserDownloadUrl)
|
fileDownload, err2 := g.Client().Get(ctx, asset.BrowserDownloadUrl)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//filename := gfile.Name()
|
updateFile := path.Join("download", asset.Name)
|
||||||
err = gfile.PutBytes(path.Join("download", asset.Name), fileDownload.ReadAll())
|
err = gfile.PutBytes(updateFile, fileDownload.ReadAll())
|
||||||
|
|
||||||
|
err = s.Update(ctx, updateFile)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
// 保存最新版本号到文件
|
// 保存最新版本号到文件
|
||||||
gfile.PutContents("download/version.txt", latestVersion)
|
gfile.PutContents("download/version.txt", latestVersion)
|
||||||
break
|
break
|
||||||
@@ -201,4 +219,5 @@ func (s *sSystem) CheckUpdate() {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Printf("当前已是最新版本:%s\n", localVersion)
|
fmt.Printf("当前已是最新版本:%s\n", localVersion)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ import (
|
|||||||
type (
|
type (
|
||||||
ISystem interface {
|
ISystem interface {
|
||||||
Init()
|
Init()
|
||||||
Update(ctx context.Context) (err error)
|
Update(ctx context.Context, gzFile string) (err error)
|
||||||
// RestartSelf 实现 Windows 平台下的程序自重启
|
// RestartSelf 实现 Windows 平台下的程序自重启
|
||||||
RestartSelf() error
|
RestartSelf() error
|
||||||
// RenameRunningFile 重命名正在运行的程序文件(如 message.exe → message.exe~)
|
// RenameRunningFile 重命名正在运行的程序文件(如 message.exe → message.exe~)
|
||||||
RenameRunningFile(exePath string) (string, error)
|
RenameRunningFile(exePath string) (string, error)
|
||||||
CheckUpdate()
|
CheckUpdate() (err error)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user