2020-07-29 21:11:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-05 16:32:49 +00:00
|
|
|
"strings"
|
2020-07-29 21:11:28 +00:00
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-03-29 20:49:46 +00:00
|
|
|
"github.com/hrfee/mediabrowser"
|
2021-04-06 20:25:44 +00:00
|
|
|
"github.com/itchyny/timefmt-go"
|
2020-08-16 12:36:54 +00:00
|
|
|
"gopkg.in/ini.v1"
|
2020-07-29 21:11:28 +00:00
|
|
|
)
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
func respond(code int, message string, gc *gin.Context) {
|
|
|
|
resp := stringResponse{}
|
|
|
|
if code == 200 || code == 204 {
|
|
|
|
resp.Response = message
|
|
|
|
} else {
|
|
|
|
resp.Error = message
|
|
|
|
}
|
|
|
|
gc.JSON(code, resp)
|
|
|
|
gc.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func respondBool(code int, val bool, gc *gin.Context) {
|
|
|
|
resp := boolResponse{}
|
|
|
|
if !val {
|
|
|
|
resp.Error = true
|
|
|
|
} else {
|
|
|
|
resp.Success = true
|
|
|
|
}
|
|
|
|
gc.JSON(code, resp)
|
|
|
|
gc.Abort()
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) loadStrftime() {
|
2021-05-07 20:53:29 +00:00
|
|
|
app.datePattern = app.config.Section("messages").Key("date_format").String()
|
2020-08-16 12:36:54 +00:00
|
|
|
app.timePattern = `%H:%M`
|
2021-05-07 20:53:29 +00:00
|
|
|
if val, _ := app.config.Section("messages").Key("use_24h").Bool(); !val {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.timePattern = `%I:%M %p`
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) prettyTime(dt time.Time) (date, time string) {
|
2021-04-06 20:25:44 +00:00
|
|
|
date = timefmt.Format(dt, app.datePattern)
|
|
|
|
time = timefmt.Format(dt, app.timePattern)
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) formatDatetime(dt time.Time) string {
|
|
|
|
d, t := app.prettyTime(dt)
|
2020-07-31 11:48:37 +00:00
|
|
|
return d + " " + t
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/36530251/time-since-with-months-and-years/36531443#36531443 THANKS
|
|
|
|
func timeDiff(a, b time.Time) (year, month, day, hour, min, sec int) {
|
|
|
|
if a.Location() != b.Location() {
|
|
|
|
b = b.In(a.Location())
|
|
|
|
}
|
|
|
|
if a.After(b) {
|
|
|
|
a, b = b, a
|
|
|
|
}
|
|
|
|
y1, M1, d1 := a.Date()
|
|
|
|
y2, M2, d2 := b.Date()
|
|
|
|
|
|
|
|
h1, m1, s1 := a.Clock()
|
|
|
|
h2, m2, s2 := b.Clock()
|
|
|
|
|
|
|
|
year = int(y2 - y1)
|
|
|
|
month = int(M2 - M1)
|
|
|
|
day = int(d2 - d1)
|
|
|
|
hour = int(h2 - h1)
|
|
|
|
min = int(m2 - m1)
|
|
|
|
sec = int(s2 - s1)
|
|
|
|
|
|
|
|
// Normalize negative values
|
|
|
|
if sec < 0 {
|
|
|
|
sec += 60
|
|
|
|
min--
|
|
|
|
}
|
|
|
|
if min < 0 {
|
|
|
|
min += 60
|
|
|
|
hour--
|
|
|
|
}
|
|
|
|
if hour < 0 {
|
|
|
|
hour += 24
|
|
|
|
day--
|
|
|
|
}
|
|
|
|
if day < 0 {
|
|
|
|
// days in month:
|
|
|
|
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
|
|
|
|
day += 32 - t.Day()
|
|
|
|
month--
|
|
|
|
}
|
|
|
|
if month < 0 {
|
|
|
|
month += 12
|
|
|
|
year--
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
// Routes from now on!
|
2020-07-31 12:59:25 +00:00
|
|
|
|
2021-06-07 12:46:46 +00:00
|
|
|
// @Summary Resets a user's password with a PIN, and optionally set a new password if given.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Success 400 {object} PasswordValidation
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Param ResetPasswordDTO body ResetPasswordDTO true "Pin and optional Password."
|
|
|
|
// @Router /reset [post]
|
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) ResetSetPassword(gc *gin.Context) {
|
|
|
|
var req ResetPasswordDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
validation := app.validator.validate(req.Password)
|
|
|
|
valid := true
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
valid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid || req.PIN == "" {
|
|
|
|
// 200 bcs idk what i did in js
|
|
|
|
app.info.Printf("%s: Password reset failed: Invalid password", req.PIN)
|
|
|
|
gc.JSON(400, validation)
|
|
|
|
return
|
|
|
|
}
|
2021-10-13 14:04:22 +00:00
|
|
|
isInternal := false
|
|
|
|
var userID, username string
|
|
|
|
if reset, ok := app.internalPWRs[req.PIN]; ok {
|
|
|
|
isInternal = true
|
|
|
|
if time.Now().After(reset.Expiry) {
|
|
|
|
app.info.Printf("Password reset failed: PIN \"%s\" has expired", reset.PIN)
|
|
|
|
respondBool(401, false, gc)
|
|
|
|
delete(app.internalPWRs, req.PIN)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userID = reset.ID
|
|
|
|
username = reset.Username
|
|
|
|
status, err := app.jf.ResetPasswordAdmin(userID)
|
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
|
|
|
app.err.Printf("Password Reset failed (%d): %v", status, err)
|
|
|
|
respondBool(status, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resp, status, err := app.jf.ResetPassword(req.PIN)
|
|
|
|
if status != 200 || err != nil || !resp.Success {
|
|
|
|
app.err.Printf("Password Reset failed (%d): %v", status, err)
|
|
|
|
respondBool(status, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.Password == "" || len(resp.UsersReset) == 0 {
|
|
|
|
respondBool(200, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username = resp.UsersReset[0]
|
2021-06-07 12:46:46 +00:00
|
|
|
}
|
2021-10-13 14:04:22 +00:00
|
|
|
var user mediabrowser.User
|
|
|
|
var status int
|
|
|
|
var err error
|
|
|
|
if isInternal {
|
|
|
|
user, status, err = app.jf.UserByID(userID, false)
|
|
|
|
} else {
|
|
|
|
user, status, err = app.jf.UserByName(username, false)
|
2021-06-07 12:46:46 +00:00
|
|
|
}
|
|
|
|
if status != 200 || err != nil {
|
2021-10-13 14:04:22 +00:00
|
|
|
app.err.Printf("Failed to get user \"%s\" (%d): %v", username, status, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-10-13 14:04:22 +00:00
|
|
|
prevPassword := req.PIN
|
|
|
|
if isInternal {
|
|
|
|
prevPassword = ""
|
|
|
|
}
|
|
|
|
status, err = app.jf.SetPassword(user.ID, prevPassword, req.Password)
|
2021-06-07 12:46:46 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-10-13 14:04:22 +00:00
|
|
|
app.err.Printf("Failed to change password for \"%s\" (%d): %v", username, status, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
|
|
|
// Silently fail for changing ombi passwords
|
|
|
|
if status != 200 || err != nil {
|
2021-10-13 14:04:22 +00:00
|
|
|
app.err.Printf("Failed to get user \"%s\" from jellyfin/emby (%d): %v", username, status, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ombiUser, status, err := app.getOmbiUser(user.ID)
|
|
|
|
if status != 200 || err != nil {
|
2021-10-13 14:04:22 +00:00
|
|
|
app.err.Printf("Failed to get user \"%s\" from ombi (%d): %v", username, status, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ombiUser["password"] = req.Password
|
|
|
|
status, err = app.ombi.ModifyUser(ombiUser)
|
|
|
|
if status != 200 || err != nil {
|
|
|
|
app.err.Printf("Failed to set password for ombi user \"%s\" (%d): %v", ombiUser["userName"], status, err)
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.debug.Printf("Reset password for ombi user \"%s\"", ombiUser["userName"])
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Get jfa-go configuration.
|
|
|
|
// @Produce json
|
2021-01-05 18:16:23 +00:00
|
|
|
// @Success 200 {object} settings "Uses the same format as config-base.json"
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Router /config [get]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Configuration
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) GetConfig(gc *gin.Context) {
|
|
|
|
app.info.Println("Config requested")
|
2021-01-05 18:16:23 +00:00
|
|
|
resp := app.configBase
|
|
|
|
// Load language options
|
2021-01-31 18:50:04 +00:00
|
|
|
formOptions := app.storage.lang.Form.getOptions()
|
2021-01-12 23:37:22 +00:00
|
|
|
fl := resp.Sections["ui"].Settings["language-form"]
|
|
|
|
fl.Options = formOptions
|
2021-01-31 18:50:04 +00:00
|
|
|
fl.Value = app.config.Section("ui").Key("language-form").MustString("en-us")
|
2021-03-30 21:41:28 +00:00
|
|
|
pwrOptions := app.storage.lang.PasswordReset.getOptions()
|
|
|
|
pl := resp.Sections["password_resets"].Settings["language"]
|
|
|
|
pl.Options = pwrOptions
|
|
|
|
pl.Value = app.config.Section("password_resets").Key("language").MustString("en-us")
|
2021-01-31 18:50:04 +00:00
|
|
|
adminOptions := app.storage.lang.Admin.getOptions()
|
2021-01-12 23:37:22 +00:00
|
|
|
al := resp.Sections["ui"].Settings["language-admin"]
|
|
|
|
al.Options = adminOptions
|
2021-01-31 18:50:04 +00:00
|
|
|
al.Value = app.config.Section("ui").Key("language-admin").MustString("en-us")
|
|
|
|
emailOptions := app.storage.lang.Email.getOptions()
|
2021-01-14 20:24:28 +00:00
|
|
|
el := resp.Sections["email"].Settings["language"]
|
|
|
|
el.Options = emailOptions
|
2021-01-31 18:50:04 +00:00
|
|
|
el.Value = app.config.Section("email").Key("language").MustString("en-us")
|
2021-05-07 20:53:29 +00:00
|
|
|
telegramOptions := app.storage.lang.Email.getOptions()
|
|
|
|
tl := resp.Sections["telegram"].Settings["language"]
|
|
|
|
tl.Options = telegramOptions
|
|
|
|
tl.Value = app.config.Section("telegram").Key("language").MustString("en-us")
|
2021-03-20 23:32:32 +00:00
|
|
|
if updater == "" {
|
|
|
|
delete(resp.Sections, "updates")
|
|
|
|
for i, v := range resp.Order {
|
|
|
|
if v == "updates" {
|
|
|
|
resp.Order = append(resp.Order[:i], resp.Order[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-17 00:16:59 +00:00
|
|
|
if PLATFORM == "windows" {
|
|
|
|
delete(resp.Sections["smtp"].Settings, "ssl_cert")
|
|
|
|
for i, v := range resp.Sections["smtp"].Order {
|
|
|
|
if v == "ssl_cert" {
|
|
|
|
sect := resp.Sections["smtp"]
|
|
|
|
sect.Order = append(sect.Order[:i], sect.Order[i+1:]...)
|
|
|
|
resp.Sections["smtp"] = sect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 14:41:08 +00:00
|
|
|
if !MatrixE2EE() {
|
|
|
|
delete(resp.Sections["matrix"].Settings, "encryption")
|
|
|
|
for i, v := range resp.Sections["matrix"].Order {
|
|
|
|
if v == "encryption" {
|
|
|
|
sect := resp.Sections["matrix"]
|
|
|
|
sect.Order = append(sect.Order[:i], sect.Order[i+1:]...)
|
|
|
|
resp.Sections["matrix"] = sect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
for sectName, section := range resp.Sections {
|
|
|
|
for settingName, setting := range section.Settings {
|
|
|
|
val := app.config.Section(sectName).Key(settingName)
|
|
|
|
s := resp.Sections[sectName].Settings[settingName]
|
|
|
|
switch setting.Type {
|
|
|
|
case "text", "email", "select", "password":
|
|
|
|
s.Value = val.MustString("")
|
|
|
|
case "number":
|
|
|
|
s.Value = val.MustInt(0)
|
|
|
|
case "bool":
|
|
|
|
s.Value = val.MustBool(false)
|
2020-07-31 15:09:30 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
resp.Sections[sectName].Settings[settingName] = s
|
2020-07-31 15:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-13 22:34:47 +00:00
|
|
|
if discordEnabled {
|
|
|
|
r, err := app.discord.ListRoles()
|
|
|
|
if err == nil {
|
|
|
|
roles := make([][2]string, len(r)+1)
|
|
|
|
roles[0] = [2]string{"", "None"}
|
|
|
|
for i, role := range r {
|
|
|
|
roles[i+1] = role
|
|
|
|
}
|
|
|
|
s := resp.Sections["discord"].Settings["apply_role"]
|
|
|
|
s.Options = roles
|
|
|
|
resp.Sections["discord"].Settings["apply_role"] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 23:37:22 +00:00
|
|
|
resp.Sections["ui"].Settings["language-form"] = fl
|
|
|
|
resp.Sections["ui"].Settings["language-admin"] = al
|
2021-01-14 20:24:28 +00:00
|
|
|
resp.Sections["email"].Settings["language"] = el
|
2021-03-30 21:41:28 +00:00
|
|
|
resp.Sections["password_resets"].Settings["language"] = pl
|
2021-05-07 20:53:29 +00:00
|
|
|
resp.Sections["telegram"].Settings["language"] = tl
|
2021-05-30 10:47:41 +00:00
|
|
|
resp.Sections["discord"].Settings["language"] = tl
|
|
|
|
resp.Sections["matrix"].Settings["language"] = tl
|
2021-01-09 20:38:13 +00:00
|
|
|
|
2021-07-27 15:48:24 +00:00
|
|
|
// if setting := resp.Sections["invite_emails"].Settings["url_base"]; setting.Value == "" {
|
|
|
|
// setting.Value = strings.TrimSuffix(resp.Sections["password_resets"].Settings["url_base"].Value.(string), "/invite")
|
|
|
|
// resp.Sections["invite_emails"].Settings["url_base"] = setting
|
|
|
|
// }
|
|
|
|
// if setting := resp.Sections["password_resets"].Settings["url_base"]; setting.Value == "" {
|
|
|
|
// setting.Value = strings.TrimSuffix(resp.Sections["invite_emails"].Settings["url_base"].Value.(string), "/invite")
|
|
|
|
// resp.Sections["password_resets"].Settings["url_base"] = setting
|
|
|
|
// }
|
|
|
|
|
2020-07-31 15:09:30 +00:00
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
2020-07-31 21:07:09 +00:00
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Modify app config.
|
|
|
|
// @Produce json
|
|
|
|
// @Param appConfig body configDTO true "Config split into sections as in config.ini, all values as strings."
|
|
|
|
// @Success 200 {object} boolResponse
|
2022-02-03 19:19:21 +00:00
|
|
|
// @Failure 500 {object} stringResponse
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Router /config [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Configuration
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) ModifyConfig(gc *gin.Context) {
|
|
|
|
app.info.Println("Config modification requested")
|
2020-09-24 16:51:13 +00:00
|
|
|
var req configDTO
|
2020-07-31 21:07:09 +00:00
|
|
|
gc.BindJSON(&req)
|
2021-04-05 15:34:47 +00:00
|
|
|
// Load a new config, as we set various default values in app.config that shouldn't be stored.
|
2020-11-22 16:36:43 +00:00
|
|
|
tempConfig, _ := ini.Load(app.configPath)
|
2020-07-31 21:07:09 +00:00
|
|
|
for section, settings := range req {
|
2020-09-23 16:20:48 +00:00
|
|
|
if section != "restart-program" {
|
|
|
|
_, err := tempConfig.GetSection(section)
|
|
|
|
if err != nil {
|
|
|
|
tempConfig.NewSection(section)
|
|
|
|
}
|
2020-07-31 21:07:09 +00:00
|
|
|
for setting, value := range settings.(map[string]interface{}) {
|
2021-05-07 20:53:29 +00:00
|
|
|
if section == "email" && setting == "method" && value == "disabled" {
|
|
|
|
value = ""
|
|
|
|
}
|
2022-01-14 17:01:42 +00:00
|
|
|
if (section == "discord" || section == "matrix") && setting == "language" {
|
|
|
|
tempConfig.Section("telegram").Key("language").SetValue(value.(string))
|
2022-03-22 14:58:39 +00:00
|
|
|
} else if value.(string) != app.config.Section(section).Key(setting).MustString("") {
|
|
|
|
tempConfig.Section(section).Key(setting).SetValue(value.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
tempConfig.Section("").Key("first_run").SetValue("false")
|
|
|
|
if err := tempConfig.SaveTo(app.configPath); err != nil {
|
|
|
|
app.err.Printf("Failed to save config to \"%s\": %v", app.configPath, err)
|
|
|
|
respond(500, err.Error(), gc)
|
2021-07-24 18:49:08 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
app.debug.Println("Config saved")
|
|
|
|
gc.JSON(200, map[string]bool{"success": true})
|
|
|
|
if req["restart-program"] != nil && req["restart-program"].(bool) {
|
|
|
|
app.info.Println("Restarting...")
|
|
|
|
if TRAY {
|
|
|
|
TRAYRESTART <- true
|
|
|
|
} else {
|
|
|
|
RESTART <- true
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
app.loadConfig()
|
|
|
|
// Reinitialize password validator on config change, as opposed to every applicable request like in python.
|
|
|
|
if _, ok := req["password_validation"]; ok {
|
|
|
|
app.debug.Println("Reinitializing validator")
|
|
|
|
validatorConf := ValidatorConf{
|
|
|
|
"length": app.config.Section("password_validation").Key("min_length").MustInt(0),
|
|
|
|
"uppercase": app.config.Section("password_validation").Key("upper").MustInt(0),
|
|
|
|
"lowercase": app.config.Section("password_validation").Key("lower").MustInt(0),
|
|
|
|
"number": app.config.Section("password_validation").Key("number").MustInt(0),
|
|
|
|
"special": app.config.Section("password_validation").Key("special").MustInt(0),
|
|
|
|
}
|
|
|
|
if !app.config.Section("password_validation").Key("enabled").MustBool(false) {
|
|
|
|
for key := range validatorConf {
|
|
|
|
validatorConf[key] = 0
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
app.validator.init(validatorConf)
|
2021-02-22 00:43:36 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 20:28:36 +00:00
|
|
|
// @Summary Returns whether there's a new update, and extra info if there is.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} checkUpdateDTO
|
|
|
|
// @Router /config/update [get]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2021-04-12 20:28:36 +00:00
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) CheckUpdate(gc *gin.Context) {
|
|
|
|
if !app.newUpdate {
|
|
|
|
app.update = Update{}
|
|
|
|
}
|
|
|
|
gc.JSON(200, checkUpdateDTO{New: app.newUpdate, Update: app.update})
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Apply an update.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Success 400 {object} stringResponse
|
|
|
|
// @Success 500 {object} boolResponse
|
|
|
|
// @Router /config/update [post]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2021-04-12 20:28:36 +00:00
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) ApplyUpdate(gc *gin.Context) {
|
|
|
|
if !app.update.CanUpdate {
|
|
|
|
respond(400, "Update is manual", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := app.update.update()
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Failed to apply update: %v", err)
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-05-03 19:08:23 +00:00
|
|
|
if PLATFORM == "windows" {
|
|
|
|
respondBool(500, true, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-04-12 20:28:36 +00:00
|
|
|
respondBool(200, true, gc)
|
2021-05-03 19:08:23 +00:00
|
|
|
app.HardRestart()
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Logout by deleting refresh token from cookies.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /logout [post]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Other
|
2020-08-19 21:30:54 +00:00
|
|
|
func (app *appContext) Logout(gc *gin.Context) {
|
2020-08-20 19:20:31 +00:00
|
|
|
cookie, err := gc.Cookie("refresh")
|
|
|
|
if err != nil {
|
|
|
|
app.debug.Printf("Couldn't get cookies: %s", err)
|
|
|
|
respond(500, "Couldn't fetch cookies", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.invalidTokens = append(app.invalidTokens, cookie)
|
|
|
|
gc.SetCookie("refresh", "invalid", -1, "/", gc.Request.URL.Hostname(), true, true)
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-08-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 19:17:43 +00:00
|
|
|
// @Summary Returns a map of available language codes to their full names, usable in the lang query parameter.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} langDTO
|
|
|
|
// @Failure 500 {object} stringResponse
|
2021-05-02 19:41:08 +00:00
|
|
|
// @Param page path string true "admin/form/setup/email/pwr"
|
2021-02-19 21:38:20 +00:00
|
|
|
// @Router /lang/{page} [get]
|
2021-01-11 19:17:43 +00:00
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) GetLanguages(gc *gin.Context) {
|
2021-01-12 23:43:44 +00:00
|
|
|
page := gc.Param("page")
|
2021-01-11 19:17:43 +00:00
|
|
|
resp := langDTO{}
|
2021-05-02 13:15:03 +00:00
|
|
|
switch page {
|
|
|
|
case "form":
|
2021-01-12 23:43:44 +00:00
|
|
|
for key, lang := range app.storage.lang.Form {
|
2021-01-19 00:29:29 +00:00
|
|
|
resp[key] = lang.Meta.Name
|
2021-01-12 23:43:44 +00:00
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "admin":
|
2021-01-12 23:43:44 +00:00
|
|
|
for key, lang := range app.storage.lang.Admin {
|
2021-01-19 00:29:29 +00:00
|
|
|
resp[key] = lang.Meta.Name
|
2021-01-12 23:43:44 +00:00
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "setup":
|
2021-01-26 22:57:29 +00:00
|
|
|
for key, lang := range app.storage.lang.Setup {
|
|
|
|
resp[key] = lang.Meta.Name
|
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "email":
|
2021-01-26 22:57:29 +00:00
|
|
|
for key, lang := range app.storage.lang.Email {
|
|
|
|
resp[key] = lang.Meta.Name
|
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "pwr":
|
|
|
|
for key, lang := range app.storage.lang.PasswordReset {
|
|
|
|
resp[key] = lang.Meta.Name
|
|
|
|
}
|
2021-01-11 19:17:43 +00:00
|
|
|
}
|
|
|
|
if len(resp) == 0 {
|
|
|
|
respond(500, "Couldn't get languages", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:41:08 +00:00
|
|
|
// @Summary Serves a translations for pages "admin" or "form".
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} adminLang
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Param page path string true "admin or form."
|
|
|
|
// @Param language path string true "language code, e.g en-us."
|
|
|
|
// @Router /lang/{page}/{language} [get]
|
2021-02-02 18:09:02 +00:00
|
|
|
// @tags Other
|
2021-01-19 00:29:29 +00:00
|
|
|
func (app *appContext) ServeLang(gc *gin.Context) {
|
|
|
|
page := gc.Param("page")
|
|
|
|
lang := strings.Replace(gc.Param("file"), ".json", "", 1)
|
|
|
|
if page == "admin" {
|
|
|
|
gc.JSON(200, app.storage.lang.Admin[lang])
|
|
|
|
return
|
|
|
|
} else if page == "form" {
|
|
|
|
gc.JSON(200, app.storage.lang.Form[lang])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:41:08 +00:00
|
|
|
// @Summary Restarts the program. No response means success.
|
|
|
|
// @Router /restart [post]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2021-05-02 19:41:08 +00:00
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) restart(gc *gin.Context) {
|
|
|
|
app.info.Println("Restarting...")
|
|
|
|
err := app.Restart()
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Couldn't restart, try restarting manually: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 17:54:27 +00:00
|
|
|
// @Summary Returns the last 100 lines of the log.
|
|
|
|
// @Router /log [get]
|
|
|
|
// @Success 200 {object} LogDTO
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) GetLog(gc *gin.Context) {
|
|
|
|
gc.JSON(200, LogDTO{lineCache.String()})
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:08:50 +00:00
|
|
|
// no need to syscall.exec anymore!
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) Restart() error {
|
2021-08-16 19:41:07 +00:00
|
|
|
if TRAY {
|
|
|
|
TRAYRESTART <- true
|
|
|
|
} else {
|
|
|
|
RESTART <- true
|
|
|
|
}
|
2020-09-08 22:08:50 +00:00
|
|
|
return nil
|
2020-08-01 23:05:35 +00:00
|
|
|
}
|