File size: 6,932 Bytes
7107f0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
package server
import (
"math/rand"
"time"
"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/message"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/alist-org/alist/v3/server/handles"
"github.com/alist-org/alist/v3/server/middlewares"
"github.com/alist-org/alist/v3/server/static"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
// 生成指定长度的随机字符串
func generateRandomString(length int) string {
rand.Seed(time.Now().UnixNano())
// 定义包含数字和字母的字符集
charSet := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var result string
for i := 0; i < length; i++ {
randomIndex := rand.Intn(len(charSet))
result += string(charSet[randomIndex])
}
return result
}
func Init(e *gin.Engine) {
randomStr := generateRandomString(10)
if !utils.SliceContains([]string{"", "/"}, conf.URL.Path) {
e.GET("/", func(c *gin.Context) {
c.Redirect(302, conf.URL.Path)
})
}
Cors(e)
g := e.Group(conf.URL.Path)
if conf.Conf.Scheme.HttpPort != -1 && conf.Conf.Scheme.HttpsPort != -1 && conf.Conf.Scheme.ForceHttps {
e.Use(middlewares.ForceHttps)
}
g.Any("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
g.GET("/favicon.ico", handles.Favicon)
g.GET("/robots.txt", handles.Robots)
g.GET("/i/:link_name", handles.Plist)
common.SecretKey = []byte(conf.Conf.JwtSecret)
g.Use(middlewares.StoragesLoaded)
if conf.Conf.MaxConnections > 0 {
g.Use(middlewares.MaxAllowed(conf.Conf.MaxConnections))
}
WebDav(g.Group("/dav"))
S3(g.Group("/s3"))
g.GET("/d/*path", middlewares.Down, handles.Down)
g.GET("/p/*path", middlewares.Down, handles.Proxy)
g.HEAD("/d/*path", middlewares.Down, handles.Down)
g.HEAD("/p/*path", middlewares.Down, handles.Proxy)
api := g.Group("/api")
auth := api.Group("", middlewares.Auth)
webauthn := api.Group("/authn", middlewares.Authn)
api.POST("/auth/login", handles.Login)
api.POST("/auth/login/hash", handles.LoginHash)
api.POST("/auth/login/ldap", handles.LoginLdap)
auth.GET("/me", handles.CurrentUser)
auth.POST("/me/update", handles.UpdateCurrent)
auth.POST("/auth/2fa/generate", handles.Generate2FA)
auth.POST("/auth/2fa/verify", handles.Verify2FA)
// auth
api.GET("/auth/sso", handles.SSOLoginRedirect)
api.GET("/auth/sso_callback", handles.SSOLoginCallback)
api.GET("/auth/get_sso_id", handles.SSOLoginCallback)
api.GET("/auth/sso_get_token", handles.SSOLoginCallback)
//启动时生成随机字符串,用来验证唯一性
api.GET("/instanceid", func(c *gin.Context) {
c.String(200, randomStr)
})
//webauthn
webauthn.GET("/webauthn_begin_registration", handles.BeginAuthnRegistration)
webauthn.POST("/webauthn_finish_registration", handles.FinishAuthnRegistration)
webauthn.GET("/webauthn_begin_login", handles.BeginAuthnLogin)
webauthn.POST("/webauthn_finish_login", handles.FinishAuthnLogin)
webauthn.POST("/delete_authn", handles.DeleteAuthnLogin)
webauthn.GET("/getcredentials", handles.GetAuthnCredentials)
// no need auth
public := api.Group("/public")
public.Any("/settings", handles.PublicSettings)
public.Any("/offline_download_tools", handles.OfflineDownloadTools)
_fs(auth.Group("/fs"))
admin(auth.Group("/admin", middlewares.AuthAdmin))
if flags.Debug || flags.Dev {
debug(g.Group("/debug"))
}
static.Static(g, func(handlers ...gin.HandlerFunc) {
e.NoRoute(handlers...)
})
}
func admin(g *gin.RouterGroup) {
meta := g.Group("/meta")
meta.GET("/list", handles.ListMetas)
meta.GET("/get", handles.GetMeta)
meta.POST("/create", handles.CreateMeta)
meta.POST("/update", handles.UpdateMeta)
meta.POST("/delete", handles.DeleteMeta)
user := g.Group("/user")
user.GET("/list", handles.ListUsers)
user.GET("/get", handles.GetUser)
user.POST("/create", handles.CreateUser)
user.POST("/update", handles.UpdateUser)
user.POST("/cancel_2fa", handles.Cancel2FAById)
user.POST("/delete", handles.DeleteUser)
user.POST("/del_cache", handles.DelUserCache)
storage := g.Group("/storage")
storage.GET("/list", handles.ListStorages)
storage.GET("/get", handles.GetStorage)
storage.POST("/create", handles.CreateStorage)
storage.POST("/copy", handles.CopyStorage)
storage.POST("/update", handles.UpdateStorage)
storage.POST("/delete", handles.DeleteStorage)
storage.POST("/enable", handles.EnableStorage)
storage.POST("/disable", handles.DisableStorage)
storage.POST("/load_all", handles.LoadAllStorages)
driver := g.Group("/driver")
driver.GET("/list", handles.ListDriverInfo)
driver.GET("/names", handles.ListDriverNames)
driver.GET("/info", handles.GetDriverInfo)
setting := g.Group("/setting")
setting.GET("/get", handles.GetSetting)
setting.GET("/list", handles.ListSettings)
setting.POST("/save", handles.SaveSettings)
setting.POST("/delete", handles.DeleteSetting)
setting.POST("/reset_token", handles.ResetToken)
setting.POST("/set_aria2", handles.SetAria2)
setting.POST("/set_qbit", handles.SetQbittorrent)
task := g.Group("/task")
handles.SetupTaskRoute(task)
ms := g.Group("/message")
ms.POST("/get", message.HttpInstance.GetHandle)
ms.POST("/send", message.HttpInstance.SendHandle)
index := g.Group("/index")
index.POST("/build", middlewares.SearchIndex, handles.BuildIndex)
index.POST("/update", middlewares.SearchIndex, handles.UpdateIndex)
index.POST("/stop", middlewares.SearchIndex, handles.StopIndex)
index.POST("/clear", middlewares.SearchIndex, handles.ClearIndex)
index.GET("/progress", middlewares.SearchIndex, handles.GetProgress)
}
func _fs(g *gin.RouterGroup) {
g.Any("/list", handles.FsList)
g.Any("/search", middlewares.SearchIndex, handles.Search)
g.Any("/get", handles.FsGet)
g.Any("/other", handles.FsOther)
g.Any("/dirs", handles.FsDirs)
g.POST("/mkdir", handles.FsMkdir)
g.POST("/rename", handles.FsRename)
g.POST("/batch_rename", handles.FsBatchRename)
g.POST("/regex_rename", handles.FsRegexRename)
g.POST("/move", handles.FsMove)
g.POST("/recursive_move", handles.FsRecursiveMove)
g.POST("/copy", handles.FsCopy)
g.POST("/copy_item", handles.FsCopyItem)
g.POST("/remove", handles.FsRemove)
g.POST("/remove_empty_directory", handles.FsRemoveEmptyDirectory)
g.PUT("/put", middlewares.FsUp, handles.FsStream)
g.PUT("/form", middlewares.FsUp, handles.FsForm)
g.POST("/link", middlewares.AuthAdmin, handles.Link)
//g.POST("/add_aria2", handles.AddOfflineDownload)
//g.POST("/add_qbit", handles.AddQbittorrent)
g.POST("/add_offline_download", handles.AddOfflineDownload)
}
func Cors(r *gin.Engine) {
config := cors.DefaultConfig()
//config.AllowAllOrigins = true
config.AllowOrigins = conf.Conf.Cors.AllowOrigins
config.AllowHeaders = conf.Conf.Cors.AllowHeaders
config.AllowMethods = conf.Conf.Cors.AllowMethods
r.Use(cors.New(config))
}
func InitS3(e *gin.Engine) {
Cors(e)
S3Server(e.Group("/"))
}
|