2017-05-06 14:37:24 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2017-05-07 03:37:30 +02:00
|
|
|
"time"
|
2017-05-06 14:37:24 +02:00
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
"dev.sum7.eu/genofire/golang-lib/file"
|
2017-05-06 14:37:24 +02:00
|
|
|
"github.com/NYTimes/gziphandler"
|
2019-06-10 02:57:36 +02:00
|
|
|
"github.com/bdlm/log"
|
2018-06-30 01:45:51 +02:00
|
|
|
|
|
|
|
respondYanic "github.com/FreifunkBremen/yanic/respond"
|
|
|
|
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
|
2017-05-06 14:37:24 +02:00
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
|
|
|
2017-05-06 19:44:37 +02:00
|
|
|
"github.com/FreifunkBremen/freifunkmanager/runtime"
|
2017-05-06 14:37:24 +02:00
|
|
|
"github.com/FreifunkBremen/freifunkmanager/ssh"
|
2017-05-08 19:13:29 +02:00
|
|
|
"github.com/FreifunkBremen/freifunkmanager/websocket"
|
2017-05-06 14:37:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-06-30 01:45:51 +02:00
|
|
|
configFile string
|
|
|
|
config = &runtime.Config{}
|
|
|
|
collector *respondYanic.Collector
|
|
|
|
verbose bool
|
2017-05-06 14:37:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
|
2018-06-30 01:45:51 +02:00
|
|
|
flag.BoolVar(&verbose, "v", false, "verbose logging")
|
2017-05-06 14:37:24 +02:00
|
|
|
flag.Parse()
|
2018-06-30 01:45:51 +02:00
|
|
|
if verbose {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
2017-05-06 14:37:24 +02:00
|
|
|
|
2018-06-30 01:45:51 +02:00
|
|
|
if err := file.ReadTOML(configFile, config); err != nil {
|
|
|
|
log.Panicf("Error during read config: %s", err)
|
|
|
|
}
|
2017-05-06 14:37:24 +02:00
|
|
|
|
2018-06-30 01:45:51 +02:00
|
|
|
log.Info("starting...")
|
2017-05-06 14:37:24 +02:00
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
db, err := gorm.Open(config.DatabaseType, config.DatabaseConnection)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("failed to connect database")
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
db.AutoMigrate(&runtime.Node{}, &websocket.Session{})
|
2018-07-26 14:14:23 +02:00
|
|
|
|
2018-07-24 15:07:11 +02:00
|
|
|
sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration)
|
2018-06-30 01:45:51 +02:00
|
|
|
nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{})
|
2017-05-07 03:37:30 +02:00
|
|
|
|
2018-08-10 17:17:00 +02:00
|
|
|
ws := websocket.NewWebsocketServer(config.Secret, config.SSHIPAddressPrefix, db, config.BlacklistFor.Duration, nodesYanic)
|
2017-05-06 19:44:37 +02:00
|
|
|
|
2018-08-10 17:17:00 +02:00
|
|
|
yanic := runtime.NewYanicDB(db, sshmanager, config.BlacklistFor.Duration, ws.SendNode, ws.SendStats, config.SSHIPAddressPrefix)
|
2019-06-05 12:00:11 +02:00
|
|
|
if config.Pinger.Enable {
|
|
|
|
pinger, err := runtime.NewPinger(db, &config.Pinger, config.BlacklistFor.Duration, ws.SendPing)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
go pinger.Start()
|
2019-06-03 15:38:46 +02:00
|
|
|
}
|
2017-05-15 21:59:48 +02:00
|
|
|
|
2018-07-17 23:11:47 +02:00
|
|
|
if config.YanicEnable {
|
|
|
|
if duration := config.YanicSynchronize.Duration; duration > 0 {
|
|
|
|
now := time.Now()
|
|
|
|
delay := duration - now.Sub(now.Truncate(duration))
|
|
|
|
log.Printf("delaying %0.1f seconds", delay.Seconds())
|
|
|
|
time.Sleep(delay)
|
|
|
|
}
|
2018-07-26 14:14:23 +02:00
|
|
|
collector = respondYanic.NewCollector(yanic, nodesYanic, make(map[string][]string), []respondYanic.InterfaceConfig{config.Yanic})
|
2018-07-17 23:11:47 +02:00
|
|
|
if duration := config.YanicCollectInterval.Duration; duration > 0 {
|
|
|
|
collector.Start(config.YanicCollectInterval.Duration)
|
|
|
|
}
|
2018-06-30 01:45:51 +02:00
|
|
|
defer collector.Close()
|
2018-07-17 23:11:47 +02:00
|
|
|
log.Info("started Yanic collector")
|
2017-05-06 19:44:37 +02:00
|
|
|
}
|
2017-05-06 14:37:24 +02:00
|
|
|
|
2017-05-08 19:13:29 +02:00
|
|
|
// Startwebserver
|
|
|
|
http.Handle("/", gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot))))
|
2017-05-06 14:37:24 +02:00
|
|
|
|
|
|
|
srv := &http.Server{
|
2017-05-08 19:13:29 +02:00
|
|
|
Addr: config.WebserverBind,
|
2017-05-06 14:37:24 +02:00
|
|
|
}
|
2017-05-08 19:13:29 +02:00
|
|
|
|
2017-05-06 14:37:24 +02:00
|
|
|
go func() {
|
2018-06-30 16:20:54 +02:00
|
|
|
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
2018-06-30 01:45:51 +02:00
|
|
|
log.Panic(err)
|
2017-05-06 14:37:24 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-06-30 01:45:51 +02:00
|
|
|
log.Info("started")
|
2017-05-06 14:37:24 +02:00
|
|
|
|
|
|
|
// Wait for system signal
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
log.Debug("stop recieve:", sig)
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
ws.Close()
|
2018-06-30 01:45:51 +02:00
|
|
|
|
|
|
|
// Stop services
|
2017-05-06 14:37:24 +02:00
|
|
|
srv.Close()
|
2018-07-26 14:14:23 +02:00
|
|
|
db.Close()
|
|
|
|
|
|
|
|
log.Info("stop freifunkmanager")
|
2017-05-06 14:37:24 +02:00
|
|
|
|
|
|
|
}
|