Skip to content

Commit

Permalink
allow blocking of all subdomains in domain
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Feb 14, 2024
1 parent 3335255 commit 1390540
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
18 changes: 15 additions & 3 deletions fed/blocklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -134,10 +135,21 @@ func NewBlockList(log *slog.Logger, path string) (*BlockList, error) {

// Contains determines if a domain is blocked.
func (b *BlockList) Contains(domain string) bool {
domain = strings.Trim(domain, ".")

b.lock.Lock()
_, contains := b.domains[domain]
b.lock.Unlock()
return contains
defer b.lock.Unlock()

for {
if _, contains := b.domains[domain]; contains {
return true
}
if i := strings.IndexRune(domain, '.'); i == -1 {
return false
} else {
domain = domain[i+1:]
}
}
}

// Close frees resources.
Expand Down
88 changes: 88 additions & 0 deletions fed/blocklist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2024 Dima Krasner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fed

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestBlockList_NotBlockedDomain(t *testing.T) {
assert := assert.New(t)

blockList := BlockList{}
blockList.domains = map[string]struct{}{
"0.0.0.0.com": struct{}{},
}

assert.False(blockList.Contains("127.0.0.1.com"))
}

func TestBlockList_BlockedDomain(t *testing.T) {
assert := assert.New(t)

blockList := BlockList{}
blockList.domains = map[string]struct{}{
"0.0.0.0.com": struct{}{},
}

assert.True(blockList.Contains("0.0.0.0.com"))
}

func TestBlockList_BlockedSubdomain(t *testing.T) {
assert := assert.New(t)

blockList := BlockList{}
blockList.domains = map[string]struct{}{
"social.0.0.0.0.com": struct{}{},
}

assert.True(blockList.Contains("social.0.0.0.0.com"))
}

func TestBlockList_NotBlockedSubdomain(t *testing.T) {
assert := assert.New(t)

blockList := BlockList{}
blockList.domains = map[string]struct{}{
"social.0.0.0.0.com": struct{}{},
}

assert.False(blockList.Contains("blog.0.0.0.0.com"))
}

func TestBlockList_BlockedSubdomainByDomain(t *testing.T) {
assert := assert.New(t)

blockList := BlockList{}
blockList.domains = map[string]struct{}{
"0.0.0.0.com": struct{}{},
}

assert.True(blockList.Contains("social.0.0.0.0.com"))
}

func TestBlockList_BlockedSubdomainByDomainEndsWithDot(t *testing.T) {
assert := assert.New(t)

blockList := BlockList{}
blockList.domains = map[string]struct{}{
"0.0.0.0.com": struct{}{},
}

assert.True(blockList.Contains("social.0.0.0.0.com."))
}

0 comments on commit 1390540

Please sign in to comment.