Skip to content

Commit

Permalink
Merge pull request #6 from alexandreh2ag/remove_ips_from_domains_request
Browse files Browse the repository at this point in the history
Remove IP v4 & v6 from domain requests
  • Loading branch information
alexandreh2ag authored Jan 19, 2025
2 parents 378e0b7 + c4d790e commit ae133e6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 10 deletions.
13 changes: 11 additions & 2 deletions apps/agent/requester/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/valyala/fasthttp"
"log/slog"
"net/http"
"slices"
"sync"
"time"
)
Expand Down Expand Up @@ -74,17 +75,25 @@ func (t *traefik) Fetch() ([]*types.DomainRequest, error) {
defer wg.Done()
domainsAgent, err := t.FetchInstance(address)
if err != nil {
formatError := fmt.Errorf("agent (%s) failed to fetch with: %v", t.id, err)
formatError := fmt.Errorf("requester (%s) failed to fetch with: %v", t.id, err)
t.logger.Error(formatError.Error())
merr = multierror.Append(merr, fmt.Errorf("agent (%s) failed to fetch with: %v", t.id, formatError))
merr = multierror.Append(merr, fmt.Errorf("requester (%s) failed to fetch with: %v", t.id, formatError))
}

lock.Lock()
defer lock.Unlock()
domains = append(domains, domainsAgent...)
}()
}
wg.Wait()

domains = slices.DeleteFunc(domains, func(item *types.DomainRequest) bool {
if item.IsIP() {
return true
}
return false
})

for _, domain := range domains {
domain.Requester = t
}
Expand Down
13 changes: 8 additions & 5 deletions apps/agent/requester/traefik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
"testing"
)

func Test_agent_ID(t *testing.T) {
func Test_traefik_ID(t *testing.T) {
want := "foo"
instance := &traefik{id: want}
assert.Equalf(t, want, instance.ID(), "ID()")
}

func Test_createAgentProvider(t *testing.T) {
func Test_createTraefikV2Provider(t *testing.T) {
ctx := context.TestContext(nil)
want := &traefik{id: "foo", logger: ctx.Logger}
want.addresses = []string{"127.0.0.1:80", "127.0.0.1:81"}
Expand Down Expand Up @@ -80,7 +80,7 @@ func Test_createAgentProvider(t *testing.T) {
}
}

func Test_agent_FetchAgent(t *testing.T) {
func Test_traefik_FetchIntance(t *testing.T) {
ctrl := gomock.NewController(t)
a := &traefik{}
want := []types.DomainRequest{
Expand Down Expand Up @@ -155,7 +155,7 @@ func Test_agent_FetchAgent(t *testing.T) {
}
}

func Test_agent_Fetch(t *testing.T) {
func Test_traefik_Fetch(t *testing.T) {
ctx := context.TestContext(nil)
ctrl := gomock.NewController(t)
a := &traefik{addresses: []string{"127.0.0.1:80", "127.0.0.1:81"}, logger: ctx.Logger}
Expand All @@ -179,7 +179,10 @@ func Test_agent_Fetch(t *testing.T) {
funcMock: func(clientHttp *mockHttp.MockClient) {
resp1 := fasthttp.Response{}
resp1.SetStatusCode(http.StatusOK)
body, _ := json.Marshal([]traefikConfigDynamic.Router{{Rule: "Host(`foo.com`)", TLS: &traefikConfigDynamic.RouterTLSConfig{}}})
body, _ := json.Marshal([]traefikConfigDynamic.Router{
{Rule: "Host(`foo.com`)", TLS: &traefikConfigDynamic.RouterTLSConfig{}},
{Rule: "Host(`127.0.0.1`)", TLS: &traefikConfigDynamic.RouterTLSConfig{}},
})
resp1.SetBody(body)

resp2 := fasthttp.Response{}
Expand Down
4 changes: 2 additions & 2 deletions apps/server/requester/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func (a *agent) Fetch() ([]*types.DomainRequest, error) {
defer wg.Done()
domainsAgent, err := a.FetchAgent(address)
if err != nil {
formatError := fmt.Errorf("agent (%s) failed to fetch with: %v", a.id, err)
formatError := fmt.Errorf("requester (%s) failed to fetch with: %v", a.id, err)
a.logger.Error(formatError.Error())
merr = multierror.Append(merr, fmt.Errorf("agent (%s) failed to fetch with: %v", a.id, formatError))
merr = multierror.Append(merr, fmt.Errorf("requester (%s) failed to fetch with: %v", a.id, formatError))
}
lock.Lock()
defer lock.Unlock()
Expand Down
8 changes: 8 additions & 0 deletions requester/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/alexandreh2ag/lets-go-tls/types"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"slices"
)

const (
Expand Down Expand Up @@ -56,5 +57,12 @@ func createStaticProvider(_ context.Context, cfg config.RequesterConfig) (types.
}
}

instance.domainRequests = slices.DeleteFunc(instance.domainRequests, func(item *types.DomainRequest) bool {
if item.IsIP() {
return true
}
return false
})

return instance, nil
}
2 changes: 1 addition & 1 deletion requester/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Test_createStaticProvider(t *testing.T) {
name: "Success",
cfg: config.RequesterConfig{
Id: "foo",
Config: map[string]interface{}{"domains": [][]string{{"foo.com"}}},
Config: map[string]interface{}{"domains": [][]string{{"foo.com"}, {"127.0.0.1"}}},
},
want: want,
wantErr: false,
Expand Down
11 changes: 11 additions & 0 deletions types/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"cmp"
"net"
"slices"
"strings"
)
Expand Down Expand Up @@ -52,6 +53,16 @@ type DomainRequest struct {
Requester Requester `json:"-"`
}

func (dr DomainRequest) IsIP() bool {
for _, domain := range dr.Domains {
ip := net.ParseIP(string(domain))
if ip != nil {
return true
}
}
return false
}

func SortDomainsRequests(domainsRequests []*DomainRequest) {
slices.SortFunc(domainsRequests, func(a, b *DomainRequest) int {
a.Domains.Sort()
Expand Down
43 changes: 43 additions & 0 deletions types/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,46 @@ func TestDomains_ContainsWildcard(t *testing.T) {
})
}
}

func TestDomainRequest_IsIP(t *testing.T) {

tests := []struct {
name string
Domains Domains
want bool
}{
{
name: "isIpPrivateV4",
Domains: Domains{"127.0.0.1"},
want: true,
},
{
name: "isIpPublicV4",
Domains: Domains{"8.8.8.8"},
want: true,
},
{
name: "isIpPrivateV6",
Domains: Domains{"::1"},
want: true,
},
{
name: "isIpPublicV6",
Domains: Domains{"2001:4860:4860::8888"},
want: true,
},
{
name: "isDomain",
Domains: Domains{"example.com"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dr := DomainRequest{
Domains: tt.Domains,
}
assert.Equalf(t, tt.want, dr.IsIP(), "IsIP()")
})
}
}

0 comments on commit ae133e6

Please sign in to comment.