重构项目
This commit is contained in:
199
pkg/core/meta.go
199
pkg/core/meta.go
@@ -2,6 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -16,15 +17,11 @@ import (
|
||||
"gopkg.d7z.net/middleware/tools"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
||||
)
|
||||
|
||||
var regexpHostname = regexp.MustCompile(`^(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61})\.([a-z0-9]{2,7})$`)
|
||||
|
||||
type ServerMeta struct {
|
||||
Backend
|
||||
Domain string
|
||||
@@ -34,6 +31,52 @@ type ServerMeta struct {
|
||||
locker *utils.Locker
|
||||
}
|
||||
|
||||
// PageConfig 配置
|
||||
|
||||
type PageMetaContent struct {
|
||||
CommitID string `json:"commit_id"` // 提交 COMMIT ID
|
||||
LastModified time.Time `json:"last_modified"` // 上次更新时间
|
||||
IsPage bool `json:"is_page"` // 是否为 Page
|
||||
ErrorMsg string `json:"error"` // 错误消息 (作为 500 错误日志暴露至前端)
|
||||
|
||||
Alias []string `json:"alias"` // alias
|
||||
|
||||
Filters []Filter `json:"filters"` // 路由消息
|
||||
}
|
||||
|
||||
func NewEmptyPageMetaContent() *PageMetaContent {
|
||||
return &PageMetaContent{
|
||||
IsPage: false,
|
||||
Filters: []Filter{
|
||||
{
|
||||
Path: "**",
|
||||
Type: "default_not_found",
|
||||
Params: map[string]any{},
|
||||
},
|
||||
{ // 默认阻塞
|
||||
Path: ".git/**",
|
||||
Type: "block",
|
||||
Params: map[string]any{
|
||||
"code": "404",
|
||||
"message": "Not found",
|
||||
},
|
||||
}, { // 默认阻塞
|
||||
Path: ".pages.yaml",
|
||||
Type: "block",
|
||||
Params: map[string]any{
|
||||
"code": "404",
|
||||
"message": "Not found",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (m *PageMetaContent) String() string {
|
||||
marshal, _ := json.Marshal(m)
|
||||
return string(marshal)
|
||||
}
|
||||
|
||||
func NewServerMeta(client *http.Client, backend Backend, kv kv.KV, domain string, ttl time.Duration) *ServerMeta {
|
||||
return &ServerMeta{
|
||||
Backend: backend,
|
||||
@@ -44,15 +87,15 @@ func NewServerMeta(client *http.Client, backend Backend, kv kv.KV, domain string
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, *PageVFS, error) {
|
||||
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, error) {
|
||||
repos, err := s.Repos(ctx, owner)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defBranch := repos[repo]
|
||||
if defBranch == "" {
|
||||
return nil, nil, os.ErrNotExist
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
if branch == "" {
|
||||
@@ -61,21 +104,21 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
|
||||
|
||||
branches, err := s.Branches(ctx, owner, repo)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := branches[branch]
|
||||
if info == nil {
|
||||
return nil, nil, os.ErrNotExist
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
key := fmt.Sprintf("%s/%s/%s", owner, repo, branch)
|
||||
|
||||
if cache, found := s.cache.Load(ctx, key); found {
|
||||
if cache.IsPage {
|
||||
return &cache, NewPageVFS(s.client, s.Backend, owner, repo, cache.CommitID), nil
|
||||
return &cache, nil
|
||||
}
|
||||
return nil, nil, os.ErrNotExist
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
mux := s.locker.Open(key)
|
||||
@@ -84,9 +127,9 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
|
||||
|
||||
if cache, found := s.cache.Load(ctx, key); found {
|
||||
if cache.IsPage {
|
||||
return &cache, NewPageVFS(s.client, s.Backend, owner, repo, cache.CommitID), nil
|
||||
return &cache, nil
|
||||
}
|
||||
return nil, nil, os.ErrNotExist
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
rel := NewEmptyPageMetaContent()
|
||||
@@ -98,39 +141,42 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
|
||||
if exists, _ := vfs.Exists(ctx, "index.html"); !exists {
|
||||
rel.IsPage = false
|
||||
_ = s.cache.Store(ctx, key, *rel)
|
||||
return nil, nil, os.ErrNotExist
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
rel.IsPage = true
|
||||
|
||||
// 添加默认跳过的内容
|
||||
for _, defIgnore := range rel.Ignore {
|
||||
rel.ignoreL = append(rel.ignoreL, glob.MustCompile(defIgnore))
|
||||
}
|
||||
|
||||
// 解析配置
|
||||
if err := s.parsePageConfig(ctx, rel, vfs); err != nil {
|
||||
rel.IsPage = false
|
||||
rel.ErrorMsg = err.Error()
|
||||
_ = s.cache.Store(ctx, key, *rel)
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 处理 CNAME 文件
|
||||
if err := s.parseCNAME(ctx, rel, vfs); err != nil {
|
||||
rel.IsPage = false
|
||||
rel.ErrorMsg = err.Error()
|
||||
_ = s.cache.Store(ctx, key, *rel)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
rel.Alias = utils.ClearDuplicates(rel.Alias)
|
||||
rel.Ignore = utils.ClearDuplicates(rel.Ignore)
|
||||
_ = s.cache.Store(ctx, key, *rel)
|
||||
return rel, vfs, nil
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
func (s *ServerMeta) parsePageConfig(ctx context.Context, rel *PageMetaContent, vfs *PageVFS) error {
|
||||
func (s *ServerMeta) parsePageConfig(ctx context.Context, meta *PageMetaContent, vfs *PageVFS) error {
|
||||
alias := make([]string, 0)
|
||||
defer func(alias *[]string) {
|
||||
meta.Alias = *alias
|
||||
direct := *alias
|
||||
meta.Filters = append(meta.Filters, Filter{
|
||||
Path: "**",
|
||||
Type: "redirect",
|
||||
Params: map[string]any{
|
||||
"targets": direct,
|
||||
},
|
||||
})
|
||||
}(&alias)
|
||||
cname, err := vfs.ReadString(ctx, "CNAME")
|
||||
if cname != "" && err == nil {
|
||||
if al, ok := s.aliasCheck(cname); ok {
|
||||
alias = append(alias, al)
|
||||
} else {
|
||||
return fmt.Errorf("invalid alias %s", cname)
|
||||
}
|
||||
}
|
||||
data, err := vfs.ReadString(ctx, ".pages.yaml")
|
||||
if err != nil {
|
||||
zap.L().Debug("failed to read meta data", zap.String("error", err.Error()))
|
||||
@@ -141,43 +187,48 @@ func (s *ServerMeta) parsePageConfig(ctx context.Context, rel *PageMetaContent,
|
||||
if err = yaml.Unmarshal([]byte(data), cfg); err != nil {
|
||||
return errors.Wrap(err, "parse .pages.yaml failed")
|
||||
}
|
||||
|
||||
rel.VRoute = cfg.VirtualRoute
|
||||
if cfg.VirtualRoute {
|
||||
meta.Filters = append(meta.Filters, Filter{
|
||||
Path: "**",
|
||||
Type: "forward",
|
||||
Params: map[string]any{
|
||||
"path": "index.html",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// 处理别名
|
||||
for _, cname := range cfg.Alias {
|
||||
if err := s.addAlias(rel, cname); err != nil {
|
||||
return err
|
||||
if cname == "" {
|
||||
continue
|
||||
}
|
||||
if al, ok := s.aliasCheck(cname); ok {
|
||||
alias = append(alias, al)
|
||||
} else {
|
||||
return fmt.Errorf("invalid alias %s", cname)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理渲染器
|
||||
for sType, pattern := range cfg.Renders() {
|
||||
r := GetRender(sType)
|
||||
if r == nil {
|
||||
return errors.Errorf("render not found %s", sType)
|
||||
}
|
||||
|
||||
g, err := glob.Compile(strings.TrimSpace(pattern))
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "compile render pattern failed: %s", pattern)
|
||||
}
|
||||
|
||||
rel.rendersL = append(rel.rendersL, &renderCompiler{
|
||||
regex: g,
|
||||
Render: r,
|
||||
meta.Filters = append(meta.Filters, Filter{
|
||||
Path: pattern,
|
||||
Type: sType,
|
||||
Params: map[string]any{},
|
||||
})
|
||||
rel.Renders[sType] = append(rel.Renders[sType], pattern)
|
||||
}
|
||||
|
||||
// 处理跳过内容
|
||||
for _, pattern := range cfg.Ignores() {
|
||||
g, err := glob.Compile(pattern)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "compile ignore pattern failed: %s", pattern)
|
||||
}
|
||||
rel.ignoreL = append(rel.ignoreL, g)
|
||||
rel.Ignore = append(rel.Ignore, pattern)
|
||||
meta.Filters = append(meta.Filters, Filter{ // 默认直连
|
||||
Path: pattern,
|
||||
Type: "block",
|
||||
Params: map[string]any{
|
||||
"code": "404",
|
||||
"message": "Not found",
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 处理反向代理
|
||||
@@ -196,35 +247,29 @@ func (s *ServerMeta) parsePageConfig(ctx context.Context, rel *PageMetaContent,
|
||||
if rURL.Scheme != "http" && rURL.Scheme != "https" {
|
||||
return errors.Errorf("invalid backend url scheme: %s", backend)
|
||||
}
|
||||
|
||||
rel.Proxy[path] = rURL.String()
|
||||
meta.Filters = append(meta.Filters, Filter{
|
||||
Path: path,
|
||||
Type: "reverse_proxy",
|
||||
Params: map[string]any{
|
||||
"prefix": path,
|
||||
"target": rURL.String(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ServerMeta) parseCNAME(ctx context.Context, rel *PageMetaContent, vfs *PageVFS) error {
|
||||
cname, err := vfs.ReadString(ctx, "CNAME")
|
||||
if err != nil {
|
||||
return nil // CNAME 文件不存在是正常情况
|
||||
}
|
||||
if err := s.addAlias(rel, cname); err != nil {
|
||||
zap.L().Debug("指定的 CNAME 不合法", zap.String("cname", cname), zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var regexpHostname = regexp.MustCompile(`^(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61})\.([a-z0-9]{2,7})$`)
|
||||
|
||||
func (s *ServerMeta) addAlias(rel *PageMetaContent, cname string) error {
|
||||
func (s *ServerMeta) aliasCheck(cname string) (string, bool) {
|
||||
cname = strings.TrimSpace(cname)
|
||||
if !regexpHostname.MatchString(cname) {
|
||||
return errors.New("invalid domain name format")
|
||||
return "", false
|
||||
}
|
||||
|
||||
if strings.HasSuffix(strings.ToLower(cname), strings.ToLower(s.Domain)) {
|
||||
return errors.New("alias cannot be subdomain of main domain")
|
||||
return "", false
|
||||
}
|
||||
|
||||
rel.Alias = append(rel.Alias, cname)
|
||||
return nil
|
||||
return cname, true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user