Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit

  • Loading branch information...
commit 21ea25c23259be95eda9f321760f3f8a502c37b7 0 parents
@ksexton ksexton authored
26 .gitignore
@@ -0,0 +1,26 @@
+# Ignore bundler config
+/.bundle
+
+# Ignore the default SQLite database.
+/db/*.sqlite3
+
+# Ignore all logfiles and tempfiles.
+/log/*.log
+/tmp
+
+# Ignore other unneeded files.
+doc/
+*.swp
+*~
+.project
+.DS_Store
+
+files/*
+test/*
+
+keys/*
+
+*.log
+*.key
+*.private
+*.tmproj
0  README.md
No changes.
4 Rakefile
@@ -0,0 +1,4 @@
+require "./lib/sendtodns.rb"
+require "./lib/file.rb"
+require "./lib/generaterecords.rb"
+require "resque/tasks"
125 get.rb
@@ -0,0 +1,125 @@
+#!/usr/bin/env ruby
+require "pp"
+require "logging"
+require "digest/md5"
+
+Logging.color_scheme( 'bright',
+ :levels => {
+ :info => :green,
+ :warn => :yellow,
+ :error => :red,
+ :fatal => [:white, :on_red]
+ },
+ :date => :blue,
+ :logger => :cyan,
+ :message => :magenta
+)
+
+Logging.appenders.stdout(
+ 'stdout',
+ :layout => Logging.layouts.pattern(
+ :pattern => '[%d] %-5l %c:%M %m\n',
+ :color_scheme => 'bright'
+ )
+)
+
+@logger = Logging.logger[self]
+@logger.add_appenders(
+ Logging.appenders.stdout,
+ Logging.appenders.file('get.log')
+)
+
+getname = "e8fkiy" # elephants dream w/ checksum
+# getname = "4qefx5" # ubuntu ISO w/ checksum
+# getname = "fcynuv" # asterisk w/ checksum
+
+nameserver = "ns1.sendtodns.org"
+domain = "sendtodns.org"
+filedomain = getname + "." + domain
+fileinfo = `dig @#{nameserver} TXT +short fileid.#{filedomain}`.split(",")
+filename = fileinfo[0].to_s[1..-1]
+startpart = fileinfo[1].split(".")[1].to_i
+endpart = fileinfo[2].split(".")[1].to_i
+md5sum = fileinfo[3][0...-2].to_s
+
+partdomainlist = Hash.new
+
+
+@logger.info "Filename: #{filename}"
+@logger.info "Start: #{startpart}"
+@logger.info "End: #{endpart}"
+@logger.info "md5sum: #{md5sum}"
+
+i = 0
+until i == endpart do
+ i = i + 1
+ partfileinfodomain ="fileid.#{getname}.%03d.#{domain}" % i
+ part = `dig @#{nameserver} TXT +short #{partfileinfodomain}`
+ partcount = part.split(",")[1].to_i
+ md5sum = part.split(",")[2][0...-2].to_s
+ # puts partcount
+ partdomainlist["#{partfileinfodomain}"] = [partcount, md5sum]
+end
+# puts partdomain
+
+# pp partdomainlist
+i = 0
+x = 0
+partdomainlist.each do |k,v|
+ fork do
+
+ @file_part = k.split(".")[2].to_s
+ partcount = v[0]
+ md5sum = v[1]
+ @logger.debug "Working on #{k} with #{partcount} parts and md5 #{md5sum}"
+ until i == partcount + 1 do
+ digcommand = "dig @#{nameserver} TXT +short +vc #{i}.#{k.split(".")[1..-1].join(".")}| grep -v ^\\; | cut -d\\\" -f 2- | sed -e 's/" + "\" " + "\"" + "/\\'$\'\\n/g' | sed -E 's/^[0-9]+ //g' | sed 's/\\\"$//g' >> #{getname}.get.#{k.split(".")[2].to_s}"
+ y = 0
+
+ # digcommand = "dig @#{nameserver} TXT +short +vc #{i}.#{k.split(".")[1..-1].join(".")}| grep -v ^\\; | cut -d\\\" -f 2- | sed -e 's/" + "\" " + "\"" + "/\\'$\'\\n/g' | sed 's/\\\"$//g' >> #{getname}.get.#{k.split(".")[2].to_s}"
+
+ # digcommand = "dig @#{nameserver} TXT +short +vc #{i}.#{k.split(".")[1..-1].join(".")} >> #{getname}.get.#{k.split(".")[2].to_s}"
+ `#{digcommand}`
+ i = i + 1
+ y = y + 1
+ end
+ i = 0
+ end
+
+ x = x + 1
+ # puts (x / 20.0)%1 == 0.0
+
+ if (x / 20.0)%1 == 0.0
+ @logger.info "Waiting for processes to finish"
+ Process.waitall
+ end
+end
+Process.waitall
+
+partdomainlist.each do |k,v|
+ @file_part = k.split(".")[1].to_s + ".get." + k.split(".")[2].to_s
+ partcount = v[0]
+ md5sum = v[1]
+ downloaded_md5 = Digest::MD5.file(@file_part).to_s
+ # @logger.debug "Working on #{k} with #{partcount} parts and md5 #{md5sum}"
+ # @logger.debug "File md5: " + Digest::MD5.file(@file_part).to_s
+ # @logger.debug "File part: #{@file_part}, md5: #{md5sum}"
+ if downloaded_md5 == md5sum
+ # @logger.debug "#{@file_part} matched md5 sum"
+ else
+ # @logger.error "#{@file_part} did not match md5 sum!"
+ # @logger.debug "Redownloading #{@file_part}"
+ `rm #{@file_part}`
+ until i == partcount + 1 do
+ digcommand = "dig @#{nameserver} TXT +short +vc #{i}.#{k.split(".")[1..-1].join(".")}| grep -v ^\\; | cut -d\\\" -f 2- | sed -e 's/" + "\" " + "\"" + "/\\'$\'\\n/g' | sed -E 's/^[0-9]+ //g' | sed 's/\\\"$//g' >> #{getname}.get.#{k.split(".")[2].to_s}"
+ `#{digcommand}`
+ i = i + 1
+ end
+ end
+end
+
+
+`uudecode #{getname}.get.*`
+`lxsplit -j #{getname}.001`
+`mv #{getname} #{filename}`
+`rm #{getname}.*`
94 get.sh
@@ -0,0 +1,94 @@
+#!/bin/bash
+#
+# Usage: getfile.sh filecode
+#
+# This example downloads the Elephants Dream video
+#
+# Example: getfile e8fkiy
+#
+
+function nameserver {
+ echo $NSLIST | cut -d\, -f `expr \( $RANDOM % 1 \) + 1`
+}
+
+function prereq {
+ EXISTS=`which $1; echo $?`
+ if [ "$EXISTS" == "1" ]; then
+ echo "Please install $1"
+ exit 1
+ fi
+}
+
+function getfile {
+ for (( i=0;i<$ENDPART + 1;i++)); do
+ dig @$(nameserver) TXT +short +vc $i.$1.$PART.sendtodns.org >> $1.unparsed.$PART
+ done
+}
+
+function parsefile {
+ cat $1.unparsed.$PART | cut -d\" -f 2- | sed -e 's/" "/\'$'\n/g' | sed -E 's/^[0-9]+ //g' | sed 's/\"$//g' > $1.get.$PART
+}
+
+function die {
+ echo >&2 "$@"
+ exit 1
+}
+
+[ "$#" -eq 1 ] || die "1 argument (the file key) is required, $# provided"
+
+NSLIST="ns1.sendtodns.org"
+FILENAME=`dig @$(nameserver) TXT +short +vc fileid.$1.sendtodns.org | cut -d\, -f 1 | sed 's/^"//'`
+MD5=`dig @$(nameserver) TXT +short +vc fileid.$1.sendtodns.org | cut -d\, -f 4 | sed 's/"$//'`
+END=`dig @$(nameserver) TXT +short +vc fileid.$1.sendtodns.org | cut -d\, -f3 | cut -d\. -f2 | sed s/^0//`
+BATCHSIZE="15.0" # Must be a float in a string
+prereq lxsplit
+prereq uudecode
+prereq dig
+prereq cut
+prereq sed
+prereq bc
+prereq md5sum
+
+
+echo "Getting $FILENAME with $END parts."
+
+for (( x=1;x<$END + 1;x++)); do
+ WAIT="`echo "scale=2; $x / $BATCHSIZE" | bc`"
+ WAITMOD="`echo "scale=0; $WAIT % 1" | bc`"
+ PART=`printf %03d $x`
+ ENDPART=`dig @$(nameserver) TXT +short +vc fileid.$1.$PART.sendtodns.org | cut -d\, -f2`
+
+ echo "Getting $1.$PART"
+ getfile $1 &
+
+ if [ "$WAITMOD" == "0" ]; then
+ echo "Settling processes."
+ wait
+ fi
+
+
+done
+
+wait
+
+for (( x=1;x<$END + 1;x++)); do
+ PART=`printf %03d $x`
+ parsefile $1 $PART
+done
+
+echo "Decoding files."
+uudecode *get*
+echo "Joining files."
+lxsplit -j $1.001 > /dev/null
+mv $1 $FILENAME
+
+DOWNLOADMD5=`md5sum $FILENAME | awk '{print $1}'`
+
+if [ "$DOWNLOADMD5" == "$MD5" ]; then
+ echo "MD5 sum of downloaded file is correct."
+ rm $1.*
+else
+ echo "MD5 sum of downloaded file did not match. Leaving downloaded parts."
+fi
+
+
15 lib/encode.rb
@@ -0,0 +1,15 @@
+module SendToDNS
+ module Encode
+ extend self
+
+ def uuencode(file)
+ # @encodedfile = Hash.new
+ # @encodedfile[ :"#{file}" ] = `uuencode -m #{@file} #{@file}`.split("\n")
+ `uuencode -m -o #{@file}.uu #{@file} #{@file}`
+ # return @encodedfile
+ end
+
+ end
+end
+
+
99 lib/file.rb
@@ -0,0 +1,99 @@
+require "digest/md5"
+
+module SendToDNS
+ module File
+ extend self
+ def randomname(length=6)
+ chars = ("a".."z").to_a + ("a".."z").to_a + ("0".."9").to_a
+ randomname = ""
+ 1.upto(length) { |i| randomname << chars[rand(chars.size-1)] }
+ return randomname
+ end
+
+ def maketemp(tempdir="/tmp/sendtodns")
+ @stagedirectory = "#{tempdir}/#{randomname}/"
+ @logger.debug "Making staging area #{@stagedirectory}"
+ `mkdir -p #{@stagedirectory}`
+ return @stagedirectory
+ end
+
+ def copyfile(file=@file)
+ @logger.debug "Copying ./files/#{file} into #{@stagedirectory}"
+ `cp ./files/#{file} #{@stagedirectory}`
+ end
+
+ def renamefile(file=@file)
+ @logger.debug "Renaming #{file} to #{randomname}"
+ `#{changedir}; mv #{file} #{randomname}`
+ end
+
+ def splitfile(file=@file, size="1m")
+ @logger.debug "Splitting #{randomname} into #{size} chunks"
+ `#{changedir}; lxsplit -s #{randomname} #{size}`
+ end
+
+ def cleanfile()
+ @logger.debug "Cleaning file move"
+ `#{changedir}; rm #{randomname}.???`
+ end
+
+ def changedir()
+ "cd #{@stagedirectory}"
+ end
+
+ def uuencode()
+ # @encodedfile = Hash.new
+ # @encodedfile[ :"#{file}" ] = `uuencode -m #{@file} #{@file}`.split("\n")
+ `#{changedir}; rm #{randomname}`;
+ filelist.each do |i|
+ command = "#{changedir}; mkdir -p tmp; uuencode -m -o ./tmp/#{i} #{i} #{i}; mv tmp/* ./; rmdir tmp"
+ `#{command}`
+ @logger.debug "#{command}"
+
+ end
+ # return @encodedfile
+ end
+
+ def generate_md5(file)
+ `#{changedir}`
+ md5sum = Digest::MD5.file("#{@stagedirectory}/#{file}")
+ @logger.debug "#{file} md5: #{md5sum}"
+ return md5sum
+ end
+
+ def numberfiles()
+ filelist.each do |i|
+ `#{changedir}; mv #{i} #{i}.tmp; nl #{i}.tmp > #{i}; rm #{i}.tmp`
+ @logger.debug "#{changedir}; mv #{i} #{i}.tmp; nl #{i}.tmp > #{i}; rm #{i}.tmp"
+ end
+ end
+
+ def stage(file=@file)
+ maketemp
+ copyfile
+ @main_md5 = generate_md5(file)
+ renamefile
+ splitfile
+ # cleanfile
+ uuencode
+ md5list
+ numberfiles
+ # cleanfile
+ filelist
+ # sleep 100
+ end
+
+ def md5list()
+ @md5list = Hash.new
+ filelist.each do |i|
+ @md5list[i] = generate_md5(i)
+ end
+ return @md5list
+ end
+
+ def filelist()
+ @filelist = `ls -1 #{@stagedirectory}`.split("\n")
+ return @filelist
+ end
+ end
+end
87 lib/generaterecords.rb
@@ -0,0 +1,87 @@
+module SendToDNS
+ module GenerateRecords
+
+ def block(record, size)
+ @logger.info "In block"
+ iterator = 0
+ block = Array.new
+ tempblock = ""
+ record.lines.each do |line|
+ # @logger.debug "#{self} #{line}"
+ iterator = iterator + 1
+ tempblock << line
+ if iterator == size
+ # Resque.enqueue(DNSUpdate,tempblock)
+ block << tempblock
+
+ tempblock = ""
+ iterator = 0
+ end
+ end
+ block << tempblock
+ @logger.debug "Leaving block #{block.size}"
+ return block
+ end
+
+ def zoneblock(file,recordsize=40,blocksize=1)
+ @logger.debug "In zoneblock working on #{file}"
+ holder = Array.new
+ interval = 0
+ @logger.debug "Reading file with IO.read"
+ filecontents = IO.read("#{@stagedirectory}#{file}")
+ @logger.debug "recordblock = block(file,recordsize)"
+ recordblock = block(filecontents,recordsize)
+ @logger.debug "#{recordblock.class}"
+ recordblock.each do |i|
+ # @logger.debug "zoneblock recordblock.each"
+ record = ""
+ i.each_line do |line|
+ record = record + "\"#{line.gsub(/\s+/, ' ').lstrip.rstrip}\" "
+ end
+
+
+ record_update = record_fluff(interval,file,domain,record)
+
+ holder << record_update
+ # @logger.debug "#{holder.length}"
+
+ interval = interval + 1
+ end
+ @logger.error "Returning an #{holder.class}"
+ return holder
+ end
+
+ def record_fluff(interval,file,domain,record)
+ record_update = "update add #{interval}.#{file}.#{domain}. 604800 A 192.168.1.100\n" +
+ "update add #{interval}.#{file}.#{domain}. 604800 TXT #{record}"
+ end
+
+ def nsupdate(updateblock)
+ zonevalue = "server ns1.sendtodns.org\n" +
+ "zone sendtodns.org\n" +
+ "#{updateblock}\n" +
+ "show\n" +
+ "send\n"
+ return zonevalue
+ end
+
+ def send_block(blocklength)
+ @filelist.each do |file|
+ @logger.debug "Working on #{file} with md5 #{@md5list[file]}"
+ block = self.zoneblock(file)
+ block << record_fluff("fileid", "#{@randomname}", domain, "\"#{@file},#{@filelist.first},#{@filelist.last},#{@main_md5}\"")
+ block << record_fluff("fileid", file, domain, "\"#{file},#{block.length.to_i - 2},#{@md5list[file]}\"")
+ while block.length > 0
+ if block.slice(blocklength) != nil
+ cut = block.slice!(0..blocklength)
+ else
+ cut = block.slice!(0..-1)
+ end
+ Resque.enqueue(DNSUpdate, nsupdate(cut.join("\n")))
+ # pp cut
+ end
+ end
+ end
+
+ end
+end
44 lib/sendtodns.rb
@@ -0,0 +1,44 @@
+require "./lib/file.rb"
+require "./lib/generaterecords.rb"
+require "pp"
+
+module SendToDNS
+ extend self
+
+ include SendToDNS::File
+ include SendToDNS::GenerateRecords
+end
+
+class PushFile
+ attr_accessor :domain, :key, :file, :randomname
+
+ include SendToDNS
+ # include SendToDNS::File
+
+
+ def initialize(domain, key, file)
+ @domain, @key, @file = domain, key, file
+ @randomname = SendToDNS::File::randomname
+ @logger = Logging.logger[self]
+ @logger.add_appenders(
+ Logging.appenders.stdout,
+ Logging.appenders.file('development.log')
+ )
+ self.stage
+ self.send_block(5)
+
+ end
+
+
+
+end
+
+class DNSUpdate
+ @queue = :sendtodns
+
+ def self.perform(records)
+ IO.popen('nsupdate -v -k ./keys/nsupdatekey.private', 'w') {|io| io.puts records}
+ # sleep 0.2
+ # puts records
+ end
+end
31 send.rb
@@ -0,0 +1,31 @@
+#!/usr/bin/env ruby
+require "./lib/sendtodns.rb"
+require "logging"
+require "resque"
+
+Logging.color_scheme( 'bright',
+ :levels => {
+ :info => :green,
+ :warn => :yellow,
+ :error => :red,
+ :fatal => [:white, :on_red]
+ },
+ :date => :blue,
+ :logger => :cyan,
+ :message => :magenta
+)
+
+Logging.appenders.stdout(
+ 'stdout',
+ :layout => Logging.layouts.pattern(
+ :pattern => '[%d] %-5l %c:%M %m\n',
+ :color_scheme => 'bright'
+ )
+)
+
+domain = "bindnameserver.org"
+key = "./keys/nsupdatekey.private"
+file = "filename"
+foo = PushFile.new(domain, key, file)
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.