49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
"github.com/cloudwego/hertz/pkg/app/server"
|
|
"github.com/cloudwego/hertz/pkg/common/hlog"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
OldDomain = "liteyuki.icu"
|
|
NewDomain = "liteyuki.org"
|
|
)
|
|
|
|
func main() {
|
|
h := server.Default()
|
|
h.Use(func(ctx context.Context, c *app.RequestContext) {
|
|
host := string(c.Host())
|
|
if strings.HasSuffix(host, OldDomain) {
|
|
c.Header("X-Redirected-By", "liteyuki.org")
|
|
newHost := strings.TrimSuffix(host, OldDomain) + NewDomain
|
|
scheme := "https"
|
|
if proto := c.Request.Header.Get("X-Forwarded-Proto"); len(proto) > 0 {
|
|
scheme = string(proto)
|
|
} else if forwarded := c.Request.Header.Get("Forwarded"); len(forwarded) > 0 {
|
|
// Forwarded: proto=https;host=xxx
|
|
for _, part := range strings.Split(string(forwarded), ";") {
|
|
if strings.HasPrefix(part, "proto=") {
|
|
scheme = strings.TrimPrefix(part, "proto=")
|
|
break
|
|
}
|
|
}
|
|
}
|
|
oldURL := scheme + "://" + host + string(c.Request.RequestURI())
|
|
newURL := scheme + "://" + newHost + string(c.Request.RequestURI())
|
|
hlog.Infof("Redirecting from %s to %s", oldURL, newURL)
|
|
c.Redirect(301, []byte(newURL))
|
|
c.Abort()
|
|
return
|
|
}
|
|
})
|
|
|
|
h.GET("/*any", func(ctx context.Context, c *app.RequestContext) {
|
|
c.JSON(200, map[string]interface{}{"message": "pong"})
|
|
})
|
|
h.Spin()
|
|
}
|