-
Notifications
You must be signed in to change notification settings - Fork 0
Adsblock to master #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c42cb54
feat: Implement adblock functionality with Rust core, dynamic filter …
vkhangstack 2a5914c
feat: Implement cursor-based pagination with search and filtering for…
vkhangstack 41b0273
feat: Consolidate Linux package building into a single script, add de…
vkhangstack e28750d
feat: Implement adblock engine with Rust library and proxy server, in…
vkhangstack a25f9a5
feat: add Traffic page to display network logs, system connections, a…
vkhangstack 35488e0
feat: Implement SOCKS5 proxy server with adblocking, custom rules, an…
vkhangstack 9fd64d1
feat: add NSIS installer script for Windows builds.
vkhangstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,13 +1,18 @@ | ||||||
| package main | ||||||
|
|
||||||
| import ( | ||||||
| "bufio" | ||||||
| "context" | ||||||
| "encoding/json" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "log" | ||||||
| "net/http" | ||||||
| "os" | ||||||
| "path/filepath" | ||||||
| "strconv" | ||||||
| "strings" | ||||||
| "sync" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/vkhangstack/Custos/internal/system" | ||||||
|
|
@@ -34,6 +39,8 @@ type App struct { | |||||
| proxyServer *proxy.Server | ||||||
| dnsServer *dns.Server | ||||||
| systemTracker *system.Tracker | ||||||
| blocklist *core.BlocklistManager | ||||||
| refreshMu sync.Mutex | ||||||
| } | ||||||
|
|
||||||
| // NewApp creates a new App application struct | ||||||
|
|
@@ -79,6 +86,7 @@ func NewApp() *App { | |||||
| proxyServer: proxy.NewServer(s, bm, systemTracker, port), | ||||||
| dnsServer: dns.NewServer(s, bm, 5353), | ||||||
| systemTracker: systemTracker, | ||||||
| blocklist: bm, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -96,7 +104,6 @@ func (a *App) startup(ctx context.Context) { | |||||
| // fmt.Printf("Failed to set system proxy on startup: %v\n", err) | ||||||
| // } | ||||||
|
|
||||||
| // Restore Protection State | ||||||
| if enabled := a.GetProtectionStatus(); enabled { | ||||||
| a.proxyServer.SetProtection(true) | ||||||
| a.SetSystemProxy(true) | ||||||
|
|
@@ -105,6 +112,19 @@ func (a *App) startup(ctx context.Context) { | |||||
| a.SetSystemProxy(false) | ||||||
| } | ||||||
|
|
||||||
| // Restore Adblock State | ||||||
| if enabled := a.GetAdblockStatus(); enabled { | ||||||
| a.proxyServer.SetAdblockEnabled(true) | ||||||
| } else { | ||||||
| a.proxyServer.SetAdblockEnabled(false) | ||||||
| } | ||||||
|
|
||||||
| // Seed and Refresh Filters | ||||||
| go func() { | ||||||
| a.seedFilters() | ||||||
| a.RefreshAdblockFilters() | ||||||
| }() | ||||||
|
|
||||||
| // Start a ticker to emit logs to frontend | ||||||
| go a.broadcastLogs() | ||||||
| } | ||||||
|
|
@@ -116,6 +136,7 @@ func (a *App) shutdown(ctx context.Context) { | |||||
| fmt.Printf("Failed to disable system proxy on shutdown: %v\n", err) | ||||||
| } | ||||||
| a.proxyServer.Stop() | ||||||
| ctx.Done() | ||||||
| } | ||||||
|
|
||||||
| // broadcastLogs sends new logs to frontend events | ||||||
|
|
@@ -133,6 +154,20 @@ func (a *App) GetLogs() []core.LogEntry { | |||||
| return a.store.GetRecentLogs(50) | ||||||
| } | ||||||
|
|
||||||
| // GetLogsPaginated returns paginated logs for the frontend | ||||||
| func (a *App) GetLogsPaginated(cursor string, limit int, search, status, logType string) core.PaginatedLogs { | ||||||
| logs, nextCursor, hasMore, total, err := a.store.GetLogsPaginated(cursor, limit, search, status, logType) | ||||||
| if err != nil { | ||||||
| return core.PaginatedLogs{Logs: []core.LogEntry{}, Total: 0} | ||||||
| } | ||||||
| return core.PaginatedLogs{ | ||||||
| Logs: logs, | ||||||
| NextCursor: nextCursor, | ||||||
| HasMore: hasMore, | ||||||
| Total: total, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // GetStats returns current stats | ||||||
| func (a *App) GetStats() core.Stats { | ||||||
| return a.store.GetStats() | ||||||
|
|
@@ -174,6 +209,27 @@ func (a *App) GetProtectionStatus() bool { | |||||
| return val == "true" | ||||||
| } | ||||||
|
|
||||||
| // EnableAdblock toggles the adblock engine | ||||||
| func (a *App) EnableAdblock(enabled bool) { | ||||||
| a.proxyServer.SetAdblockEnabled(enabled) | ||||||
| // Persist | ||||||
| val := "false" | ||||||
| if enabled { | ||||||
| val = "true" | ||||||
| } | ||||||
| a.store.SetSetting("adblock_enabled", val) | ||||||
| } | ||||||
|
|
||||||
| // GetAdblockStatus returns the current status | ||||||
| func (a *App) GetAdblockStatus() bool { | ||||||
| val, err := a.store.GetSetting("adblock_enabled") | ||||||
| if err != nil || val == "" { | ||||||
| // Default to enabled if not set | ||||||
| return true | ||||||
| } | ||||||
| return val == "true" | ||||||
| } | ||||||
|
|
||||||
| // GetChartData returns historical traffic data for the chart | ||||||
| func (a *App) GetChartData(durationStr string) []core.TrafficDataPoint { | ||||||
| // Parse duration | ||||||
|
|
@@ -279,9 +335,10 @@ func (a *App) GetAppInfo() *AppInfo { | |||||
|
|
||||||
| // AppSettings defines configurable settings | ||||||
| type AppSettings struct { | ||||||
| Port int `json:"port"` | ||||||
| Notifications bool `json:"notifications"` | ||||||
| AutoStart bool `json:"auto_start"` | ||||||
| Port int `json:"port"` | ||||||
| Notifications bool `json:"notifications"` | ||||||
| AutoStart bool `json:"auto_start"` | ||||||
| AdblockEnabled bool `json:"adblock_enabled"` | ||||||
| } | ||||||
|
|
||||||
| // GetAppSettings returns current settings | ||||||
|
|
@@ -299,10 +356,14 @@ func (a *App) GetAppSettings() AppSettings { | |||||
| // AutoStart | ||||||
| autoStart := a.GetStartupStatus() | ||||||
|
|
||||||
| // Adblock | ||||||
| adblockEnabled := a.GetAdblockStatus() | ||||||
|
|
||||||
| return AppSettings{ | ||||||
| Port: port, | ||||||
| Notifications: notifications, | ||||||
| AutoStart: autoStart, | ||||||
| Port: port, | ||||||
| Notifications: notifications, | ||||||
| AutoStart: autoStart, | ||||||
| AdblockEnabled: adblockEnabled, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -337,5 +398,223 @@ func (a *App) SaveAppSettings(settings AppSettings) error { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Adblock | ||||||
| a.EnableAdblock(settings.AdblockEnabled) | ||||||
|
|
||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| // Adblock Filter Management | ||||||
|
|
||||||
| func (a *App) GetAdblockFilters() []core.AdblockFilter { | ||||||
| return a.store.GetAdblockFilters() | ||||||
| } | ||||||
|
|
||||||
| func (a *App) AddAdblockFilter(name, url string) error { | ||||||
| filter := core.AdblockFilter{ | ||||||
| ID: utils.GenerateIDString(), | ||||||
| Name: name, | ||||||
| URL: url, | ||||||
| Enabled: true, | ||||||
| } | ||||||
| err := a.store.AddAdblockFilter(filter) | ||||||
| if err == nil { | ||||||
| go a.RefreshAdblockFilters() | ||||||
| } | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| func (a *App) DeleteAdblockFilter(id string) error { | ||||||
| err := a.store.DeleteAdblockFilter(id) | ||||||
| if err == nil { | ||||||
| go a.RefreshAdblockFilters() | ||||||
| } | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| func (a *App) ToggleAdblockFilter(id string, enabled bool) error { | ||||||
| filters := a.store.GetAdblockFilters() | ||||||
| for _, f := range filters { | ||||||
| if f.ID == id { | ||||||
| f.Enabled = enabled | ||||||
| err := a.store.UpdateAdblockFilter(f) | ||||||
| if err == nil { | ||||||
| go a.RefreshAdblockFilters() | ||||||
| } | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| return fmt.Errorf("filter not found") | ||||||
| } | ||||||
|
|
||||||
| func (a *App) RefreshAdblockFilters() error { | ||||||
| a.refreshMu.Lock() | ||||||
| defer a.refreshMu.Unlock() | ||||||
|
|
||||||
| filters := a.store.GetAdblockFilters() | ||||||
| var allRules strings.Builder | ||||||
| var blocklistSources []string | ||||||
| // Always include the default hosts list as a base for the blocklist | ||||||
| blocklistSources = append(blocklistSources, "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts") | ||||||
|
|
||||||
| // Default hardcoded rules for adblock engine | ||||||
| allRules.WriteString(`||ads.google.com^ | ||||||
| ||doubleclick.net^ | ||||||
| ||adnxs.com^ | ||||||
| ||googleadservices.com^ | ||||||
| ||pagead2.googlesyndication.com^ | ||||||
| ||analytics.google.com^ | ||||||
| ||facebook.com/tr/^ | ||||||
| `) | ||||||
|
|
||||||
| for _, f := range filters { | ||||||
| if !f.Enabled { | ||||||
| continue | ||||||
| } | ||||||
|
|
||||||
| // Add to blocklist sources | ||||||
| homeDir, _ := os.UserHomeDir() | ||||||
| filterDir := filepath.Join(homeDir, ".custos", "filters") | ||||||
| filePath := filepath.Join(filterDir, f.ID+".txt") | ||||||
|
|
||||||
| if _, err := os.Stat(filePath); err == nil { | ||||||
| blocklistSources = append(blocklistSources, filePath) | ||||||
| } else if f.URL != "" { | ||||||
| blocklistSources = append(blocklistSources, f.URL) | ||||||
| } | ||||||
|
|
||||||
| content, err := a.getFilterContent(f) | ||||||
| if err == nil { | ||||||
| allRules.WriteString("\n") | ||||||
| allRules.WriteString(a.normalizeFilterRules(content)) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Update and reload adblock engine | ||||||
| a.proxyServer.ReloadAdblockEngine(allRules.String()) | ||||||
|
|
||||||
| // Update and reload blocklist | ||||||
| if a.blocklist != nil { | ||||||
| a.blocklist.SetSources(blocklistSources) | ||||||
| a.blocklist.Load() | ||||||
| } | ||||||
|
|
||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func (a *App) getFilterContent(f core.AdblockFilter) (string, error) { | ||||||
| homeDir, _ := os.UserHomeDir() | ||||||
| filterDir := filepath.Join(homeDir, ".custos", "filters") | ||||||
| os.MkdirAll(filterDir, 0755) | ||||||
| filePath := filepath.Join(filterDir, f.ID+".txt") | ||||||
|
|
||||||
| if f.URL == "" { | ||||||
| return "", nil | ||||||
| } | ||||||
|
|
||||||
| // Check if file exists and is less than 24h old | ||||||
| info, err := os.Stat(filePath) | ||||||
| if err == nil && time.Since(info.ModTime()) < 24*time.Hour { | ||||||
| content, err := os.ReadFile(filePath) | ||||||
| if err == nil { | ||||||
| return string(content), nil | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Download | ||||||
| log.Printf("Downloading adblock filter: %s from %s", f.Name, f.URL) | ||||||
| resp, err := http.Get(f.URL) | ||||||
| if err != nil { | ||||||
| return "", err | ||||||
| } | ||||||
| defer resp.Body.Close() | ||||||
|
|
||||||
| content, err := io.ReadAll(resp.Body) | ||||||
| if err != nil { | ||||||
| return "", err | ||||||
| } | ||||||
|
|
||||||
| os.WriteFile(filePath, content, 0644) | ||||||
|
|
||||||
| f.LastUpdated = time.Now() | ||||||
| a.store.UpdateAdblockFilter(f) | ||||||
|
|
||||||
| return string(content), nil | ||||||
| } | ||||||
|
|
||||||
| func (a *App) seedFilters() { | ||||||
| // Truncate before seeding as requested | ||||||
| a.store.ClearAdblockFilters() | ||||||
|
|
||||||
|
Comment on lines
+546
to
+548
|
||||||
| // Truncate before seeding as requested | |
| a.store.ClearAdblockFilters() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling ctx.Done() has no effect as it only returns a channel. If the intention is to cancel the context or wait for cancellation, this line should be removed or replaced with appropriate context cancellation logic.