Skip to content

Commit 3190a84

Browse files
committed
Generate Caddy config
The generated configuration sends all traffic through the dns01proxy handler. It also configures trusted proxies so that we can get the real client IP in our logs.
1 parent 027d215 commit 3190a84

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

app.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ package caddydns01proxy
22

33
import (
44
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"strconv"
58

69
"github.com/caddyserver/caddy/v2"
10+
"github.com/caddyserver/caddy/v2/caddyconfig"
11+
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
712
)
813

914
func init() {
@@ -19,9 +24,14 @@ type App struct {
1924

2025
// Configures the set of trusted proxies.
2126
TrustedProxiesRaw json.RawMessage `json:"trusted_proxies,omitempty" caddy:"namespace=http.ip_sources inline_key=source"`
27+
28+
// The http module instance that implements this app.
29+
httpApp *caddyhttp.App `json:"-"`
2230
}
2331

2432
var _ caddy.Module = (*App)(nil)
33+
var _ caddy.Provisioner = (*App)(nil)
34+
var _ caddy.App = (*App)(nil)
2535

2636
func (App) CaddyModule() caddy.ModuleInfo {
2737
return caddy.ModuleInfo{
@@ -31,3 +41,63 @@ func (App) CaddyModule() caddy.ModuleInfo {
3141
},
3242
}
3343
}
44+
45+
func (app *App) Provision(ctx caddy.Context) error {
46+
module, err := ctx.LoadModuleByID(
47+
"http",
48+
caddyconfig.JSON(
49+
caddyhttp.App{
50+
Servers: map[string]*caddyhttp.Server{
51+
"dns01proxy": {
52+
Listen: app.Listen,
53+
Routes: app.makeRoutes(),
54+
TrustedProxiesRaw: app.TrustedProxiesRaw,
55+
56+
// Turns on logging.
57+
Logs: &caddyhttp.ServerLogConfig{},
58+
},
59+
},
60+
},
61+
nil,
62+
),
63+
)
64+
if err != nil {
65+
return fmt.Errorf("unable to load http guest module: %w", err)
66+
}
67+
68+
app.httpApp = module.(*caddyhttp.App)
69+
return nil
70+
}
71+
72+
func (app *App) Start() error {
73+
return app.httpApp.Start()
74+
}
75+
76+
func (app *App) Stop() error {
77+
return app.httpApp.Stop()
78+
}
79+
80+
func (app *App) makeRoutes() caddyhttp.RouteList {
81+
return caddyhttp.RouteList{
82+
{
83+
HandlersRaw: []json.RawMessage{
84+
caddyconfig.JSONModuleObject(
85+
app.Handler,
86+
"handler",
87+
"dns01proxy",
88+
nil,
89+
),
90+
caddyconfig.JSONModuleObject(
91+
caddyhttp.StaticResponse{
92+
StatusCode: caddyhttp.WeakString(strconv.Itoa(
93+
http.StatusNotFound,
94+
)),
95+
},
96+
"handler",
97+
"static_response",
98+
nil,
99+
),
100+
},
101+
},
102+
}
103+
}

caddy_config.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package caddydns01proxy
22

33
import (
4-
"fmt"
5-
64
"github.com/caddyserver/caddy/v2"
5+
"github.com/caddyserver/caddy/v2/caddyconfig"
76
"github.com/liujed/caddy-dns01proxy/jsonutil"
7+
"github.com/liujed/goutil/ptr"
88
)
99

1010
// A dns01proxy configuration file is the same as the app configuration.
1111
type ConfigFile = App
1212

13+
const defaultListen = "127.0.0.1:9095"
14+
1315
// Reads a dns01proxy configuration file and returns a corresponding Caddy
1416
// configuration.
1517
func caddyConfigFromConfigFile(path string) (*caddy.Config, error) {
@@ -18,8 +20,20 @@ func caddyConfigFromConfigFile(path string) (*caddy.Config, error) {
1820
return nil, err
1921
}
2022

21-
// TODO: generate a Caddy configuration.
22-
_ = config
23+
// Set default listen sockets.
24+
if len(config.Listen) == 0 {
25+
config.Listen = []string{defaultListen}
26+
}
2327

24-
return nil, fmt.Errorf("implement me")
28+
return &caddy.Config{
29+
Admin: &caddy.AdminConfig{
30+
Disabled: true,
31+
Config: &caddy.ConfigSettings{
32+
Persist: ptr.Of(false),
33+
},
34+
},
35+
AppsRaw: caddy.ModuleMap{
36+
"dns01proxy": caddyconfig.JSON(config, nil),
37+
},
38+
}, nil
2539
}

command.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/liujed/caddy-dns01proxy/flags"
99
"github.com/liujed/goutil/optionals"
1010
"github.com/spf13/cobra"
11+
"go.uber.org/zap"
1112
)
1213

1314
// Flag definitions.
@@ -71,6 +72,19 @@ func cmdRun(fs caddycmd.Flags) (int, error) {
7172
return caddy.ExitCodeFailedStartup, err
7273
}
7374

75+
// Turn on debug logs if requested.
76+
if fs.Bool(flgDebug.Name) {
77+
cfg.Logging = &caddy.Logging{
78+
Logs: map[string]*caddy.CustomLog{
79+
"default": {
80+
BaseLog: caddy.BaseLog{
81+
Level: zap.DebugLevel.CapitalString(),
82+
},
83+
},
84+
},
85+
}
86+
}
87+
7488
caddy.Log().Info(fmt.Sprintf("Starting %s", Release()))
7589

7690
err = caddy.Run(cfg)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/smallstep/certificates v0.26.1
1010
github.com/spf13/cobra v1.9.1
1111
github.com/spf13/pflag v1.0.6
12+
go.uber.org/zap v1.27.0
1213
)
1314

1415
require (
@@ -99,7 +100,6 @@ require (
99100
go.uber.org/automaxprocs v1.6.0 // indirect
100101
go.uber.org/mock v0.5.0 // indirect
101102
go.uber.org/multierr v1.11.0 // indirect
102-
go.uber.org/zap v1.27.0 // indirect
103103
go.uber.org/zap/exp v0.3.0 // indirect
104104
golang.org/x/crypto v0.37.0 // indirect
105105
golang.org/x/crypto/x509roots/fallback v0.0.0-20250305170421-49bf5b80c810 // indirect

0 commit comments

Comments
 (0)