mirror of
https://github.com/ayflying/p2p.git
synced 2026-03-05 09:49:22 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d3f3687201 | |||
| 63f5bef038 | |||
| f90cf8b855 | |||
| a0f367efeb | |||
| ceb44e936f | |||
| 49c84b886b | |||
| 607af816c4 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -16,7 +16,6 @@ manifest/output/
|
|||||||
temp/
|
temp/
|
||||||
temp.yaml
|
temp.yaml
|
||||||
bin
|
bin
|
||||||
**/config/config.yaml
|
|
||||||
v1.0.0/
|
v1.0.0/
|
||||||
config/local.yaml
|
config/local.yaml
|
||||||
main.exe~
|
main.exe~
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"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/gcron"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,10 +20,21 @@ 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()
|
// 每天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 {
|
} else {
|
||||||
g.Log().Debugf(gctx.New(), "开发模式,不检查更新")
|
g.Log().Debugf(gctx.New(), "开发模式,不检查更新")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (system *sSystem) Init() {}
|
func (s *sSystem) Init() {}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"compress/gzip"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -17,12 +20,15 @@ import (
|
|||||||
"github.com/ayflying/p2p/internal/service"
|
"github.com/ayflying/p2p/internal/service"
|
||||||
"github.com/gogf/gf/v2/encoding/gcompress"
|
"github.com/gogf/gf/v2/encoding/gcompress"
|
||||||
"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/os/gcmd"
|
"github.com/gogf/gf/v2/os/gcmd"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 本地版本号(建议从编译参数注入,如 -ldflags "-X main.version=v0.1.3")
|
// 本地版本号(建议从编译参数注入,如 -ldflags "-X main.version=v0.1.3")
|
||||||
|
const versionFile = "version.txt"
|
||||||
|
|
||||||
var localVersion = "v0.0.0"
|
var localVersion = "v0.0.0"
|
||||||
|
|
||||||
// 对应 GitHub API 响应的核心字段(按需精简)
|
// 对应 GitHub API 响应的核心字段(按需精简)
|
||||||
@@ -48,15 +54,30 @@ 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")
|
||||||
|
}
|
||||||
|
//结束后删除压缩包
|
||||||
|
//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, gfile.Dir(runFile))
|
||||||
|
err = s.UnTarGz(gzFile, gfile.Dir(runFile))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Println("5秒后开始重启...")
|
log.Println("5秒后开始重启...")
|
||||||
@@ -69,8 +90,76 @@ func (s *sSystem) Update(ctx context.Context) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnTarGz 解压tar.gz文件到指定目录
|
||||||
|
func (s *sSystem) UnTarGz(tarGzFileName, targetDir string) (err error) {
|
||||||
|
// 打开tar.gz文件
|
||||||
|
file, err := os.Open(tarGzFileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// 创建gzip reader
|
||||||
|
gzr, err := gzip.NewReader(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer gzr.Close()
|
||||||
|
|
||||||
|
// 创建tar reader
|
||||||
|
tr := tar.NewReader(gzr)
|
||||||
|
|
||||||
|
// 遍历tar中的每个文件
|
||||||
|
for {
|
||||||
|
hdr, err := tr.Next()
|
||||||
|
if err == io.EOF {
|
||||||
|
// 到达文件末尾,退出循环
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建解压后的文件路径
|
||||||
|
targetPath := targetDir + string(os.PathSeparator) + hdr.Name
|
||||||
|
|
||||||
|
// 如果是目录,创建目录
|
||||||
|
if hdr.Typeflag == tar.TypeDir {
|
||||||
|
err := os.MkdirAll(targetPath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是文件,创建文件并写入内容
|
||||||
|
outFile, err := os.Create(targetPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer outFile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(outFile, tr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// RestartSelf 实现 Windows 平台下的程序自重启
|
// RestartSelf 实现 Windows 平台下的程序自重启
|
||||||
func (s *sSystem) RestartSelf() error {
|
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. 获取当前程序的绝对路径
|
// 1. 获取当前程序的绝对路径
|
||||||
exePath, err := os.Executable()
|
exePath, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -166,7 +255,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 {
|
||||||
@@ -174,7 +263,7 @@ func (s *sSystem) CheckUpdate() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
localVersion = gfile.GetContents("download/version.txt")
|
localVersion = gfile.GetContents(versionFile)
|
||||||
|
|
||||||
if s.isNewVersion(localVersion, latestVersion) {
|
if s.isNewVersion(localVersion, latestVersion) {
|
||||||
g.Log().Printf(ctx, "发现新版本:%s(当前版本:%s)", latestVersion, localVersion)
|
g.Log().Printf(ctx, "发现新版本:%s(当前版本:%s)", latestVersion, localVersion)
|
||||||
@@ -186,19 +275,25 @@ 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(versionFile, latestVersion)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/gogf/gf/v2/os/gcmd"
|
"github.com/gogf/gf/v2/os/gcmd"
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
|
|
||||||
//步骤1:加载驱动
|
//步骤1:加载驱动
|
||||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||||
|
|
||||||
@@ -24,7 +23,7 @@ var (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
g.Log().Infof(ctx, "启动文件最后修改时间:%v", gtime.New(gfile.MTime(gcmd.GetArg(0).String())).String())
|
g.Log().Infof(ctx, "启动文件最后修改时间:%v", gtime.New(gfile.MTime(gcmd.GetArg(0).String())).String())
|
||||||
g.Dump("v1.0.0.2")
|
//g.Dump("v1.0.0.2")
|
||||||
|
|
||||||
if ok := gfile.Exists("runtime"); !ok {
|
if ok := gfile.Exists("runtime"); !ok {
|
||||||
gfile.Mkdir("runtime")
|
gfile.Mkdir("runtime")
|
||||||
|
|||||||
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