Skip to content

Commit

Permalink
提取frpc.ini到根目录,添加更新frp功能
Browse files Browse the repository at this point in the history
  • Loading branch information
luoqiz committed Nov 19, 2020
1 parent 3180b42 commit e55294d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 21 deletions.
12 changes: 7 additions & 5 deletions frp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import (
func GetIniFilePath() string {
// 获取配置文件路径
workspace, _ := filepath.Abs("")
frpcpath := utils.GetDirectory(workspace + "/frpc")
if len(frpcpath) == 1 {
frpc := frpcpath[0]
return frpc + "/frpc.ini"
frpcpath := workspace + "/frpc.ini"
_, err := os.Stat(frpcpath)
if err != nil {
return ""
}
return ""
return frpcpath
}

func FullContent() (string, error) {
filename := GetIniFilePath()
println(filename)
println(filename)
//读取文本内容到文本框中
bytes, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
19 changes: 8 additions & 11 deletions frp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func CheckStatus() int {

//开启frpc
func Start() int {
workspace, _ := filepath.Abs("./")
println(workspace)
workspace, _ := filepath.Abs("")
dir := utils.GetDirectory(workspace + "/frpc")
if len(dir) < 1 {
return 0
Expand All @@ -35,13 +34,10 @@ func Start() int {

client := cmd.CMDFactory{}.Generate()
if (client == linux.Linux{}) {
client.RunCommandBg("nohup " + frpc + "/frpc -c " + frpc + "/frpc.ini &")
client.RunCommandBg("nohup " + frpc + "/frpc -c " + workspace + "/frpc.ini &")
} else if (client == windows.Windows{}) {
cmdstr := frpc + "/frpc.exe -c " + frpc + "/frpc.ini"
println(cmdstr)
client.RunCommandBg(frpc + "/frpc.exe -c " + frpc + "/frpc.ini")
client.RunCommandBg(frpc + "/frpc.exe -c " + workspace + "/frpc.ini")
}

utils.Log.Info("frpc start success...")
return CheckStatus()
}
Expand Down Expand Up @@ -71,15 +67,16 @@ func Download(fb func(length, downLen int64)) {

frpName, frpUrl := ValidUrlOnOs()
frpDownloadPath := filepath.Join(workDir, frpName)
//frpDownloadPath := workDir + "/" + frpName
utils.Log.Infof("frp download path : %s", frpDownloadPath)
println(frpUrl, frpDownloadPath)
utils.DownloadProcess(frpUrl, frpDownloadPath, fb)
utils.Log.Info("frp download end...")

//utils.Log.Infof("frp unzip start into %s", workDir)
// 解压之前先删除已解压文件
utils.Log.Infof("frp unzip start into %s", workDir)
utils.DeleteDir(workDir + "/frpc")
utils.UnCompress(frpDownloadPath, workDir+"/frpc", true)
//utils.Log.Info("frp.zip end unzip...")
utils.DeleteFile(frpDownloadPath)
utils.Log.Info("frp.zip end unzip...")
}

// 获取最新tag
Expand Down
3 changes: 2 additions & 1 deletion ui/nav/service/advanced_screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fyne.io/fyne/container"
"fyne.io/fyne/widget"
"go-frpc/frp"
"go-frpc/utils"
)

func AdvancedScreen(w fyne.Window) fyne.CanvasObject {
Expand All @@ -13,7 +14,7 @@ func AdvancedScreen(w fyne.Window) fyne.CanvasObject {
// 保存按钮
saveButton := widget.NewButton("save", func() {
frp.SetContent(content.Text)
fyne.CurrentApp().SendNotification(fyne.NewNotification("go-frpc客户端", "frpc.ini文件保存成功"))
utils.SendNotifiction("frpc.ini文件保存成功")
})
// 重新加载按钮
reloadButton := widget.NewButton("reload", func() {
Expand Down
9 changes: 8 additions & 1 deletion ui/nav/service/info_screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ func InfoScreen(w fyne.Window) fyne.CanvasObject {
proxyEntry := widget.NewEntry()
proxyBox := widget.NewForm(widget.NewFormItem("http代理", proxyEntry))
// 文件下载按钮
downloadButton := widget.NewButton("download",
downloadButton := widget.NewButton("download && update",
func() {

// 若是填写了代理则将代理设置到软件全局
if proxyEntry.Text != "" {
utils.AppConfig.ProxyAddr = proxyEntry.Text
}

prog := dialog.NewProgress("文件下载", "下载进度", w)
prog.Show()
frp.Download(func(length, downLen int64) {
Expand All @@ -31,6 +34,10 @@ func InfoScreen(w fyne.Window) fyne.CanvasObject {
}
})

// 检测是否已存在配置文件,不存在则创建
if frp.GetIniFilePath() == "" {
utils.CreateFile("./frpc.ini")
}
},
)
return container.NewVBox(
Expand Down
67 changes: 67 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,73 @@ func Download(url string, savePath string) {
}
}

func CreateFile(file string) error {
_, err := os.Create(file)
if err != nil {
return err
}
return nil
}

func CopyFile(srcFile string, destFile string) error {

var BUFFERSIZE int64

// 判断是否是可读取常规文件
srcFileStat, err := os.Stat(srcFile)
if err != nil {
return err
}
if !srcFileStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file.", srcFile)
}

// 打开源文件
source, err := os.Open(srcFile)
if err != nil {
return err
}
defer source.Close()

dest, err := os.Open(destFile)
if err != nil {
return err
}
defer dest.Close()

buf := make([]byte, BUFFERSIZE)

for {
n, err := source.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := dest.Write(buf[:n]); err != nil {
return err
}
}
return err
}

func DeleteFile(filename string) error {
_, err := os.Stat(filename)
if err != nil {
return err
}
return os.Remove(filename)
}

func DeleteDir(path string) error {
_, err := os.Stat(path)
if err != nil {
return err
}
return os.RemoveAll(path)
}

func GetClient() *http.Client {
if AppConfig.ProxyAddr != "" {
proxy := func(_ *http.Request) (*url.URL, error) {
Expand Down
2 changes: 1 addition & 1 deletion utils/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "fyne.io/fyne"

func SendNotifiction(content string) {
fyne.CurrentApp().SendNotification(&fyne.Notification{
Title: "frpc",
Title: "go-frpc 客户端",
Content: content,
})
}
3 changes: 1 addition & 2 deletions utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ func ParseURL(urlStr string) *url.URL {
if err != nil {
fyne.LogError("Could not parse URL", err)
}

return link
}
}

0 comments on commit e55294d

Please sign in to comment.