This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
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.
- Loading branch information
Showing
10 changed files
with
561 additions
and
207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package config | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
mutex sync.RWMutex | ||
cachedData []byte | ||
lastUpdated time.Time | ||
cacheFile = Config.CachePath+"cached_data.json" // Name of the file to store cached data | ||
) | ||
|
||
func init() { | ||
// Load cached data from file when the package is initialized | ||
loadCachedDataFromFile() | ||
} | ||
|
||
func loadCachedDataFromFile() { | ||
// Check if the cache file exists | ||
if _, err := os.Stat(cacheFile); os.IsNotExist(err) { | ||
return // Cache file does not exist, no data to load | ||
} | ||
|
||
// Read cached data from file | ||
data, err := ioutil.ReadFile(cacheFile) | ||
if err != nil { | ||
// Error reading cache file | ||
// Log the error or handle it accordingly | ||
return | ||
} | ||
|
||
// Update cached data and lastUpdated time | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
cachedData = data | ||
lastUpdated = time.Now() | ||
} | ||
|
||
func SetCachedData(data []byte) { | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
cachedData = data | ||
lastUpdated = time.Now() | ||
|
||
// Save cached data to file | ||
err := ioutil.WriteFile(cacheFile, data, 0644) | ||
if err != nil { | ||
// Error saving cache file | ||
// Log the error or handle it accordingly | ||
} | ||
} | ||
|
||
func GetCachedData() []byte { | ||
mutex.RLock() | ||
defer mutex.RUnlock() | ||
return cachedData | ||
} | ||
|
||
func NeedToUpdateCache() bool { | ||
// Update cache every hour | ||
return time.Since(lastUpdated) >= time.Hour | ||
} |
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,61 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"strings" | ||
"fmt" | ||
) | ||
|
||
// checks if the given string is a valid IP address. | ||
func IsValidIPAddress(ip string) bool { | ||
return net.ParseIP(ip) != nil | ||
} | ||
|
||
// checks if the given string is a valid domain name. | ||
func IsValidDomain(domain string) bool { | ||
parts := strings.Split(domain, ".") | ||
for _, part := range parts { | ||
if len(part) == 0 { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// isAddressValid checks if the address is a valid IP address or a domain. | ||
// If it's a domain, it resolves it to its public IP address and compares with the provided value. | ||
func IsAddressValid(address, value string) bool { | ||
if IsValidIPAddress(address) { | ||
log.Printf("Valid IP address: %s", address) | ||
return address == value | ||
} | ||
if IsValidDomain(address) { | ||
log.Printf("Valid domain: %s", address) | ||
ipAddr, err := GetPublicIP(address) | ||
if err != nil { | ||
log.Printf("Error resolving domain: %s", err) | ||
return false | ||
} | ||
log.Printf("Resolved IP address for domain %s: %s", address, ipAddr) | ||
return ipAddr == value | ||
} | ||
log.Printf("Invalid address format: %s", address) | ||
return false | ||
} | ||
|
||
func GetPublicIP(domain string) (string, error) { | ||
addrs, err := net.LookupIP(domain) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Find the IPv4 address | ||
for _, addr := range addrs { | ||
if ipv4 := addr.To4(); ipv4 != nil { | ||
return ipv4.String(), nil | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("no IPv4 address found for domain %s", domain) | ||
} |
Oops, something went wrong.