frp/client/admin_api.go

256 lines
5.9 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 (
"encoding/json"
2018-01-16 18:09:33 +01:00
"fmt"
"io"
2022-06-14 08:24:34 +02:00
"net"
"net/http"
"os"
2018-01-16 18:09:33 +01:00
"sort"
2022-06-14 08:24:34 +02:00
"strconv"
2018-01-16 18:09:33 +01:00
"strings"
"github.com/samber/lo"
2018-12-09 15:06:22 +01:00
"github.com/fatedier/frp/client/proxy"
2020-09-23 07:49:14 +02:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/util/log"
)
type GeneralResponse struct {
2019-02-11 04:26:06 +01:00
Code int
Msg string
}
2021-08-04 08:33:53 +02:00
// /healthz
func (svr *Service) healthz(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}
2019-01-31 11:35:44 +01:00
// GET api/reload
2018-05-20 13:06:05 +02:00
func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
2019-02-11 04:26:06 +01:00
res := GeneralResponse{Code: 200}
2019-01-31 11:35:44 +01:00
log.Info("api request [/api/reload]")
defer func() {
log.Info("api response [/api/reload], code [%d]", res.Code)
2019-02-11 04:26:06 +01:00
w.WriteHeader(res.Code)
if len(res.Msg) > 0 {
2022-08-28 19:02:53 +02:00
_, _ = w.Write([]byte(res.Msg))
2019-02-11 04:26:06 +01:00
}
}()
_, pxyCfgs, visitorCfgs, err := config.ParseClientConfig(svr.cfgFile)
if err != nil {
2019-02-11 04:26:06 +01:00
res.Code = 400
res.Msg = err.Error()
2019-02-11 04:26:06 +01:00
log.Warn("reload frpc proxy config error: %s", res.Msg)
return
}
if err = svr.ReloadConf(pxyCfgs, visitorCfgs); err != nil {
2019-02-11 04:26:06 +01:00
res.Code = 500
2018-01-16 18:09:33 +01:00
res.Msg = err.Error()
2019-02-11 04:26:06 +01:00
log.Warn("reload frpc proxy config error: %s", res.Msg)
2018-01-16 18:09:33 +01:00
return
}
log.Info("success reload conf")
}
2018-01-16 18:09:33 +01:00
2023-02-25 19:54:53 +01:00
type StatusResp map[string][]ProxyStatusResp
2018-01-16 18:09:33 +01: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 11:48:37 +02:00
func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxyStatusResp {
2018-01-16 18:09:33 +01:00
psr := ProxyStatusResp{
Name: status.Name,
Type: status.Type,
2020-05-24 11:48:37 +02:00
Status: status.Phase,
2018-01-16 18:09:33 +01:00
Err: status.Err,
}
2023-05-30 14:25:22 +02:00
baseCfg := status.Cfg.GetBaseConfig()
2023-02-25 19:54:53 +01:00
if baseCfg.LocalPort != 0 {
psr.LocalAddr = net.JoinHostPort(baseCfg.LocalIP, strconv.Itoa(baseCfg.LocalPort))
}
psr.Plugin = baseCfg.Plugin
if status.Err == "" {
2018-01-17 07:40:08 +01:00
psr.RemoteAddr = status.RemoteAddr
if lo.Contains([]string{"tcp", "udp"}, status.Type) {
2023-02-25 19:54:53 +01:00
psr.RemoteAddr = serverAddr + psr.RemoteAddr
2018-01-16 18:09:33 +01:00
}
}
return psr
}
2019-01-31 11:35:44 +01:00
// GET api/status
2018-05-20 13:06:05 +02:00
func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) {
2018-01-16 18:09:33 +01:00
var (
buf []byte
2023-02-25 19:54:53 +01:00
res StatusResp = make(map[string][]ProxyStatusResp)
2018-01-16 18:09:33 +01:00
)
2019-01-31 11:35:44 +01:00
2019-02-11 04:26:06 +01:00
log.Info("Http request [/api/status]")
2018-01-16 18:09:33 +01:00
defer func() {
log.Info("Http response [/api/status]")
buf, _ = json.Marshal(&res)
2022-08-28 19:02:53 +02:00
_, _ = w.Write(buf)
2018-01-16 18:09:33 +01:00
}()
ps := svr.ctl.pm.GetAllProxyStatus()
for _, status := range ps {
2023-02-25 19:54:53 +01:00
res[status.Type] = append(res[status.Type], NewProxyStatusResp(status, svr.cfg.ServerAddr))
}
for _, arrs := range res {
if len(arrs) <= 1 {
continue
2018-01-16 18:09:33 +01:00
}
2023-02-25 19:54:53 +01:00
sort.Slice(arrs, func(i, j int) bool {
return strings.Compare(arrs[i].Name, arrs[j].Name) < 0
})
2018-01-16 18:09:33 +01:00
}
}
2019-01-31 10:17:34 +01:00
2019-01-31 11:35:44 +01:00
// GET api/config
2019-01-31 10:17:34 +01:00
func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) {
2019-02-11 04:26:06 +01:00
res := GeneralResponse{Code: 200}
2019-01-31 11:35:44 +01:00
2019-02-11 04:26:06 +01:00
log.Info("Http get request [/api/config]")
2019-01-31 10:17:34 +01:00
defer func() {
2019-02-11 04:26:06 +01:00
log.Info("Http get response [/api/config], code [%d]", res.Code)
w.WriteHeader(res.Code)
if len(res.Msg) > 0 {
2022-08-28 19:02:53 +02:00
_, _ = w.Write([]byte(res.Msg))
2019-01-31 10:17:34 +01:00
}
}()
if svr.cfgFile == "" {
2019-02-11 04:26:06 +01:00
res.Code = 400
res.Msg = "frpc has no config file path"
2019-01-31 11:35:44 +01:00
log.Warn("%s", res.Msg)
2019-01-31 10:17:34 +01:00
return
}
content, err := config.GetRenderedConfFromFile(svr.cfgFile)
2019-01-31 10:17:34 +01:00
if err != nil {
2019-02-11 04:26:06 +01:00
res.Code = 400
2019-01-31 10:17:34 +01:00
res.Msg = err.Error()
2019-02-11 04:26:06 +01:00
log.Warn("load frpc config file error: %s", res.Msg)
2019-01-31 10:17:34 +01:00
return
}
rows := strings.Split(string(content), "\n")
2019-01-31 10:17:34 +01:00
newRows := make([]string, 0, len(rows))
for _, row := range rows {
row = strings.TrimSpace(row)
if strings.HasPrefix(row, "token") {
continue
}
newRows = append(newRows, row)
}
2019-02-11 04:26:06 +01:00
res.Msg = strings.Join(newRows, "\n")
2019-01-31 10:17:34 +01:00
}
2019-01-31 11:35:44 +01:00
// PUT api/config
func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
2019-02-11 04:26:06 +01:00
res := GeneralResponse{Code: 200}
2019-01-31 11:35:44 +01:00
2019-02-11 04:26:06 +01:00
log.Info("Http put request [/api/config]")
2019-01-31 11:35:44 +01:00
defer func() {
2019-02-11 04:26:06 +01:00
log.Info("Http put response [/api/config], code [%d]", res.Code)
w.WriteHeader(res.Code)
if len(res.Msg) > 0 {
2022-08-28 19:02:53 +02:00
_, _ = w.Write([]byte(res.Msg))
2019-02-11 04:26:06 +01:00
}
2019-01-31 11:35:44 +01:00
}()
// get new config content
body, err := io.ReadAll(r.Body)
2019-01-31 11:35:44 +01:00
if err != nil {
2019-02-11 04:26:06 +01:00
res.Code = 400
2019-01-31 11:35:44 +01:00
res.Msg = fmt.Sprintf("read request body error: %v", err)
log.Warn("%s", res.Msg)
return
}
if len(body) == 0 {
2019-02-11 04:26:06 +01:00
res.Code = 400
res.Msg = "body can't be empty"
log.Warn("%s", res.Msg)
return
}
2019-01-31 11:35:44 +01:00
// get token from origin content
token := ""
b, err := os.ReadFile(svr.cfgFile)
2019-01-31 11:35:44 +01:00
if err != nil {
2019-02-11 04:26:06 +01:00
res.Code = 400
2019-01-31 11:35:44 +01:00
res.Msg = err.Error()
2019-02-11 04:26:06 +01:00
log.Warn("load frpc config file error: %s", res.Msg)
2019-01-31 11:35:44 +01:00
return
}
content := string(b)
for _, row := range strings.Split(content, "\n") {
row = strings.TrimSpace(row)
if strings.HasPrefix(row, "token") {
token = row
break
}
}
tmpRows := make([]string, 0)
for _, row := range strings.Split(string(body), "\n") {
row = strings.TrimSpace(row)
if strings.HasPrefix(row, "token") {
continue
}
tmpRows = append(tmpRows, row)
}
newRows := make([]string, 0)
if token != "" {
for _, row := range tmpRows {
newRows = append(newRows, row)
if strings.HasPrefix(row, "[common]") {
newRows = append(newRows, token)
}
}
2019-02-12 07:59:30 +01:00
} else {
newRows = tmpRows
2019-01-31 11:35:44 +01:00
}
content = strings.Join(newRows, "\n")
2022-08-28 19:02:53 +02:00
err = os.WriteFile(svr.cfgFile, []byte(content), 0o644)
2019-01-31 11:35:44 +01:00
if err != nil {
2019-02-11 04:26:06 +01:00
res.Code = 500
2019-01-31 11:35:44 +01:00
res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)
log.Warn("%s", res.Msg)
return
}
}