@@ -2,8 +2,13 @@ package caddydns01proxy
22
33import (
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
914func 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
2432var _ caddy.Module = (* App )(nil )
33+ var _ caddy.Provisioner = (* App )(nil )
34+ var _ caddy.App = (* App )(nil )
2535
2636func (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+ }
0 commit comments