frpc: new parameter 'server_dynamic',support --user,--server-dynamic command.
This commit is contained in:
parent
1e905839f0
commit
9dc574d49a
@ -17,7 +17,11 @@ package client
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -211,6 +215,41 @@ func (ctl *Control) init() {
|
||||
ctl.closedCh = make(chan int)
|
||||
}
|
||||
|
||||
func (ctl *Control) getDynamicServer() (err error) {
|
||||
if config.ClientCommonCfg.ServerDynamic == "" {
|
||||
return
|
||||
}
|
||||
|
||||
serv := config.ClientCommonCfg.ServerDynamic
|
||||
if strings.ToLower(serv[:4]) != "http" {
|
||||
return
|
||||
}
|
||||
|
||||
res, err := http.Get(serv + "?user=" + config.ClientCommonCfg.User)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serv = string(data)
|
||||
addr := strings.Split(serv, ":")
|
||||
if len(addr) != 2 {
|
||||
ctl.Error("dynamic server info error: %v", serv)
|
||||
return
|
||||
}
|
||||
serverPort, err := strconv.ParseInt(addr[1], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config.ClientCommonCfg.ServerAddr = addr[0]
|
||||
config.ClientCommonCfg.ServerPort = serverPort
|
||||
return
|
||||
}
|
||||
|
||||
// login send a login message to server and wait for a loginResp message.
|
||||
func (ctl *Control) login() (err error) {
|
||||
if ctl.conn != nil {
|
||||
@ -220,6 +259,11 @@ func (ctl *Control) login() (err error) {
|
||||
ctl.session.Close()
|
||||
}
|
||||
|
||||
errDynamic := ctl.getDynamicServer()
|
||||
if errDynamic != nil {
|
||||
ctl.Error("Get dynamic server error: %s", errDynamic.Error())
|
||||
}
|
||||
|
||||
conn, err := frpNet.ConnectServerByHttpProxy(config.ClientCommonCfg.HttpProxy, config.ClientCommonCfg.Protocol,
|
||||
fmt.Sprintf("%s:%d", config.ClientCommonCfg.ServerAddr, config.ClientCommonCfg.ServerPort))
|
||||
if err != nil {
|
||||
|
@ -43,7 +43,7 @@ var (
|
||||
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 [-c config_file] [-L log_file] [--log-level=<log_level>] [--user=<user_name>] [--server-addr=<server_addr>] [--server-dynamic=<http_url>]
|
||||
frpc [-c config_file] --reload
|
||||
frpc -h | --help
|
||||
frpc -v | --version
|
||||
@ -53,6 +53,7 @@ Options:
|
||||
-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
|
||||
--server-dynamic=<http_url> get frps addr from http_url, example: http://server.com/frp/
|
||||
--reload reload configure file without program exit
|
||||
-h --help show this screen
|
||||
-v --version show version
|
||||
@ -134,6 +135,14 @@ func main() {
|
||||
config.ClientCommonCfg.LogLevel = args["--log-level"].(string)
|
||||
}
|
||||
|
||||
if args["--user"] != nil {
|
||||
config.ClientCommonCfg.User = args["--user"].(string)
|
||||
}
|
||||
|
||||
if args["--server-dynamic"] != nil {
|
||||
config.ClientCommonCfg.ServerDynamic = args["--server-dynamic"].(string)
|
||||
}
|
||||
|
||||
if args["--server-addr"] != nil {
|
||||
addr := strings.Split(args["--server-addr"].(string), ":")
|
||||
if len(addr) != 2 {
|
||||
|
@ -28,6 +28,7 @@ var ClientCommonCfg *ClientCommonConf
|
||||
// client common config
|
||||
type ClientCommonConf struct {
|
||||
ConfigFile string
|
||||
ServerDynamic string
|
||||
ServerAddr string
|
||||
ServerPort int64
|
||||
ServerUdpPort int64 // this is specified by login response message from frps
|
||||
@ -54,6 +55,7 @@ type ClientCommonConf struct {
|
||||
func GetDeaultClientCommonConf() *ClientCommonConf {
|
||||
return &ClientCommonConf{
|
||||
ConfigFile: "./frpc.ini",
|
||||
ServerDynamic: "",
|
||||
ServerAddr: "0.0.0.0",
|
||||
ServerPort: 7000,
|
||||
ServerUdpPort: 0,
|
||||
@ -86,9 +88,9 @@ func LoadClientCommonConf(conf ini.File) (cfg *ClientCommonConf, err error) {
|
||||
)
|
||||
cfg = GetDeaultClientCommonConf()
|
||||
|
||||
tmpStr, ok = conf.Get("common", "server_addr")
|
||||
tmpStr, ok = conf.Get("common", "server_dynamic")
|
||||
if ok {
|
||||
cfg.ServerAddr = tmpStr
|
||||
cfg.ServerDynamic = tmpStr
|
||||
}
|
||||
|
||||
tmpStr, ok = conf.Get("common", "server_port")
|
||||
|
Loading…
Reference in New Issue
Block a user