2023-03-19 22:03:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-22 22:56:25 +01:00
|
|
|
"context"
|
2023-03-19 22:03:12 +01:00
|
|
|
"flag"
|
2023-04-13 00:07:08 +02:00
|
|
|
"fmt"
|
2023-03-22 22:13:23 +01:00
|
|
|
"net/http"
|
2023-03-22 22:56:25 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-05-02 00:04:04 +02:00
|
|
|
"strconv"
|
2023-04-13 00:07:08 +02:00
|
|
|
"syscall"
|
2023-03-22 22:56:25 +01:00
|
|
|
"time"
|
2023-03-19 22:03:12 +01:00
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
// ent pure go sqlite3 driver instead of "github.com/mattn/go-sqlite3".
|
|
|
|
_ "github.com/xiaoqidun/entps"
|
|
|
|
|
2023-03-22 22:13:23 +01:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/app"
|
2023-05-03 02:18:29 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/app/settings"
|
2023-03-19 22:03:12 +01:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/config"
|
2023-04-13 00:07:08 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent"
|
2023-04-19 05:30:52 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
2023-04-13 00:07:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-04-19 21:22:00 +02:00
|
|
|
banner = `
|
|
|
|
██████╗ ██████╗███╗ ███╗████████╗
|
|
|
|
██╔══██╗██╔════╝████╗ ████║╚══██╔══╝
|
|
|
|
██████╔╝██║ ██╔████╔██║ ██║
|
|
|
|
██╔═══╝ ██║ ██║╚██╔╝██║ ██║
|
|
|
|
██║ ╚██████╗██║ ╚═╝ ██║ ██║
|
|
|
|
╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝
|
|
|
|
`
|
|
|
|
slug = `Password Compromise Monitoring Tool
|
|
|
|
https://git.dotya.ml/mirre-mt/pcmt
|
2023-04-19 23:36:12 +02:00
|
|
|
____________________________________`
|
2023-04-13 00:07:08 +02:00
|
|
|
|
|
|
|
licenseHeader = `pcmt - Password Compromise Monitoring Tool
|
|
|
|
Copyright (C) git.dotya.ml/wanderer
|
|
|
|
|
|
|
|
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 version 3 of the License.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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/>.`
|
2023-03-19 22:03:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-05-04 23:49:25 +02:00
|
|
|
host = flag.String("host", "unset", "host address to listen on")
|
|
|
|
port = flag.Int("port", -1, "TCP port to listen on")
|
|
|
|
configFlag = flag.String("config", "config.dhall", "Default path of the config file")
|
|
|
|
configIsPathFlag = flag.Bool("configIsPath", true, "Whether the provided config is path or raw config")
|
|
|
|
devel = flag.Bool("devel", false, "Run the application in dev mode, connect to a local browser-sync instance for hot-reloading")
|
|
|
|
license = flag.Bool("license", false, "Print licensing information and exit")
|
|
|
|
version = "dev"
|
|
|
|
log *slogging.Logger
|
2023-03-19 22:03:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func run() error {
|
2023-04-13 00:07:08 +02:00
|
|
|
flag.Parse()
|
2023-03-22 22:13:23 +01:00
|
|
|
|
2023-04-19 23:36:12 +02:00
|
|
|
if *license {
|
|
|
|
fmt.Fprintln(os.Stderr, licenseHeader)
|
2023-05-03 04:26:42 +02:00
|
|
|
return nil
|
2023-04-19 23:36:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printHeader()
|
|
|
|
|
2023-04-28 22:54:09 +02:00
|
|
|
log = slogging.Init(true)
|
2023-04-19 05:30:52 +02:00
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
// TODO: allow different configuration formats (toml, ni)
|
|
|
|
// TODO: rename main.go to pcmt.go
|
|
|
|
// TODO: add .golangci-lint
|
|
|
|
// TODO: add flake.nix
|
|
|
|
// TODO: connect to postgres
|
|
|
|
// TODO: design user schemas, models.
|
|
|
|
// TODO: SBOM: https://actuated.dev/blog/sbom-in-github-actions
|
|
|
|
// TODO: SBOM: https://www.docker.com/blog/generate-sboms-with-buildkit/
|
|
|
|
|
2023-05-04 23:49:25 +02:00
|
|
|
conf, err := config.LoadConfig(*configFlag, *configIsPathFlag)
|
2023-03-19 22:03:12 +01:00
|
|
|
if err != nil {
|
2023-05-04 23:49:25 +02:00
|
|
|
log.Errorf("error loading config file '%s', bailing", *configFlag)
|
2023-03-22 22:13:23 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
// for "github.com/xiaoqidun/entps".
|
|
|
|
connstr := "file:ent?mode=memory&cache=shared&_fk=1"
|
2023-04-19 21:41:51 +02:00
|
|
|
|
2023-04-19 05:30:52 +02:00
|
|
|
log.Infof("connecting to db at '%s'", connstr)
|
2023-04-19 21:41:51 +02:00
|
|
|
|
2023-05-03 02:38:35 +02:00
|
|
|
// TODO: conditionally set db type.
|
2023-04-13 00:07:08 +02:00
|
|
|
db, err := ent.Open("sqlite3", connstr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open a connection to sqlite: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
2023-03-22 22:13:23 +01:00
|
|
|
|
2023-04-19 05:30:52 +02:00
|
|
|
log.Info("attempting to automatically migrate db schema")
|
2023-04-13 00:07:08 +02:00
|
|
|
// Run the auto migration tool.
|
|
|
|
if err = db.Schema.Create(context.Background()); err != nil {
|
|
|
|
return fmt.Errorf("failed creating schema resources: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
setting := settings.New()
|
|
|
|
|
|
|
|
setting.Consolidate(
|
|
|
|
conf, host, port, devel, version,
|
|
|
|
)
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
a := &app.App{}
|
2023-03-22 22:13:23 +01:00
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
err = a.Init(setting, log, db)
|
2023-03-22 22:13:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
a.PrintConfiguration()
|
|
|
|
|
2023-03-22 23:03:21 +01:00
|
|
|
a.SetupRoutes()
|
2023-03-22 22:13:23 +01:00
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
a.SetEchoSettings()
|
|
|
|
|
|
|
|
// a.SetConfig(conf)
|
|
|
|
a.SetEmbeds(templates, assets)
|
|
|
|
|
|
|
|
// // TODO: add check for prometheus config setting.
|
|
|
|
// if true {
|
|
|
|
// // import "github.com/labstack/echo-contrib/prometheus"
|
|
|
|
// p := prometheus.NewPrometheus("echo", nil)
|
|
|
|
// p.Use(e)
|
|
|
|
// }
|
|
|
|
|
|
|
|
e := a.E()
|
2023-05-02 00:04:04 +02:00
|
|
|
|
|
|
|
// channel used to check whether the app had troubles starting up.
|
|
|
|
started := make(chan error, 1)
|
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
defer close(started)
|
|
|
|
|
2023-05-02 00:04:04 +02:00
|
|
|
go func(ok chan error) {
|
2023-05-03 02:18:29 +02:00
|
|
|
p := setting.Port()
|
|
|
|
h := setting.Host()
|
2023-05-02 00:04:04 +02:00
|
|
|
|
|
|
|
address := h + ":" + strconv.Itoa(p)
|
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
if err := e.Start(address); err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Error("troubles running the server, bailing...", "error", err)
|
2023-05-02 00:04:04 +02:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
started <- err
|
2023-05-02 00:04:04 +02:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
return
|
2023-03-22 22:56:25 +01:00
|
|
|
}
|
2023-05-03 04:58:47 +02:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
started <- nil
|
2023-05-02 00:04:04 +02:00
|
|
|
}(started)
|
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
signal.Notify(quit, os.Interrupt)
|
|
|
|
signal.Notify(quit, syscall.SIGTERM)
|
|
|
|
signal.Notify(quit, syscall.SIGHUP)
|
|
|
|
|
2023-05-03 03:03:06 +02:00
|
|
|
// non-blocking channel receive.
|
|
|
|
select {
|
|
|
|
case err := <-started:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
case <-quit:
|
|
|
|
shutdownTimeout := 10 * time.Second
|
2023-03-22 22:56:25 +01:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
|
|
|
defer func() {
|
|
|
|
log.Infof("Interrupt received, gracefully shutting down the server (timeout %s)", shutdownTimeout)
|
|
|
|
cancel()
|
2023-03-22 22:56:25 +01:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
signal.Stop(quit)
|
2023-04-19 21:41:51 +02:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
close(quit)
|
2023-05-02 00:04:04 +02:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
log.Info("Bye!")
|
|
|
|
}()
|
2023-05-02 00:04:04 +02:00
|
|
|
|
2023-05-03 05:58:09 +02:00
|
|
|
if err = e.Shutdown(ctx); err != nil {
|
|
|
|
log.Error("There was an error shutting the server down")
|
|
|
|
return err
|
|
|
|
}
|
2023-03-19 22:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-13 00:07:08 +02:00
|
|
|
|
|
|
|
func printHeader() {
|
2023-04-19 21:22:00 +02:00
|
|
|
fmt.Fprintf(os.Stderr,
|
2023-04-19 23:36:12 +02:00
|
|
|
"\033[34m%s%s\033[0m\n\n\n",
|
2023-04-19 21:22:00 +02:00
|
|
|
banner,
|
|
|
|
slug,
|
|
|
|
)
|
2023-04-13 00:07:08 +02:00
|
|
|
}
|