mirror of
https://github.com/koho/frpmgr.git
synced 2025-10-20 16:03:47 +08:00
Support automatic deletion of config after x days (#34)
* Support automatic deletion after x days * Add a clock icon suffix * Update README * Rename config model
This commit is contained in:
@ -3,7 +3,11 @@ package services
|
||||
import (
|
||||
_ "github.com/fatedier/frp/assets/frpc"
|
||||
frpc "github.com/fatedier/frp/cmd/frpc/sub"
|
||||
"github.com/fatedier/frp/pkg/config"
|
||||
frpconfig "github.com/fatedier/frp/pkg/config"
|
||||
"github.com/fatedier/frp/pkg/util/log"
|
||||
"github.com/koho/frpmgr/pkg/config"
|
||||
"github.com/koho/frpmgr/pkg/util"
|
||||
"os"
|
||||
)
|
||||
|
||||
func runFrpClient() {
|
||||
@ -12,14 +16,36 @@ func runFrpClient() {
|
||||
frpc.Execute()
|
||||
}
|
||||
|
||||
func deleteFrpConfig(serviceName string, configPath string, c config.Config) {
|
||||
// Delete logs
|
||||
log.Log.Close()
|
||||
if logs, _, err := util.FindLogFiles(c.GetLogFile()); err == nil {
|
||||
util.DeleteFiles(logs)
|
||||
}
|
||||
// Delete config file
|
||||
os.Remove(configPath)
|
||||
// Delete service
|
||||
m, err := serviceManager()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer m.Disconnect()
|
||||
service, err := m.OpenService(serviceName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer service.Close()
|
||||
service.Delete()
|
||||
}
|
||||
|
||||
// VerifyClientConfig validates the frp client config file
|
||||
func VerifyClientConfig(path string) (err error) {
|
||||
_, _, _, err = config.ParseClientConfig(path)
|
||||
_, _, _, err = frpconfig.ParseClientConfig(path)
|
||||
return
|
||||
}
|
||||
|
||||
// VerifyClientProxy validates the frp proxy
|
||||
func VerifyClientProxy(source []byte) (err error) {
|
||||
_, _, err = config.LoadAllProxyConfsFromIni("", source, nil)
|
||||
_, _, err = frpconfig.LoadAllProxyConfsFromIni("", source, nil)
|
||||
return
|
||||
}
|
||||
|
@ -2,11 +2,13 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/koho/frpmgr/pkg/config"
|
||||
"github.com/koho/frpmgr/pkg/util"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ServiceNameOfClient(name string) string {
|
||||
@ -36,6 +38,25 @@ func (service *frpService) Execute(args []string, r <-chan svc.ChangeRequest, ch
|
||||
log.Println("Shutting down")
|
||||
}()
|
||||
|
||||
cc, err := config.UnmarshalClientConfFromIni(service.configPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var expired <-chan time.Time
|
||||
t, err := config.Expiry(service.configPath, cc.DeleteAfterDays)
|
||||
switch err {
|
||||
case nil:
|
||||
if t <= 0 {
|
||||
deleteFrpConfig(args[0], service.configPath, cc)
|
||||
return
|
||||
}
|
||||
expired = time.After(t)
|
||||
case os.ErrNoDeadline:
|
||||
break
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
go runFrpClient()
|
||||
|
||||
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
|
||||
@ -52,6 +73,9 @@ func (service *frpService) Execute(args []string, r <-chan svc.ChangeRequest, ch
|
||||
default:
|
||||
log.Printf("Unexpected services control request #%d\n", c)
|
||||
}
|
||||
case <-expired:
|
||||
deleteFrpConfig(args[0], service.configPath, cc)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user