mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 07:35:07 +00:00
frpc: add admin server for reload configure file
This commit is contained in:
60
client/admin.go
Normal file
60
client/admin.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2017 fatedier, fatedier@gmail.com
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/fatedier/frp/models/config"
|
||||
frpNet "github.com/fatedier/frp/utils/net"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
var (
|
||||
httpServerReadTimeout = 10 * time.Second
|
||||
httpServerWriteTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
func (svr *Service) RunAdminServer(addr string, port int64) (err error) {
|
||||
// url router
|
||||
router := httprouter.New()
|
||||
|
||||
user, passwd := config.ClientCommonCfg.AdminUser, config.ClientCommonCfg.AdminPwd
|
||||
|
||||
// api, see dashboard_api.go
|
||||
router.GET("/api/reload", frpNet.HttprouterBasicAuth(svr.apiReload, user, passwd))
|
||||
|
||||
address := fmt.Sprintf("%s:%d", addr, port)
|
||||
server := &http.Server{
|
||||
Addr: address,
|
||||
Handler: router,
|
||||
ReadTimeout: httpServerReadTimeout,
|
||||
WriteTimeout: httpServerWriteTimeout,
|
||||
}
|
||||
if address == "" {
|
||||
address = ":http"
|
||||
}
|
||||
ln, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go server.Serve(ln)
|
||||
return
|
||||
}
|
78
client/admin_api.go
Normal file
78
client/admin_api.go
Normal file
@@ -0,0 +1,78 @@
|
||||
// Copyright 2017 fatedier, fatedier@gmail.com
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
ini "github.com/vaughan0/go-ini"
|
||||
|
||||
"github.com/fatedier/frp/models/config"
|
||||
"github.com/fatedier/frp/utils/log"
|
||||
)
|
||||
|
||||
type GeneralResponse struct {
|
||||
Code int64 `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
// api/reload
|
||||
type ReloadResp struct {
|
||||
GeneralResponse
|
||||
}
|
||||
|
||||
func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
var (
|
||||
buf []byte
|
||||
res ReloadResp
|
||||
)
|
||||
defer func() {
|
||||
log.Info("Http response [/api/reload]: code [%d]", res.Code)
|
||||
buf, _ = json.Marshal(&res)
|
||||
w.Write(buf)
|
||||
}()
|
||||
|
||||
log.Info("Http request: [/api/reload]")
|
||||
|
||||
conf, err := ini.LoadFile(config.ClientCommonCfg.ConfigFile)
|
||||
if err != nil {
|
||||
res.Code = 1
|
||||
res.Msg = err.Error()
|
||||
log.Error("reload frpc config file error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
newCommonCfg, err := config.LoadClientCommonConf(conf)
|
||||
if err != nil {
|
||||
res.Code = 2
|
||||
res.Msg = err.Error()
|
||||
log.Error("reload frpc common section error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
pxyCfgs, vistorCfgs, err := config.LoadProxyConfFromFile(newCommonCfg.User, conf, newCommonCfg.Start)
|
||||
if err != nil {
|
||||
res.Code = 3
|
||||
res.Msg = err.Error()
|
||||
log.Error("reload frpc proxy config error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
svr.ctl.reloadConf(pxyCfgs, vistorCfgs)
|
||||
log.Info("success reload conf")
|
||||
return
|
||||
}
|
@@ -388,7 +388,7 @@ func (ctl *Control) manager() {
|
||||
ctl.Warn("[%s] start error: %s", m.ProxyName, m.Error)
|
||||
continue
|
||||
}
|
||||
cfg, ok := ctl.pxyCfgs[m.ProxyName]
|
||||
cfg, ok := ctl.getProxyConf(m.ProxyName)
|
||||
if !ok {
|
||||
// it will never go to this branch now
|
||||
ctl.Warn("[%s] no proxy conf found", m.ProxyName)
|
||||
@@ -424,20 +424,36 @@ func (ctl *Control) controler() {
|
||||
maxDelayTime := 30 * time.Second
|
||||
delayTime := time.Second
|
||||
|
||||
checkInterval := 30 * time.Second
|
||||
checkInterval := 10 * time.Second
|
||||
checkProxyTicker := time.NewTicker(checkInterval)
|
||||
for {
|
||||
select {
|
||||
case <-checkProxyTicker.C:
|
||||
// Every 30 seconds, check which proxy registered failed and reregister it to server.
|
||||
// Every 10 seconds, check which proxy registered failed and reregister it to server.
|
||||
ctl.mu.RLock()
|
||||
for _, cfg := range ctl.pxyCfgs {
|
||||
if _, exist := ctl.getProxy(cfg.GetName()); !exist {
|
||||
ctl.Info("try to reregister proxy [%s]", cfg.GetName())
|
||||
if _, exist := ctl.proxies[cfg.GetName()]; !exist {
|
||||
ctl.Info("try to register proxy [%s]", cfg.GetName())
|
||||
var newProxyMsg msg.NewProxy
|
||||
cfg.UnMarshalToMsg(&newProxyMsg)
|
||||
ctl.sendCh <- &newProxyMsg
|
||||
}
|
||||
}
|
||||
|
||||
for _, cfg := range ctl.vistorCfgs {
|
||||
if _, exist := ctl.vistors[cfg.GetName()]; !exist {
|
||||
ctl.Info("try to start vistor [%s]", cfg.GetName())
|
||||
vistor := NewVistor(ctl, cfg)
|
||||
err = vistor.Run()
|
||||
if err != nil {
|
||||
vistor.Warn("start error: %v", err)
|
||||
continue
|
||||
}
|
||||
ctl.vistors[cfg.GetName()] = vistor
|
||||
vistor.Info("start vistor success")
|
||||
}
|
||||
}
|
||||
ctl.mu.RUnlock()
|
||||
case _, ok := <-ctl.closedCh:
|
||||
// we won't get any variable from this channel
|
||||
if !ok {
|
||||
@@ -485,11 +501,13 @@ func (ctl *Control) controler() {
|
||||
go ctl.reader()
|
||||
|
||||
// send NewProxy message for all configured proxies
|
||||
ctl.mu.RLock()
|
||||
for _, cfg := range ctl.pxyCfgs {
|
||||
var newProxyMsg msg.NewProxy
|
||||
cfg.UnMarshalToMsg(&newProxyMsg)
|
||||
ctl.sendCh <- &newProxyMsg
|
||||
}
|
||||
ctl.mu.RUnlock()
|
||||
|
||||
checkProxyTicker.Stop()
|
||||
checkProxyTicker = time.NewTicker(checkInterval)
|
||||
@@ -522,3 +540,82 @@ func (ctl *Control) addProxy(name string, pxy Proxy) {
|
||||
defer ctl.mu.Unlock()
|
||||
ctl.proxies[name] = pxy
|
||||
}
|
||||
|
||||
func (ctl *Control) getProxyConf(name string) (conf config.ProxyConf, ok bool) {
|
||||
ctl.mu.RLock()
|
||||
defer ctl.mu.RUnlock()
|
||||
conf, ok = ctl.pxyCfgs[name]
|
||||
return
|
||||
}
|
||||
|
||||
func (ctl *Control) reloadConf(pxyCfgs map[string]config.ProxyConf, vistorCfgs map[string]config.ProxyConf) {
|
||||
ctl.mu.Lock()
|
||||
defer ctl.mu.Unlock()
|
||||
|
||||
removedPxyNames := make([]string, 0)
|
||||
for name, oldCfg := range ctl.pxyCfgs {
|
||||
del := false
|
||||
cfg, ok := pxyCfgs[name]
|
||||
if !ok {
|
||||
del = true
|
||||
} else {
|
||||
if !oldCfg.Compare(cfg) {
|
||||
del = true
|
||||
}
|
||||
}
|
||||
|
||||
if del {
|
||||
removedPxyNames = append(removedPxyNames, name)
|
||||
delete(ctl.pxyCfgs, name)
|
||||
if pxy, ok := ctl.proxies[name]; ok {
|
||||
pxy.Close()
|
||||
}
|
||||
delete(ctl.proxies, name)
|
||||
ctl.sendCh <- &msg.CloseProxy{
|
||||
ProxyName: name,
|
||||
}
|
||||
}
|
||||
}
|
||||
ctl.Info("proxy removed: %v", removedPxyNames)
|
||||
|
||||
addedPxyNames := make([]string, 0)
|
||||
for name, cfg := range pxyCfgs {
|
||||
if _, ok := ctl.pxyCfgs[name]; !ok {
|
||||
ctl.pxyCfgs[name] = cfg
|
||||
addedPxyNames = append(addedPxyNames, name)
|
||||
}
|
||||
}
|
||||
ctl.Info("proxy added: %v", addedPxyNames)
|
||||
|
||||
removedVistorName := make([]string, 0)
|
||||
for name, oldVistorCfg := range ctl.vistorCfgs {
|
||||
del := false
|
||||
cfg, ok := vistorCfgs[name]
|
||||
if !ok {
|
||||
del = true
|
||||
} else {
|
||||
if !oldVistorCfg.Compare(cfg) {
|
||||
del = true
|
||||
}
|
||||
}
|
||||
|
||||
if del {
|
||||
removedVistorName = append(removedVistorName, name)
|
||||
delete(ctl.vistorCfgs, name)
|
||||
if vistor, ok := ctl.vistors[name]; ok {
|
||||
vistor.Close()
|
||||
}
|
||||
delete(ctl.vistors, name)
|
||||
}
|
||||
}
|
||||
ctl.Info("vistor removed: %v", removedVistorName)
|
||||
|
||||
addedVistorName := make([]string, 0)
|
||||
for name, vistorCfg := range vistorCfgs {
|
||||
if _, ok := ctl.vistorCfgs[name]; !ok {
|
||||
ctl.vistorCfgs[name] = vistorCfg
|
||||
addedVistorName = append(addedVistorName, name)
|
||||
}
|
||||
}
|
||||
ctl.Info("vistor added: %v", addedVistorName)
|
||||
}
|
||||
|
@@ -14,7 +14,10 @@
|
||||
|
||||
package client
|
||||
|
||||
import "github.com/fatedier/frp/models/config"
|
||||
import (
|
||||
"github.com/fatedier/frp/models/config"
|
||||
"github.com/fatedier/frp/utils/log"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
// manager control connection with server
|
||||
@@ -38,6 +41,14 @@ func (svr *Service) Run() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.ClientCommonCfg.AdminPort != 0 {
|
||||
err = svr.RunAdminServer(config.ClientCommonCfg.AdminAddr, config.ClientCommonCfg.AdminPort)
|
||||
if err != nil {
|
||||
log.Warn("run admin server error: %v", err)
|
||||
}
|
||||
log.Info("admin server listen on %s:%d", config.ClientCommonCfg.AdminAddr, config.ClientCommonCfg.AdminPort)
|
||||
}
|
||||
|
||||
<-svr.closedCh
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user