Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.