fix blocked domains search/list
This commit is contained in:
135
handlers.go
135
handlers.go
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -158,7 +157,14 @@ func mfaSetupHandler(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) {
|
||||
@@ -304,109 +310,64 @@ func urlListsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func domainsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
action := r.FormValue("action")
|
||||
domain := sanitizeInput(r.FormValue("domain"))
|
||||
|
||||
configMu.RLock()
|
||||
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
|
||||
pageData := map[string]interface{}{
|
||||
"Domains": []string{},
|
||||
"Query": "",
|
||||
"Notification": "",
|
||||
"NotificationType": "",
|
||||
}
|
||||
|
||||
renderTemplate(w, r, "domains.html", pageData)
|
||||
}
|
||||
|
||||
func searchDomainsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
configMu.RLock()
|
||||
blocklistFile := config.BlocklistFile
|
||||
configMu.RUnlock()
|
||||
|
||||
query := r.URL.Query().Get("query")
|
||||
domains, _ := readLines(blocklistFile)
|
||||
query := strings.TrimSpace(r.FormValue("query"))
|
||||
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
|
||||
if query != "" {
|
||||
query = strings.ReplaceAll(query, "*", ".*")
|
||||
re, err := regexp.Compile("(?i)" + query)
|
||||
if strings.Contains(query, "*") {
|
||||
// Wildcard search
|
||||
pattern := strings.ReplaceAll(regexp.QuoteMeta(query), "\\*", ".*")
|
||||
re, err := regexp.Compile("(?i)^" + pattern + "$")
|
||||
if err == nil {
|
||||
for _, d := range domains {
|
||||
if re.MatchString(d) {
|
||||
filtered = append(filtered, d)
|
||||
if len(filtered) >= 100 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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 {
|
||||
Domains []string
|
||||
Query string
|
||||
}{
|
||||
Domains: filtered,
|
||||
Query: query,
|
||||
}
|
||||
renderTemplate(w, r, "domains.html", data)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"domains": filtered})
|
||||
}
|
||||
|
||||
func applyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
1
main.go
1
main.go
@@ -65,6 +65,7 @@ func main() {
|
||||
// Protected routes
|
||||
r.HandleFunc("/profile", authMiddleware(profileHandler)).Methods("GET", "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("/urllists", authMiddleware(urlListsHandler)).Methods("GET", "POST")
|
||||
r.HandleFunc("/domains", authMiddleware(domainsHandler)).Methods("GET", "POST")
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -8,13 +8,13 @@
|
||||
</head>
|
||||
<body class="bg-gray-900 text-gray-100 min-h-screen flex flex-col">
|
||||
{{ 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">
|
||||
<a href="/" class="text-xl font-bold">{{ .AppName }}</a>
|
||||
{{ if .Authenticated }}
|
||||
<ul class="flex space-x-4">
|
||||
<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="/profile">Profile</a></li>
|
||||
<li><a href="/logout">Logout</a></li>
|
||||
@@ -22,7 +22,7 @@
|
||||
{{ end }}
|
||||
</nav>
|
||||
</header>
|
||||
<main class="flex-grow container mx-auto p-4">
|
||||
<main class="flex-grow container mx-auto p-4 pt-20">
|
||||
{{ block "content" . }}{{ end }}
|
||||
</main>
|
||||
<footer class="bg-gray-800 p-4 text-center">
|
||||
|
||||
@@ -1,32 +1,48 @@
|
||||
{{ define "content" }}
|
||||
<div class="max-w-4xl mx-auto">
|
||||
<h1 class="text-3xl font-bold mb-6">Domains</h1>
|
||||
<form method="GET" class="mb-6 bg-gray-800 p-4 rounded">
|
||||
<h1 class="text-3xl font-bold mb-6">Blocked Domains</h1>
|
||||
<div class="mb-6 bg-gray-800 p-4 rounded">
|
||||
<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">
|
||||
<button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white font-bold px-4 py-2 rounded transition">Search</button>
|
||||
<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="button" onclick="searchDomains()" class="bg-blue-600 hover:bg-blue-700 text-white font-bold px-4 py-2 rounded transition">Search</button>
|
||||
</div>
|
||||
</form>
|
||||
<form method="POST" class="mb-6 bg-gray-800 p-4 rounded">
|
||||
<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 id="results"></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 }}
|
||||
Reference in New Issue
Block a user