Add support for Caddy on-demand TLS.

This commit is contained in:
Catherine
2025-09-15 07:39:02 +00:00
parent abaf6d993b
commit aa2ce12970
5 changed files with 58 additions and 17 deletions

View File

@@ -1,9 +1,13 @@
data-dir = "./data"
[listen]
[pages]
protocol = "tcp"
address = ":3333"
[caddy]
protocol = "tcp"
address = ":3334"
[wildcard]
domain = "codeberg.page"
clone-url = "https://codeberg.org/%s/%s.git"

25
src/caddy.go Normal file
View File

@@ -0,0 +1,25 @@
package main
import (
"log"
"net/http"
"os"
"path/filepath"
)
func ServeCaddy(w http.ResponseWriter, r *http.Request) {
domain := r.URL.Query().Get("domain")
if domain == "" {
http.Error(w, "domain parameter required", http.StatusBadRequest)
return
}
wwwRoot := filepath.Join(config.DataDir, "www", domain)
if stat, err := os.Stat(wwwRoot); err == nil && stat.IsDir() {
log.Println("caddy:", domain, 200)
w.WriteHeader(http.StatusOK)
} else {
log.Println("caddy:", domain, 404)
w.WriteHeader(http.StatusNotFound)
}
}

View File

@@ -6,12 +6,15 @@ import (
"github.com/pelletier/go-toml/v2"
)
type Listen struct {
Protocol string `toml:"protocol"`
Address string `toml:"address"`
}
type Config struct {
DataDir string `toml:"data-dir"`
Listen struct {
Protocol string `toml:"protocol"`
Address string `toml:"address"`
} `toml:"listen"`
DataDir string `toml:"data-dir"`
Pages Listen `toml:"pages"`
Caddy Listen `toml:"caddy"`
Wildcard struct {
Domain string `toml:"domain"`
CloneURL string `toml:"clone-url"`

View File

@@ -9,21 +9,30 @@ import (
var config Config
func serveHandler(name string, listen Listen, serve func(http.ResponseWriter, *http.Request)) {
listener, err := net.Listen(listen.Protocol, listen.Address)
if err != nil {
log.Fatalf("%s: %s\n", name, err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", serve)
if err := http.Serve(listener, mux); err != nil {
log.Fatalf("%s: %s\n", name, err)
}
}
func main() {
configPath := flag.String("config", "config.toml", "path to configuration file")
flag.Parse()
if err := readConfig(*configPath, &config); err != nil {
log.Fatalln("failed to read configuration:", err)
log.Fatalln("configuration:", err)
}
listener, err := net.Listen(config.Listen.Protocol, config.Listen.Address)
if err != nil {
log.Fatalln("failed to listen:", err)
if config.Caddy != (Listen{}) {
go serveHandler("caddy", config.Caddy, ServeCaddy)
}
http.HandleFunc("/", Serve)
if err := http.Serve(listener, nil); err != nil {
log.Fatalln("failed to serve:", err)
}
serveHandler("pages", config.Pages, ServePages)
}

View File

@@ -233,8 +233,8 @@ func postPage(w http.ResponseWriter, r *http.Request) error {
return result.err
}
func Serve(w http.ResponseWriter, r *http.Request) {
log.Println("serve:", r.Method, r.Host, r.URL)
func ServePages(w http.ResponseWriter, r *http.Request) {
log.Println("pages:", r.Method, r.Host, r.URL)
err := error(nil)
switch r.Method {
case http.MethodGet:
@@ -248,6 +248,6 @@ func Serve(w http.ResponseWriter, r *http.Request) {
err = fmt.Errorf("method %s not allowed", r.Method)
}
if err != nil {
log.Println("serve err:", err)
log.Println("pages err:", err)
}
}