Compare commits

...

2 Commits

Author SHA1 Message Date
Egor Zavialov
b3f1ac0e21
Merge 95681518606083c638840a31835c36d9efa289d2 into f390e4a401cf9a07fd7e8b9e0c50049c1993b967 2024-06-05 10:07:20 -05:00
Egor Zavialov
9568151860 feat: add client reload on config change 2024-06-01 16:28:15 +09:00
5 changed files with 77 additions and 0 deletions

View File

@ -30,6 +30,9 @@ import (
"github.com/fatedier/frp/client/proxy"
"github.com/fatedier/frp/pkg/auth"
v1 "github.com/fatedier/frp/pkg/config/v1"
"github.com/fatedier/frp/pkg/config/v1/validation"
"github.com/radovskyb/watcher"
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/msg"
httppkg "github.com/fatedier/frp/pkg/util/http"
"github.com/fatedier/frp/pkg/util/log"
@ -156,6 +159,11 @@ func NewService(options ServiceOptions) (*Service, error) {
if webServer != nil {
webServer.RouteRegister(s.registerRouteHandlers)
}
if options.Common.ConfigAutoReload {
go s.MonitorClientConfig(options.ConfigFilePath)
}
return s, nil
}
@ -408,3 +416,63 @@ type statusExporterImpl struct {
func (s *statusExporterImpl) GetProxyStatus(name string) (*proxy.WorkingStatus, bool) {
return s.getProxyStatusFunc(name)
}
func (s *Service) MonitorClientConfig(cfgFilePath string) {
cliCfg, _, _, _, err := config.LoadClientConfig(cfgFilePath, false)
if err != nil {
log.Errorf("%v", err)
}
if !cliCfg.ConfigAutoReload {
log.Infof("auto reload on config change is disabled.")
return
}
w := watcher.New()
w.SetMaxEvents(1)
w.FilterOps(watcher.Write)
done := make(chan bool)
go func() {
for {
select {
case event := <-w.Event:
log.Infof("config file changed: %s", event.Path)
cliCfg, pxyCfgs, visitorCfgs, _, err := config.LoadClientConfig(cfgFilePath, false)
if err != nil {
log.Infof("reload frpc proxy config error: %s", err.Error())
continue
}
if _, err := validation.ValidateAllClientConfig(cliCfg, pxyCfgs, visitorCfgs); err != nil {
log.Infof("reload frpc proxy config error: %s", err.Error())
continue
}
if err := s.UpdateAllConfigurer(pxyCfgs, visitorCfgs); err != nil {
log.Infof("reload frpc proxy config error: %s", err.Error())
continue
}
log.Infof("success reload conf")
case err := <-w.Error:
log.Infof("error: %s", err.Error())
case <-w.Closed:
done <- true
return
}
}
}()
if err := w.Add(cfgFilePath); err != nil {
log.Infof("error adding file to watcher: %s", err.Error())
}
go func() {
if err := w.Start(time.Second); err != nil {
log.Infof("error starting watcher: %s", err.Error())
}
}()
<-done
}

View File

@ -12,6 +12,9 @@ serverPort = 7000
# STUN server to help penetrate NAT hole.
# natHoleStunServer = "stun.easyvoip.com:3478"
# if true, autoreload is enabled
configAutoReload = true
# Decide if exit program when first login failed, otherwise continuous relogin to frps
# default is true
loginFailExit = true

1
go.mod
View File

@ -59,6 +59,7 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/radovskyb/watcher v1.0.7 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/templexxx/cpu v0.1.0 // indirect
github.com/templexxx/xorsimd v0.4.2 // indirect

2
go.sum
View File

@ -112,6 +112,8 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/quic-go/quic-go v0.42.0 h1:uSfdap0eveIl8KXnipv9K7nlwZ5IqLlYOpJ58u5utpM=
github.com/quic-go/quic-go v0.42.0/go.mod h1:132kz4kL3F9vxhW3CtQJLDVwcFe5wdWeJXXijhsO57M=
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rodaine/table v1.2.0 h1:38HEnwK4mKSHQJIkavVj+bst1TEY7j9zhLMWu4QJrMA=

View File

@ -46,6 +46,9 @@ type ClientCommonConfig struct {
ServerPort int `json:"serverPort,omitempty"`
// STUN server to help penetrate NAT hole.
NatHoleSTUNServer string `json:"natHoleStunServer,omitempty"`
//
ConfigAutoReload bool `json:"configAutoReload,omitempty"`
// DNSServer specifies a DNS server address for FRPC to use. If this value
// is "", the default DNS will be used.
DNSServer string `json:"dnsServer,omitempty"`