mirror of
https://github.com/gotify/server.git
synced 2024-01-28 15:20:56 +03:00
We should use `(*regexp.Regexp).MatchString` instead of
`(*regexp.Regexp).Match([]byte(...))` when matching string to avoid
unnecessary `[]byte` conversions and reduce allocations.
Example benchmark:
var allowedOrigin = regexp.MustCompile(".*.example.com")
func BenchmarkMatch(b *testing.B) {
for i := 0; i < b.N; i++ {
if match := allowedOrigin.Match([]byte("www.example.com")); !match {
b.Fail()
}
}
}
func BenchmarkMatchString(b *testing.B) {
for i := 0; i < b.N; i++ {
if match := allowedOrigin.MatchString("wwww.example.com"); !match {
b.Fail()
}
}
}
goos: linux
goarch: amd64
pkg: github.com/gotify/server/v2/api/stream
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16 2076819 647.7 ns/op 16 B/op 1 allocs/op
BenchmarkMatchString-16 2536326 442.0 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/gotify/server/v2/api/stream 3.552s
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gotify/server/v2/config"
|
|
"github.com/gotify/server/v2/mode"
|
|
)
|
|
|
|
// CorsConfig generates a config to use in gin cors middleware based on server configuration.
|
|
func CorsConfig(conf *config.Configuration) cors.Config {
|
|
corsConf := cors.Config{
|
|
MaxAge: 12 * time.Hour,
|
|
AllowBrowserExtensions: true,
|
|
}
|
|
if mode.IsDev() {
|
|
corsConf.AllowAllOrigins = true
|
|
corsConf.AllowMethods = []string{"GET", "POST", "DELETE", "OPTIONS", "PUT"}
|
|
corsConf.AllowHeaders = []string{
|
|
"X-Gotify-Key", "Authorization", "Content-Type", "Upgrade", "Origin",
|
|
"Connection", "Accept-Encoding", "Accept-Language", "Host",
|
|
}
|
|
} else {
|
|
compiledOrigins := compileAllowedCORSOrigins(conf.Server.Cors.AllowOrigins)
|
|
corsConf.AllowMethods = conf.Server.Cors.AllowMethods
|
|
corsConf.AllowHeaders = conf.Server.Cors.AllowHeaders
|
|
corsConf.AllowOriginFunc = func(origin string) bool {
|
|
for _, compiledOrigin := range compiledOrigins {
|
|
if compiledOrigin.MatchString(strings.ToLower(origin)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
if allowedOrigin := headerIgnoreCase(conf, "access-control-allow-origin"); allowedOrigin != "" && len(compiledOrigins) == 0 {
|
|
corsConf.AllowOrigins = append(corsConf.AllowOrigins, allowedOrigin)
|
|
}
|
|
}
|
|
|
|
return corsConf
|
|
}
|
|
|
|
func headerIgnoreCase(conf *config.Configuration, search string) (value string) {
|
|
for key, value := range conf.Server.ResponseHeaders {
|
|
if strings.ToLower(key) == search {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func compileAllowedCORSOrigins(allowedOrigins []string) []*regexp.Regexp {
|
|
var compiledAllowedOrigins []*regexp.Regexp
|
|
for _, origin := range allowedOrigins {
|
|
compiledAllowedOrigins = append(compiledAllowedOrigins, regexp.MustCompile(origin))
|
|
}
|
|
|
|
return compiledAllowedOrigins
|
|
}
|