2016-01-07 17:27:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-08 18:33:05 +00:00
|
|
|
"context"
|
2016-01-07 17:27:53 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2017-10-26 12:14:18 +00:00
|
|
|
"net/url"
|
2016-01-07 17:28:16 +00:00
|
|
|
"os"
|
2018-03-08 18:33:05 +00:00
|
|
|
"os/signal"
|
2016-11-19 15:26:23 +00:00
|
|
|
"path"
|
2016-01-20 21:44:34 +00:00
|
|
|
"path/filepath"
|
2017-10-26 12:14:18 +00:00
|
|
|
"strings"
|
2018-03-08 18:33:05 +00:00
|
|
|
"syscall"
|
2016-11-19 15:26:23 +00:00
|
|
|
"text/template"
|
2016-01-13 19:25:25 +00:00
|
|
|
|
2016-12-08 08:12:18 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/api"
|
2018-01-21 13:18:26 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/pki"
|
2017-11-27 01:45:33 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
2016-01-13 19:25:25 +00:00
|
|
|
"srs.epita.fr/fic-server/libfic"
|
2017-11-25 15:05:03 +00:00
|
|
|
"srs.epita.fr/fic-server/settings"
|
2016-01-07 17:27:53 +00:00
|
|
|
)
|
|
|
|
|
2016-11-19 15:35:39 +00:00
|
|
|
var StaticDir string
|
2016-01-07 17:27:53 +00:00
|
|
|
|
2017-10-26 12:59:52 +00:00
|
|
|
type ResponseWriterPrefix struct {
|
2019-07-12 17:22:05 +00:00
|
|
|
real http.ResponseWriter
|
2017-10-26 12:59:52 +00:00
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ResponseWriterPrefix) Header() http.Header {
|
|
|
|
return r.real.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ResponseWriterPrefix) WriteHeader(s int) {
|
|
|
|
if v, exists := r.real.Header()["Location"]; exists {
|
2019-07-12 17:22:05 +00:00
|
|
|
r.real.Header().Set("Location", r.prefix+v[0])
|
2017-10-26 12:59:52 +00:00
|
|
|
}
|
|
|
|
r.real.WriteHeader(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ResponseWriterPrefix) Write(z []byte) (int, error) {
|
|
|
|
return r.real.Write(z)
|
|
|
|
}
|
|
|
|
|
2017-10-26 12:14:18 +00:00
|
|
|
func StripPrefix(prefix string, h http.Handler) http.Handler {
|
|
|
|
if prefix == "" {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-10-26 12:59:52 +00:00
|
|
|
if prefix != "/" && r.URL.Path == "/" {
|
2019-07-12 17:22:05 +00:00
|
|
|
http.Redirect(w, r, prefix+"/", http.StatusFound)
|
2017-10-26 12:59:52 +00:00
|
|
|
} else if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
|
2017-10-26 12:14:18 +00:00
|
|
|
r2 := new(http.Request)
|
|
|
|
*r2 = *r
|
|
|
|
r2.URL = new(url.URL)
|
|
|
|
*r2.URL = *r.URL
|
|
|
|
r2.URL.Path = p
|
2017-10-26 12:59:52 +00:00
|
|
|
h.ServeHTTP(ResponseWriterPrefix{w, prefix}, r2)
|
2017-10-26 12:14:18 +00:00
|
|
|
} else {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:27:53 +00:00
|
|
|
func main() {
|
2017-12-12 06:13:38 +00:00
|
|
|
cloudDAVBase := ""
|
|
|
|
cloudUsername := "fic"
|
|
|
|
cloudPassword := ""
|
|
|
|
localImporterDirectory := ""
|
|
|
|
localImporterSymlink := false
|
|
|
|
|
2017-11-25 15:05:35 +00:00
|
|
|
// Read paremeters from environment
|
2018-01-21 13:18:26 +00:00
|
|
|
if v, exists := os.LookupEnv("FICCA_PASS"); exists {
|
|
|
|
pki.SetCAPassword(v)
|
|
|
|
} else {
|
|
|
|
log.Println("WARNING: no password defined for the CA, will use empty password to secure CA private key")
|
2019-10-02 14:20:06 +00:00
|
|
|
log.Println("WARNING: PLEASE DEFINE ENVIRONMENT VARIABLE: FICCA_PASS")
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
2017-11-25 15:05:35 +00:00
|
|
|
if v, exists := os.LookupEnv("FICCLOUD_URL"); exists {
|
2017-12-12 06:13:38 +00:00
|
|
|
cloudDAVBase = v
|
2017-11-25 15:05:35 +00:00
|
|
|
}
|
|
|
|
if v, exists := os.LookupEnv("FICCLOUD_USER"); exists {
|
2017-12-12 06:13:38 +00:00
|
|
|
cloudUsername = v
|
2017-11-25 15:05:35 +00:00
|
|
|
}
|
|
|
|
if v, exists := os.LookupEnv("FICCLOUD_PASS"); exists {
|
2017-12-12 06:13:38 +00:00
|
|
|
cloudPassword = v
|
2017-11-25 15:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read parameters from command line
|
2016-11-19 15:17:53 +00:00
|
|
|
var bind = flag.String("bind", "127.0.0.1:8081", "Bind port/socket")
|
2017-10-17 04:47:10 +00:00
|
|
|
var dsn = flag.String("dsn", fic.DSNGenerator(), "DSN to connect to the MySQL server")
|
2016-12-08 08:12:18 +00:00
|
|
|
var baseURL = flag.String("baseurl", "/", "URL prepended to each URL")
|
2019-10-31 15:05:58 +00:00
|
|
|
flag.StringVar(&api.TimestampCheck, "timestampCheck", api.TimestampCheck, "Path regularly touched by frontend to check time synchronisation")
|
2018-01-21 13:18:26 +00:00
|
|
|
flag.StringVar(&pki.PKIDir, "pki", "./PKI", "Base directory where found PKI scripts")
|
2016-11-19 15:35:39 +00:00
|
|
|
flag.StringVar(&StaticDir, "static", "./htdocs-admin/", "Directory containing static files")
|
2017-01-15 01:37:59 +00:00
|
|
|
flag.StringVar(&api.TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
2018-12-06 02:46:14 +00:00
|
|
|
flag.StringVar(&api.DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files")
|
2017-11-25 15:05:03 +00:00
|
|
|
flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where load and save settings")
|
|
|
|
flag.StringVar(&fic.FilesDir, "files", fic.FilesDir, "Base directory where found challenges files, local part")
|
2017-12-12 06:13:38 +00:00
|
|
|
flag.StringVar(&localImporterDirectory, "localimport", localImporterDirectory,
|
|
|
|
"Base directory where found challenges files to import, local part")
|
|
|
|
flag.BoolVar(&localImporterSymlink, "localimportsymlink", localImporterSymlink,
|
|
|
|
"Copy files or just create symlink?")
|
|
|
|
flag.StringVar(&cloudDAVBase, "clouddav", cloudDAVBase,
|
|
|
|
"Base directory where found challenges files to import, cloud part")
|
|
|
|
flag.StringVar(&cloudUsername, "clouduser", cloudUsername, "Username used to sync")
|
|
|
|
flag.StringVar(&cloudPassword, "cloudpass", cloudPassword, "Password used to sync")
|
2017-11-25 15:05:03 +00:00
|
|
|
flag.BoolVar(&fic.OptionalDigest, "optionaldigest", fic.OptionalDigest, "Is the digest required when importing files?")
|
2017-12-12 06:13:38 +00:00
|
|
|
flag.BoolVar(&fic.StrongDigest, "strongdigest", fic.StrongDigest, "Are BLAKE2b digests required or is SHA-1 good enough?")
|
2016-01-07 17:27:53 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2016-10-13 17:02:41 +00:00
|
|
|
log.SetPrefix("[admin] ")
|
|
|
|
|
2017-12-12 06:13:38 +00:00
|
|
|
// Instantiate importer
|
|
|
|
if localImporterDirectory != "" && cloudDAVBase != "" {
|
|
|
|
log.Fatal("Cannot have both --clouddav and --localimport defined.")
|
|
|
|
return
|
|
|
|
} else if localImporterDirectory != "" {
|
2018-02-16 09:50:44 +00:00
|
|
|
sync.GlobalImporter = sync.LocalImporter{Base: localImporterDirectory, Symlink: localImporterSymlink}
|
2017-12-12 06:13:38 +00:00
|
|
|
} else if cloudDAVBase != "" {
|
|
|
|
sync.GlobalImporter, _ = sync.NewCloudImporter(cloudDAVBase, cloudUsername, cloudPassword)
|
|
|
|
}
|
|
|
|
if sync.GlobalImporter != nil {
|
|
|
|
log.Println("Using", sync.GlobalImporter.Kind())
|
|
|
|
}
|
2017-12-04 07:40:57 +00:00
|
|
|
|
2017-11-21 21:24:06 +00:00
|
|
|
// Sanitize options
|
2016-01-20 21:44:34 +00:00
|
|
|
var err error
|
|
|
|
log.Println("Checking paths...")
|
2016-11-19 15:35:39 +00:00
|
|
|
if StaticDir, err = filepath.Abs(StaticDir); err != nil {
|
2016-01-22 16:34:34 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-06-23 13:45:32 +00:00
|
|
|
sync.DeepReportPath = path.Join(StaticDir, sync.DeepReportPath)
|
2016-01-20 21:44:34 +00:00
|
|
|
if fic.FilesDir, err = filepath.Abs(fic.FilesDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-01-21 13:18:26 +00:00
|
|
|
if pki.PKIDir, err = filepath.Abs(pki.PKIDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-12-06 02:46:14 +00:00
|
|
|
if api.DashboardDir, err = filepath.Abs(api.DashboardDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-01-16 12:12:03 +00:00
|
|
|
if api.TeamsDir, err = filepath.Abs(api.TeamsDir); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-31 15:05:58 +00:00
|
|
|
if api.TimestampCheck, err = filepath.Abs(api.TimestampCheck); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-11-25 15:05:03 +00:00
|
|
|
if settings.SettingsDir, err = filepath.Abs(settings.SettingsDir); err != nil {
|
2017-11-22 01:00:40 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-12-15 23:51:56 +00:00
|
|
|
if *baseURL != "/" {
|
|
|
|
tmp := path.Clean(*baseURL)
|
|
|
|
baseURL = &tmp
|
|
|
|
} else {
|
|
|
|
tmp := ""
|
|
|
|
baseURL = &tmp
|
|
|
|
}
|
2016-01-20 21:44:34 +00:00
|
|
|
|
2018-08-19 19:00:32 +00:00
|
|
|
// Creating minimal directories structure
|
|
|
|
os.MkdirAll(fic.FilesDir, 0777)
|
|
|
|
os.MkdirAll(pki.PKIDir, 0711)
|
|
|
|
os.MkdirAll(api.TeamsDir, 0777)
|
2018-12-06 02:46:14 +00:00
|
|
|
os.MkdirAll(api.DashboardDir, 0777)
|
2018-08-19 19:00:32 +00:00
|
|
|
os.MkdirAll(settings.SettingsDir, 0777)
|
|
|
|
|
|
|
|
// Initialize settings and load them
|
|
|
|
if !settings.ExistsSettings(path.Join(settings.SettingsDir, settings.SettingsFile)) {
|
2019-01-17 15:55:54 +00:00
|
|
|
if err = settings.SaveSettings(path.Join(settings.SettingsDir, settings.SettingsFile), settings.FICSettings{
|
2019-07-12 17:22:05 +00:00
|
|
|
Title: "Challenge FIC",
|
|
|
|
Authors: "Laboratoire SRS, ÉPITA",
|
|
|
|
FirstBlood: fic.FirstBlood,
|
|
|
|
SubmissionCostBase: fic.SubmissionCostBase,
|
|
|
|
ExerciceCurCoefficient: 1,
|
|
|
|
HintCurCoefficient: 1,
|
|
|
|
WChoiceCurCoefficient: 1,
|
|
|
|
AllowRegistration: false,
|
|
|
|
CanJoinTeam: false,
|
|
|
|
DenyNameChange: false,
|
2020-01-20 14:56:02 +00:00
|
|
|
AcceptNewIssue: true,
|
2019-07-12 17:22:05 +00:00
|
|
|
EnableResolutionRoute: false,
|
|
|
|
PartialValidation: true,
|
|
|
|
UnlockedChallengeDepth: 0,
|
|
|
|
SubmissionUniqueness: false,
|
|
|
|
DisplayAllFlags: false,
|
|
|
|
EventKindness: false,
|
2018-08-19 19:00:32 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Fatal("Unable to initialize settings.json:", err)
|
|
|
|
}
|
2018-12-06 20:25:09 +00:00
|
|
|
} else {
|
2019-01-17 15:55:54 +00:00
|
|
|
var config settings.FICSettings
|
|
|
|
if config, err = settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)); err != nil {
|
|
|
|
log.Fatal("Unable to read settings.json:", err)
|
|
|
|
} else {
|
|
|
|
api.ApplySettings(config)
|
|
|
|
}
|
2018-08-19 19:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Database connection
|
2018-01-21 13:18:26 +00:00
|
|
|
log.Println("Opening database...")
|
2019-01-17 15:55:54 +00:00
|
|
|
if err = fic.DBInit(*dsn); err != nil {
|
2016-01-07 17:28:16 +00:00
|
|
|
log.Fatal("Cannot open the database: ", err)
|
|
|
|
}
|
2016-01-13 19:25:25 +00:00
|
|
|
defer fic.DBClose()
|
2016-01-07 17:28:16 +00:00
|
|
|
|
|
|
|
log.Println("Creating database...")
|
2019-01-17 15:55:54 +00:00
|
|
|
if err = fic.DBCreate(); err != nil {
|
2016-01-07 17:28:16 +00:00
|
|
|
log.Fatal("Cannot create database: ", err)
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:24:06 +00:00
|
|
|
// Update base URL on main page
|
2019-07-12 17:22:05 +00:00
|
|
|
log.Println("Changing base URL to", *baseURL+"/", "...")
|
2016-11-19 15:35:39 +00:00
|
|
|
if file, err := os.OpenFile(path.Join(StaticDir, "index.html"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0644)); err != nil {
|
2016-11-19 15:26:23 +00:00
|
|
|
log.Println("Unable to open index.html: ", err)
|
|
|
|
} else if indexTmpl, err := template.New("index").Parse(indextpl); err != nil {
|
|
|
|
log.Println("Cannot create template: ", err)
|
2019-07-12 17:22:05 +00:00
|
|
|
} else if err = indexTmpl.Execute(file, map[string]string{"urlbase": path.Clean(path.Join(*baseURL+"/", "nuke"))[:len(path.Clean(path.Join(*baseURL+"/", "nuke")))-4]}); err != nil {
|
2016-11-19 15:26:23 +00:00
|
|
|
log.Println("An error occurs during template execution: ", err)
|
|
|
|
}
|
|
|
|
|
2018-03-08 18:33:05 +00:00
|
|
|
// Prepare graceful shutdown
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
srv := &http.Server{
|
2019-07-12 17:22:05 +00:00
|
|
|
Addr: *bind,
|
2018-03-08 18:33:05 +00:00
|
|
|
Handler: StripPrefix(*baseURL, api.Router()),
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:24:06 +00:00
|
|
|
// Serve content
|
2018-03-08 18:33:05 +00:00
|
|
|
go func() {
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}()
|
2016-01-07 17:27:53 +00:00
|
|
|
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
2018-03-08 18:33:05 +00:00
|
|
|
|
|
|
|
// Wait shutdown signal
|
|
|
|
<-interrupt
|
|
|
|
|
|
|
|
log.Print("The service is shutting down...")
|
|
|
|
srv.Shutdown(context.Background())
|
|
|
|
log.Println("done")
|
2016-01-07 17:27:53 +00:00
|
|
|
}
|