cbefb93020
Implementa sistema completo de import/export y gestión de cookies. Incluye: - GetAllCookies() y FilterCookies() para búsqueda - ExportCookiesToFile() / ImportCookiesFromFile() en JSON y Netscape - DeleteCookiesByDomain() para limpieza - ListProfiles() para gestión de perfiles - Comando CLI cookies.go con subcomandos Formatos soportados: JSON estándar y Netscape cookies.txt Archivo: pkg/browser/profile_cookies.go, cmd/cookies.go
208 lines
5.5 KiB
Go
208 lines
5.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
|
|
"navegator/pkg/browser"
|
|
)
|
|
|
|
func main() {
|
|
// Subcomandos
|
|
listCmd := flag.NewFlagSet("list", flag.ExitOnError)
|
|
listURL := listCmd.String("url", "", "URL to navigate before listing cookies")
|
|
listDomain := listCmd.String("domain", "", "Filter by domain")
|
|
|
|
exportCmd := flag.NewFlagSet("export", flag.ExitOnError)
|
|
exportURL := exportCmd.String("url", "", "URL to navigate before exporting")
|
|
exportFile := exportCmd.String("output", "cookies.json", "Output file")
|
|
exportFormat := exportCmd.String("format", "json", "Format: json or netscape")
|
|
|
|
importCmd := flag.NewFlagSet("import", flag.ExitOnError)
|
|
importURL := importCmd.String("url", "", "URL to navigate before importing")
|
|
importFile := importCmd.String("input", "", "Input file (required)")
|
|
importFormat := importCmd.String("format", "json", "Format: json or netscape")
|
|
|
|
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
|
|
deleteURL := deleteCmd.String("url", "", "URL to navigate before deleting")
|
|
deleteDomain := deleteCmd.String("domain", "", "Domain to delete cookies from (required)")
|
|
|
|
profilesCmd := flag.NewFlagSet("profiles", flag.ExitOnError)
|
|
|
|
if len(flag.Args()) < 1 {
|
|
fmt.Println("Usage: cookies <command> [options]")
|
|
fmt.Println("\nCommands:")
|
|
fmt.Println(" list List cookies")
|
|
fmt.Println(" export Export cookies to file")
|
|
fmt.Println(" import Import cookies from file")
|
|
fmt.Println(" delete Delete cookies by domain")
|
|
fmt.Println(" profiles List available profiles")
|
|
return
|
|
}
|
|
|
|
command := flag.Args()[0]
|
|
|
|
ctx := context.Background()
|
|
|
|
switch command {
|
|
case "list":
|
|
listCmd.Parse(flag.Args()[1:])
|
|
listCookies(ctx, *listURL, *listDomain)
|
|
|
|
case "export":
|
|
exportCmd.Parse(flag.Args()[1:])
|
|
exportCookies(ctx, *exportURL, *exportFile, *exportFormat)
|
|
|
|
case "import":
|
|
importCmd.Parse(flag.Args()[1:])
|
|
if *importFile == "" {
|
|
log.Fatal("Error: -input is required")
|
|
}
|
|
importCookies(ctx, *importURL, *importFile, *importFormat)
|
|
|
|
case "delete":
|
|
deleteCmd.Parse(flag.Args()[1:])
|
|
if *deleteDomain == "" {
|
|
log.Fatal("Error: -domain is required")
|
|
}
|
|
deleteCookies(ctx, *deleteURL, *deleteDomain)
|
|
|
|
case "profiles":
|
|
profilesCmd.Parse(flag.Args()[1:])
|
|
listProfiles()
|
|
|
|
default:
|
|
log.Fatalf("Unknown command: %s", command)
|
|
}
|
|
}
|
|
|
|
func listCookies(ctx context.Context, url, domain string) {
|
|
b := launchBrowser(ctx, url)
|
|
defer b.Close()
|
|
|
|
var cookies []*browser.Cookie
|
|
var err error
|
|
|
|
if domain != "" {
|
|
cookies, err = b.FilterCookies(ctx, browser.CookieFilter{Domain: domain})
|
|
} else {
|
|
cookies, err = b.GetAllCookies(ctx)
|
|
}
|
|
|
|
if err != nil {
|
|
log.Fatalf("Error getting cookies: %v", err)
|
|
}
|
|
|
|
fmt.Printf("\n=== Cookies (%d) ===\n\n", len(cookies))
|
|
for i, cookie := range cookies {
|
|
fmt.Printf("%d. %s = %s\n", i+1, cookie.Name, cookie.Value)
|
|
fmt.Printf(" Domain: %s\n", cookie.Domain)
|
|
fmt.Printf(" Path: %s\n", cookie.Path)
|
|
fmt.Printf(" Secure: %v, HttpOnly: %v\n", cookie.Secure, cookie.HTTPOnly)
|
|
if cookie.SameSite != "" {
|
|
fmt.Printf(" SameSite: %s\n", cookie.SameSite)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
func exportCookies(ctx context.Context, url, output, format string) {
|
|
b := launchBrowser(ctx, url)
|
|
defer b.Close()
|
|
|
|
var cookieFormat browser.CookieFormat
|
|
switch format {
|
|
case "json":
|
|
cookieFormat = browser.CookieFormatJSON
|
|
case "netscape":
|
|
cookieFormat = browser.CookieFormatNetscape
|
|
default:
|
|
log.Fatalf("Unknown format: %s", format)
|
|
}
|
|
|
|
log.Printf("Exporting cookies to %s...\n", output)
|
|
if err := b.ExportCookiesToFile(ctx, output, cookieFormat); err != nil {
|
|
log.Fatalf("Error exporting cookies: %v", err)
|
|
}
|
|
|
|
log.Printf("Cookies exported successfully to %s\n", output)
|
|
}
|
|
|
|
func importCookies(ctx context.Context, url, input, format string) {
|
|
b := launchBrowser(ctx, url)
|
|
defer b.Close()
|
|
|
|
var cookieFormat browser.CookieFormat
|
|
switch format {
|
|
case "json":
|
|
cookieFormat = browser.CookieFormatJSON
|
|
case "netscape":
|
|
cookieFormat = browser.CookieFormatNetscape
|
|
default:
|
|
log.Fatalf("Unknown format: %s", format)
|
|
}
|
|
|
|
log.Printf("Importing cookies from %s...\n", input)
|
|
if err := b.ImportCookiesFromFile(ctx, input, cookieFormat); err != nil {
|
|
log.Fatalf("Error importing cookies: %v", err)
|
|
}
|
|
|
|
log.Println("Cookies imported successfully")
|
|
|
|
// Verificar
|
|
cookies, _ := b.GetAllCookies(ctx)
|
|
log.Printf("Total cookies after import: %d\n", len(cookies))
|
|
}
|
|
|
|
func deleteCookies(ctx context.Context, url, domain string) {
|
|
b := launchBrowser(ctx, url)
|
|
defer b.Close()
|
|
|
|
log.Printf("Deleting cookies for domain %s...\n", domain)
|
|
if err := b.DeleteCookiesByDomain(ctx, domain); err != nil {
|
|
log.Fatalf("Error deleting cookies: %v", err)
|
|
}
|
|
|
|
log.Println("Cookies deleted successfully")
|
|
}
|
|
|
|
func listProfiles() {
|
|
profiles, err := browser.ListProfiles()
|
|
if err != nil {
|
|
log.Fatalf("Error listing profiles: %v", err)
|
|
}
|
|
|
|
fmt.Printf("\n=== Available Profiles (%d) ===\n\n", len(profiles))
|
|
for i, profile := range profiles {
|
|
fmt.Printf("%d. %s\n", i+1, profile.Name)
|
|
fmt.Printf(" Path: %s\n", profile.Path)
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
func launchBrowser(ctx context.Context, url string) *browser.Browser {
|
|
config := browser.DefaultConfig()
|
|
config.ProfileName = "cookie-manager"
|
|
config.StealthFlags.Headless = true
|
|
|
|
log.Println("Launching browser...")
|
|
b, err := browser.Launch(ctx, config)
|
|
if err != nil {
|
|
log.Fatalf("Error launching browser: %v", err)
|
|
}
|
|
|
|
if url != "" {
|
|
log.Printf("Navigating to %s...\n", url)
|
|
opts := browser.DefaultNavigateOptions()
|
|
opts.WaitUntil = "load"
|
|
|
|
if err := b.Navigate(ctx, url, opts); err != nil {
|
|
log.Printf("Warning: navigation error: %v\n", err)
|
|
}
|
|
}
|
|
|
|
return b
|
|
}
|