fix blocked domains search/list
This commit is contained in:
+48
-87
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -158,7 +157,14 @@ func mfaSetupHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
func dashboardHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
renderTemplate(w, r, "dashboard.html", nil)
|
pageData := map[string]interface{}{
|
||||||
|
"Domains": []string{},
|
||||||
|
"Query": "",
|
||||||
|
"Notification": "",
|
||||||
|
"NotificationType": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
renderTemplate(w, r, "domains.html", pageData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlListsHandler(w http.ResponseWriter, r *http.Request) {
|
func urlListsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -304,109 +310,64 @@ func urlListsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func domainsHandler(w http.ResponseWriter, r *http.Request) {
|
func domainsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
pageData := map[string]interface{}{
|
||||||
action := r.FormValue("action")
|
"Domains": []string{},
|
||||||
domain := sanitizeInput(r.FormValue("domain"))
|
"Query": "",
|
||||||
|
"Notification": "",
|
||||||
configMu.RLock()
|
"NotificationType": "",
|
||||||
blocklistFile := config.BlocklistFile
|
|
||||||
configMu.RUnlock()
|
|
||||||
|
|
||||||
domains, err := readLines(blocklistFile)
|
|
||||||
if err != nil {
|
|
||||||
renderTemplate(w, r, "domains.html", struct {
|
|
||||||
Domains []string
|
|
||||||
Query string
|
|
||||||
Notification string
|
|
||||||
NotificationType string
|
|
||||||
}{
|
|
||||||
Domains: domains,
|
|
||||||
Query: "",
|
|
||||||
Notification: "Error reading domains",
|
|
||||||
NotificationType: "error",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
errorMsg := ""
|
|
||||||
switch action {
|
|
||||||
case "add":
|
|
||||||
if !isValidDomain(domain) {
|
|
||||||
errorMsg = "Invalid domain format"
|
|
||||||
} else {
|
|
||||||
domains = append(domains, domain)
|
|
||||||
sort.Strings(domains)
|
|
||||||
}
|
|
||||||
case "remove":
|
|
||||||
for i, d := range domains {
|
|
||||||
if d == domain {
|
|
||||||
domains = append(domains[:i], domains[i+1:]...)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if errorMsg != "" {
|
|
||||||
renderTemplate(w, r, "domains.html", struct {
|
|
||||||
Domains []string
|
|
||||||
Query string
|
|
||||||
Notification string
|
|
||||||
NotificationType string
|
|
||||||
}{
|
|
||||||
Domains: domains,
|
|
||||||
Query: "",
|
|
||||||
Notification: errorMsg,
|
|
||||||
NotificationType: "error",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writeLines(blocklistFile, domains)
|
|
||||||
|
|
||||||
renderTemplate(w, r, "domains.html", struct {
|
|
||||||
Domains []string
|
|
||||||
Query string
|
|
||||||
Notification string
|
|
||||||
NotificationType string
|
|
||||||
}{
|
|
||||||
Domains: domains,
|
|
||||||
Query: "",
|
|
||||||
Notification: "Domain updated successfully",
|
|
||||||
NotificationType: "success",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderTemplate(w, r, "domains.html", pageData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func searchDomainsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
configMu.RLock()
|
configMu.RLock()
|
||||||
blocklistFile := config.BlocklistFile
|
blocklistFile := config.BlocklistFile
|
||||||
configMu.RUnlock()
|
configMu.RUnlock()
|
||||||
|
|
||||||
query := r.URL.Query().Get("query")
|
query := strings.TrimSpace(r.FormValue("query"))
|
||||||
domains, _ := readLines(blocklistFile)
|
if query == "" {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]interface{}{"domains": []string{}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
domains, err := readLines(blocklistFile)
|
||||||
|
if err != nil {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]interface{}{"domains": []string{}})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var filtered []string
|
var filtered []string
|
||||||
if query != "" {
|
if strings.Contains(query, "*") {
|
||||||
query = strings.ReplaceAll(query, "*", ".*")
|
// Wildcard search
|
||||||
re, err := regexp.Compile("(?i)" + query)
|
pattern := strings.ReplaceAll(regexp.QuoteMeta(query), "\\*", ".*")
|
||||||
|
re, err := regexp.Compile("(?i)^" + pattern + "$")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, d := range domains {
|
for _, d := range domains {
|
||||||
if re.MatchString(d) {
|
if re.MatchString(d) {
|
||||||
filtered = append(filtered, d)
|
filtered = append(filtered, d)
|
||||||
|
if len(filtered) >= 100 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
filtered = domains
|
// Exact match, case insensitive
|
||||||
|
for _, d := range domains {
|
||||||
|
if strings.EqualFold(d, query) {
|
||||||
|
filtered = append(filtered, d)
|
||||||
|
if len(filtered) >= 100 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data := struct {
|
w.Header().Set("Content-Type", "application/json")
|
||||||
Domains []string
|
json.NewEncoder(w).Encode(map[string]interface{}{"domains": filtered})
|
||||||
Query string
|
|
||||||
}{
|
|
||||||
Domains: filtered,
|
|
||||||
Query: query,
|
|
||||||
}
|
|
||||||
renderTemplate(w, r, "domains.html", data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyHandler(w http.ResponseWriter, r *http.Request) {
|
func applyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ func main() {
|
|||||||
// Protected routes
|
// Protected routes
|
||||||
r.HandleFunc("/profile", authMiddleware(profileHandler)).Methods("GET", "POST")
|
r.HandleFunc("/profile", authMiddleware(profileHandler)).Methods("GET", "POST")
|
||||||
r.HandleFunc("/api/mfa-setup", authMiddleware(mfaSetupHandler)).Methods("POST")
|
r.HandleFunc("/api/mfa-setup", authMiddleware(mfaSetupHandler)).Methods("POST")
|
||||||
|
r.HandleFunc("/api/search-domains", authMiddleware(searchDomainsHandler)).Methods("POST")
|
||||||
r.HandleFunc("/", authMiddleware(dashboardHandler))
|
r.HandleFunc("/", authMiddleware(dashboardHandler))
|
||||||
r.HandleFunc("/urllists", authMiddleware(urlListsHandler)).Methods("GET", "POST")
|
r.HandleFunc("/urllists", authMiddleware(urlListsHandler)).Methods("GET", "POST")
|
||||||
r.HandleFunc("/domains", authMiddleware(domainsHandler)).Methods("GET", "POST")
|
r.HandleFunc("/domains", authMiddleware(domainsHandler)).Methods("GET", "POST")
|
||||||
|
|||||||
+1
-1
File diff suppressed because one or more lines are too long
+3
-3
@@ -8,13 +8,13 @@
|
|||||||
</head>
|
</head>
|
||||||
<body class="bg-gray-900 text-gray-100 min-h-screen flex flex-col">
|
<body class="bg-gray-900 text-gray-100 min-h-screen flex flex-col">
|
||||||
{{ template "notifications" . }}
|
{{ template "notifications" . }}
|
||||||
<header class="bg-gray-800 p-4">
|
<header class="bg-gray-800 p-4 fixed top-0 w-full z-10">
|
||||||
<nav class="flex justify-between">
|
<nav class="flex justify-between">
|
||||||
<a href="/" class="text-xl font-bold">{{ .AppName }}</a>
|
<a href="/" class="text-xl font-bold">{{ .AppName }}</a>
|
||||||
{{ if .Authenticated }}
|
{{ if .Authenticated }}
|
||||||
<ul class="flex space-x-4">
|
<ul class="flex space-x-4">
|
||||||
<li><a href="/urllists">URL Lists</a></li>
|
<li><a href="/urllists">URL Lists</a></li>
|
||||||
<li><a href="/domains">Domains</a></li>
|
<li><a href="/domains">Blocked Domains</a></li>
|
||||||
<li><a href="/logs">Logs</a></li>
|
<li><a href="/logs">Logs</a></li>
|
||||||
<li><a href="/profile">Profile</a></li>
|
<li><a href="/profile">Profile</a></li>
|
||||||
<li><a href="/logout">Logout</a></li>
|
<li><a href="/logout">Logout</a></li>
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
<main class="flex-grow container mx-auto p-4">
|
<main class="flex-grow container mx-auto p-4 pt-20">
|
||||||
{{ block "content" . }}{{ end }}
|
{{ block "content" . }}{{ end }}
|
||||||
</main>
|
</main>
|
||||||
<footer class="bg-gray-800 p-4 text-center">
|
<footer class="bg-gray-800 p-4 text-center">
|
||||||
|
|||||||
+42
-26
@@ -1,32 +1,48 @@
|
|||||||
{{ define "content" }}
|
{{ define "content" }}
|
||||||
<div class="max-w-4xl mx-auto">
|
<div class="max-w-4xl mx-auto">
|
||||||
<h1 class="text-3xl font-bold mb-6">Domains</h1>
|
<h1 class="text-3xl font-bold mb-6">Blocked Domains</h1>
|
||||||
<form method="GET" class="mb-6 bg-gray-800 p-4 rounded">
|
<div class="mb-6 bg-gray-800 p-4 rounded">
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
<input type="text" name="query" placeholder="Search domains (use * for wildcard)" value="{{ .PageData.Query }}" class="flex-1 p-2 bg-gray-700 rounded text-white placeholder-gray-400">
|
<input type="text" id="query" placeholder="Search domains exact match (use * as wildcard)" onkeydown="if(event.key === 'Enter') searchDomains()" class="flex-1 p-2 bg-gray-700 rounded text-white placeholder-gray-400">
|
||||||
<button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white font-bold px-4 py-2 rounded transition">Search</button>
|
<button type="button" onclick="searchDomains()" class="bg-blue-600 hover:bg-blue-700 text-white font-bold px-4 py-2 rounded transition">Search</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</div>
|
||||||
<form method="POST" class="mb-6 bg-gray-800 p-4 rounded">
|
<div id="results"></div>
|
||||||
<input type="hidden" name="csrf_token" value="{{ .CSRFToken }}">
|
|
||||||
<input type="hidden" name="action" value="add">
|
|
||||||
<div class="flex gap-2">
|
|
||||||
<input type="text" name="domain" placeholder="Add domain (e.g., ads.example.com)" class="flex-1 p-2 bg-gray-700 rounded text-white placeholder-gray-400">
|
|
||||||
<button type="submit" class="bg-green-600 hover:bg-green-700 text-white font-bold px-4 py-2 rounded transition">Add</button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
<ul class="space-y-2">
|
|
||||||
{{ range .PageData.Domains }}
|
|
||||||
<li class="flex justify-between items-center bg-gray-800 p-3 rounded">
|
|
||||||
<span class="text-sm text-gray-300 break-all">{{ . }}</span>
|
|
||||||
<form method="POST" class="inline ml-4">
|
|
||||||
<input type="hidden" name="csrf_token" value="{{ $.CSRFToken }}">
|
|
||||||
<input type="hidden" name="action" value="remove">
|
|
||||||
<input type="hidden" name="domain" value="{{ . }}">
|
|
||||||
<button type="submit" class="bg-red-600 hover:bg-red-700 text-white font-bold px-3 py-1 rounded text-sm transition">Remove</button>
|
|
||||||
</form>
|
|
||||||
</li>
|
|
||||||
{{ end }}
|
|
||||||
</ul>
|
|
||||||
</div>
|
</div>
|
||||||
|
<script>
|
||||||
|
function searchDomains() {
|
||||||
|
const query = document.getElementById('query').value.trim();
|
||||||
|
if (!query) {
|
||||||
|
document.getElementById('results').innerHTML = '<p class="text-gray-300">Please enter a search query.</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
document.getElementById('results').innerHTML = '<p class="text-gray-300">Searching...</p>';
|
||||||
|
fetch('/api/search-domains', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
'X-CSRF-Token': '{{ .CSRFToken }}'
|
||||||
|
},
|
||||||
|
body: 'query=' + encodeURIComponent(query)
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
let html = '';
|
||||||
|
if (data.domains && data.domains.length > 0) {
|
||||||
|
html = '<h2 class="text-xl font-bold mb-4">Results (' + data.domains.length + ' found)</h2>';
|
||||||
|
html += '<ul class="space-y-2 max-h-96 overflow-y-auto">';
|
||||||
|
data.domains.forEach(d => {
|
||||||
|
html += '<li class="bg-gray-800 p-3 rounded"><span class="text-sm text-gray-300 break-all">' + d + '</span></li>';
|
||||||
|
});
|
||||||
|
html += '</ul>';
|
||||||
|
} else {
|
||||||
|
html = '<p class="text-gray-300">No results found for \'' + query + '\'</p>';
|
||||||
|
}
|
||||||
|
document.getElementById('results').innerHTML = html;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
document.getElementById('results').innerHTML = '<p class="text-red-400">Error searching domains.</p>';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
Reference in New Issue
Block a user