- /group ticket del: only the chat creator may remove a group registration - /node: always dial user-supplied node hosts through the netguard client; drop the https://127.0.0.1 prefix exemption - /app/@{username}: accept only username characters, render the greeting as text, add basic security headers Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gorilla/mux"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Server struct {
|
|
httpServer *http.Server
|
|
router *mux.Router
|
|
}
|
|
|
|
const (
|
|
StatusError = "ERROR"
|
|
StatusOk = "OK"
|
|
)
|
|
|
|
func NewServer(address string) *Server {
|
|
srv := &http.Server{
|
|
Addr: address,
|
|
// Good practice: enforce timeouts for servers you create!
|
|
WriteTimeout: 90 * time.Second,
|
|
ReadTimeout: 90 * time.Second,
|
|
}
|
|
apiServer := &Server{
|
|
httpServer: srv,
|
|
}
|
|
apiServer.router = mux.NewRouter()
|
|
apiServer.router.Use(securityHeaders)
|
|
apiServer.httpServer.Handler = apiServer.router
|
|
go apiServer.httpServer.ListenAndServe()
|
|
log.Infof("[api] Server started at %s", address)
|
|
return apiServer
|
|
}
|
|
|
|
func (w *Server) ListenAndServe() {
|
|
go w.httpServer.ListenAndServe()
|
|
}
|
|
|
|
// Use adds a middleware that runs for every route of the server.
|
|
func (w *Server) Use(middleware mux.MiddlewareFunc) {
|
|
w.router.Use(middleware)
|
|
}
|
|
func (w *Server) PathPrefix(path string, handler http.Handler) {
|
|
w.router.PathPrefix(path).Handler(handler)
|
|
}
|
|
func (w *Server) AppendAuthorizedRoute(path string, authType AuthType, accessType AccessKeyType, database *gorm.DB, handler func(http.ResponseWriter, *http.Request), methods ...string) {
|
|
r := w.router.HandleFunc(path, LoggingMiddleware("API", AuthorizationMiddleware(database, authType, accessType, handler)))
|
|
if len(methods) > 0 {
|
|
r.Methods(methods...)
|
|
}
|
|
}
|
|
func (w *Server) AppendRoute(path string, handler func(http.ResponseWriter, *http.Request), methods ...string) {
|
|
r := w.router.HandleFunc(path, LoggingMiddleware("API", handler))
|
|
if len(methods) > 0 {
|
|
r.Methods(methods...)
|
|
}
|
|
}
|
|
|
|
func NotFoundHandler(writer http.ResponseWriter, err error) {
|
|
log.Errorln(err)
|
|
// return 404 on any error
|
|
http.Error(writer, "404 page not found", http.StatusNotFound)
|
|
}
|
|
|
|
func WriteResponse(writer http.ResponseWriter, response interface{}) error {
|
|
jsonResponse, err := json.Marshal(response)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = writer.Write(jsonResponse)
|
|
return err
|
|
}
|
|
|
|
// securityHeaders sets the headers that need no change to the pages. A
|
|
// stricter Content-Security-Policy would need the inline scripts of the
|
|
// userpage templates to move out first.
|
|
func securityHeaders(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
h := w.Header()
|
|
h.Set("X-Content-Type-Options", "nosniff")
|
|
h.Set("Referrer-Policy", "no-referrer")
|
|
h.Set("Content-Security-Policy", "object-src 'none'; base-uri 'none'")
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|