2018-11-21 04:19:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2022-06-06 08:47:38 +00:00
|
|
|
"io/fs"
|
2018-11-21 04:19:57 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
"srs.epita.fr/fic-server/settings"
|
|
|
|
)
|
|
|
|
|
2018-12-06 02:46:14 +00:00
|
|
|
var DashboardDir string
|
2018-11-21 04:19:57 +00:00
|
|
|
var TeamsDir string
|
|
|
|
|
|
|
|
func main() {
|
2022-06-06 08:47:38 +00:00
|
|
|
var baseURL string
|
2021-07-21 10:30:51 +00:00
|
|
|
// Read paremeters from environment
|
|
|
|
if v, exists := os.LookupEnv("FIC_BASEURL"); exists {
|
2022-06-06 08:47:38 +00:00
|
|
|
baseURL = v
|
2021-07-21 10:30:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 04:19:57 +00:00
|
|
|
// Read parameters from command line
|
|
|
|
var bind = flag.String("bind", "127.0.0.1:8082", "Bind port/socket")
|
2022-06-06 08:48:06 +00:00
|
|
|
htpasswd_file := flag.String("htpasswd", "", "Restrict access with password, Apache htpasswd format")
|
2022-06-06 09:02:35 +00:00
|
|
|
restrict_ip := flag.String("restrict-to-ips", "", "Restrict access to IP listed in this JSON array")
|
2022-06-06 08:47:38 +00:00
|
|
|
flag.StringVar(&baseURL, "baseurl", baseURL, "URL prepended to each URL")
|
|
|
|
staticDir := flag.String("static", "./htdocs-dashboard/", "Directory containing static files")
|
2019-01-19 02:08:07 +00:00
|
|
|
flag.StringVar(&fic.FilesDir, "files", fic.FilesDir, "Base directory where found challenges files, local part")
|
2018-12-06 02:46:14 +00:00
|
|
|
flag.StringVar(&DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files")
|
2018-11-21 04:19:57 +00:00
|
|
|
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
2022-06-08 00:51:17 +00:00
|
|
|
flag.StringVar(&settings.SettingsDir, "settings", "./SETTINGSDIST", "Base directory where load and save settings")
|
2019-01-21 10:36:52 +00:00
|
|
|
var fwdr = flag.String("forwarder", "", "URL of another dashboard where send traffic to, except static assets")
|
|
|
|
flag.BoolVar(&fwdPublicJson, "fwdpublicjson", fwdPublicJson, "Also forward public.json files to forwarder")
|
2018-11-21 04:19:57 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
log.SetPrefix("[public] ")
|
|
|
|
|
|
|
|
// Sanitize options
|
|
|
|
var err error
|
|
|
|
log.Println("Checking paths...")
|
2022-06-06 08:47:38 +00:00
|
|
|
if staticDir != nil && *staticDir != "" {
|
|
|
|
if sDir, err := filepath.Abs(*staticDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
log.Println("Serving pages from", sDir)
|
|
|
|
staticFS = http.Dir(sDir)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sub, err := fs.Sub(assets, "static")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to cd to static/ directory:", err)
|
|
|
|
}
|
|
|
|
log.Println("Serving pages from memory.")
|
|
|
|
staticFS = http.FS(sub)
|
2018-11-21 04:19:57 +00:00
|
|
|
}
|
|
|
|
if fic.FilesDir, err = filepath.Abs(fic.FilesDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if settings.SettingsDir, err = filepath.Abs(settings.SettingsDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-06-06 08:47:38 +00:00
|
|
|
if baseURL != "/" {
|
|
|
|
baseURL = path.Clean(baseURL)
|
2018-11-21 04:19:57 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
baseURL = ""
|
2018-11-21 04:19:57 +00:00
|
|
|
}
|
2019-01-21 10:36:52 +00:00
|
|
|
if fwdr != nil && len(*fwdr) > 0 {
|
|
|
|
forwarder = fwdr
|
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
|
|
|
|
// Prepare graceful shutdown
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2022-06-06 09:02:35 +00:00
|
|
|
app := NewApp(htpasswd_file, restrict_ip, baseURL, *bind)
|
2022-06-06 08:47:38 +00:00
|
|
|
go app.Start()
|
2018-11-21 04:19:57 +00:00
|
|
|
|
|
|
|
// Wait shutdown signal
|
|
|
|
<-interrupt
|
|
|
|
|
|
|
|
log.Print("The service is shutting down...")
|
2022-06-06 08:47:38 +00:00
|
|
|
app.Stop()
|
2018-11-21 04:19:57 +00:00
|
|
|
log.Println("done")
|
|
|
|
}
|