|
/* |
|
USAGE: |
|
1. git clone this `cookie-cleaner.go` script |
|
2. change `dataSource` to your chromium broswer cookies sqlite file |
|
3. open `dataSource` file in sqlite3 shell, excuting below to export all access hosts: |
|
|
|
``` |
|
.output allowlist.txt |
|
select distinct host_key from cookies; |
|
.quit |
|
``` |
|
|
|
4. edit `allowlist.txt` exclude your unwanted site |
|
5. close your broswer before run it |
|
5. execute: |
|
|
|
```bash |
|
go mod init cookie-cleaner |
|
go get github.com/mattn/go-sqlite3 |
|
go run cookie-cleaner.go |
|
# or you can add it to system schedule using the built binary: |
|
# go build cookie-cleaner.go |
|
``` |
|
*/ |
|
|
|
package main |
|
|
|
import ( |
|
"bufio" |
|
"database/sql" |
|
_ "github.com/mattn/go-sqlite3" // Import go-sqlite3 library |
|
"log" |
|
"os" |
|
) |
|
|
|
const dataSource = "<YOUR_CHROMIUM_COOKIES_FILE_PATH>" |
|
|
|
const allowlistFilename = "allowlist.txt" |
|
|
|
func main() { |
|
var allowlist []string |
|
|
|
allowlistFileStat, e := os.Stat(allowlistFilename) |
|
if e != nil && os.IsNotExist(e) || allowlistFileStat.IsDir() { |
|
log.Fatal("no `" + allowlistFilename + "`, read the script USAGE !!") |
|
} |
|
|
|
allowlistFile, err := os.Open(allowlistFilename) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
defer allowlistFile.Close() |
|
|
|
scanner := bufio.NewScanner(allowlistFile) |
|
for scanner.Scan() { |
|
allowlist = append(allowlist, scanner.Text()) |
|
} |
|
|
|
db, err := sql.Open("sqlite3", dataSource) |
|
if err != nil { |
|
log.Fatal(err.Error()) |
|
} |
|
defer db.Close() |
|
|
|
_, _ = db.Exec("create temporary table allowlist(_ TEXT PRIMARY KEY)") |
|
|
|
addHostStat, _ := db.Prepare("insert into temp.allowlist values (?)") |
|
for _, host := range allowlist { |
|
_, _ = addHostStat.Exec(host) |
|
} |
|
|
|
// sql expression `not in` for allow list, change to 'in' for deny list |
|
_, _ = db.Exec("DELETE FROM cookies where host_key not in (SELECT * from temp.allowlist)") |
|
} |