File size: 465 Bytes
7107f0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package middlewares

import (
	"fmt"
	"strings"

	"github.com/alist-org/alist/v3/internal/conf"
	"github.com/gin-gonic/gin"
)

func ForceHttps(c *gin.Context) {
	if c.Request.TLS == nil {
		host := c.Request.Host
		// change port to https port
		host = strings.Replace(host, fmt.Sprintf(":%d", conf.Conf.Scheme.HttpPort), fmt.Sprintf(":%d", conf.Conf.Scheme.HttpsPort), 1)
		c.Redirect(302, "https://"+host+c.Request.RequestURI)
		c.Abort()
		return
	}
	c.Next()
}