Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehiah/e565d60fa759a6921c4c3af30f64e922 to your computer and use it in GitHub Desktop.
Save jehiah/e565d60fa759a6921c4c3af30f64e922 to your computer and use it in GitHub Desktop.
check ruleset files in HTTPS-Everywhere for duplicate target definitions
package main
// This script is designed to check ruleset files in HTTPS-Everywhere for duplicate target definitions
import (
"encoding/xml"
"flag"
"path/filepath"
"io/ioutil"
"log"
)
type Target struct {
Host string `xml:"host,attr"`
}
type Ruleset struct {
XMLName xml.Name `xml:"ruleset"`
Targets []Target `xml:"target"`
}
func main() {
var (
rulesDirectory = flag.String("rules-dir", "src/chrome/content/rules", "path to rules.xml directory")
)
flag.Parse()
existingHosts := make(map[string]string)
files, err := filepath.Glob(filepath.Join(*rulesDirectory, "*.xml"))
if err != nil {
log.Fatal(err)
}
for _, filename := range files {
base := filepath.Base(filename)
body, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("err %s", err)
}
var r Ruleset
err = xml.Unmarshal(body, &r)
if err != nil {
log.Fatalf("err %s", err)
}
for _, t := range r.Targets {
if existingFile, ok := existingHosts[t.Host]; ok {
log.Printf("%s found in %s and %s", t.Host, existingFile, base)
}
existingHosts[t.Host] = base
}
}
log.Printf("%d hosts checked", len(existingHosts))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment