7 Commits

8 changed files with 151 additions and 37 deletions

1
.gitignore vendored
View File

@@ -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~

9
go.mod
View File

@@ -1,13 +1,15 @@
module github.com/ayflying/p2p module github.com/ayflying/p2p
go 1.24.0 go 1.24.8
toolchain go1.24.9
require ( require (
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9 github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9
github.com/dop251/goja v0.0.0-20250630131328-58d95d85e994 github.com/dop251/goja v0.0.0-20250630131328-58d95d85e994
github.com/getlantern/systray v1.2.2 github.com/getlantern/systray v1.2.2
github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.3 github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.3
github.com/gogf/gf/v2 v2.9.3 github.com/gogf/gf/v2 v2.9.4
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/libp2p/go-libp2p v0.43.0 github.com/libp2p/go-libp2p v0.43.0
@@ -18,6 +20,7 @@ require (
require ( require (
github.com/BurntSushi/toml v1.5.0 // indirect github.com/BurntSushi/toml v1.5.0 // indirect
github.com/ayflying/update-github-release v0.0.2 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
@@ -97,7 +100,7 @@ require (
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect
github.com/olekukonko/errors v1.1.0 // indirect github.com/olekukonko/errors v1.1.0 // indirect
github.com/olekukonko/ll v0.1.1 // indirect github.com/olekukonko/ll v0.1.1 // indirect
github.com/olekukonko/tablewriter v1.0.9 // indirect github.com/olekukonko/tablewriter v1.1.0 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/philhofer/fwd v1.2.0 // indirect github.com/philhofer/fwd v1.2.0 // indirect

6
go.sum
View File

@@ -15,6 +15,8 @@ github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/ayflying/update-github-release v0.0.2 h1:pZ2W0VKagBsJ5OsrREfniy/0fE70nbU8dJU7TtpzY0w=
github.com/ayflying/update-github-release v0.0.2/go.mod h1:9ctXuagiEKABWLaS4ocRCUcnN77jdYm7zir7NQ+rIeM=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
@@ -102,6 +104,8 @@ github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.3 h1:VTbeHq8XpBCWFIwBGmuBl+jP8Aep
github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.3/go.mod h1:gcidgAYn4IWbx08QUThg7jw6bz3KklXI9/5zg8jnVHY= github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.3/go.mod h1:gcidgAYn4IWbx08QUThg7jw6bz3KklXI9/5zg8jnVHY=
github.com/gogf/gf/v2 v2.9.3 h1:qjN4s55FfUzxZ1AE8vUHNDX3V0eIOUGXhF2DjRTVZQ4= github.com/gogf/gf/v2 v2.9.3 h1:qjN4s55FfUzxZ1AE8vUHNDX3V0eIOUGXhF2DjRTVZQ4=
github.com/gogf/gf/v2 v2.9.3/go.mod h1:w6rcfD13SmO7FKI80k9LSLiSMGqpMYp50Nfkrrc2sEE= github.com/gogf/gf/v2 v2.9.3/go.mod h1:w6rcfD13SmO7FKI80k9LSLiSMGqpMYp50Nfkrrc2sEE=
github.com/gogf/gf/v2 v2.9.4 h1:6vleEWypot9WBPncP2GjbpgAUeG6Mzb1YESb9nPMkjY=
github.com/gogf/gf/v2 v2.9.4/go.mod h1:Ukl+5HUH9S7puBmNLR4L1zUqeRwi0nrW4OigOknEztU=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
@@ -283,6 +287,8 @@ github.com/olekukonko/ll v0.1.1 h1:9Dfeed5/Mgaxb9lHRAftLK9pVfYETvHn+If6lywVhJc=
github.com/olekukonko/ll v0.1.1/go.mod h1:2dJo+hYZcJMLMbKwHEWvxCUbAOLc/CXWS9noET22Mdo= github.com/olekukonko/ll v0.1.1/go.mod h1:2dJo+hYZcJMLMbKwHEWvxCUbAOLc/CXWS9noET22Mdo=
github.com/olekukonko/tablewriter v1.0.9 h1:XGwRsYLC2bY7bNd93Dk51bcPZksWZmLYuaTHR0FqfL8= github.com/olekukonko/tablewriter v1.0.9 h1:XGwRsYLC2bY7bNd93Dk51bcPZksWZmLYuaTHR0FqfL8=
github.com/olekukonko/tablewriter v1.0.9/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo= github.com/olekukonko/tablewriter v1.0.9/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo=
github.com/olekukonko/tablewriter v1.1.0 h1:N0LHrshF4T39KvI96fn6GT8HEjXRXYNDrDjKFDB7RIY=
github.com/olekukonko/tablewriter v1.1.0/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=

View File

@@ -2,29 +2,6 @@ package system
import "time" import "time"
type T struct {
Url string `json:"url"`
AssetsUrl string `json:"assets_url"`
UploadUrl string `json:"upload_url"`
HtmlUrl string `json:"html_url"`
Id int `json:"id"`
Author *Author `json:"author"`
NodeId string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Immutable bool `json:"immutable"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PublishedAt time.Time `json:"published_at"`
Assets []*Assets
TarballUrl string `json:"tarball_url"`
ZipballUrl string `json:"zipball_url"`
Body string `json:"body"`
}
type Author struct { type Author struct {
Login string `json:"login"` Login string `json:"login"`
Id int `json:"id"` Id int `json:"id"`

View File

@@ -1,8 +1,12 @@
package system package system
import ( import (
"context"
"github.com/ayflying/p2p/internal/service" "github.com/ayflying/p2p/internal/service"
updateGithub "github.com/ayflying/update-github-release"
"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 +21,23 @@ func init() {
getDev, _ := g.Cfg().GetWithEnv(gctx.New(), "dev") getDev, _ := g.Cfg().GetWithEnv(gctx.New(), "dev")
if !getDev.Bool() { if !getDev.Bool() {
err := service.System().CheckUpdate() var update = updateGithub.New("https://api.github.com/repos/ayflying/p2p/releases/latest")
if err != nil {
g.Log().Errorf(gctx.New(), "检查更新失败:%v", err) // 每天0点检查更新
} gcron.Add(gctx.New(), "0 0 0 * * *", func(ctx context.Context) {
err := update.CheckUpdate()
if err != nil {
g.Log().Errorf(ctx, "检查更新失败:%v", err)
}
})
go func() {
//在协程中检查更新,预防主程序阻塞
err := update.CheckUpdate()
if err != nil {
g.Log().Errorf(gctx.New(), "检查更新失败:%v", err)
}
}()
} else { } else {
g.Log().Debugf(gctx.New(), "开发模式,不检查更新") g.Log().Debugf(gctx.New(), "开发模式,不检查更新")
} }

View File

@@ -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"
@@ -24,6 +27,8 @@ import (
) )
// 本地版本号(建议从编译参数注入,如 -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 响应的核心字段(按需精简)
@@ -59,18 +64,22 @@ func (s *sSystem) Update(ctx context.Context, gzFile string) (err error) {
if gzFile == "" { if gzFile == "" {
gzFile = path.Join("download", platform+".gz") gzFile = path.Join("download", platform+".gz")
} }
//结束后删除压缩包
defer gfile.RemoveFile(gzFile)
ext := gfile.Ext(gzFile) ext := gfile.Ext(gzFile)
if ext == ".zip" { if ext == ".zip" {
g.Log().Debugf(ctx, "zip解压%v到%v", gzFile, gfile.Dir(runFile)) g.Log().Debugf(ctx, "zip解压%v到%v", gzFile, gfile.Dir(runFile))
err = gcompress.UnZipFile(gzFile, gfile.Dir(runFile)) err = gcompress.UnZipFile(gzFile, gfile.Dir(runFile))
} else { } else {
g.Log().Debugf(ctx, "gzip解压%v到%v", gzFile, runFile) g.Log().Debugf(ctx, "gzip解压%v到%v", gzFile, gfile.Dir(runFile))
err = gcompress.UnGzipFile(gzFile, runFile) err = s.UnTarGz(gzFile, gfile.Dir(runFile))
} }
if err != nil { if err != nil {
return return
} }
//修改文件权限为755
err = gfile.Chmod(runFile, 0755)
go func() { go func() {
log.Println("5秒后开始重启...") log.Println("5秒后开始重启...")
@@ -83,6 +92,64 @@ func (s *sSystem) Update(ctx context.Context, gzFile string) (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() ctx := gctx.New()
@@ -198,7 +265,7 @@ func (s *sSystem) CheckUpdate() (err error) {
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)
@@ -223,7 +290,7 @@ func (s *sSystem) CheckUpdate() (err error) {
return return
} }
// 保存最新版本号到文件 // 保存最新版本号到文件
gfile.PutContents("download/version.txt", latestVersion) gfile.PutContents(versionFile, latestVersion)
break break
} }
} }

View File

@@ -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")

View 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: "host.yunloli.cn"
port: 51888
ssl: false
ws: ws
- host: "ay.cname.com"
port: 51888
ssl: false
ws: ws