diff --git a/gitea.go b/gitea.go index 52fd66f..07fd631 100644 --- a/gitea.go +++ b/gitea.go @@ -26,11 +26,12 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) // Middleware implements gitea plugin. type Middleware struct { - Client *gitea.Client `json:"-"` - Server string `json:"server,omitempty"` - Token string `json:"token,omitempty"` - GiteaPages string `json:"gitea_pages,omitempty"` - Domain string `json:"domain,omitempty"` + Client *gitea.Client `json:"-"` + Server string `json:"server,omitempty"` + Token string `json:"token,omitempty"` + GiteaPages string `json:"gitea_pages,omitempty"` + GiteaPagesAllowAll string `json:"gitea_pages_allowall,omitempty"` + Domain string `json:"domain,omitempty"` } // CaddyModule returns the Caddy module information. @@ -44,7 +45,7 @@ func (Middleware) CaddyModule() caddy.ModuleInfo { // Provision provisions gitea client. func (m *Middleware) Provision(ctx caddy.Context) error { var err error - m.Client, err = gitea.NewClient(m.Server, m.Token, m.GiteaPages) + m.Client, err = gitea.NewClient(m.Server, m.Token, m.GiteaPages, m.GiteaPagesAllowAll) return err } @@ -65,6 +66,8 @@ func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { d.Args(&m.Token) case "gitea_pages": d.Args(&m.GiteaPages) + case "gitea_pages_allowall": + d.Args(&m.GiteaPagesAllowAll) case "domain": d.Args(&m.Domain) } diff --git a/pkg/gitea/gitea.go b/pkg/gitea/gitea.go index 11f2380..ef496e8 100644 --- a/pkg/gitea/gitea.go +++ b/pkg/gitea/gitea.go @@ -15,35 +15,37 @@ import ( ) type Client struct { - serverURL string - token string - giteapages string - gc *gclient.Client + serverURL string + token string + giteapages string + giteapagesAllowAll string + gc *gclient.Client } -func NewClient(serverURL, token, giteapages string) (*Client, error) { +func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client, error) { if giteapages == "" { giteapages = "gitea-pages" } + if giteapagesAllowAll == "" { + giteapagesAllowAll = "gitea-pages-allowall" + } + gc, err := gclient.NewClient(serverURL, gclient.SetToken(token), gclient.SetGiteaVersion("")) if err != nil { return nil, err } return &Client{ - serverURL: serverURL, - token: token, - gc: gc, - giteapages: giteapages, + serverURL: serverURL, + token: token, + gc: gc, + giteapages: giteapages, + giteapagesAllowAll: giteapagesAllowAll, }, nil } func (c *Client) Open(name, ref string) (fs.File, error) { - if ref == "" { - ref = c.giteapages - } - owner, repo, filepath := splitName(name) // if repo is empty they want to have the gitea-pages repo @@ -58,7 +60,8 @@ func (c *Client) Open(name, ref string) (fs.File, error) { } // we need to check if the repo exists (and allows access) - if !c.allowsPages(owner, repo) { + limited, allowall := c.allowsPages(owner, repo) + if !limited && !allowall { // if we're checking the gitea-pages and it doesn't exist, return 404 if repo == c.giteapages && !c.hasRepoBranch(owner, repo, c.giteapages) { return nil, fs.ErrNotExist @@ -69,7 +72,12 @@ func (c *Client) Open(name, ref string) (fs.File, error) { filepath = repo repo = c.giteapages - if !c.allowsPages(owner, repo) || !c.hasRepoBranch(owner, repo, c.giteapages) { + if ref == "" { + ref = c.giteapages + } + + limited, allowall = c.allowsPages(owner, repo) + if !limited && !allowall || !c.hasRepoBranch(owner, repo, c.giteapages) { return nil, fs.ErrNotExist } } @@ -79,7 +87,7 @@ func (c *Client) Open(name, ref string) (fs.File, error) { if err := c.readConfig(owner, repo); err != nil { // we don't need a config for gitea-pages // no config is only exposing the gitea-pages branch - if repo != c.giteapages { + if repo != c.giteapages && !allowall { return nil, err } @@ -90,7 +98,7 @@ func (c *Client) Open(name, ref string) (fs.File, error) { // always overwrite the ref to the gitea-pages branch if !hasConfig && (repo == c.giteapages || ref == c.giteapages) { ref = c.giteapages - } else if !validRefs(ref) { + } else if !validRefs(ref, allowall) { return nil, fs.ErrNotExist } @@ -174,7 +182,9 @@ func handleMD(res []byte) ([]byte, error) { return nil, err } - res = append([]byte("\n\n
\n