2018-04-10 17:46:49 +08:00
|
|
|
// Copyright 2018 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 sub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rodaine/table"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/client"
|
2019-01-15 23:41:06 +08:00
|
|
|
"github.com/fatedier/frp/models/config"
|
2018-04-10 17:46:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(statusCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var statusCmd = &cobra.Command{
|
|
|
|
Use: "status",
|
|
|
|
Short: "Overview of all proxies status",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-01-15 23:41:06 +08:00
|
|
|
iniContent, err := config.GetRenderedConfFromFile(cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
clientCfg, err := parseClientCommonCfg(CfgFileTypeIni, iniContent)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
err = status(clientCfg)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("frpc get status error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
func status(clientCfg config.ClientCommonConf) error {
|
|
|
|
if clientCfg.AdminPort == 0 {
|
2018-04-10 17:46:49 +08:00
|
|
|
return fmt.Errorf("admin_port shoud be set if you want to get proxy status")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "http://"+
|
2019-08-21 04:53:27 +08:00
|
|
|
clientCfg.AdminAddr+":"+fmt.Sprintf("%d", clientCfg.AdminPort)+"/api/status", nil)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-21 04:53:27 +08:00
|
|
|
authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(clientCfg.AdminUser+":"+
|
|
|
|
clientCfg.AdminPwd))
|
2018-04-10 17:46:49 +08:00
|
|
|
|
|
|
|
req.Header.Add("Authorization", authStr)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
2019-05-31 15:56:05 +08:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2019-05-30 12:11:59 +08:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
|
|
|
|
}
|
2018-04-10 17:46:49 +08:00
|
|
|
|
2019-05-30 12:11:59 +08:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res := &client.StatusResp{}
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Proxy Status...")
|
|
|
|
if len(res.Tcp) > 0 {
|
|
|
|
fmt.Printf("TCP")
|
|
|
|
tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
|
|
|
|
for _, ps := range res.Tcp {
|
|
|
|
tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
tbl.Print()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
if len(res.Udp) > 0 {
|
|
|
|
fmt.Printf("UDP")
|
|
|
|
tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
|
|
|
|
for _, ps := range res.Udp {
|
|
|
|
tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
tbl.Print()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
if len(res.Http) > 0 {
|
|
|
|
fmt.Printf("HTTP")
|
|
|
|
tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
|
|
|
|
for _, ps := range res.Http {
|
|
|
|
tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
tbl.Print()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
if len(res.Https) > 0 {
|
|
|
|
fmt.Printf("HTTPS")
|
|
|
|
tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
|
|
|
|
for _, ps := range res.Https {
|
|
|
|
tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
tbl.Print()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
if len(res.Stcp) > 0 {
|
|
|
|
fmt.Printf("STCP")
|
|
|
|
tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
|
|
|
|
for _, ps := range res.Stcp {
|
|
|
|
tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
tbl.Print()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
if len(res.Xtcp) > 0 {
|
|
|
|
fmt.Printf("XTCP")
|
|
|
|
tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
|
|
|
|
for _, ps := range res.Xtcp {
|
|
|
|
tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
tbl.Print()
|
|
|
|
fmt.Println("")
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
return nil
|
|
|
|
}
|