Browse Source

add support for command line with frpc

fatedier 9 years ago
parent
commit
a56b29b153
4 changed files with 72 additions and 15 deletions
  1. 0 1
      .gitignore
  2. 1 3
      conf/frpc.ini
  3. 61 1
      src/frp/cmd/frpc/main.go
  4. 10 10
      src/frp/models/client/config.go

+ 0 - 1
.gitignore

@@ -28,4 +28,3 @@ bin/
 
 # Cache
 *.swp
-*.swo

+ 1 - 3
conf/frpc.ini

@@ -1,13 +1,11 @@
 # [common] is integral section
 [common]
-server_addr = 127.0.0.1
+server_addr = 0.0.0.0
 server_port = 7000
 # console or real logFile path like ./frpc.log
 log_file = console
 # debug, info, warn, error
 log_level = debug
-# file, console
-log_way = file
 
 # test1 is the proxy name same as server's configuration
 [test1]

+ 61 - 1
src/frp/cmd/frpc/main.go

@@ -1,19 +1,79 @@
 package main
 
 import (
+	"fmt"
 	"os"
+	"strconv"
+	"strings"
 	"sync"
 
+	docopt "github.com/docopt/docopt-go"
+
 	"frp/models/client"
 	"frp/utils/log"
+	"frp/utils/version"
+)
+
+var (
+	configFile string = "./frpc.ini"
 )
 
+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>]
+    frpc -h | --help | --version
+
+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
+    --version                   show version
+`
+
 func main() {
-	err := client.LoadConf("./frpc.ini")
+	// 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 {
+		configFile = args["-c"].(string)
+	}
+	err = client.LoadConf(configFile)
 	if err != nil {
+		fmt.Println(err)
 		os.Exit(-1)
 	}
 
+	if args["-L"] != nil {
+		if args["-L"].(string) == "console" {
+			client.LogWay = "console"
+		} else {
+			client.LogWay = "file"
+			client.LogFile = args["-L"].(string)
+		}
+	}
+
+	if args["--log-level"] != nil {
+		client.LogLevel = args["--log-level"].(string)
+	}
+
+	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)
+		}
+		client.ServerAddr = addr[0]
+		client.ServerPort = serverPort
+	}
+
 	log.InitLog(client.LogWay, client.LogFile, client.LogLevel)
 
 	// wait until all control goroutine exit

+ 10 - 10
src/frp/models/client/config.go

@@ -11,11 +11,11 @@ import (
 var (
 	ServerAddr        string = "0.0.0.0"
 	ServerPort        int64  = 7000
-	LogFile           string = "./frpc.log"
-	LogLevel          string = "warn"
-	LogWay            string = "file"
-	HeartBeatInterval int64  = 5
-	HeartBeatTimeout  int64  = 30
+	LogFile           string = "console"
+	LogWay            string = "console"
+	LogLevel          string = "info"
+	HeartBeatInterval int64  = 20
+	HeartBeatTimeout  int64  = 60
 )
 
 var ProxyClients map[string]*ProxyClient = make(map[string]*ProxyClient)
@@ -43,6 +43,11 @@ func LoadConf(confFile string) (err error) {
 	tmpStr, ok = conf.Get("common", "log_file")
 	if ok {
 		LogFile = tmpStr
+		if LogFile == "console" {
+			LogWay = "console"
+		} else {
+			LogWay = "file"
+		}
 	}
 
 	tmpStr, ok = conf.Get("common", "log_level")
@@ -50,11 +55,6 @@ func LoadConf(confFile string) (err error) {
 		LogLevel = tmpStr
 	}
 
-	tmpStr, ok = conf.Get("common", "log_way")
-	if ok {
-		LogWay = tmpStr
-	}
-
 	// proxies
 	for name, section := range conf {
 		if name != "common" {