2023-12-24 10:18:08 +00:00
|
|
|
// This file is part of the happyDomain (R) project.
|
|
|
|
// Copyright (c) 2020-2024 happyDomain
|
|
|
|
// Authors: Pierre-Olivier Mercier, et al.
|
2020-05-04 14:58:02 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// This program is offered under a commercial and under the AGPL license.
|
|
|
|
// For commercial licensing, contact us at <contact@happydomain.org>.
|
2020-05-04 14:58:02 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// For AGPL licensing:
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2020-05-04 14:58:02 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
2020-05-04 14:58:02 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-05-04 14:58:02 +00:00
|
|
|
|
2019-07-16 09:07:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-08 19:07:53 +00:00
|
|
|
"fmt"
|
2019-07-16 09:07:03 +00:00
|
|
|
"log"
|
2020-04-20 09:55:37 +00:00
|
|
|
"math/rand"
|
2020-06-08 19:07:53 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2020-04-20 09:55:37 +00:00
|
|
|
"time"
|
2019-07-16 09:07:03 +00:00
|
|
|
|
2023-05-18 17:34:54 +00:00
|
|
|
"github.com/fatih/color"
|
|
|
|
|
2023-09-07 09:37:18 +00:00
|
|
|
"git.happydns.org/happyDomain/api"
|
|
|
|
"git.happydns.org/happyDomain/config"
|
|
|
|
"git.happydns.org/happyDomain/internal/app"
|
|
|
|
"git.happydns.org/happyDomain/storage"
|
2020-04-22 11:12:27 +00:00
|
|
|
|
2023-09-07 09:37:18 +00:00
|
|
|
_ "git.happydns.org/happyDomain/services/providers/google"
|
2020-10-10 16:44:17 +00:00
|
|
|
|
2023-09-07 09:37:18 +00:00
|
|
|
_ "git.happydns.org/happyDomain/storage/leveldb"
|
2019-07-16 09:07:03 +00:00
|
|
|
)
|
|
|
|
|
2021-10-31 18:30:04 +00:00
|
|
|
var (
|
|
|
|
Version = "custom-build"
|
|
|
|
)
|
|
|
|
|
2019-07-16 09:07:03 +00:00
|
|
|
func main() {
|
2020-04-19 06:49:30 +00:00
|
|
|
var err error
|
2019-07-16 09:07:03 +00:00
|
|
|
|
2023-08-05 16:15:52 +00:00
|
|
|
api.HDVersion = api.Version{
|
|
|
|
Version: Version,
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:52:15 +00:00
|
|
|
log.Println("This is happyDomain", Version)
|
2020-04-20 09:55:37 +00:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
2023-05-18 17:34:54 +00:00
|
|
|
// Disabled colors in dnscontrol corrections
|
|
|
|
color.NoColor = true
|
|
|
|
|
2020-04-19 06:49:30 +00:00
|
|
|
// Load and parse options
|
|
|
|
var opts *config.Options
|
|
|
|
if opts, err = config.ConsolidateConfig(); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-07-16 09:07:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 18:13:50 +00:00
|
|
|
// Initialize storage
|
|
|
|
if s, ok := storage.StorageEngines[opts.StorageEngine]; !ok {
|
2023-02-23 01:21:12 +00:00
|
|
|
log.Fatal(fmt.Sprintf("Nonexistent storage engine: %q, please select one of: %v", opts.StorageEngine, storage.GetStorageEngines()))
|
2020-04-20 09:55:37 +00:00
|
|
|
} else {
|
2020-07-07 18:13:50 +00:00
|
|
|
log.Println("Opening database...")
|
|
|
|
if store, err := s(); err != nil {
|
2023-02-23 01:21:12 +00:00
|
|
|
log.Fatal("Could not open the database: ", err)
|
2020-07-07 18:13:50 +00:00
|
|
|
} else {
|
|
|
|
defer store.Close()
|
|
|
|
storage.MainStore = store
|
|
|
|
}
|
2020-04-20 09:55:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 21:40:47 +00:00
|
|
|
if opts.NoAuth {
|
2023-02-23 01:21:12 +00:00
|
|
|
log.Println("WARNING: NoAuth option must be used for testing or private use behind another restriction/authentication method.")
|
2021-01-03 21:40:47 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 01:21:12 +00:00
|
|
|
log.Println("Performing database migrations...")
|
2020-04-19 06:49:30 +00:00
|
|
|
if err = storage.MainStore.DoMigration(); err != nil {
|
2023-02-23 01:21:12 +00:00
|
|
|
log.Fatal("Could not migrate database: ", err)
|
2019-07-16 09:07:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 19:07:53 +00:00
|
|
|
// Prepare graceful shutdown
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2021-05-05 01:48:16 +00:00
|
|
|
var adminSrv *app.Admin
|
2020-06-08 19:07:53 +00:00
|
|
|
if opts.AdminBind != "" {
|
2021-05-05 01:48:16 +00:00
|
|
|
adminSrv := app.NewAdmin(opts)
|
|
|
|
go adminSrv.Start()
|
2020-06-08 19:07:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 14:48:16 +00:00
|
|
|
a := app.NewApp(opts)
|
|
|
|
go a.Start()
|
2020-06-08 19:07:53 +00:00
|
|
|
|
|
|
|
// Wait shutdown signal
|
|
|
|
<-interrupt
|
|
|
|
|
2021-05-04 14:48:16 +00:00
|
|
|
log.Println("Stopping the service...")
|
|
|
|
a.Stop()
|
|
|
|
if adminSrv != nil {
|
2021-05-05 01:48:16 +00:00
|
|
|
adminSrv.Stop()
|
2021-05-04 14:48:16 +00:00
|
|
|
}
|
|
|
|
log.Println("Stopped")
|
2019-07-16 09:07:03 +00:00
|
|
|
}
|