2017-07-13 02:20:49 +08:00
|
|
|
// 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-17 01:09:33 +08:00
|
|
|
"fmt"
|
2019-01-31 18:35:44 +08:00
|
|
|
"io/ioutil"
|
2017-07-13 02:20:49 +08:00
|
|
|
"net/http"
|
2018-01-17 01:09:33 +08:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-07-13 02:20:49 +08:00
|
|
|
|
2018-12-09 22:06:22 +08:00
|
|
|
"github.com/fatedier/frp/client/proxy"
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/config"
|
|
|
|
"github.com/fatedier/frp/pkg/util/log"
|
2017-07-13 02:20:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type GeneralResponse struct {
|
2019-02-11 11:26:06 +08:00
|
|
|
Code int
|
|
|
|
Msg string
|
2017-07-13 02:20:49 +08:00
|
|
|
}
|
|
|
|
|
2019-01-31 18:35:44 +08:00
|
|
|
// GET api/reload
|
2018-05-20 19:06:05 +08:00
|
|
|
func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 11:26:06 +08:00
|
|
|
res := GeneralResponse{Code: 200}
|
2019-01-31 18:35:44 +08:00
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
log.Info("api request [/api/reload]")
|
2017-07-13 02:20:49 +08:00
|
|
|
defer func() {
|
2021-06-02 23:54:22 +08:00
|
|
|
log.Info("api response [/api/reload], code [%d]", res.Code)
|
2019-02-11 11:26:06 +08:00
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
if len(res.Msg) > 0 {
|
|
|
|
w.Write([]byte(res.Msg))
|
|
|
|
}
|
2017-07-13 02:20:49 +08:00
|
|
|
}()
|
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
_, pxyCfgs, visitorCfgs, err := config.ParseClientConfig(svr.cfgFile)
|
2017-07-13 02:20:49 +08:00
|
|
|
if err != nil {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 400
|
2017-07-13 02:20:49 +08:00
|
|
|
res.Msg = err.Error()
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Warn("reload frpc proxy config error: %s", res.Msg)
|
2017-07-13 02:20:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
if err = svr.ReloadConf(pxyCfgs, visitorCfgs); err != nil {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 500
|
2018-01-17 01:09:33 +08:00
|
|
|
res.Msg = err.Error()
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Warn("reload frpc proxy config error: %s", res.Msg)
|
2018-01-17 01:09:33 +08:00
|
|
|
return
|
|
|
|
}
|
2017-07-13 02:20:49 +08:00
|
|
|
log.Info("success reload conf")
|
|
|
|
return
|
|
|
|
}
|
2018-01-17 01:09:33 +08:00
|
|
|
|
|
|
|
type StatusResp struct {
|
2020-05-24 17:48:37 +08:00
|
|
|
TCP []ProxyStatusResp `json:"tcp"`
|
|
|
|
UDP []ProxyStatusResp `json:"udp"`
|
|
|
|
HTTP []ProxyStatusResp `json:"http"`
|
|
|
|
HTTPS []ProxyStatusResp `json:"https"`
|
|
|
|
STCP []ProxyStatusResp `json:"stcp"`
|
|
|
|
XTCP []ProxyStatusResp `json:"xtcp"`
|
|
|
|
SUDP []ProxyStatusResp `json:"sudp"`
|
2018-01-17 01:09:33 +08: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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ByProxyStatusResp []ProxyStatusResp
|
|
|
|
|
|
|
|
func (a ByProxyStatusResp) Len() int { return len(a) }
|
|
|
|
func (a ByProxyStatusResp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByProxyStatusResp) Less(i, j int) bool { return strings.Compare(a[i].Name, a[j].Name) < 0 }
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewProxyStatusResp(status *proxy.WorkingStatus, serverAddr string) ProxyStatusResp {
|
2018-01-17 01:09:33 +08:00
|
|
|
psr := ProxyStatusResp{
|
|
|
|
Name: status.Name,
|
|
|
|
Type: status.Type,
|
2020-05-24 17:48:37 +08:00
|
|
|
Status: status.Phase,
|
2018-01-17 01:09:33 +08:00
|
|
|
Err: status.Err,
|
|
|
|
}
|
|
|
|
switch cfg := status.Cfg.(type) {
|
2020-05-24 17:48:37 +08:00
|
|
|
case *config.TCPProxyConf:
|
2018-01-17 01:09:33 +08:00
|
|
|
if cfg.LocalPort != 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2018-01-17 21:49:37 +08:00
|
|
|
if status.Err != "" {
|
2019-08-21 04:53:27 +08:00
|
|
|
psr.RemoteAddr = fmt.Sprintf("%s:%d", serverAddr, cfg.RemotePort)
|
2018-01-17 21:49:37 +08:00
|
|
|
} else {
|
2019-08-21 04:53:27 +08:00
|
|
|
psr.RemoteAddr = serverAddr + status.RemoteAddr
|
2018-01-17 21:49:37 +08:00
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
case *config.UDPProxyConf:
|
2018-01-17 01:09:33 +08:00
|
|
|
if cfg.LocalPort != 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
2018-01-17 21:49:37 +08:00
|
|
|
if status.Err != "" {
|
2019-08-21 04:53:27 +08:00
|
|
|
psr.RemoteAddr = fmt.Sprintf("%s:%d", serverAddr, cfg.RemotePort)
|
2018-01-17 21:49:37 +08:00
|
|
|
} else {
|
2019-08-21 04:53:27 +08:00
|
|
|
psr.RemoteAddr = serverAddr + status.RemoteAddr
|
2018-01-17 21:49:37 +08:00
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
case *config.HTTPProxyConf:
|
2018-01-17 01:09:33 +08:00
|
|
|
if cfg.LocalPort != 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2018-01-17 14:40:08 +08:00
|
|
|
psr.RemoteAddr = status.RemoteAddr
|
2020-05-24 17:48:37 +08:00
|
|
|
case *config.HTTPSProxyConf:
|
2018-01-17 01:09:33 +08:00
|
|
|
if cfg.LocalPort != 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2018-01-17 14:40:08 +08:00
|
|
|
psr.RemoteAddr = status.RemoteAddr
|
2020-05-24 17:48:37 +08:00
|
|
|
case *config.STCPProxyConf:
|
2018-01-17 01:09:33 +08:00
|
|
|
if cfg.LocalPort != 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2020-05-24 17:48:37 +08:00
|
|
|
case *config.XTCPProxyConf:
|
2018-01-17 01:09:33 +08:00
|
|
|
if cfg.LocalPort != 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2020-05-24 17:48:37 +08:00
|
|
|
case *config.SUDPProxyConf:
|
2020-04-22 21:37:45 +08:00
|
|
|
if cfg.LocalPort != 0 {
|
2020-05-24 17:48:37 +08:00
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIP, cfg.LocalPort)
|
2020-04-22 21:37:45 +08:00
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
return psr
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:35:44 +08:00
|
|
|
// GET api/status
|
2018-05-20 19:06:05 +08:00
|
|
|
func (svr *Service) apiStatus(w http.ResponseWriter, r *http.Request) {
|
2018-01-17 01:09:33 +08:00
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res StatusResp
|
|
|
|
)
|
2020-05-24 17:48:37 +08:00
|
|
|
res.TCP = make([]ProxyStatusResp, 0)
|
|
|
|
res.UDP = make([]ProxyStatusResp, 0)
|
|
|
|
res.HTTP = make([]ProxyStatusResp, 0)
|
|
|
|
res.HTTPS = make([]ProxyStatusResp, 0)
|
|
|
|
res.STCP = make([]ProxyStatusResp, 0)
|
|
|
|
res.XTCP = make([]ProxyStatusResp, 0)
|
|
|
|
res.SUDP = make([]ProxyStatusResp, 0)
|
2019-01-31 18:35:44 +08:00
|
|
|
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Info("Http request [/api/status]")
|
2018-01-17 01:09:33 +08:00
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/status]")
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ps := svr.ctl.pm.GetAllProxyStatus()
|
|
|
|
for _, status := range ps {
|
|
|
|
switch status.Type {
|
|
|
|
case "tcp":
|
2020-05-24 17:48:37 +08:00
|
|
|
res.TCP = append(res.TCP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
|
2018-01-17 01:09:33 +08:00
|
|
|
case "udp":
|
2020-05-24 17:48:37 +08:00
|
|
|
res.UDP = append(res.UDP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
|
2018-01-17 01:09:33 +08:00
|
|
|
case "http":
|
2020-05-24 17:48:37 +08:00
|
|
|
res.HTTP = append(res.HTTP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
|
2018-01-17 01:09:33 +08:00
|
|
|
case "https":
|
2020-05-24 17:48:37 +08:00
|
|
|
res.HTTPS = append(res.HTTPS, NewProxyStatusResp(status, svr.cfg.ServerAddr))
|
2018-01-17 01:09:33 +08:00
|
|
|
case "stcp":
|
2020-05-24 17:48:37 +08:00
|
|
|
res.STCP = append(res.STCP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
|
2018-01-17 01:09:33 +08:00
|
|
|
case "xtcp":
|
2020-05-24 17:48:37 +08:00
|
|
|
res.XTCP = append(res.XTCP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
|
2020-04-22 21:37:45 +08:00
|
|
|
case "sudp":
|
2020-05-24 17:48:37 +08:00
|
|
|
res.SUDP = append(res.SUDP, NewProxyStatusResp(status, svr.cfg.ServerAddr))
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
sort.Sort(ByProxyStatusResp(res.TCP))
|
|
|
|
sort.Sort(ByProxyStatusResp(res.UDP))
|
|
|
|
sort.Sort(ByProxyStatusResp(res.HTTP))
|
|
|
|
sort.Sort(ByProxyStatusResp(res.HTTPS))
|
|
|
|
sort.Sort(ByProxyStatusResp(res.STCP))
|
|
|
|
sort.Sort(ByProxyStatusResp(res.XTCP))
|
|
|
|
sort.Sort(ByProxyStatusResp(res.SUDP))
|
2018-01-17 01:09:33 +08:00
|
|
|
return
|
|
|
|
}
|
2019-01-31 17:17:34 +08:00
|
|
|
|
2019-01-31 18:35:44 +08:00
|
|
|
// GET api/config
|
2019-01-31 17:17:34 +08:00
|
|
|
func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 11:26:06 +08:00
|
|
|
res := GeneralResponse{Code: 200}
|
2019-01-31 18:35:44 +08:00
|
|
|
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Info("Http get request [/api/config]")
|
2019-01-31 17:17:34 +08:00
|
|
|
defer func() {
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Info("Http get response [/api/config], code [%d]", res.Code)
|
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
if len(res.Msg) > 0 {
|
|
|
|
w.Write([]byte(res.Msg))
|
2019-01-31 17:17:34 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
if svr.cfgFile == "" {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 400
|
|
|
|
res.Msg = "frpc has no config file path"
|
2019-01-31 18:35:44 +08:00
|
|
|
log.Warn("%s", res.Msg)
|
2019-01-31 17:17:34 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
content, err := config.GetRenderedConfFromFile(svr.cfgFile)
|
2019-01-31 17:17:34 +08:00
|
|
|
if err != nil {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 400
|
2019-01-31 17:17:34 +08:00
|
|
|
res.Msg = err.Error()
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Warn("load frpc config file error: %s", res.Msg)
|
2019-01-31 17:17:34 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
rows := strings.Split(string(content), "\n")
|
2019-01-31 17:17:34 +08: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 11:26:06 +08:00
|
|
|
res.Msg = strings.Join(newRows, "\n")
|
2019-01-31 17:17:34 +08:00
|
|
|
}
|
2019-01-31 18:35:44 +08:00
|
|
|
|
|
|
|
// PUT api/config
|
|
|
|
func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
|
2019-02-11 11:26:06 +08:00
|
|
|
res := GeneralResponse{Code: 200}
|
2019-01-31 18:35:44 +08:00
|
|
|
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Info("Http put request [/api/config]")
|
2019-01-31 18:35:44 +08:00
|
|
|
defer func() {
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Info("Http put response [/api/config], code [%d]", res.Code)
|
|
|
|
w.WriteHeader(res.Code)
|
|
|
|
if len(res.Msg) > 0 {
|
|
|
|
w.Write([]byte(res.Msg))
|
|
|
|
}
|
2019-01-31 18:35:44 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
// get new config content
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 400
|
2019-01-31 18:35:44 +08:00
|
|
|
res.Msg = fmt.Sprintf("read request body error: %v", err)
|
|
|
|
log.Warn("%s", res.Msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-02 10:49:24 +08:00
|
|
|
if len(body) == 0 {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 400
|
2019-02-02 10:49:24 +08:00
|
|
|
res.Msg = "body can't be empty"
|
|
|
|
log.Warn("%s", res.Msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:35:44 +08:00
|
|
|
// get token from origin content
|
|
|
|
token := ""
|
2019-08-21 04:53:27 +08:00
|
|
|
b, err := ioutil.ReadFile(svr.cfgFile)
|
2019-01-31 18:35:44 +08:00
|
|
|
if err != nil {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 400
|
2019-01-31 18:35:44 +08:00
|
|
|
res.Msg = err.Error()
|
2019-02-11 11:26:06 +08:00
|
|
|
log.Warn("load frpc config file error: %s", res.Msg)
|
2019-01-31 18:35:44 +08: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 14:59:30 +08:00
|
|
|
} else {
|
|
|
|
newRows = tmpRows
|
2019-01-31 18:35:44 +08:00
|
|
|
}
|
|
|
|
content = strings.Join(newRows, "\n")
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
err = ioutil.WriteFile(svr.cfgFile, []byte(content), 0644)
|
2019-01-31 18:35:44 +08:00
|
|
|
if err != nil {
|
2019-02-11 11:26:06 +08:00
|
|
|
res.Code = 500
|
2019-01-31 18:35:44 +08:00
|
|
|
res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)
|
|
|
|
log.Warn("%s", res.Msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|