2017-07-12 20:20:49 +02: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-16 18:09:33 +01:00
|
|
|
"fmt"
|
2017-07-12 20:20:49 +02:00
|
|
|
"net/http"
|
2018-01-16 18:09:33 +01:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-07-12 20:20:49 +02:00
|
|
|
|
2018-12-09 15:06:22 +01:00
|
|
|
"github.com/fatedier/frp/client/proxy"
|
2018-04-10 11:46:49 +02:00
|
|
|
"github.com/fatedier/frp/g"
|
2017-07-12 20:20:49 +02:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2018-05-20 13:06:05 +02:00
|
|
|
func (svr *Service) apiReload(w http.ResponseWriter, r *http.Request) {
|
2017-07-12 20:20:49 +02:00
|
|
|
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]")
|
|
|
|
|
2018-12-11 04:46:12 +01:00
|
|
|
content, err := config.GetRenderedConfFromFile(g.GlbClientCfg.CfgFile)
|
2017-07-12 20:20:49 +02:00
|
|
|
if err != nil {
|
|
|
|
res.Code = 1
|
|
|
|
res.Msg = err.Error()
|
|
|
|
log.Error("reload frpc config file error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 11:46:49 +02:00
|
|
|
newCommonCfg, err := config.UnmarshalClientConfFromIni(nil, content)
|
2017-07-12 20:20:49 +02:00
|
|
|
if err != nil {
|
|
|
|
res.Code = 2
|
|
|
|
res.Msg = err.Error()
|
|
|
|
log.Error("reload frpc common section error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-11 04:46:12 +01:00
|
|
|
pxyCfgs, visitorCfgs, err := config.LoadAllConfFromIni(g.GlbClientCfg.User, content, newCommonCfg.Start)
|
2017-07-12 20:20:49 +02:00
|
|
|
if err != nil {
|
|
|
|
res.Code = 3
|
|
|
|
res.Msg = err.Error()
|
|
|
|
log.Error("reload frpc proxy config error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-30 04:12:28 +01:00
|
|
|
err = svr.ReloadConf(pxyCfgs, visitorCfgs)
|
2018-01-16 18:09:33 +01:00
|
|
|
if err != nil {
|
|
|
|
res.Code = 4
|
|
|
|
res.Msg = err.Error()
|
|
|
|
log.Error("reload frpc proxy config error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-12 20:20:49 +02:00
|
|
|
log.Info("success reload conf")
|
|
|
|
return
|
|
|
|
}
|
2018-01-16 18:09:33 +01:00
|
|
|
|
|
|
|
type StatusResp struct {
|
|
|
|
Tcp []ProxyStatusResp `json:"tcp"`
|
|
|
|
Udp []ProxyStatusResp `json:"udp"`
|
|
|
|
Http []ProxyStatusResp `json:"http"`
|
|
|
|
Https []ProxyStatusResp `json:"https"`
|
|
|
|
Stcp []ProxyStatusResp `json:"stcp"`
|
|
|
|
Xtcp []ProxyStatusResp `json:"xtcp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2018-12-09 15:06:22 +01:00
|
|
|
func NewProxyStatusResp(status *proxy.ProxyStatus) ProxyStatusResp {
|
2018-01-16 18:09:33 +01:00
|
|
|
psr := ProxyStatusResp{
|
|
|
|
Name: status.Name,
|
|
|
|
Type: status.Type,
|
|
|
|
Status: status.Status,
|
|
|
|
Err: status.Err,
|
|
|
|
}
|
|
|
|
switch cfg := status.Cfg.(type) {
|
|
|
|
case *config.TcpProxyConf:
|
|
|
|
if cfg.LocalPort != 0 {
|
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
|
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2018-01-17 14:49:37 +01:00
|
|
|
if status.Err != "" {
|
2018-04-10 11:46:49 +02:00
|
|
|
psr.RemoteAddr = fmt.Sprintf("%s:%d", g.GlbClientCfg.ServerAddr, cfg.RemotePort)
|
2018-01-17 14:49:37 +01:00
|
|
|
} else {
|
2018-04-10 11:46:49 +02:00
|
|
|
psr.RemoteAddr = g.GlbClientCfg.ServerAddr + status.RemoteAddr
|
2018-01-17 14:49:37 +01:00
|
|
|
}
|
2018-01-16 18:09:33 +01:00
|
|
|
case *config.UdpProxyConf:
|
|
|
|
if cfg.LocalPort != 0 {
|
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
|
|
|
|
}
|
2018-01-17 14:49:37 +01:00
|
|
|
if status.Err != "" {
|
2018-04-10 11:46:49 +02:00
|
|
|
psr.RemoteAddr = fmt.Sprintf("%s:%d", g.GlbClientCfg.ServerAddr, cfg.RemotePort)
|
2018-01-17 14:49:37 +01:00
|
|
|
} else {
|
2018-04-10 11:46:49 +02:00
|
|
|
psr.RemoteAddr = g.GlbClientCfg.ServerAddr + status.RemoteAddr
|
2018-01-17 14:49:37 +01:00
|
|
|
}
|
2018-01-16 18:09:33 +01:00
|
|
|
case *config.HttpProxyConf:
|
|
|
|
if cfg.LocalPort != 0 {
|
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
|
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2018-01-17 07:40:08 +01:00
|
|
|
psr.RemoteAddr = status.RemoteAddr
|
2018-01-16 18:09:33 +01:00
|
|
|
case *config.HttpsProxyConf:
|
|
|
|
if cfg.LocalPort != 0 {
|
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
|
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
2018-01-17 07:40:08 +01:00
|
|
|
psr.RemoteAddr = status.RemoteAddr
|
2018-01-16 18:09:33 +01:00
|
|
|
case *config.StcpProxyConf:
|
|
|
|
if cfg.LocalPort != 0 {
|
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
|
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
|
|
|
case *config.XtcpProxyConf:
|
|
|
|
if cfg.LocalPort != 0 {
|
|
|
|
psr.LocalAddr = fmt.Sprintf("%s:%d", cfg.LocalIp, cfg.LocalPort)
|
|
|
|
}
|
|
|
|
psr.Plugin = cfg.Plugin
|
|
|
|
}
|
|
|
|
return psr
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
res StatusResp
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http response [/api/status]")
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Info("Http request: [/api/status]")
|
|
|
|
|
|
|
|
ps := svr.ctl.pm.GetAllProxyStatus()
|
|
|
|
for _, status := range ps {
|
|
|
|
switch status.Type {
|
|
|
|
case "tcp":
|
|
|
|
res.Tcp = append(res.Tcp, NewProxyStatusResp(status))
|
|
|
|
case "udp":
|
|
|
|
res.Udp = append(res.Udp, NewProxyStatusResp(status))
|
|
|
|
case "http":
|
|
|
|
res.Http = append(res.Http, NewProxyStatusResp(status))
|
|
|
|
case "https":
|
|
|
|
res.Https = append(res.Https, NewProxyStatusResp(status))
|
|
|
|
case "stcp":
|
|
|
|
res.Stcp = append(res.Stcp, NewProxyStatusResp(status))
|
|
|
|
case "xtcp":
|
|
|
|
res.Xtcp = append(res.Xtcp, NewProxyStatusResp(status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
return
|
|
|
|
}
|
2019-01-31 10:17:34 +01:00
|
|
|
|
|
|
|
// api/config
|
|
|
|
func (svr *Service) apiGetConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
res GeneralResponse
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
log.Info("Http get response [/api/config]")
|
|
|
|
if len(buf) > 0 {
|
|
|
|
w.Write(buf)
|
|
|
|
} else {
|
|
|
|
buf, _ = json.Marshal(&res)
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
log.Info("Http get request: [/api/config]")
|
|
|
|
|
|
|
|
if g.GlbClientCfg.CfgFile == "" {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
res.Code = 1
|
|
|
|
res.Msg = "frpc don't configure a config file path"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := config.GetRenderedConfFromFile(g.GlbClientCfg.CfgFile)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
res.Code = 2
|
|
|
|
res.Msg = err.Error()
|
|
|
|
log.Error("load frpc config file error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rows := strings.Split(content, "\n")
|
|
|
|
newRows := make([]string, 0, len(rows))
|
|
|
|
for _, row := range rows {
|
|
|
|
row = strings.TrimSpace(row)
|
|
|
|
if strings.HasPrefix(row, "token") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newRows = append(newRows, row)
|
|
|
|
}
|
|
|
|
buf = []byte(strings.Join(newRows, "\n"))
|
|
|
|
}
|