-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented blocklist for URLs and IP addresses.
- Loading branch information
Bartlomiej Mika
committed
Aug 13, 2024
1 parent
a405fbd
commit 9d843bf
Showing
7 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package blacklist | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// Provider provides an interface for abstracting time. | ||
type Provider interface { | ||
IsBannedIPAddress(ipAddress string) bool | ||
IsBannedURL(url string) bool | ||
} | ||
|
||
type blacklistProvider struct { | ||
bannedIPAddresses map[string]bool | ||
bannedURLs map[string]bool | ||
} | ||
|
||
// readBlacklistFileContent reads the contents of the blacklist file and returns | ||
// the list of banned items (ex: IP, URLs, etc). | ||
func readBlacklistFileContent(filePath string) ([]string, error) { | ||
// Check if the file exists | ||
if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("file %s does not exist", filePath) | ||
} | ||
|
||
// Read the file contents | ||
data, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file %s: %v", filePath, err) | ||
} | ||
|
||
// Parse the JSON content as a list of IPs | ||
var ips []string | ||
if err := json.Unmarshal(data, &ips); err != nil { | ||
return nil, fmt.Errorf("failed to parse JSON file %s: %v", filePath, err) | ||
} | ||
|
||
return ips, nil | ||
} | ||
|
||
// NewProvider Provider contructor that returns the default time provider. | ||
func NewProvider() Provider { | ||
bannedIPAddresses := make(map[string]bool) | ||
bannedIPAddressesFilePath := "static/blacklist/ips.json" | ||
ips, err := readBlacklistFileContent(bannedIPAddressesFilePath) | ||
if err == nil { // Aka: if the file exists... | ||
for _, ip := range ips { | ||
bannedIPAddresses[ip] = true | ||
} | ||
} | ||
|
||
bannedURLs := make(map[string]bool) | ||
bannedURLsFilePath := "static/blacklist/urls.json" | ||
urls, err := readBlacklistFileContent(bannedURLsFilePath) | ||
if err == nil { // Aka: if the file exists... | ||
for _, url := range urls { | ||
bannedURLs[url] = true | ||
} | ||
} | ||
|
||
return blacklistProvider{ | ||
bannedIPAddresses: bannedIPAddresses, | ||
bannedURLs: bannedURLs, | ||
} | ||
} | ||
|
||
func (p blacklistProvider) IsBannedIPAddress(ipAddress string) bool { | ||
return p.bannedIPAddresses[ipAddress] | ||
} | ||
|
||
func (p blacklistProvider) IsBannedURL(url string) bool { | ||
return p.bannedURLs[url] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
This file is empty, but if you would like to add ip addresses to ban then do the following: | ||
|
||
1. Create a new file called `ips.json`. | ||
|
||
2. Open that file and your ip addresses using this JSON style: ``{"192.168.9.1","192.168.9.2","192.168.9.3"}`` | ||
|
||
3. Rebuild your docker container and deploy. You are done. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.