2017-06-27 11:00:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-10 08:17:00 +02:00
|
|
|
"context"
|
2017-07-01 23:25:08 +02:00
|
|
|
"crypto/subtle"
|
2017-06-27 11:00:33 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-07-03 06:08:47 +02:00
|
|
|
"time"
|
2018-03-15 18:58:11 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-05-08 13:42:48 +02:00
|
|
|
"golang.org/x/net/netutil"
|
2021-04-26 13:52:50 +02:00
|
|
|
|
2021-09-30 16:23:30 +02:00
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/errorreport"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/ierrors"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/reuseport"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/router"
|
2017-06-27 11:00:33 +02:00
|
|
|
)
|
|
|
|
|
2018-10-05 17:17:36 +02:00
|
|
|
var (
|
2018-10-25 13:31:31 +02:00
|
|
|
imgproxyIsRunningMsg = []byte("imgproxy is running")
|
2018-10-05 22:29:55 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
errInvalidSecret = ierrors.New(403, "Invalid secret", "Forbidden")
|
2019-01-17 10:51:19 +02:00
|
|
|
)
|
2018-10-25 13:31:31 +02:00
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func buildRouter() *router.Router {
|
|
|
|
r := router.New(config.PathPrefix)
|
2018-09-10 08:17:00 +02:00
|
|
|
|
2019-08-20 15:26:17 +02:00
|
|
|
r.GET("/", handleLanding, true)
|
|
|
|
r.GET("/health", handleHealth, true)
|
|
|
|
r.GET("/favicon.ico", handleFavicon, true)
|
2022-01-13 18:36:06 +02:00
|
|
|
r.GET("/", withCORS(withPanicHandler(withSecret(handleProcessing))), false)
|
2020-01-30 12:07:43 +02:00
|
|
|
r.HEAD("/", withCORS(handleHead), false)
|
|
|
|
r.OPTIONS("/", withCORS(handleHead), false)
|
2019-06-03 17:16:49 +02:00
|
|
|
|
|
|
|
return r
|
2018-10-25 13:31:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:44:59 +02:00
|
|
|
func startServer(cancel context.CancelFunc) (*http.Server, error) {
|
2021-04-26 13:52:50 +02:00
|
|
|
l, err := reuseport.Listen(config.Network, config.Bind)
|
2019-05-08 13:42:48 +02:00
|
|
|
if err != nil {
|
2020-02-27 17:44:59 +02:00
|
|
|
return nil, fmt.Errorf("Can't start server: %s", err)
|
2019-05-08 13:42:48 +02:00
|
|
|
}
|
2021-04-26 13:52:50 +02:00
|
|
|
l = netutil.LimitListener(l, config.MaxClients)
|
2019-06-03 17:16:49 +02:00
|
|
|
|
2019-05-08 13:42:48 +02:00
|
|
|
s := &http.Server{
|
2019-06-03 17:16:49 +02:00
|
|
|
Handler: buildRouter(),
|
2021-04-26 13:52:50 +02:00
|
|
|
ReadTimeout: time.Duration(config.ReadTimeout) * time.Second,
|
2019-05-08 13:42:48 +02:00
|
|
|
MaxHeaderBytes: 1 << 20,
|
2018-09-10 08:17:00 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
if config.KeepAliveTimeout > 0 {
|
|
|
|
s.IdleTimeout = time.Duration(config.KeepAliveTimeout) * time.Second
|
2019-06-21 12:32:37 +02:00
|
|
|
} else {
|
|
|
|
s.SetKeepAlivesEnabled(false)
|
|
|
|
}
|
|
|
|
|
2018-09-10 08:17:00 +02:00
|
|
|
go func() {
|
2021-04-26 13:52:50 +02:00
|
|
|
log.Infof("Starting server at %s", config.Bind)
|
2019-06-03 17:16:49 +02:00
|
|
|
if err := s.Serve(l); err != nil && err != http.ErrServerClosed {
|
2021-04-26 13:52:50 +02:00
|
|
|
log.Error(err)
|
2018-10-05 17:17:36 +02:00
|
|
|
}
|
2020-02-27 17:44:59 +02:00
|
|
|
cancel()
|
2018-09-10 08:17:00 +02:00
|
|
|
}()
|
|
|
|
|
2020-02-27 17:44:59 +02:00
|
|
|
return s, nil
|
2018-09-10 08:17:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 13:42:48 +02:00
|
|
|
func shutdownServer(s *http.Server) {
|
2021-04-26 13:52:50 +02:00
|
|
|
log.Info("Shutting down the server...")
|
2019-05-08 13:42:48 +02:00
|
|
|
|
|
|
|
ctx, close := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
s.Shutdown(ctx)
|
2018-09-10 08:17:00 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func withCORS(h router.RouteHandler) router.RouteHandler {
|
2019-06-03 17:16:49 +02:00
|
|
|
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
|
2021-04-26 13:52:50 +02:00
|
|
|
if len(config.AllowOrigin) > 0 {
|
|
|
|
rw.Header().Set("Access-Control-Allow-Origin", config.AllowOrigin)
|
2019-06-03 17:16:49 +02:00
|
|
|
rw.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
|
|
|
|
}
|
2019-06-03 13:06:13 +02:00
|
|
|
|
2019-06-03 17:16:49 +02:00
|
|
|
h(reqID, rw, r)
|
|
|
|
}
|
2018-11-20 14:53:44 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func withSecret(h router.RouteHandler) router.RouteHandler {
|
|
|
|
if len(config.Secret) == 0 {
|
2019-06-03 17:16:49 +02:00
|
|
|
return h
|
2019-05-06 10:42:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
authHeader := []byte(fmt.Sprintf("Bearer %s", config.Secret))
|
2019-05-06 10:42:30 +02:00
|
|
|
|
2019-06-03 17:16:49 +02:00
|
|
|
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), authHeader) == 1 {
|
|
|
|
h(reqID, rw, r)
|
|
|
|
} else {
|
2019-06-03 19:02:46 +02:00
|
|
|
panic(errInvalidSecret)
|
2019-06-03 17:16:49 +02:00
|
|
|
}
|
2017-07-03 11:36:37 +02:00
|
|
|
}
|
2017-07-04 16:05:53 +02:00
|
|
|
}
|
|
|
|
|
2022-01-13 18:36:06 +02:00
|
|
|
func withPanicHandler(h router.RouteHandler) router.RouteHandler {
|
|
|
|
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if rerr := recover(); rerr != nil {
|
|
|
|
err, ok := rerr.(error)
|
|
|
|
if !ok {
|
|
|
|
panic(rerr)
|
|
|
|
}
|
2019-06-03 19:02:46 +02:00
|
|
|
|
2022-01-13 18:36:06 +02:00
|
|
|
ierr := ierrors.Wrap(err, 3)
|
2019-08-15 15:15:37 +02:00
|
|
|
|
2022-01-13 18:36:06 +02:00
|
|
|
if ierr.Unexpected {
|
|
|
|
errorreport.Report(err, r)
|
|
|
|
}
|
2019-06-03 19:02:46 +02:00
|
|
|
|
2022-01-13 18:36:06 +02:00
|
|
|
router.LogResponse(reqID, r, ierr.StatusCode, ierr)
|
2019-06-03 19:02:46 +02:00
|
|
|
|
2022-01-13 18:36:06 +02:00
|
|
|
rw.WriteHeader(ierr.StatusCode)
|
|
|
|
|
|
|
|
if config.DevelopmentErrorsMode {
|
|
|
|
rw.Write([]byte(ierr.Message))
|
|
|
|
} else {
|
|
|
|
rw.Write([]byte(ierr.PublicMessage))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
h(reqID, rw, r)
|
2019-06-03 17:16:49 +02:00
|
|
|
}
|
2017-07-04 16:05:53 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 17:16:49 +02:00
|
|
|
func handleHealth(reqID string, rw http.ResponseWriter, r *http.Request) {
|
2021-04-26 13:52:50 +02:00
|
|
|
router.LogResponse(reqID, r, 200, nil)
|
2019-06-03 17:16:49 +02:00
|
|
|
rw.WriteHeader(200)
|
|
|
|
rw.Write(imgproxyIsRunningMsg)
|
2018-10-25 13:31:31 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:07:43 +02:00
|
|
|
func handleHead(reqID string, rw http.ResponseWriter, r *http.Request) {
|
2021-04-26 13:52:50 +02:00
|
|
|
router.LogResponse(reqID, r, 200, nil)
|
2019-06-03 17:16:49 +02:00
|
|
|
rw.WriteHeader(200)
|
2018-10-25 13:31:31 +02:00
|
|
|
}
|
2019-08-20 15:26:17 +02:00
|
|
|
|
|
|
|
func handleFavicon(reqID string, rw http.ResponseWriter, r *http.Request) {
|
2021-04-26 13:52:50 +02:00
|
|
|
router.LogResponse(reqID, r, 200, nil)
|
2019-08-20 15:26:17 +02:00
|
|
|
// TODO: Add a real favicon maybe?
|
|
|
|
rw.WriteHeader(200)
|
|
|
|
}
|