crazy-max / WindowsSpyBlocker

Block spying and tracking on Windows
https://crazymax.dev/WindowsSpyBlocker/
MIT License
4.61k stars 357 forks source link

Add simplewall data format #68

Closed crazy-max closed 7 years ago

crazy-max commented 7 years ago

As simplewall by @henrypp uses WindowsSpyBlocker rules it will be more efficient if WindowsSpyBlocker generates the rules for him.

More info : henrypp/simplewall#35

henrypp commented 7 years ago

im used this simple php code to generate 2 xml's

  1. for only "spy" rules - blocklist.xml (it will be used in production)
  2. for all rules - blocklist_full.xml
<?php
    $crlf = "\n"; // use unix eol, it save 1 symbol for each line ;)
    $current_directory = realpath ('.');
    $rules_dir = $current_directory . '\..\..\..\WindowsSpyBlocker\data\firewall'; // WindowsSpyBlocker data directory
    $out_file = $current_directory . '\blocklist.xml'; // only "spy" rules
    $out_file_full = $current_directory . '\blocklist_full.xml'; // full (include "extra" & "update" rules)

    $array = [];

    foreach (glob ($rules_dir .'\*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {

        $dirname = pathinfo ($dir, PATHINFO_FILENAME);

        foreach (glob ($dir . '/*.txt', GLOB_NOSORT) as $file) {

            $filename = pathinfo ($file, PATHINFO_FILENAME);
            $filename_full = pathinfo ($file, PATHINFO_BASENAME);

            $content = file_get_contents ($file);

            $content_arr = explode ($crlf, $content);

            printf ('Parse "%s\%s"... OK!' . $crlf, $dirname, $filename_full);

            foreach ($content_arr as $val) {

                if(empty($val))
                    continue;

                $val = trim ($val);

                if(empty($val) || $val[0] == '#')
                    continue;

                $array[$filename .'_'. $val] = $val;
            }

            unset ($val);
        }

        unset ($file);

        // sort rules by key
        ksort ($array, SORT_NATURAL);

        // copy header to buffer
        $buffer_full = '<!-- Block spying and tracking on Windows -->' . $crlf . '<!-- https://github.com/crazy-max/WindowsSpyBlocker -->'  . $crlf . $crlf . '<!-- Last-Modified: ' . date ('r') .' -->'. $crlf . $crlf . '<?xml version="1.0"?>'. $crlf .'<root>'. $crlf;

        $buffer = $buffer_full;

        foreach ($array as $key => $val) {

            $is_spy_rule = strncmp ($key, 'spy', 3) == 0;

            $buff = sprintf("\t" . '<item name="%s" rule="%s" dir="0" protocol="0" version="0" is_block="1" is_enabled="%s" />'. $crlf, $key, $val, $is_spy_rule ? 1 : 0);

            $buffer_full .= $buff;

            if (!$is_spy_rule)
                continue;

            $buffer .= $buff;
        }

        unset ($key, $val);

        $buffer .= '</root>'. $crlf;
        $buffer_full .= '</root>'. $crlf;

        file_put_contents ($out_file, $buffer);
        file_put_contents ($out_file_full, $buffer_full);
    }

    unset ($dir);
?>
crazy-max commented 7 years ago

Thanks @henrypp but WindowsSpyBlocker is written in Go ;) Btw it's solved in 4.7.1 (look at data/simplewall)