| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package web | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 18:45:15 +00:00
										 |  |  | 	"codeberg.org/gruf/go-cache/v3" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/middleware" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/processing" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/router" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/uris" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	confirmEmailPath   = "/" + uris.ConfirmEmailPath | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	profileGroupPath   = "/@:" + usernameKey | 
					
						
							|  |  |  | 	statusPath         = "/statuses/:" + statusIDKey // leave out the '/@:username' prefix as this will be served within the profile group | 
					
						
							|  |  |  | 	customCSSPath      = profileGroupPath + "/custom.css" | 
					
						
							|  |  |  | 	rssFeedPath        = profileGroupPath + "/feed.rss" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	assetsPathPrefix   = "/assets" | 
					
						
							|  |  |  | 	distPathPrefix     = assetsPathPrefix + "/dist" | 
					
						
							|  |  |  | 	settingsPathPrefix = "/settings" | 
					
						
							|  |  |  | 	settingsPanelGlob  = settingsPathPrefix + "/*panel" | 
					
						
							|  |  |  | 	userPanelPath      = settingsPathPrefix + "/user" | 
					
						
							|  |  |  | 	adminPanelPath     = settingsPathPrefix + "/admin" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tokenParam  = "token" | 
					
						
							|  |  |  | 	usernameKey = "username" | 
					
						
							|  |  |  | 	statusIDKey = "status" | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cacheControlHeader    = "Cache-Control"     // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control | 
					
						
							|  |  |  | 	cacheControlNoCache   = "no-cache"          // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives | 
					
						
							|  |  |  | 	ifModifiedSinceHeader = "If-Modified-Since" // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since | 
					
						
							|  |  |  | 	ifNoneMatchHeader     = "If-None-Match"     // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match | 
					
						
							|  |  |  | 	eTagHeader            = "ETag"              // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag | 
					
						
							|  |  |  | 	lastModifiedHeader    = "Last-Modified"     // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Module struct { | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	processor    *processing.Processor | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	eTagCache    cache.Cache[string, eTagCacheEntry] | 
					
						
							|  |  |  | 	isURIBlocked func(context.Context, *url.URL) (bool, db.Error) | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func New(db db.DB, processor *processing.Processor) *Module { | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	return &Module{ | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 		processor:    processor, | 
					
						
							|  |  |  | 		eTagCache:    newETagCache(), | 
					
						
							|  |  |  | 		isURIBlocked: db.IsURIBlocked, | 
					
						
							| 
									
										
										
										
											2022-09-04 14:41:42 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-03 10:50:59 +00:00
										 |  |  | func (m *Module) Route(r router.Router, mi ...gin.HandlerFunc) { | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	// Group all static files from assets dir at /assets, | 
					
						
							|  |  |  | 	// so that they can use the same cache control middleware. | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	webAssetsAbsFilePath, err := filepath.Abs(config.GetWebAssetBaseDir()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 		log.Panicf(nil, "error getting absolute path of assets dir: %s", err) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	fs := fileSystem{http.Dir(webAssetsAbsFilePath)} | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	assetsGroup := r.AttachGroup(assetsPathPrefix) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	assetsGroup.Use(m.assetsCacheControlMiddleware(fs)) | 
					
						
							| 
									
										
										
										
											2023-01-03 10:50:59 +00:00
										 |  |  | 	assetsGroup.Use(mi...) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	assetsGroup.StaticFS("/", fs) | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	// handlers that serve profiles and statuses should use the SignatureCheck | 
					
						
							|  |  |  | 	// middleware, so that requests with content-type application/activity+json | 
					
						
							|  |  |  | 	// can still be served | 
					
						
							|  |  |  | 	profileGroup := r.AttachGroup(profileGroupPath) | 
					
						
							|  |  |  | 	profileGroup.Use(mi...) | 
					
						
							|  |  |  | 	profileGroup.Use(middleware.SignatureCheck(m.isURIBlocked), middleware.CacheControl("no-store")) | 
					
						
							|  |  |  | 	profileGroup.Handle(http.MethodGet, "", m.profileGETHandler) // use empty path here since it's the base of the group | 
					
						
							|  |  |  | 	profileGroup.Handle(http.MethodGet, statusPath, m.threadGETHandler) | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	// Attach individual web handlers which require no specific middlewares | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	r.AttachHandler(http.MethodGet, "/", m.baseHandler) // front-page | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, settingsPathPrefix, m.SettingsPanelHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, settingsPanelGlob, m.SettingsPanelHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, customCSSPath, m.customCSSGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, rssFeedPath, m.rssFeedGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, confirmEmailPath, m.confirmEmailGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, robotsPath, m.robotsGETHandler) | 
					
						
							| 
									
										
										
										
											2023-02-20 16:29:29 +01:00
										 |  |  | 	r.AttachHandler(http.MethodGet, aboutPath, m.aboutGETHandler) | 
					
						
							| 
									
										
										
										
											2023-01-25 18:06:41 +01:00
										 |  |  | 	r.AttachHandler(http.MethodGet, domainBlockListPath, m.domainBlockListGETHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 14:57:09 +01:00
										 |  |  | 	// Attach redirects from old endpoints to current ones for backwards compatibility | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	r.AttachHandler(http.MethodGet, "/auth/edit", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, userPanelPath) }) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, "/user", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, userPanelPath) }) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, "/admin", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, adminPanelPath) }) | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | } |