2017-02-12 04:13:29 +00:00
|
|
|
package main
|
|
|
|
|
2017-03-04 18:18:08 +00:00
|
|
|
import (
|
2017-03-22 21:43:36 +00:00
|
|
|
"context"
|
2017-03-04 18:18:08 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-03-30 00:28:28 +00:00
|
|
|
"io"
|
2017-03-26 22:33:40 +00:00
|
|
|
"io/ioutil"
|
2017-03-04 18:18:08 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2017-02-12 04:13:29 +00:00
|
|
|
|
2017-03-11 20:28:49 +00:00
|
|
|
"github.com/spf13/viper"
|
2017-03-30 00:28:28 +00:00
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
2017-03-26 22:33:40 +00:00
|
|
|
|
2017-03-04 18:18:08 +00:00
|
|
|
"git.daplie.com/Daplie/go-rvpn-server/rvpn/genericlistener"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-27 21:09:25 +00:00
|
|
|
logfile = "stdout"
|
|
|
|
configPath = "./"
|
2017-03-30 00:28:28 +00:00
|
|
|
configFile = "go-rvpn-server"
|
2017-03-27 21:09:25 +00:00
|
|
|
|
2017-03-04 18:18:08 +00:00
|
|
|
loginfo *log.Logger
|
|
|
|
logdebug *log.Logger
|
|
|
|
logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
|
|
|
|
argWssClientListener string
|
2017-03-11 20:28:49 +00:00
|
|
|
argGenericBinding int
|
2017-03-04 18:18:08 +00:00
|
|
|
argServerBinding string
|
|
|
|
argServerAdminBinding string
|
|
|
|
argServerExternalBinding string
|
|
|
|
argDeadTime int
|
|
|
|
connectionTable *genericlistener.Table
|
|
|
|
secretKey = "abc123"
|
2017-03-11 05:36:42 +00:00
|
|
|
wssHostName = "localhost.daplie.me"
|
2017-03-11 20:28:49 +00:00
|
|
|
adminHostName = "rvpn.daplie.invalid"
|
|
|
|
idle int
|
|
|
|
dwell int
|
|
|
|
cancelcheck int
|
2017-03-19 14:56:33 +00:00
|
|
|
lbDefaultMethod string
|
2017-03-20 00:04:47 +00:00
|
|
|
serverName string
|
2017-03-04 18:18:08 +00:00
|
|
|
)
|
|
|
|
|
2017-03-26 22:33:40 +00:00
|
|
|
func init() {
|
|
|
|
flag.StringVar(&logfile, "log", logfile, "Log file (or stdout/stderr; empty for none)")
|
2017-03-27 21:09:25 +00:00
|
|
|
flag.StringVar(&configPath, "config-path", configPath, "Configuration File Path")
|
|
|
|
flag.StringVar(&configFile, "config-file", configFile, "Configuration File Name")
|
|
|
|
|
2017-03-26 22:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var logoutput io.Writer
|
|
|
|
|
2017-03-04 18:18:08 +00:00
|
|
|
//Main -- main entry point
|
2017-02-12 04:13:29 +00:00
|
|
|
func main() {
|
2017-03-04 18:18:08 +00:00
|
|
|
flag.Parse()
|
2017-03-26 22:33:40 +00:00
|
|
|
switch logfile {
|
|
|
|
case "stdout":
|
|
|
|
logoutput = os.Stdout
|
|
|
|
case "stderr":
|
|
|
|
logoutput = os.Stderr
|
|
|
|
case "":
|
|
|
|
logoutput = ioutil.Discard
|
|
|
|
default:
|
|
|
|
logoutput = &lumberjack.Logger{
|
|
|
|
Filename: logfile,
|
|
|
|
MaxSize: 100,
|
|
|
|
MaxAge: 120,
|
|
|
|
MaxBackups: 10,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send the output io.Writing to the other packages
|
|
|
|
genericlistener.InitLogging(logoutput)
|
|
|
|
|
|
|
|
loginfo = log.New(logoutput, "INFO: main: ", logFlags)
|
|
|
|
logdebug = log.New(logoutput, "DEBUG: main:", logFlags)
|
|
|
|
|
2017-03-30 00:28:28 +00:00
|
|
|
viper.SetConfigName(configFile)
|
|
|
|
viper.AddConfigPath(configPath)
|
2017-03-11 20:28:49 +00:00
|
|
|
viper.AddConfigPath("./")
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2017-03-20 00:04:47 +00:00
|
|
|
panic(fmt.Errorf("fatal error config file: %s", err))
|
2017-03-11 20:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flag.IntVar(&argDeadTime, "dead-time-counter", 5, "deadtime counter in seconds")
|
2017-03-04 18:18:08 +00:00
|
|
|
|
2017-03-11 20:28:49 +00:00
|
|
|
wssHostName = viper.Get("rvpn.wssdomain").(string)
|
|
|
|
adminHostName = viper.Get("rvpn.admindomain").(string)
|
|
|
|
argGenericBinding = viper.GetInt("rvpn.genericlistener")
|
2017-03-23 23:57:43 +00:00
|
|
|
deadtime := viper.Get("rvpn.deadtime").(map[string]interface{})
|
|
|
|
idle = deadtime["idle"].(int)
|
|
|
|
dwell = deadtime["dwell"].(int)
|
|
|
|
cancelcheck = deadtime["cancelcheck"].(int)
|
2017-03-19 14:56:33 +00:00
|
|
|
lbDefaultMethod = viper.Get("rvpn.loadbalancing.defaultmethod").(string)
|
2017-03-20 00:04:47 +00:00
|
|
|
serverName = viper.Get("rvpn.serverName").(string)
|
2017-03-04 18:18:08 +00:00
|
|
|
|
|
|
|
loginfo.Println("startup")
|
|
|
|
|
|
|
|
certbundle, err := tls.LoadX509KeyPair("certs/fullchain.pem", "certs/privkey.pem")
|
|
|
|
if err != nil {
|
|
|
|
loginfo.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelContext := context.WithCancel(context.Background())
|
|
|
|
defer cancelContext()
|
|
|
|
|
2017-03-20 00:04:47 +00:00
|
|
|
serverStatus := genericlistener.NewStatus(ctx)
|
|
|
|
serverStatus.AdminDomain = adminHostName
|
|
|
|
serverStatus.WssDomain = wssHostName
|
|
|
|
serverStatus.Name = serverName
|
|
|
|
serverStatus.StartTime = time.Now()
|
|
|
|
serverStatus.DeadTime = genericlistener.NewStatusDeadTime(dwell, idle, cancelcheck)
|
|
|
|
serverStatus.LoadbalanceDefaultMethod = lbDefaultMethod
|
|
|
|
|
2017-03-04 18:18:08 +00:00
|
|
|
// Setup for GenericListenServe.
|
|
|
|
// - establish context for the generic listener
|
|
|
|
// - startup listener
|
|
|
|
// - accept with peek buffer.
|
|
|
|
// - peek at the 1st 30 bytes.
|
|
|
|
// - check for tls
|
|
|
|
// - if tls, establish, protocol peek buffer, else decrypted
|
|
|
|
// - match protocol
|
|
|
|
|
|
|
|
connectionTracking := genericlistener.NewTracking()
|
2017-03-20 00:04:47 +00:00
|
|
|
serverStatus.ConnectionTracking = connectionTracking
|
2017-03-04 18:18:08 +00:00
|
|
|
go connectionTracking.Run(ctx)
|
|
|
|
|
2017-03-11 20:28:49 +00:00
|
|
|
connectionTable = genericlistener.NewTable(dwell, idle)
|
2017-03-20 00:04:47 +00:00
|
|
|
serverStatus.ConnectionTable = connectionTable
|
2017-03-25 20:01:57 +00:00
|
|
|
go connectionTable.Run(ctx, lbDefaultMethod)
|
2017-03-04 18:18:08 +00:00
|
|
|
|
2017-03-20 00:04:47 +00:00
|
|
|
genericListeners := genericlistener.NewGenerListeners(ctx, secretKey, certbundle, serverStatus)
|
|
|
|
serverStatus.GenericListeners = genericListeners
|
2017-03-19 14:56:33 +00:00
|
|
|
|
2017-03-20 00:04:47 +00:00
|
|
|
go genericListeners.Run(ctx, argGenericBinding)
|
2017-03-04 18:18:08 +00:00
|
|
|
|
2017-03-20 00:04:47 +00:00
|
|
|
select {}
|
2017-02-12 04:13:29 +00:00
|
|
|
}
|