frp/src/frp/cmd/frps/main.go

126 lines
3.2 KiB
Go
Raw Normal View History

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 (
2016-03-14 00:21:40 +08:00
"fmt"
2016-01-27 21:24:36 +08:00
"os"
2016-03-14 00:21:40 +08:00
"strconv"
"strings"
2016-04-18 15:16:40 +08:00
"time"
2016-03-14 00:21:40 +08:00
docopt "github.com/docopt/docopt-go"
2016-01-27 21:24:36 +08:00
"frp/models/server"
"frp/utils/conn"
"frp/utils/log"
2016-03-14 00:21:40 +08:00
"frp/utils/version"
2016-04-18 15:16:40 +08:00
"frp/utils/vhost"
2016-03-14 00:21:40 +08:00
)
var (
configFile string = "./frps.ini"
2016-01-27 21:24:36 +08:00
)
2016-03-14 00:21:40 +08:00
var usage string = `frps is the server of frp
Usage:
frps [-c config_file] [-L log_file] [--log-level=<log_level>] [--addr=<bind_addr>]
frps -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
--addr=<bind_addr> listen addr for client, example: 0.0.0.0:7000
-h --help show this screen
--version show version
`
2016-01-27 21:24:36 +08:00
func main() {
2016-03-14 00:21:40 +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 {
configFile = args["-c"].(string)
}
err = server.LoadConf(configFile)
2016-01-27 21:24:36 +08:00
if err != nil {
2016-03-14 00:21:40 +08:00
fmt.Println(err)
2016-01-27 21:24:36 +08:00
os.Exit(-1)
}
2016-03-14 00:21:40 +08:00
if args["-L"] != nil {
if args["-L"].(string) == "console" {
server.LogWay = "console"
} else {
server.LogWay = "file"
server.LogFile = args["-L"].(string)
}
}
if args["--log-level"] != nil {
server.LogLevel = args["--log-level"].(string)
}
if args["--addr"] != nil {
addr := strings.Split(args["--addr"].(string), ":")
if len(addr) != 2 {
fmt.Println("--addr format error: example 0.0.0.0:7000")
os.Exit(1)
}
bindPort, err := strconv.ParseInt(addr[1], 10, 64)
if err != nil {
fmt.Println("--addr format error, example 0.0.0.0:7000")
os.Exit(1)
}
server.BindAddr = addr[0]
server.BindPort = bindPort
}
log.InitLog(server.LogWay, server.LogFile, server.LogLevel, server.LogMaxDays)
2016-01-27 21:24:36 +08:00
2016-02-18 18:24:48 +08:00
l, err := conn.Listen(server.BindAddr, server.BindPort)
2016-01-27 21:24:36 +08:00
if err != nil {
2016-04-18 15:16:40 +08:00
log.Error("Create server listener error, %v", err)
os.Exit(1)
}
// create vhost if VhostHttpPort != 0
2016-04-18 15:16:40 +08:00
if server.VhostHttpPort != 0 {
vhostListener, err := conn.Listen(server.BindAddr, server.VhostHttpPort)
if err != nil {
log.Error("Create vhost http listener error, %v", err)
os.Exit(1)
}
server.VhostMuxer, err = vhost.NewHttpMuxer(vhostListener, 30*time.Second)
if err != nil {
log.Error("Create vhost httpMuxer error, %v", err)
}
2016-01-27 21:24:36 +08:00
}
// create dashboard web server if DashboardPort != 0
if server.DashboardPort != 0 {
err := server.RunDashboardServer(server.BindAddr, server.DashboardPort)
if err != nil {
log.Error("Create dashboard web server error, %v", err)
os.Exit(1)
}
}
2016-01-27 21:24:36 +08:00
log.Info("Start frps success")
ProcessControlConn(l)
}