Skip to content

Commit

Permalink
feat: finish libdns migration
Browse files Browse the repository at this point in the history
The config parser now supports nesting, and all of the DNS providers
support configuration via blocks.
  • Loading branch information
Lemmmy committed Jan 28, 2024
1 parent d529a84 commit 8cd4ce9
Show file tree
Hide file tree
Showing 12 changed files with 1,109 additions and 139 deletions.
37 changes: 18 additions & 19 deletions casket/casketmain/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ import (
_ "github.com/tmpim/casket-plugins/realip"
_ "github.com/tmpim/casket-plugins/tmpauth"
_ "github.com/tmpim/casket-plugins/webdav"
// TODO: Temporary
// _ "github.com/tmpim/dnsproviders/azure"
// _ "github.com/tmpim/dnsproviders/cloudflare"
// _ "github.com/tmpim/dnsproviders/digitalocean"
// _ "github.com/tmpim/dnsproviders/dnsimple"
// _ "github.com/tmpim/dnsproviders/duckdns"
// _ "github.com/tmpim/dnsproviders/dyn"
// _ "github.com/tmpim/dnsproviders/godaddy"
// _ "github.com/tmpim/dnsproviders/googlecloud"
// _ "github.com/tmpim/dnsproviders/httpreq"
// _ "github.com/tmpim/dnsproviders/lightsail"
// _ "github.com/tmpim/dnsproviders/linode"
// _ "github.com/tmpim/dnsproviders/namecheap"
// _ "github.com/tmpim/dnsproviders/namedotcom"
// _ "github.com/tmpim/dnsproviders/ovh"
// _ "github.com/tmpim/dnsproviders/rackspace"
// _ "github.com/tmpim/dnsproviders/rfc2136"
// _ "github.com/tmpim/dnsproviders/route53"
// _ "github.com/tmpim/dnsproviders/vultr"
_ "github.com/tmpim/dnsproviders/acmedns"
_ "github.com/tmpim/dnsproviders/azure"
_ "github.com/tmpim/dnsproviders/cloudflare"
_ "github.com/tmpim/dnsproviders/digitalocean"
_ "github.com/tmpim/dnsproviders/dnspod"
_ "github.com/tmpim/dnsproviders/duckdns"
_ "github.com/tmpim/dnsproviders/gandi"
_ "github.com/tmpim/dnsproviders/godaddy"
_ "github.com/tmpim/dnsproviders/googlecloud"
_ "github.com/tmpim/dnsproviders/linode"
_ "github.com/tmpim/dnsproviders/namecheap"
_ "github.com/tmpim/dnsproviders/namedotcom"
_ "github.com/tmpim/dnsproviders/ovh"
_ "github.com/tmpim/dnsproviders/pdns"
_ "github.com/tmpim/dnsproviders/rfc2136"
_ "github.com/tmpim/dnsproviders/route53"
_ "github.com/tmpim/dnsproviders/transip"
_ "github.com/tmpim/dnsproviders/vultr"
)
112 changes: 91 additions & 21 deletions casketfile/dispenser.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ func (d *Dispenser) NextArg() bool {
return false
}

// nextOnSameLine advances the cursor if the next
// token is on the same line of the same file.
func (d *Dispenser) nextOnSameLine() bool {
if d.cursor < 0 {
d.cursor++
return true
}
if d.cursor >= len(d.tokens)-1 {
return false
}
curr := d.tokens[d.cursor]
next := d.tokens[d.cursor+1]
if !isNextOnNewLine(curr, next) {
d.cursor++
return true
}
return false
}

// NextLine loads the next token only if it is not on the same
// line as the current token, and returns true if a token was
// loaded; false otherwise. If false, there is not another token
Expand All @@ -91,49 +110,82 @@ func (d *Dispenser) NextLine() bool {
d.cursor++
return true
}
if d.cursor >= len(d.tokens) {
if d.cursor >= len(d.tokens)-1 {
return false
}
if d.cursor < len(d.tokens)-1 &&
(d.tokens[d.cursor].File != d.tokens[d.cursor+1].File ||
d.tokens[d.cursor].Line+d.numLineBreaks(d.cursor) < d.tokens[d.cursor+1].Line) {
curr := d.tokens[d.cursor]
next := d.tokens[d.cursor+1]
if isNextOnNewLine(curr, next) {
d.cursor++
return true
}
return false
}

// NextBlock can be used as the condition of a for loop
// to load the next token as long as it opens a block or
// is already in a block. It returns true if a token was
// loaded, or false when the block's closing curly brace
// was loaded and thus the block ended. Nested blocks are
// not supported.
// NextBlock is equivalent to NextBlockNesting(0).
func (d *Dispenser) NextBlock() bool {
if d.nesting > 0 {
d.Next()
if d.Val() == "}" {
return d.NextBlockNesting(0)
}

// NextBlockNesting can be used as the condition of a for loop
// to load the next token as long as it opens a block or
// is already in a block nested more than initialNestingLevel.
// In other words, a loop over NextBlockNesting() will iterate
// all tokens in the block assuming the next token is an
// open curly brace, until the matching closing brace.
// The open and closing brace tokens for the outer-most
// block will be consumed internally and omitted from
// the iteration.
//
// Proper use of this method looks like this:
//
// for nesting := d.Nesting(); d.NextBlockNesting(nesting); {
// }
//
// However, in simple cases where it is known that the
// Dispenser is new and has not already traversed state
// by a loop over NextBlockNesting(), this will do:
//
// for d.NextBlockNesting() {
// }
//
// As with other token parsing logic, a loop over
// NextBlockNesting() should be contained within a loop over
// Next(), as it is usually prudent to skip the initial
// token.
func (d *Dispenser) NextBlockNesting(initialNestingLevel int) bool {
if d.nesting > initialNestingLevel {
if !d.Next() {
return false // should be EOF error
}
if d.Val() == "}" && !d.nextOnSameLine() {
d.nesting--
return false
} else if d.Val() == "{" && !d.nextOnSameLine() {
d.nesting++
}
return true
return d.nesting > initialNestingLevel
}
if !d.NextArg() { // block must open on same line
if !d.nextOnSameLine() { // block must open on same line
return false
}
if d.Val() != "{" {
d.cursor-- // roll back if not opening brace
return false
}
d.Next()
d.Next() // consume open curly brace
if d.Val() == "}" {
// Open and then closed right away
return false
return false // open and then closed right away
}
d.nesting++
return true
}

// Nesting returns the current nesting level. Necessary
// if using NextBlock()
func (d *Dispenser) Nesting() int {
return d.nesting
}

// Val gets the text of the current token. If there is no token
// loaded, it returns empty string.
func (d *Dispenser) Val() string {
Expand Down Expand Up @@ -255,6 +307,24 @@ func (d *Dispenser) isNewLine() bool {
if d.cursor > len(d.tokens)-1 {
return false
}
return d.tokens[d.cursor-1].File != d.tokens[d.cursor].File ||
d.tokens[d.cursor-1].Line+d.numLineBreaks(d.cursor-1) < d.tokens[d.cursor].Line

prev := d.tokens[d.cursor-1]
curr := d.tokens[d.cursor]
return isNextOnNewLine(prev, curr)
}

// isNextOnNewLine determines whether the current token is on a different
// line (higher line number) than the next token. It handles imported
// tokens correctly. If there isn't a next token, it returns true.
func (d *Dispenser) isNextOnNewLine() bool {
if d.cursor < 0 {
return false
}
if d.cursor >= len(d.tokens)-1 {
return true
}

curr := d.tokens[d.cursor]
next := d.tokens[d.cursor+1]
return isNextOnNewLine(curr, next)
}
33 changes: 33 additions & 0 deletions casketfile/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package casketfile
import (
"bufio"
"io"
"strings"
"unicode"
)

Expand Down Expand Up @@ -148,3 +149,35 @@ func (l *lexer) next() bool {
val = append(val, ch)
}
}

// NumLineBreaks counts how many line breaks are in the token text.
func (t Token) NumLineBreaks() int {
lineBreaks := strings.Count(t.Text, "\n")
return lineBreaks
}

// isNextOnNewLine tests whether t2 is on a different line from t1
func isNextOnNewLine(t1, t2 Token) bool {
// If the second token is from a different file,
// we can assume it's from a different line
if t1.File != t2.File {
return true
}

// TODO:
// If the second token is from a different import chain,
// we can assume it's from a different line
// if len(t1.imports) != len(t2.imports) {
// return true
// }
// for i, im := range t1.imports {
// if im != t2.imports[i] {
// return true
// }
// }

// If the first token (incl line breaks) ends
// on a line earlier than the next token,
// then the second token is on a new line
return t1.Line+t1.NumLineBreaks() < t2.Line
}
1 change: 1 addition & 0 deletions caskethttp/browse/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,6 @@ func browseParse(c *casket.Controller) ([]Config, error) {
}

// The default template to use when serving up directory listings
//
//go:embed default_template.html
var defaultTemplate string
2 changes: 1 addition & 1 deletion caskethttp/markdown/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"net/http"
"path/filepath"

"github.com/russross/blackfriday"
"github.com/tmpim/casket"
"github.com/tmpim/casket/caskethttp/httpserver"
"github.com/russross/blackfriday"
)

func init() {
Expand Down
5 changes: 2 additions & 3 deletions caskettls/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/caddyserver/certmagic"
"github.com/go-acme/lego/v4/challenge/tlsalpn01"
"github.com/klauspost/cpuid"
"github.com/tmpim/casket"
)
Expand Down Expand Up @@ -216,13 +215,13 @@ func (c *Config) buildStandardTLSConfig() error {
// ensure ALPN includes the ACME TLS-ALPN protocol
var alpnFound bool
for _, a := range c.ALPN {
if a == tlsalpn01.ACMETLS1Protocol {
if a == "acme-tls/1" {
alpnFound = true
break
}
}
if !alpnFound {
c.ALPN = append(c.ALPN, tlsalpn01.ACMETLS1Protocol)
c.ALPN = append(c.ALPN, "acme-tls/1")
}

config.MinVersion = c.ProtocolMinVersion
Expand Down
47 changes: 0 additions & 47 deletions caskettls/dnsproviders.go

This file was deleted.

Loading

0 comments on commit 8cd4ce9

Please sign in to comment.