2021-02-02 18:09:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-02-19 16:12:14 +00:00
|
|
|
"github.com/fatih/color"
|
2021-02-02 18:09:02 +00:00
|
|
|
"github.com/gin-contrib/pprof"
|
|
|
|
"github.com/gin-contrib/static"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2021-02-02 18:09:02 +00:00
|
|
|
swaggerFiles "github.com/swaggo/files"
|
|
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// loads HTML templates. If [files]/html_templates is set, alternative files inside the directory are loaded in place of the internal templates.
|
|
|
|
func (app *appContext) loadHTML(router *gin.Engine) {
|
|
|
|
customPath := app.config.Section("files").Key("html_templates").MustString("")
|
|
|
|
templatePath := "html"
|
|
|
|
htmlFiles, err := fs.ReadDir(localFS, templatePath)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Fatalf(lm.FailedReading, templatePath, err)
|
2021-02-02 18:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
2024-05-26 17:08:48 +00:00
|
|
|
loadInternal := []string{}
|
|
|
|
loadExternal := []string{}
|
|
|
|
for _, f := range htmlFiles {
|
2021-02-02 18:09:02 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(customPath, f.Name())); os.IsNotExist(err) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Printf(lm.UseDefaultHTML, f.Name())
|
2024-05-26 17:08:48 +00:00
|
|
|
loadInternal = append(loadInternal, FSJoin(templatePath, f.Name()))
|
2021-02-02 18:09:02 +00:00
|
|
|
} else {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Printf(lm.UseCustomHTML, f.Name())
|
2024-05-26 17:08:48 +00:00
|
|
|
loadExternal = append(loadExternal, filepath.Join(filepath.Join(customPath, f.Name())))
|
2021-02-02 18:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-26 17:08:48 +00:00
|
|
|
var tmpl *template.Template
|
|
|
|
if len(loadInternal) != 0 {
|
|
|
|
tmpl, err = template.ParseFS(localFS, loadInternal...)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Fatalf(lm.FailedLoadTemplates, lm.Internal, err)
|
2024-05-26 17:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(loadExternal) != 0 {
|
|
|
|
tmpl, err = tmpl.ParseFiles(loadExternal...)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Fatalf(lm.FailedLoadTemplates, lm.External, err)
|
2024-05-26 17:08:48 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
}
|
|
|
|
router.SetHTMLTemplate(tmpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets gin logger.
|
|
|
|
func setGinLogger(router *gin.Engine, debugMode bool) {
|
2021-02-19 16:12:14 +00:00
|
|
|
sprintf := color.New(color.Faint).SprintfFunc()
|
2021-02-02 18:09:02 +00:00
|
|
|
if debugMode {
|
|
|
|
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
2021-02-19 16:12:14 +00:00
|
|
|
return sprintf("[GIN/DEBUG] %s: %s(%s) => %d in %s; %s\n",
|
2021-02-02 18:09:02 +00:00
|
|
|
param.TimeStamp.Format("15:04:05"),
|
|
|
|
param.Method,
|
|
|
|
param.Path,
|
|
|
|
param.StatusCode,
|
|
|
|
param.Latency,
|
|
|
|
func() string {
|
|
|
|
if param.ErrorMessage != "" {
|
|
|
|
return "Error: " + param.ErrorMessage
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}(),
|
|
|
|
)
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
2021-02-19 16:12:14 +00:00
|
|
|
return sprintf("[GIN] %s(%s) => %d\n",
|
2021-02-02 18:09:02 +00:00
|
|
|
param.Method,
|
|
|
|
param.Path,
|
|
|
|
param.StatusCode,
|
|
|
|
)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) loadRouter(address string, debug bool) *gin.Engine {
|
|
|
|
if debug {
|
|
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
} else {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
router := gin.New()
|
|
|
|
|
|
|
|
setGinLogger(router, debug)
|
|
|
|
|
|
|
|
router.Use(gin.Recovery())
|
|
|
|
app.loadHTML(router)
|
|
|
|
router.Use(static.Serve("/", app.webFS))
|
|
|
|
router.NoRoute(app.NoRouteHandler)
|
2021-03-23 21:59:04 +00:00
|
|
|
if *PPROF {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Println(lm.RegisterPprof)
|
2021-02-02 18:09:02 +00:00
|
|
|
pprof.Register(router)
|
|
|
|
}
|
|
|
|
SRV = &http.Server{
|
|
|
|
Addr: address,
|
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) loadRoutes(router *gin.Engine) {
|
|
|
|
routePrefixes := []string{app.URLBase}
|
|
|
|
if app.URLBase != "" {
|
|
|
|
routePrefixes = append(routePrefixes, "")
|
|
|
|
}
|
2023-06-16 13:43:37 +00:00
|
|
|
|
|
|
|
userPageEnabled := app.config.Section("user_page").Key("enabled").MustBool(true) && app.config.Section("ui").Key("jellyfin_login").MustBool(true)
|
|
|
|
|
2021-02-02 18:09:02 +00:00
|
|
|
for _, p := range routePrefixes {
|
|
|
|
router.GET(p+"/lang/:page", app.GetLanguages)
|
|
|
|
router.Use(static.Serve(p+"/", app.webFS))
|
|
|
|
router.GET(p+"/", app.AdminPage)
|
2021-03-30 21:41:28 +00:00
|
|
|
|
|
|
|
if app.config.Section("password_resets").Key("link_reset").MustBool(false) {
|
|
|
|
router.GET(p+"/reset", app.ResetPassword)
|
2021-06-07 12:46:46 +00:00
|
|
|
if app.config.Section("password_resets").Key("set_password").MustBool(false) {
|
|
|
|
router.POST(p+"/reset", app.ResetSetPassword)
|
|
|
|
}
|
2021-03-30 21:41:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:09:02 +00:00
|
|
|
router.GET(p+"/accounts", app.AdminPage)
|
|
|
|
router.GET(p+"/settings", app.AdminPage)
|
2023-10-22 11:31:06 +00:00
|
|
|
router.GET(p+"/activity", app.AdminPage)
|
2023-10-22 13:02:22 +00:00
|
|
|
router.GET(p+"/accounts/user/:userID", app.AdminPage)
|
|
|
|
router.GET(p+"/invites/:code", app.AdminPage)
|
2021-02-02 18:09:02 +00:00
|
|
|
router.GET(p+"/lang/:page/:file", app.ServeLang)
|
|
|
|
router.GET(p+"/token/login", app.getTokenLogin)
|
|
|
|
router.GET(p+"/token/refresh", app.getTokenRefresh)
|
2024-08-21 19:35:08 +00:00
|
|
|
router.POST(p+"/user/invite", app.NewUserFromInvite)
|
2021-02-02 18:09:02 +00:00
|
|
|
router.Use(static.Serve(p+"/invite/", app.webFS))
|
|
|
|
router.GET(p+"/invite/:invCode", app.InviteProxy)
|
2022-01-10 01:55:48 +00:00
|
|
|
if app.config.Section("captcha").Key("enabled").MustBool(false) {
|
|
|
|
router.GET(p+"/captcha/gen/:invCode", app.GenCaptcha)
|
|
|
|
router.GET(p+"/captcha/img/:invCode/:captchaID", app.GetCaptcha)
|
|
|
|
router.POST(p+"/captcha/verify/:invCode/:captchaID/:text", app.VerifyCaptcha)
|
|
|
|
}
|
2021-05-07 20:53:29 +00:00
|
|
|
if telegramEnabled {
|
2021-05-07 17:20:35 +00:00
|
|
|
router.GET(p+"/invite/:invCode/telegram/verified/:pin", app.TelegramVerifiedInvite)
|
2021-05-07 00:08:12 +00:00
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
if discordEnabled {
|
|
|
|
router.GET(p+"/invite/:invCode/discord/verified/:pin", app.DiscordVerifiedInvite)
|
2021-05-23 18:50:03 +00:00
|
|
|
if app.config.Section("discord").Key("provide_invite").MustBool(false) {
|
|
|
|
router.GET(p+"/invite/:invCode/discord/invite", app.DiscordServerInvite)
|
|
|
|
}
|
2021-05-21 20:35:25 +00:00
|
|
|
}
|
2021-05-29 16:43:11 +00:00
|
|
|
if matrixEnabled {
|
|
|
|
router.GET(p+"/invite/:invCode/matrix/verified/:userID/:pin", app.MatrixCheckPIN)
|
|
|
|
router.POST(p+"/invite/:invCode/matrix/user", app.MatrixSendPIN)
|
2021-05-30 10:47:41 +00:00
|
|
|
router.POST(p+"/users/matrix", app.MatrixConnect)
|
2021-05-29 16:43:11 +00:00
|
|
|
}
|
2023-06-16 13:43:37 +00:00
|
|
|
if userPageEnabled {
|
|
|
|
router.GET(p+"/my/account", app.MyUserPage)
|
2024-08-27 13:56:24 +00:00
|
|
|
router.GET(p+"/my/account/password/reset", app.MyUserPage)
|
2023-06-16 13:43:37 +00:00
|
|
|
router.GET(p+"/my/token/login", app.getUserTokenLogin)
|
|
|
|
router.GET(p+"/my/token/refresh", app.getUserTokenRefresh)
|
2023-06-18 18:38:09 +00:00
|
|
|
router.GET(p+"/my/confirm/:jwt", app.ConfirmMyAction)
|
2023-06-22 11:04:40 +00:00
|
|
|
router.POST(p+"/my/password/reset/:address", app.ResetMyPassword)
|
2023-06-16 13:43:37 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
}
|
|
|
|
if *SWAGGER {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Print(warning(lm.SwaggerWarning))
|
2021-02-02 18:09:02 +00:00
|
|
|
for _, p := range routePrefixes {
|
|
|
|
router.GET(p+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
|
|
}
|
|
|
|
}
|
2023-06-16 13:43:37 +00:00
|
|
|
|
2021-02-02 18:09:02 +00:00
|
|
|
api := router.Group("/", app.webAuth())
|
2023-06-16 13:43:37 +00:00
|
|
|
|
2021-02-02 18:09:02 +00:00
|
|
|
for _, p := range routePrefixes {
|
2023-10-02 08:45:38 +00:00
|
|
|
var user *gin.RouterGroup
|
|
|
|
if userPageEnabled {
|
|
|
|
user = router.Group(p+"/my", app.userAuth())
|
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
router.POST(p+"/logout", app.Logout)
|
2021-02-19 14:51:36 +00:00
|
|
|
api.DELETE(p+"/users", app.DeleteUsers)
|
2021-02-02 18:09:02 +00:00
|
|
|
api.GET(p+"/users", app.GetUsers)
|
2024-08-21 19:35:08 +00:00
|
|
|
api.POST(p+"/user", app.NewUserFromAdmin)
|
2021-02-28 17:52:24 +00:00
|
|
|
api.POST(p+"/users/extend", app.ExtendExpiry)
|
2023-11-16 11:19:49 +00:00
|
|
|
api.DELETE(p+"/users/:id/expiry", app.RemoveExpiry)
|
2021-04-12 20:28:36 +00:00
|
|
|
api.POST(p+"/users/enable", app.EnableDisableUsers)
|
2021-02-02 18:09:02 +00:00
|
|
|
api.POST(p+"/invites", app.GenerateInvite)
|
|
|
|
api.GET(p+"/invites", app.GetInvites)
|
|
|
|
api.DELETE(p+"/invites", app.DeleteInvite)
|
|
|
|
api.POST(p+"/invites/profile", app.SetProfile)
|
|
|
|
api.GET(p+"/profiles", app.GetProfiles)
|
2024-08-21 19:35:08 +00:00
|
|
|
api.GET(p+"/profiles/names", app.GetProfileNames)
|
2021-02-02 18:09:02 +00:00
|
|
|
api.POST(p+"/profiles/default", app.SetDefaultProfile)
|
|
|
|
api.POST(p+"/profiles", app.CreateProfile)
|
|
|
|
api.DELETE(p+"/profiles", app.DeleteProfile)
|
|
|
|
api.POST(p+"/invites/notify", app.SetNotify)
|
|
|
|
api.POST(p+"/users/emails", app.ModifyEmails)
|
2022-01-08 16:20:31 +00:00
|
|
|
api.POST(p+"/users/labels", app.ModifyLabels)
|
2022-01-09 19:29:17 +00:00
|
|
|
api.POST(p+"/users/accounts-admin", app.SetAccountsAdmin)
|
2021-02-02 18:09:02 +00:00
|
|
|
// api.POST(p + "/setDefaults", app.SetDefaults)
|
|
|
|
api.POST(p+"/users/settings", app.ApplySettings)
|
2021-02-18 14:58:53 +00:00
|
|
|
api.POST(p+"/users/announce", app.Announce)
|
2021-07-10 15:43:27 +00:00
|
|
|
|
|
|
|
api.GET(p+"/users/announce", app.GetAnnounceTemplates)
|
|
|
|
api.POST(p+"/users/announce/template", app.SaveAnnounceTemplate)
|
|
|
|
api.GET(p+"/users/announce/:name", app.GetAnnounceTemplate)
|
|
|
|
api.DELETE(p+"/users/announce/:name", app.DeleteAnnounceTemplate)
|
|
|
|
|
2021-10-13 14:04:22 +00:00
|
|
|
api.POST(p+"/users/password-reset", app.AdminPasswordReset)
|
|
|
|
|
2021-03-07 15:23:44 +00:00
|
|
|
api.GET(p+"/config/update", app.CheckUpdate)
|
|
|
|
api.POST(p+"/config/update", app.ApplyUpdate)
|
2023-06-20 20:43:25 +00:00
|
|
|
api.GET(p+"/config/emails", app.GetCustomContent)
|
|
|
|
api.GET(p+"/config/emails/:id", app.GetCustomMessageTemplate)
|
|
|
|
api.POST(p+"/config/emails/:id", app.SetCustomMessage)
|
|
|
|
api.POST(p+"/config/emails/:id/state/:state", app.SetCustomMessageState)
|
2021-02-02 18:09:02 +00:00
|
|
|
api.GET(p+"/config", app.GetConfig)
|
|
|
|
api.POST(p+"/config", app.ModifyConfig)
|
|
|
|
api.POST(p+"/restart", app.restart)
|
2021-12-30 17:54:27 +00:00
|
|
|
api.GET(p+"/logs", app.GetLog)
|
2023-12-21 16:47:17 +00:00
|
|
|
api.POST(p+"/backups", app.CreateBackup)
|
|
|
|
api.GET(p+"/backups/:fname", app.GetBackup)
|
|
|
|
api.GET(p+"/backups", app.GetBackups)
|
2023-12-21 17:42:07 +00:00
|
|
|
api.POST(p+"/backups/restore/:fname", app.RestoreLocalBackup)
|
2023-12-21 18:12:58 +00:00
|
|
|
api.POST(p+"/backups/restore", app.RestoreBackup)
|
2021-05-30 21:35:34 +00:00
|
|
|
if telegramEnabled || discordEnabled || matrixEnabled {
|
2021-05-07 17:20:35 +00:00
|
|
|
api.GET(p+"/telegram/pin", app.TelegramGetPin)
|
|
|
|
api.GET(p+"/telegram/verified/:pin", app.TelegramVerified)
|
|
|
|
api.POST(p+"/users/telegram", app.TelegramAddUser)
|
2023-02-05 00:23:16 +00:00
|
|
|
api.DELETE(p+"/users/telegram", app.UnlinkTelegram)
|
|
|
|
api.DELETE(p+"/users/discord", app.UnlinkDiscord)
|
|
|
|
api.DELETE(p+"/users/matrix", app.UnlinkMatrix)
|
2023-01-30 13:55:38 +00:00
|
|
|
}
|
|
|
|
if emailEnabled {
|
2021-05-21 20:35:25 +00:00
|
|
|
api.POST(p+"/users/contact", app.SetContactMethods)
|
2021-05-07 17:20:35 +00:00
|
|
|
}
|
2021-05-22 20:42:15 +00:00
|
|
|
if discordEnabled {
|
|
|
|
api.GET(p+"/users/discord/:username", app.DiscordGetUsers)
|
|
|
|
api.POST(p+"/users/discord", app.DiscordConnect)
|
|
|
|
}
|
2024-07-30 15:36:59 +00:00
|
|
|
if app.config.Section("jellyseerr").Key("enabled").MustBool(false) {
|
|
|
|
api.GET(p+"/jellyseerr/users", app.JellyseerrUsers)
|
|
|
|
api.POST(p+"/profiles/jellyseerr/:profile/:id", app.SetJellyseerrProfile)
|
|
|
|
api.DELETE(p+"/profiles/jellyseerr/:profile", app.DeleteJellyseerrProfile)
|
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
|
|
|
api.GET(p+"/ombi/users", app.OmbiUsers)
|
2021-11-13 18:53:53 +00:00
|
|
|
api.POST(p+"/profiles/ombi/:profile", app.SetOmbiProfile)
|
|
|
|
api.DELETE(p+"/profiles/ombi/:profile", app.DeleteOmbiProfile)
|
2021-02-02 18:09:02 +00:00
|
|
|
}
|
2021-05-30 21:35:34 +00:00
|
|
|
api.POST(p+"/matrix/login", app.MatrixLogin)
|
2023-06-28 15:05:24 +00:00
|
|
|
if app.config.Section("user_page").Key("referrals").MustBool(false) {
|
2023-11-10 15:07:29 +00:00
|
|
|
api.POST(p+"/users/referral/:mode/:source/:useExpiry", app.EnableReferralForUsers)
|
2023-09-07 13:00:30 +00:00
|
|
|
api.DELETE(p+"/users/referral", app.DisableReferralForUsers)
|
2023-11-10 15:07:29 +00:00
|
|
|
api.POST(p+"/profiles/referral/:profile/:invite/:useExpiry", app.EnableReferralForProfile)
|
2023-09-06 21:00:44 +00:00
|
|
|
api.DELETE(p+"/profiles/referral/:profile", app.DisableReferralForProfile)
|
2023-06-28 15:05:24 +00:00
|
|
|
}
|
2021-05-30 21:35:34 +00:00
|
|
|
|
2023-10-20 21:16:40 +00:00
|
|
|
api.POST(p+"/activity", app.GetActivities)
|
2023-10-21 12:38:11 +00:00
|
|
|
api.DELETE(p+"/activity/:id", app.DeleteActivity)
|
2023-10-23 10:34:04 +00:00
|
|
|
api.GET(p+"/activity/count", app.GetActivityCount)
|
2023-10-19 21:10:42 +00:00
|
|
|
|
2023-06-16 13:43:37 +00:00
|
|
|
if userPageEnabled {
|
2023-10-02 08:45:38 +00:00
|
|
|
user.GET("/details", app.MyDetails)
|
|
|
|
user.POST("/contact", app.SetMyContactMethods)
|
|
|
|
user.POST("/logout", app.LogoutUser)
|
|
|
|
user.POST("/email", app.ModifyMyEmail)
|
|
|
|
user.GET("/discord/invite", app.MyDiscordServerInvite)
|
|
|
|
user.GET("/pin/:service", app.GetMyPIN)
|
|
|
|
user.GET("/discord/verified/:pin", app.MyDiscordVerifiedInvite)
|
|
|
|
user.GET("/telegram/verified/:pin", app.MyTelegramVerifiedInvite)
|
|
|
|
user.POST("/matrix/user", app.MatrixSendMyPIN)
|
|
|
|
user.GET("/matrix/verified/:userID/:pin", app.MatrixCheckMyPIN)
|
|
|
|
user.DELETE("/discord", app.UnlinkMyDiscord)
|
|
|
|
user.DELETE("/telegram", app.UnlinkMyTelegram)
|
|
|
|
user.DELETE("/matrix", app.UnlinkMyMatrix)
|
|
|
|
user.POST("/password", app.ChangeMyPassword)
|
2023-06-28 15:05:24 +00:00
|
|
|
if app.config.Section("user_page").Key("referrals").MustBool(false) {
|
2023-10-02 08:45:38 +00:00
|
|
|
user.GET("/referral", app.GetMyReferral)
|
2023-06-28 15:05:24 +00:00
|
|
|
}
|
2023-06-16 13:43:37 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) loadSetup(router *gin.Engine) {
|
|
|
|
router.GET("/lang/:page", app.GetLanguages)
|
|
|
|
router.GET("/", app.ServeSetup)
|
|
|
|
router.POST("/jellyfin/test", app.TestJF)
|
|
|
|
router.POST("/config", app.ModifyConfig)
|
|
|
|
}
|