| 1 | #!/usr/bin/perl |
|---|
| 2 | # |
|---|
| 3 | # Reads hosts.txt and generates a file which can be used as privatehosts.txt |
|---|
| 4 | # for the I2P router to convert GarliCat-IDs back to the original key. |
|---|
| 5 | # |
|---|
| 6 | # The original hostname, full IPV6 address, and b32 name are included as a |
|---|
| 7 | # comment for each host. |
|---|
| 8 | # |
|---|
| 9 | # See below for perl package requirements. |
|---|
| 10 | # |
|---|
| 11 | # zzz 1/08 public domain |
|---|
| 12 | # |
|---|
| 13 | |
|---|
| 14 | use strict; |
|---|
| 15 | use CGI qw(:standard); |
|---|
| 16 | use MIME::Base64; |
|---|
| 17 | use Convert::Base32; |
|---|
| 18 | use Digest::SHA qw(sha256); |
|---|
| 19 | use Digest::SHA qw(sha256_hex); |
|---|
| 20 | |
|---|
| 21 | my $hosthash; |
|---|
| 22 | |
|---|
| 23 | # load the whole db into memory |
|---|
| 24 | sub loadhosts |
|---|
| 25 | { |
|---|
| 26 | my $hostcount = 0; |
|---|
| 27 | open(local *STATLIST, "hosts.txt") or die "Can't access hosts.txt!"; |
|---|
| 28 | while (<STATLIST>) { |
|---|
| 29 | my $name; |
|---|
| 30 | my $key; |
|---|
| 31 | my $restofline; |
|---|
| 32 | ($name,$restofline) = split(/=/); |
|---|
| 33 | $key = $restofline; |
|---|
| 34 | $name = lc($name); |
|---|
| 35 | chomp($key); |
|---|
| 36 | $hosthash->{$name} = $key; |
|---|
| 37 | $hostcount++; |
|---|
| 38 | } |
|---|
| 39 | close STATLIST; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | sub printhosts |
|---|
| 44 | { |
|---|
| 45 | my @sorted = keys %$hosthash; |
|---|
| 46 | my $name; |
|---|
| 47 | foreach $name (@sorted) { |
|---|
| 48 | my $b64 = $hosthash->{$name}; |
|---|
| 49 | $b64 =~ s/-/+/g; |
|---|
| 50 | $b64 =~ s/~/\//g; |
|---|
| 51 | my $decoded = decode_base64($b64); |
|---|
| 52 | my $hash=sha256($decoded); |
|---|
| 53 | my $hexhash = sha256_hex($decoded); |
|---|
| 54 | my $encoded = encode_base32($hash); |
|---|
| 55 | print "#" . $name . " fd60:db4d:ddb5"; |
|---|
| 56 | for (my $i = 0; $i < 20; $i += 4) { |
|---|
| 57 | printf(":%s", substr($hexhash, $i, 4)); |
|---|
| 58 | } |
|---|
| 59 | print " " . $encoded . ".b32.i2p\n"; |
|---|
| 60 | print substr($encoded, 0, 16) . ".oc.b32.i2p=" . $hosthash->{$name} . "\n"; |
|---|
| 61 | } |
|---|
| 62 | return 0; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | loadhosts(); |
|---|
| 66 | printhosts(); |
|---|
| 67 | |
|---|