func TestLoadSettingsFromFile(t *testing.T) {

	settingsfile, _ := ioutil.TempFile("", "")

	settingsfile.WriteString("[Main]\n")
	settingsfile.WriteString("\n")
	settingsfile.WriteString("[[listen]]\n")
	settingsfile.WriteString("Address = \"1.2.3.4:1234\"\n")
	settingsfile.Close()
	defer os.Remove(settingsfile.Name())

	settings, err := settings.LoadSettingsFromTomlFile(settingsfile.Name())
	if err != nil {
		t.Error("Error while loading toml file", err)
	}

	//Check if the settings are loaded from the configuation file
	if settings.Listen[0].Address != "1.2.3.4:1234" {
		t.Error("Bind property not loaded from the configuration file")
	}

}
Exemplo n.º 2
0
func main() {
	var cfg string
	var globalSettings settings.Settings

	flag.StringVar(&cfg, "c", "", "Path to config file")
	flag.Parse()

	if cfg == "" {
		log.Println("Missing required option -c")
		flag.PrintDefaults()
		os.Exit(1)
	}

	var err error
	globalSettings, err = settings.LoadSettingsFromTomlFile(cfg)
	if err != nil {
		log.Panicln("Error loading configuration file:", err)
	}

	log.Printf("[+] redis server: <%s>\n", globalSettings.Main.RedisHost)

	pool = newPool(globalSettings.Main.RedisHost, globalSettings.Main.RedisPassword)

	db := pool.Get()
	if _, err := db.Do("PING"); err != nil {
		panic(fmt.Sprintf("Failed to connect to redis: %v", err))
	}

	db.Close()

	restInterface := rest.NewRestInterface(pool, pollDataStreamManager, &globalSettings)

	go cmdreader()

	//start schedular.
	scheduler := NewScheduler(pool)
	internals["scheduler_add"] = scheduler.Add
	internals["scheduler_list"] = scheduler.List
	internals["scheduler_remove"] = scheduler.Remove

	scheduler.Start()

	hubbleAuth.Install(hubbleAuth.NewAcceptAllModule())

	// router.Static("/doc", "./doc")

	var wg sync.WaitGroup
	wg.Add(len(globalSettings.Listen))
	for _, httpBinding := range globalSettings.Listen {
		go func(httpBinding settings.HTTPBinding) {
			server := &http.Server{Addr: httpBinding.Address, Handler: restInterface.Router()}
			if httpBinding.TLSEnabled() {
				server.TLSConfig = &tls.Config{}

				if err := configureServerCertificates(httpBinding, server); err != nil {
					log.Panicln("Unable to load the server certificates", err)
				}

				if err := configureClientCertificates(httpBinding, server); err != nil {
					log.Panicln("Unable to load the clientCA's", err)
				}

				ln, err := net.Listen("tcp", server.Addr)
				if err != nil {
					log.Panicln(err)
				}

				tlsListener := tls.NewListener(ln, server.TLSConfig)
				log.Println("Listening on", httpBinding.Address, "with TLS")
				if err := server.Serve(tlsListener); err != nil {
					log.Panicln(err)
				}
				wg.Done()
			} else {
				log.Println("Listening on", httpBinding.Address)
				if err := server.ListenAndServe(); err != nil {
					log.Panicln(err)
				}
				wg.Done()
			}
		}(httpBinding)
	}

	StartSyncthingHubbleAgent(globalSettings.Syncthing.Port)

	wg.Wait()
}