2016-03-14 11:18:24 +08:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-13 02:30:25 +08:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2016-03-14 00:48:22 +08:00
|
|
|
"fmt"
|
2017-07-13 02:30:25 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-01-27 21:24:36 +08:00
|
|
|
"os"
|
2017-06-27 01:59:30 +08:00
|
|
|
"os/signal"
|
2016-03-14 00:48:22 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-06-27 01:59:30 +08:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2016-02-03 18:46:24 +08:00
|
|
|
|
2016-03-14 00:48:22 +08:00
|
|
|
docopt "github.com/docopt/docopt-go"
|
2018-01-17 01:09:33 +08:00
|
|
|
"github.com/rodaine/table"
|
2017-03-09 02:03:47 +08:00
|
|
|
ini "github.com/vaughan0/go-ini"
|
2016-03-14 00:48:22 +08:00
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
"github.com/fatedier/frp/client"
|
|
|
|
"github.com/fatedier/frp/models/config"
|
|
|
|
"github.com/fatedier/frp/utils/log"
|
|
|
|
"github.com/fatedier/frp/utils/version"
|
2016-03-14 00:48:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configFile string = "./frpc.ini"
|
2016-01-27 21:24:36 +08:00
|
|
|
)
|
|
|
|
|
2016-03-14 00:48:22 +08:00
|
|
|
var usage string = `frpc is the client of frp
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
frpc [-c config_file] [-L log_file] [--log-level=<log_level>] [--server-addr=<server_addr>]
|
2018-01-17 01:09:33 +08:00
|
|
|
frpc reload [-c config_file]
|
|
|
|
frpc status [-c config_file]
|
2016-06-07 11:15:59 +08:00
|
|
|
frpc -h | --help
|
|
|
|
frpc -v | --version
|
2016-03-14 00:48:22 +08:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-c config_file set config file
|
|
|
|
-L log_file set output log file, including console
|
|
|
|
--log-level=<log_level> set log level: debug, info, warn, error
|
|
|
|
--server-addr=<server_addr> addr which frps is listening for, example: 0.0.0.0:7000
|
|
|
|
-h --help show this screen
|
2017-05-21 22:42:42 +08:00
|
|
|
-v --version show version
|
2016-03-14 00:48:22 +08:00
|
|
|
`
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
func main() {
|
2017-03-09 02:03:47 +08:00
|
|
|
var err error
|
2017-07-19 22:56:12 +08:00
|
|
|
confFile := "./frpc.ini"
|
2016-03-14 00:48:22 +08:00
|
|
|
// the configures parsed from file will be replaced by those from command line if exist
|
|
|
|
args, err := docopt.Parse(usage, nil, true, version.Full(), false)
|
|
|
|
|
|
|
|
if args["-c"] != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
confFile = args["-c"].(string)
|
2016-03-14 00:48:22 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
conf, err := ini.LoadFile(confFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.ClientCommonCfg, err = config.LoadClientCommonConf(conf)
|
2016-01-27 21:24:36 +08:00
|
|
|
if err != nil {
|
2016-03-14 00:48:22 +08:00
|
|
|
fmt.Println(err)
|
2017-03-09 02:03:47 +08:00
|
|
|
os.Exit(1)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
2017-07-13 02:20:49 +08:00
|
|
|
config.ClientCommonCfg.ConfigFile = confFile
|
2016-01-27 21:24:36 +08:00
|
|
|
|
2017-07-13 02:30:25 +08:00
|
|
|
// check if reload command
|
2018-01-17 01:09:33 +08:00
|
|
|
if args["reload"] != nil {
|
|
|
|
if args["reload"].(bool) {
|
|
|
|
if err = CmdReload(); err != nil {
|
2018-02-07 11:39:30 +08:00
|
|
|
fmt.Printf("frpc reload error: %v\n", err)
|
2017-07-13 02:30:25 +08:00
|
|
|
os.Exit(1)
|
2018-01-17 01:09:33 +08:00
|
|
|
} else {
|
|
|
|
fmt.Printf("reload success\n")
|
|
|
|
os.Exit(0)
|
2017-07-13 02:30:25 +08:00
|
|
|
}
|
2018-01-17 01:09:33 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 02:30:25 +08:00
|
|
|
|
2018-01-17 01:09:33 +08:00
|
|
|
// check if status command
|
|
|
|
if args["status"] != nil {
|
|
|
|
if args["status"].(bool) {
|
|
|
|
if err = CmdStatus(); err != nil {
|
2018-01-26 00:23:48 +08:00
|
|
|
fmt.Printf("frpc get status error: %v\n", err)
|
2017-07-13 02:30:25 +08:00
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 00:48:22 +08:00
|
|
|
if args["-L"] != nil {
|
|
|
|
if args["-L"].(string) == "console" {
|
2017-03-09 02:03:47 +08:00
|
|
|
config.ClientCommonCfg.LogWay = "console"
|
2016-03-14 00:48:22 +08:00
|
|
|
} else {
|
2017-03-09 02:03:47 +08:00
|
|
|
config.ClientCommonCfg.LogWay = "file"
|
|
|
|
config.ClientCommonCfg.LogFile = args["-L"].(string)
|
2016-03-14 00:48:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if args["--log-level"] != nil {
|
2017-03-09 02:03:47 +08:00
|
|
|
config.ClientCommonCfg.LogLevel = args["--log-level"].(string)
|
2016-03-14 00:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if args["--server-addr"] != nil {
|
|
|
|
addr := strings.Split(args["--server-addr"].(string), ":")
|
|
|
|
if len(addr) != 2 {
|
|
|
|
fmt.Println("--server-addr format error: example 0.0.0.0:7000")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
serverPort, err := strconv.ParseInt(addr[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("--server-addr format error, example 0.0.0.0:7000")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
config.ClientCommonCfg.ServerAddr = addr[0]
|
2018-01-17 21:49:37 +08:00
|
|
|
config.ClientCommonCfg.ServerPort = int(serverPort)
|
2016-03-14 00:48:22 +08:00
|
|
|
}
|
|
|
|
|
2016-06-07 11:15:59 +08:00
|
|
|
if args["-v"] != nil {
|
|
|
|
if args["-v"].(bool) {
|
|
|
|
fmt.Println(version.Full())
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
pxyCfgs, visitorCfgs, err := config.LoadProxyConfFromFile(config.ClientCommonCfg.User, conf, config.ClientCommonCfg.Start)
|
2017-03-09 02:03:47 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
log.InitLog(config.ClientCommonCfg.LogWay, config.ClientCommonCfg.LogFile,
|
|
|
|
config.ClientCommonCfg.LogLevel, config.ClientCommonCfg.LogMaxDays)
|
2016-01-27 21:24:36 +08:00
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
svr := client.NewService(pxyCfgs, visitorCfgs)
|
2017-06-27 01:59:30 +08:00
|
|
|
|
|
|
|
// Capture the exit signal if we use kcp.
|
|
|
|
if config.ClientCommonCfg.Protocol == "kcp" {
|
|
|
|
go HandleSignal(svr)
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
err = svr.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
2017-06-27 01:59:30 +08:00
|
|
|
|
|
|
|
func HandleSignal(svr *client.Service) {
|
|
|
|
ch := make(chan os.Signal)
|
|
|
|
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-ch
|
|
|
|
svr.Close()
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2018-01-17 01:09:33 +08:00
|
|
|
|
|
|
|
func CmdReload() error {
|
|
|
|
if config.ClientCommonCfg.AdminPort == 0 {
|
|
|
|
return fmt.Errorf("admin_port shoud be set if you want to use reload feature")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "http://"+
|
|
|
|
config.ClientCommonCfg.AdminAddr+":"+fmt.Sprintf("%d", config.ClientCommonCfg.AdminPort)+"/api/reload", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(config.ClientCommonCfg.AdminUser+":"+
|
|
|
|
config.ClientCommonCfg.AdminPwd))
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", authStr)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res := &client.GeneralResponse{}
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body)))
|
|
|
|
} else if res.Code != 0 {
|
|
|
|
return fmt.Errorf(res.Msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmdStatus() error {
|
|
|
|
if config.ClientCommonCfg.AdminPort == 0 {
|
|
|
|
return fmt.Errorf("admin_port shoud be set if you want to get proxy status")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "http://"+
|
|
|
|
config.ClientCommonCfg.AdminAddr+":"+fmt.Sprintf("%d", config.ClientCommonCfg.AdminPort)+"/api/status", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(config.ClientCommonCfg.AdminUser+":"+
|
|
|
|
config.ClientCommonCfg.AdminPwd))
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", authStr)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
tbl.Print()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|