重构项目
This commit is contained in:
187
pkg/server.go
187
pkg/server.go
@@ -3,7 +3,6 @@ package pkg
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
@@ -12,7 +11,6 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -89,7 +87,6 @@ func DefaultOptions(domain string) ServerOptions {
|
||||
type Server struct {
|
||||
options *ServerOptions
|
||||
meta *core.PageDomain
|
||||
reader *core.CacheBackendBlobReader
|
||||
backend core.Backend
|
||||
fs http.Handler
|
||||
}
|
||||
@@ -97,10 +94,11 @@ type Server struct {
|
||||
var staticPrefix = "/.well-known/page-server/"
|
||||
|
||||
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
||||
backend = core.NewCacheBackend(backend, options.CacheMeta, options.CacheMetaTTL)
|
||||
backend = core.NewCacheBackend(backend, options.CacheMeta, options.CacheMetaTTL,
|
||||
options.CacheBlob, options.CacheBlobLimit,
|
||||
)
|
||||
svcMeta := core.NewServerMeta(options.HTTPClient, backend, options.CacheMeta, options.Domain, options.CacheMetaTTL)
|
||||
pageMeta := core.NewPageDomain(svcMeta, options.Alias, options.Domain, options.DefaultBranch)
|
||||
reader := core.NewCacheBackendBlobReader(options.HTTPClient, backend, options.CacheBlob, options.CacheBlobLimit)
|
||||
var fs http.Handler
|
||||
if options.StaticDir != "" {
|
||||
fs = http.StripPrefix(staticPrefix, http.FileServer(http.Dir(options.StaticDir)))
|
||||
@@ -109,7 +107,6 @@ func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
||||
backend: backend,
|
||||
options: &options,
|
||||
meta: pageMeta,
|
||||
reader: reader,
|
||||
fs: fs,
|
||||
}
|
||||
}
|
||||
@@ -139,139 +136,119 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error {
|
||||
ctx := request.Context()
|
||||
domainHost := portExp.ReplaceAllString(strings.ToLower(request.Host), "")
|
||||
meta, err := s.meta.ParseDomainMeta(
|
||||
ctx,
|
||||
domainHost,
|
||||
request.URL.Path,
|
||||
request.URL.Query().Get("branch"))
|
||||
|
||||
meta, err := s.meta.ParseDomainMeta(ctx, domainHost, request.URL.Path, request.URL.Query().Get("branch"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
zap.L().Debug("new request", zap.Any("request path", meta.Path))
|
||||
if len(meta.Alias) > 0 && !slices.Contains(meta.Alias, domainHost) {
|
||||
// 重定向到配置的地址
|
||||
zap.L().Debug("redirect", zap.Any("src", request.Host), zap.Any("dst", meta.Alias[0]))
|
||||
http.Redirect(writer, request, fmt.Sprintf("https://%s/%s", meta.Alias[0], meta.Path), http.StatusFound)
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.options.EnableProxy {
|
||||
for prefix, backend := range meta.Proxy {
|
||||
proxyPath := "/" + meta.Path
|
||||
if strings.HasPrefix(proxyPath, prefix) {
|
||||
targetPath := strings.TrimPrefix(proxyPath, prefix)
|
||||
if !strings.HasPrefix(targetPath, "/") {
|
||||
targetPath = "/" + targetPath
|
||||
}
|
||||
u, _ := url.Parse(backend)
|
||||
request.URL.Path = targetPath
|
||||
request.RequestURI = request.URL.RequestURI()
|
||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||
proxy.Transport = s.options.HTTPClient.Transport
|
||||
|
||||
if host, _, err := net.SplitHostPort(request.RemoteAddr); err == nil {
|
||||
request.Header.Set("X-Real-IP", host)
|
||||
}
|
||||
request.Header.Set("X-Page-IP", utils.GetRemoteIP(request))
|
||||
request.Header.Set("X-Page-Refer", fmt.Sprintf("%s/%s/%s", meta.Owner, meta.Repo, meta.Path))
|
||||
request.Header.Set("X-Page-Host", request.Host)
|
||||
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
|
||||
zap.Any("path", proxyPath), zap.Any("target", fmt.Sprintf("%s%s", u, targetPath)))
|
||||
// todo(security): 处理 websocket
|
||||
proxy.ServeHTTP(writer, request)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
// 处理反向代理
|
||||
if s.options.EnableProxy && s.Proxy(writer, request, meta) {
|
||||
return nil
|
||||
}
|
||||
// 在非反向代理时处理目录访问
|
||||
if strings.HasSuffix(meta.Path, "/") || meta.Path == "" {
|
||||
meta.Path += "index.html"
|
||||
}
|
||||
|
||||
// 如果不是反向代理路由则跳过任何配置
|
||||
if request.Method != http.MethodGet {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
var result io.ReadCloser
|
||||
if meta.IgnorePath(meta.Path) {
|
||||
zap.L().Debug("ignore path", zap.Any("request", request.RequestURI), zap.Any("meta.path", meta.Path))
|
||||
err = os.ErrNotExist
|
||||
}
|
||||
type resp struct {
|
||||
IsError bool
|
||||
Path string
|
||||
}
|
||||
|
||||
callPath := []resp{{false, meta.Path}}
|
||||
if meta.VRoute {
|
||||
callPath = append(callPath, resp{false, "index.html"})
|
||||
} else {
|
||||
result, err = s.reader.Open(ctx, meta.Owner, meta.Repo, meta.CommitID, meta.Path)
|
||||
callPath = append(callPath, resp{false, meta.Path + "/index.html"})
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
if meta.VRoute {
|
||||
// 回退 abc => index.html
|
||||
result, err = s.reader.Open(ctx, meta.Owner, meta.Repo, meta.CommitID, "index.html")
|
||||
if err == nil {
|
||||
meta.Path = "index.html"
|
||||
}
|
||||
} else {
|
||||
// 回退 abc => abc/ => abc/index.html
|
||||
result, err = s.reader.Open(ctx, meta.Owner, meta.Repo, meta.CommitID, meta.Path+"/index.html")
|
||||
if err == nil {
|
||||
meta.Path = strings.Trim(meta.Path+"/index.html", "/")
|
||||
}
|
||||
callPath = append(callPath, resp{true, "404.html"})
|
||||
|
||||
var callResp *http.Response
|
||||
callErr := os.ErrNotExist
|
||||
var callRespMeta resp
|
||||
for _, r := range callPath {
|
||||
callResp, callErr = meta.NativeOpen(request.Context(), r.Path, nil)
|
||||
if callErr != nil {
|
||||
if callResp != nil {
|
||||
_ = callResp.Body.Close()
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
if !errors.Is(callErr, os.ErrNotExist) {
|
||||
zap.L().Debug("error", zap.Any("error", callErr))
|
||||
}
|
||||
callRespMeta = r
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
// 处理请求错误
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
result, err = s.reader.Open(ctx, meta.Owner, meta.Repo, meta.CommitID, "404.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
if render := meta.TryRender(meta.Path, "/404.html"); render != nil && s.options.EnableRender {
|
||||
defer result.Close()
|
||||
return render.Render(writer, request, result)
|
||||
}
|
||||
_, _ = io.Copy(writer, result)
|
||||
_ = result.Close()
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
|
||||
if callResp == nil {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
if callErr != nil {
|
||||
// 回退失败
|
||||
return callErr
|
||||
}
|
||||
fileName := filepath.Base(meta.Path)
|
||||
render := meta.TryRender(meta.Path)
|
||||
if !s.options.EnableRender {
|
||||
render = nil
|
||||
}
|
||||
defer result.Close()
|
||||
if reader, ok := result.(*cache.Content); ok {
|
||||
writer.Header().Add("X-Cache", "HIT")
|
||||
writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
|
||||
writer.Header().Add("Cache-Control", s.options.CacheControl)
|
||||
if render != nil {
|
||||
if err = render.Render(writer, request, reader); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
http.ServeContent(writer, request, fileName, reader.LastModified, reader)
|
||||
writer.Header().Set("Content-Type", callResp.Header.Get("Content-Type"))
|
||||
if callRespMeta.IsError {
|
||||
render = meta.TryRender(meta.Path)
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
} else if render == nil {
|
||||
lastMod, err := time.Parse(http.TimeFormat, callResp.Header.Get("Last-Modified"))
|
||||
if seekResp, ok := callResp.Body.(io.ReadSeeker); ok && err == nil {
|
||||
http.ServeContent(writer, request, filepath.Base(callRespMeta.Path), lastMod, seekResp)
|
||||
}
|
||||
} else {
|
||||
if reader, ok := result.(*utils.SizeReadCloser); ok && render == nil {
|
||||
writer.Header().Add("Content-Length", strconv.FormatUint(reader.Size, 10))
|
||||
}
|
||||
// todo(bug) : 直连模式下告知数据长度
|
||||
writer.Header().Add("X-Cache", "MISS")
|
||||
writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
if render != nil {
|
||||
if err = render.Render(writer, request, reader); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, _ = io.Copy(writer, result)
|
||||
}
|
||||
defer callResp.Body.Close()
|
||||
return render.Render(writer, request, callResp.Body)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) Proxy(writer http.ResponseWriter, request *http.Request, meta *core.PageDomainContent) bool {
|
||||
for prefix, backend := range meta.Proxy {
|
||||
proxyPath := "/" + meta.Path
|
||||
if strings.HasPrefix(proxyPath, prefix) {
|
||||
targetPath := strings.TrimPrefix(proxyPath, prefix)
|
||||
if !strings.HasPrefix(targetPath, "/") {
|
||||
targetPath = "/" + targetPath
|
||||
}
|
||||
u, _ := url.Parse(backend)
|
||||
request.URL.Path = targetPath
|
||||
request.RequestURI = request.URL.RequestURI()
|
||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||
proxy.Transport = s.options.HTTPClient.Transport
|
||||
|
||||
if host, _, err := net.SplitHostPort(request.RemoteAddr); err == nil {
|
||||
request.Header.Set("X-Real-IP", host)
|
||||
}
|
||||
request.Header.Set("X-Page-IP", utils.GetRemoteIP(request))
|
||||
request.Header.Set("X-Page-Refer", fmt.Sprintf("%s/%s/%s", meta.Owner, meta.Repo, meta.Path))
|
||||
request.Header.Set("X-Page-Host", request.Host)
|
||||
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
|
||||
zap.Any("path", proxyPath), zap.Any("target", fmt.Sprintf("%s%s", u, targetPath)))
|
||||
// todo(security): 处理 websocket
|
||||
proxy.ServeHTTP(writer, request)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) Close() error {
|
||||
return stdErr.Join(
|
||||
s.options.CacheBlob.Close(),
|
||||
|
||||
Reference in New Issue
Block a user