frp/client/admin_api.go

263 lines
6.7 KiB
Go
Raw Normal View History

// 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 (
2024-02-20 04:01:41 +00:00
"cmp"
"encoding/json"
2018-01-16 17:09:33 +00:00
"fmt"
"io"
2022-06-14 06:24:34 +00:00
"net"
"net/http"
"os"
2024-02-20 04:01:41 +00:00
"slices"
2022-06-14 06:24:34 +00:00
"strconv"
2023-06-30 09:35:37 +00:00
"time"
2018-12-09 14:06:22 +00:00
"github.com/fatedier/frp/client/proxy"
2020-09-23 05:49:14 +00:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/config/v1/validation"
2023-11-27 07:47:49 +00:00
httppkg "github.com/fatedier/frp/pkg/util/http"
2020-09-23 05:49:14 +00:00
"github.com/fatedier/frp/pkg/util/log"
2023-11-27 07:47:49 +00:00
netpkg "github.com/fatedier/frp/pkg/util/net"
)
type GeneralResponse struct {
2019-02-11 03:26:06 +00:00
Code int
Msg string
}
2023-11-27 07:47:49 +00:00
func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper) {
helper.Router.HandleFunc("/healthz", svr.healthz)
subRouter := helper.Router.NewRoute().Subrouter()
subRouter.Use(helper.AuthMiddleware.Middleware)
// api, see admin_api.go
subRouter.HandleFunc("/api/reload", svr.apiReload).Methods("GET")
subRouter.HandleFunc("/api/stop", svr.apiStop).Methods("POST")
subRouter.HandleFunc("/api/status", svr.apiStatus).Methods("GET")
subRouter.HandleFunc("/api/config", svr.apiGetConfig).Methods("GET")
subRouter.HandleFunc("/api/config", svr.apiPutConfig).Methods("PUT")
// view
subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET")
subRouter.PathPrefix("/static/").Handler(
netpkg.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(helper.AssetsFS))),
).Methods("GET")
subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
})
}
2021-08-04 06:33:53 +00:00
// /healthz
2023-07-21 02:30:46 +00:00
func (svr *Service) healthz(w http.ResponseWriter, _ *http.Request) {
2021-08-04 06:33:53 +00:00
w.WriteHeader(200)
}
2023-06-30 09:35:37 +00:00
// GET /api/reload
2023-11-16 13:03:36 +00:00
func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
2019-02-11 03:26:06 +00:00
res := GeneralResponse{Code: 200}
2023-11-16 13:03:36 +00:00
strictConfigMode := false
strictStr := r.URL.Query().Get("strictConfig")
if strictStr != "" {
strictConfigMode, _ = strconv.ParseBool(strictStr)
}
2019-01-31 10:35:44 +00:00
2024-03-12 05:58:53 +00:00
log.Infof("api request [/api/reload]")
defer func() {
2024-03-12 05:58:53 +00:00
log.Infof("api response [/api/reload], code [%d]", res.Code)
2019-02-11 03:26:06 +00:00
w.WriteHeader(res.Code)
if len(res.Msg) > 0 {
2022-08-28 17:02:53 +00:00
_, _ = w.Write([]byte(res.Msg))
2019-02-11 03:26:06 +00:00
}
}()
2023-11-27 07:47:49 +00:00
cliCfg, proxyCfgs, visitorCfgs, _, err := config.LoadClientConfig(svr.configFilePath, strictConfigMode)
if err != nil {
2019-02-11 03:26:06 +00:00
res.Code = 400
res.Msg = err.Error()
2024-03-12 05:58:53 +00:00
log.Warnf("reload frpc proxy config error: %s", res.Msg)
return
}
2023-11-27 07:47:49 +00:00
if _, err := validation.ValidateAllClientConfig(cliCfg, proxyCfgs, visitorCfgs); err != nil {
res.Code = 400
res.Msg = err.Error()
2024-03-12 05:58:53 +00:00
log.Warnf("reload frpc proxy config error: %s", res.Msg)
return
}
2023-11-27 07:47:49 +00:00
if err := svr.UpdateAllConfigurer(proxyCfgs, visitorCfgs); err != nil {
2019-02-11 03:26:06 +00:00
res.Code = 500
2018-01-16 17:09:33 +00:00
res.Msg = err.Error()
2024-03-12 05:58:53 +00:00
log.Warnf("reload frpc proxy config error: %s", res.Msg)
2018-01-16 17:09:33 +00:00
return
}
2024-03-12 05:58:53 +00:00
log.Infof("success reload conf")
}
2018-01-16 17:09:33 +00:00
2023-06-30 09:35:37 +00:00
// POST /api/stop
2023-07-21 02:30:46 +00:00
func (svr *Service) apiStop(w http.ResponseWriter, _ *http.Request) {
2023-06-30 09:35:37 +00:00
res := GeneralResponse{Code: 200}
2024-03-12 05:58:53 +00:00
log.Infof("api request [/api/stop]")
2023-06-30 09:35:37 +00:00
defer func() {
2024-03-12 05:58:53 +00:00
log.Infof("api response [/api/stop], code [%d]", res.Code)
2023-06-30 09:35:37 +00:00
w.WriteHeader(res.Code)
if len(res.Msg) > 0 {
_, _ = w.Write([]byte(res.Msg))
}
}()
go svr.GracefulClose(100 * time.Millisecond)
}
2023-02-25 18:54:53 +00:00
type StatusResp map[string][]ProxyStatusResp
2018-01-16 17:09:33 +00:00
type ProxyStatusResp struct {
Name string `json:"name"`
Type string `json:"type"`
Status string `json:"status"`
Err string `json:"err"`
LocalAddr string `json:"local_addr"`
Plugin string `json:"plugin"`
RemoteAddr string `json:"remote_addr"`
}
2020-05-24 09:48:37 +00:00
func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxyStatusResp {
2018-01-16 17:09:33 +00:00
psr := ProxyStatusResp{
Name: status.Name,
Type: status.Type,
2020-05-24 09:48:37 +00:00
Status: status.Phase,
2018-01-16 17:09:33 +00:00
Err: status.Err,
}
2023-05-30 12:25:22 +00:00
baseCfg := status.Cfg.GetBaseConfig()
2023-02-25 18:54:53 +00:00
if baseCfg.LocalPort != 0 {
psr.LocalAddr = net.JoinHostPort(baseCfg.LocalIP, strconv.Itoa(baseCfg.LocalPort))
}
psr.Plugin = baseCfg.Plugin.Type
2023-02-25 18:54:53 +00:00
if status.Err == "" {
2018-01-17 06:40:08 +00:00
psr.RemoteAddr = status.RemoteAddr
2024-02-20 04:01:41 +00:00
if slices.Contains([]string{"tcp", "udp"}, status.Type) {
2023-02-25 18:54:53 +00:00
psr.RemoteAddr = serverAddr + psr.RemoteAddr
2018-01-16 17:09:33 +00:00
}
}
return psr
}
2023-06-30 09:35:37 +00:00
// GET /api/status
2023-07-21 02:30:46 +00:00
func (svr *Service) apiStatus(w http.ResponseWriter, _ *http.Request) {
2018-01-16 17:09:33 +00:00
var (
buf []byte
2023-02-25 18:54:53 +00:00
res StatusResp = make(map[string][]ProxyStatusResp)
2018-01-16 17:09:33 +00:00
)
2019-01-31 10:35:44 +00:00
2024-03-12 05:58:53 +00:00
log.Infof("Http request [/api/status]")
2018-01-16 17:09:33 +00:00
defer func() {
2024-03-12 05:58:53 +00:00
log.Infof("Http response [/api/status]")
2018-01-16 17:09:33 +00:00
buf, _ = json.Marshal(&res)
2022-08-28 17:02:53 +00:00
_, _ = w.Write(buf)
2018-01-16 17:09:33 +00:00
}()
svr.ctlMu.RLock()
ctl := svr.ctl
svr.ctlMu.RUnlock()
if ctl == nil {
return
}
ps := ctl.pm.GetAllProxyStatus()
2018-01-16 17:09:33 +00:00
for _, status := range ps {
2023-11-27 07:47:49 +00:00
res[status.Type] = append(res[status.Type], NewProxyStatusResp(status, svr.common.ServerAddr))
2023-02-25 18:54:53 +00:00
}
for _, arrs := range res {
if len(arrs) <= 1 {
continue
2018-01-16 17:09:33 +00:00
}
2024-02-20 04:01:41 +00:00
slices.SortFunc(arrs, func(a, b ProxyStatusResp) int {
return cmp.Compare(a.Name, b.Name)
2023-02-25 18:54:53 +00:00
})
2018-01-16 17:09:33 +00:00
}
}
2019-01-31 09:17:34 +00:00
2023-06-30 09:35:37 +00:00
// GET /api/config
2023-07-21 02:30:46 +00:00
func (svr *Service) apiGetConfig(w http.ResponseWriter, _ *http.Request) {
2019-02-11 03:26:06 +00:00
res := GeneralResponse{Code: 200}
2019-01-31 10:35:44 +00:00
2024-03-12 05:58:53 +00:00
log.Infof("Http get request [/api/config]")
2019-01-31 09:17:34 +00:00
defer func() {
2024-03-12 05:58:53 +00:00
log.Infof("Http get response [/api/config], code [%d]", res.Code)
2019-02-11 03:26:06 +00:00
w.WriteHeader(res.Code)
if len(res.Msg) > 0 {
2022-08-28 17:02:53 +00:00
_, _ = w.Write([]byte(res.Msg))
2019-01-31 09:17:34 +00:00
}
}()
2023-11-27 07:47:49 +00:00
if svr.configFilePath == "" {
2019-02-11 03:26:06 +00:00
res.Code = 400
res.Msg = "frpc has no config file path"
2024-03-12 05:58:53 +00:00
log.Warnf("%s", res.Msg)
2019-01-31 09:17:34 +00:00
return
}
2023-11-27 07:47:49 +00:00
content, err := os.ReadFile(svr.configFilePath)
2019-01-31 09:17:34 +00:00
if err != nil {
2019-02-11 03:26:06 +00:00
res.Code = 400
2019-01-31 09:17:34 +00:00
res.Msg = err.Error()
2024-03-12 05:58:53 +00:00
log.Warnf("load frpc config file error: %s", res.Msg)
2019-01-31 09:17:34 +00:00
return
}
res.Msg = string(content)
2019-01-31 09:17:34 +00:00
}
2019-01-31 10:35:44 +00:00
2023-06-30 09:35:37 +00:00
// PUT /api/config
2019-01-31 10:35:44 +00:00
func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
2019-02-11 03:26:06 +00:00
res := GeneralResponse{Code: 200}
2019-01-31 10:35:44 +00:00
2024-03-12 05:58:53 +00:00
log.Infof("Http put request [/api/config]")
2019-01-31 10:35:44 +00:00
defer func() {
2024-03-12 05:58:53 +00:00
log.Infof("Http put response [/api/config], code [%d]", res.Code)
2019-02-11 03:26:06 +00:00
w.WriteHeader(res.Code)
if len(res.Msg) > 0 {
2022-08-28 17:02:53 +00:00
_, _ = w.Write([]byte(res.Msg))
2019-02-11 03:26:06 +00:00
}
2019-01-31 10:35:44 +00:00
}()
// get new config content
body, err := io.ReadAll(r.Body)
2019-01-31 10:35:44 +00:00
if err != nil {
2019-02-11 03:26:06 +00:00
res.Code = 400
2019-01-31 10:35:44 +00:00
res.Msg = fmt.Sprintf("read request body error: %v", err)
2024-03-12 05:58:53 +00:00
log.Warnf("%s", res.Msg)
2019-01-31 10:35:44 +00:00
return
}
if len(body) == 0 {
2019-02-11 03:26:06 +00:00
res.Code = 400
res.Msg = "body can't be empty"
2024-03-12 05:58:53 +00:00
log.Warnf("%s", res.Msg)
return
}
if err := os.WriteFile(svr.configFilePath, body, 0o600); err != nil {
2019-02-11 03:26:06 +00:00
res.Code = 500
2019-01-31 10:35:44 +00:00
res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)
2024-03-12 05:58:53 +00:00
log.Warnf("%s", res.Msg)
2019-01-31 10:35:44 +00:00
return
}
}