SakuraFrp/src/frp/cmd/frpc/main.go

92 lines
2.1 KiB
Go
Raw Normal View History

2016-01-27 13:24:36 +00:00
package main
import (
2016-03-13 16:48:22 +00:00
"fmt"
2016-01-27 13:24:36 +00:00
"os"
2016-03-13 16:48:22 +00:00
"strconv"
"strings"
2016-01-27 13:24:36 +00:00
"sync"
2016-02-03 10:46:24 +00:00
2016-03-13 16:48:22 +00:00
docopt "github.com/docopt/docopt-go"
"frp/models/client"
"frp/utils/log"
2016-03-13 16:48:22 +00:00
"frp/utils/version"
)
var (
configFile string = "./frpc.ini"
2016-01-27 13:24:36 +00:00
)
2016-03-13 16:48:22 +00: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>]
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
`
2016-01-27 13:24:36 +00:00
func main() {
2016-03-13 16:48:22 +00: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 {
configFile = args["-c"].(string)
}
err = client.LoadConf(configFile)
2016-01-27 13:24:36 +00:00
if err != nil {
2016-03-13 16:48:22 +00:00
fmt.Println(err)
2016-01-27 13:24:36 +00:00
os.Exit(-1)
}
2016-03-13 16:48:22 +00:00
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
}
2016-02-18 10:24:48 +00:00
log.InitLog(client.LogWay, client.LogFile, client.LogLevel)
2016-01-27 13:24:36 +00:00
// wait until all control goroutine exit
var wait sync.WaitGroup
2016-02-18 10:24:48 +00:00
wait.Add(len(client.ProxyClients))
2016-01-27 13:24:36 +00:00
2016-02-18 10:24:48 +00:00
for _, client := range client.ProxyClients {
2016-01-27 13:24:36 +00:00
go ControlProcess(client, &wait)
}
log.Info("Start frpc success")
wait.Wait()
log.Warn("All proxy exit!")
}