Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
c5a4da5572 | |||
cf44709b69 | |||
f9a7e98702 |
@ -1,7 +1,7 @@
|
||||
FROM caddy:2.6-builder-alpine AS builder
|
||||
|
||||
RUN xcaddy build \
|
||||
--with github.com/42wim/caddy-gitea@v0.0.2
|
||||
--with github.com/42wim/caddy-gitea@v0.0.3
|
||||
|
||||
FROM caddy:2.6.2
|
||||
|
||||
|
24
README.md
24
README.md
@ -14,7 +14,8 @@ This also requires you to setup a wildcard CNAME to your gitea host.
|
||||
- [DNS config](#dns-config)
|
||||
- [Gitea config](#gitea-config)
|
||||
- [gitea-pages repo](#gitea-pages-repo)
|
||||
- [any repo](#any-repo)
|
||||
- [any repo with configurable allowed branch/tag/commits](#any-repo-with-configurable-allowed-branchtagcommits)
|
||||
- [any repo with all branches/tags/commits exposed](#any-repo-with-all-branchestagscommits-exposed)
|
||||
- [Building caddy](#building-caddy)
|
||||
|
||||
<!-- /TOC -->
|
||||
@ -52,10 +53,11 @@ Depending on the gitea config below you'll be able to access your pages using:
|
||||
|
||||
### Gitea config
|
||||
|
||||
There are 2 options to expose your repo's as a page, that you can use both at the same time.
|
||||
There are multiple options to expose your repo's as a page, that you can use both at the same time.
|
||||
|
||||
- creating a gitea-pages repo with a gitea-pages branch
|
||||
- adding a gitea-pages branch to any repo of choice
|
||||
- creating a gitea-pages repo with a gitea-pages branch and a gitea-pages topic
|
||||
- adding a gitea-pages branch to any repo of choice and a gitea-pages topic
|
||||
- adding a gitea-pages-allowall topic to your repo (easiest, but less secure)
|
||||
|
||||
#### gitea-pages repo
|
||||
|
||||
@ -68,7 +70,7 @@ e.g. we'll use the `yourorg` org.
|
||||
|
||||
Your content will now be available on <http://yourorg.pages.yourdomain.com:3000/file.html>
|
||||
|
||||
#### any repo
|
||||
#### any repo with configurable allowed branch/tag/commits
|
||||
|
||||
e.g. we'll use the `yourrepo` repo in the `yourorg` org and there is a `file.html` in the `master` branch and a `otherfile.html` in the `dev` branch. The `master` branch is your default branch.
|
||||
|
||||
@ -94,6 +96,16 @@ allowedrefs=["main","dev"]
|
||||
- Your `otherfile.html` in the `dev` branch will now be available on <http://yourorg.pages.yourdomain.com:3000/yourrepo/file.html?ref=dev>
|
||||
- Your `otherfile.html` in the `dev` branch will now be available on <http://dev.yourrepo.yourorg.pages.yourdomain.com:3000/file.html>
|
||||
|
||||
#### any repo with all branches/tags/commits exposed
|
||||
|
||||
e.g. we'll use the `yourrepo` repo in the `yourorg` org and there is a `file.html` in the `master` branch and a `otherfile.html` in the `dev` branch. The `master` branch is your default branch.
|
||||
|
||||
1. Add a `gitea-pages-allowall` topic to the `yourrepo` repo (this is used to opt-in your repo).
|
||||
|
||||
- Your `file.html` in the `master` branch will now be available on <http://yourorg.pages.yourdomain.com:3000/yourrepo/file.html>
|
||||
- Your `file.html` in the `master` branch will now be available on <http://yourrepo.yourorg.pages.yourdomain.com:3000/file.html>
|
||||
- Your `otherfile.html` in the `dev` branch will now be available on <http://yourorg.pages.yourdomain.com:3000/yourrepo/file.html?ref=dev>
|
||||
- Your `otherfile.html` in the `dev` branch will now be available on <http://dev.yourrepo.yourorg.pages.yourdomain.com:3000/file.html>
|
||||
|
||||
## Building caddy
|
||||
|
||||
@ -102,5 +114,5 @@ To build with this plugin you'll need to have go1.19 installed.
|
||||
|
||||
```go
|
||||
go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest #this will install xcaddy in ~/go/bin
|
||||
~/go/bin/xcaddy build --with github.com/42wim/caddy-gitea@v0.0.2
|
||||
~/go/bin/xcaddy build --with github.com/42wim/caddy-gitea@v0.0.3
|
||||
```
|
||||
|
15
gitea.go
15
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)
|
||||
}
|
||||
|
@ -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("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(meta["title"].(string))...)
|
||||
title, _ := meta["title"].(string)
|
||||
|
||||
res = append([]byte("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(title)...)
|
||||
res = append(res, []byte("</h1>")...)
|
||||
res = append(res, resmd...)
|
||||
res = append(res, []byte("</body></html>")...)
|
||||
@ -196,19 +206,25 @@ func (c *Client) hasRepoBranch(owner, repo, branch string) bool {
|
||||
return b.Name == branch
|
||||
}
|
||||
|
||||
func (c *Client) allowsPages(owner, repo string) bool {
|
||||
func (c *Client) allowsPages(owner, repo string) (bool, bool) {
|
||||
topics, err := c.repoTopics(owner, repo)
|
||||
if err != nil {
|
||||
return false
|
||||
return false, false
|
||||
}
|
||||
|
||||
for _, topic := range topics {
|
||||
if topic == c.giteapagesAllowAll {
|
||||
return true, true
|
||||
}
|
||||
}
|
||||
|
||||
for _, topic := range topics {
|
||||
if topic == c.giteapages {
|
||||
return true
|
||||
return true, false
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return false, false
|
||||
}
|
||||
|
||||
func (c *Client) readConfig(owner, repo string) error {
|
||||
@ -236,7 +252,11 @@ func splitName(name string) (string, string, string) {
|
||||
}
|
||||
}
|
||||
|
||||
func validRefs(ref string) bool {
|
||||
func validRefs(ref string, allowall bool) bool {
|
||||
if allowall {
|
||||
return true
|
||||
}
|
||||
|
||||
validrefs := viper.GetStringSlice("allowedrefs")
|
||||
for _, r := range validrefs {
|
||||
if r == ref {
|
||||
|
Reference in New Issue
Block a user