Skip to content

Commit 5309410

Browse files
committed
Add support in http provider
1 parent 66eeb50 commit 5309410

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

challenge/http01/http_challenge_server.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
"github.com/go-acme/lego/v4/log"
1313
)
1414

15+
type ProviderNetwork string
16+
17+
const (
18+
DefaultNetwork = "tcp"
19+
Tcp4Network = "tcp4"
20+
Tcp6Network = "tcp6"
21+
)
22+
1523
// ProviderServer implements ChallengeProvider for `http-01` challenge.
1624
// It may be instantiated without using the NewProviderServer function if
1725
// you want only to use the default values.
@@ -29,12 +37,15 @@ type ProviderServer struct {
2937
// NewProviderServer creates a new ProviderServer on the selected interface and port.
3038
// Setting iface and / or port to an empty string will make the server fall back to
3139
// the "any" interface and port 80 respectively.
32-
func NewProviderServer(iface, port string) *ProviderServer {
40+
func NewProviderServer(iface, port string, network ProviderNetwork) *ProviderServer {
3341
if port == "" {
3442
port = "80"
3543
}
44+
if network == "" {
45+
network = DefaultNetwork
46+
}
3647

37-
return &ProviderServer{network: "tcp", address: net.JoinHostPort(iface, port), matcher: &hostMatcher{}}
48+
return &ProviderServer{network: string(network), address: net.JoinHostPort(iface, port), matcher: &hostMatcher{}}
3849
}
3950

4051
func NewUnixProviderServer(socketPath string, mode fs.FileMode) *ProviderServer {

challenge/http01/http_challenge_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,27 @@ func TestProviderServer_GetAddress(t *testing.T) {
3636
}{
3737
{
3838
desc: "TCP default address",
39-
server: NewProviderServer("", ""),
39+
server: NewProviderServer("", "", ""),
4040
expected: ":80",
4141
},
4242
{
4343
desc: "TCP with explicit port",
44-
server: NewProviderServer("", "8080"),
44+
server: NewProviderServer("", "8080", ""),
4545
expected: ":8080",
4646
},
4747
{
4848
desc: "TCP with host and port",
49-
server: NewProviderServer("localhost", "8080"),
49+
server: NewProviderServer("localhost", "8080", ""),
50+
expected: "localhost:8080",
51+
},
52+
{
53+
desc: "TCP4 with host and port",
54+
server: NewProviderServer("localhost", "8080", Tcp4Network),
55+
expected: "localhost:8080",
56+
},
57+
{
58+
desc: "TCP6 with host and port",
59+
server: NewProviderServer("localhost", "8080", Tcp6Network),
5060
expected: "localhost:8080",
5161
},
5262
{
@@ -70,7 +80,7 @@ func TestProviderServer_GetAddress(t *testing.T) {
7080
func TestChallenge(t *testing.T) {
7181
_, apiURL := tester.SetupFakeAPI(t)
7282

73-
providerServer := NewProviderServer("", "23457")
83+
providerServer := NewProviderServer("", "23457", "")
7484

7585
validate := func(_ *api.Core, _ string, chlng acme.Challenge) error {
7686
uri := "http://localhost" + providerServer.GetAddress() + ChallengePath(chlng.Token)
@@ -199,7 +209,7 @@ func TestChallengeInvalidPort(t *testing.T) {
199209

200210
validate := func(_ *api.Core, _ string, _ acme.Challenge) error { return nil }
201211

202-
solver := NewChallenge(core, validate, NewProviderServer("", "123456"))
212+
solver := NewChallenge(core, validate, NewProviderServer("", "123456", ""))
203213

204214
authz := acme.Authorization{
205215
Identifier: acme.Identifier{
@@ -374,7 +384,7 @@ func testServeWithProxy(t *testing.T, header, extra *testProxyHeader, expectErro
374384

375385
_, apiURL := tester.SetupFakeAPI(t)
376386

377-
providerServer := NewProviderServer("localhost", "23457")
387+
providerServer := NewProviderServer("localhost", "23457", "")
378388
if header != nil {
379389
providerServer.SetProxyHeader(header.name)
380390
}

cmd/setup_challenges.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func setupChallenges(ctx *cli.Context, client *lego.Client) {
4242
}
4343

4444
func setupHTTPProvider(ctx *cli.Context) challenge.Provider {
45+
var network http01.ProviderNetwork
46+
switch {
47+
case ctx.IsSet("ipv4only") && ctx.IsSet("ipv6only"):
48+
network = http01.DefaultNetwork
49+
case ctx.IsSet("ipv4only"):
50+
network = http01.Tcp4Network
51+
case ctx.IsSet("ipv6only"):
52+
network = http01.Tcp6Network
53+
}
54+
4555
switch {
4656
case ctx.IsSet("http.webroot"):
4757
ps, err := webroot.NewHTTPProvider(ctx.String("http.webroot"))
@@ -66,13 +76,13 @@ func setupHTTPProvider(ctx *cli.Context) challenge.Provider {
6676
log.Fatal(err)
6777
}
6878

69-
srv := http01.NewProviderServer(host, port)
79+
srv := http01.NewProviderServer(host, port, network)
7080
if header := ctx.String("http.proxy-header"); header != "" {
7181
srv.SetProxyHeader(header)
7282
}
7383
return srv
7484
case ctx.Bool("http"):
75-
srv := http01.NewProviderServer("", "")
85+
srv := http01.NewProviderServer("", "", network)
7686
if header := ctx.String("http.proxy-header"); header != "" {
7787
srv.SetProxyHeader(header)
7888
}

0 commit comments

Comments
 (0)