class Tor::LongCircuit

This creates a Long circuit comprising of multiple sub-circuits. Methods include #built?, #extend,LongCircuit..#attach_streams,

Public Class Methods

new( tcontrolarray=[], circuitarray=[], proxyconfig ) click to toggle source

Creates an instance of Tor::LongCircuit and initialises the attributes of the instance including the cirnum, @built and @circuitarray containing an sub-circuits that are instances of Tor::Circuit

# File lib/circuits.rb, line 253
def initialize( tcontrolarray=[], circuitarray=[], proxyconfig )
    #    Would it be better to get proxyconfig from mytor.getconf("SocksPort") 
    @built = false
    @cirnum = 0
    @circuitarray = []
    i = 0; j = 2;  k=0
    while i < circuitarray.count
        if tcontrolarray[k]
            if k == 0
              @circuitarray << Circuit.new( tcontrolarray[k], circuitarray[ i..j ] , nil )
            else
              @circuitarray << Circuit.new( tcontrolarray[k], circuitarray[ i..j ] , proxyconfig + k )
            end
            i +=3
            j +=3
            k +=1
        end
    end
end

Public Instance Methods

attach_streams() click to toggle source

This tries to attach all streams to to a single pre-established circuit.

# File lib/circuits.rb, line 274
def attach_streams
    #    fork_reactor runs the reactor in a new thread thus allowing other methods to e run. Should I store the pid? When do I close the pid?
    #    EM.fork_reactor{
    EM.run {
        #    @counter = 0 if not defined?(@seun)    # To count how many times an event has been triggered
        @timer=Array.new(@circuitarray.count)
        0.upto(@circuitarray.count - 1){|z|
            if ! @circuitarray[z].launched?
                @circuitarray[z].launch
            end

            # Periodic timer to fire an event, in this case launch/attach sub circuits. If a custom value (Tor::PERIODIC_TIMER) is defined, use it instead 
            timer = (defined? Tor::PERIODIC_TIMER) ?  Tor::PERIODIC_TIMER : Tor::Constants::PERIODIC_TIMER

            # Keep checking if any of the subcircuits has been closed using the timer duration
            @timer[z] = EventMachine::PeriodicTimer.new(timer) do
                @timer[z].cancel if @circuitarray[z].closed?
                # puts "Counter = #{@counter+=1}\n"
                @circuitarray[z].start
            end
        }
    }

    y = ( built? ).index(false)
    puts "#{y} - #{built?}\n"
    extend y
    return nil
end
built?() click to toggle source

Checks if the subcircuits have been built.

# File lib/circuits.rb, line 309
def built?
    @built = true
    truth_table = @circuitarray.collect{|each_circuit| each_circuit.built? } # To know where the circuit is failing
    truth_table.each{|truth_value| @built = @built & truth_value }
    truth_table
end
circuit() click to toggle source

Returns the array of Tor::Circuit instances that make up the LongCircuit instance.

# File lib/circuits.rb, line 324
def circuit
    @circuitarray
end
cirnum() click to toggle source
# File lib/circuits.rb, line 304
def cirnum
    @circuitarray[0].cirnum
end
close_apps() click to toggle source

This closes all the sub-circuits.

# File lib/circuits.rb, line 317
def close_apps
    @circuitarray.each{|z|
        z.close_apps
    }
end
extend(*startcir) click to toggle source

Extend circuits from startcir position in the circuit array. This is used internally by Tor.attach_streams There is no need to call this directly, instead use #attach_streams method.

# File lib/circuits.rb, line 334
def extend(*startcir)
    if startcir.empty?
        tmp=0
    elsif startcir[0].nil?
        tmp=0
    else
        tmp = startcir[0]
    end

    #  Extend all sub-circuits or extend  only circuits from startcir index.
    tmp.upto(@circuitarray.count - 1 ){ |y|
        each_circuit = @circuitarray[y]
        counter_z=0
        while ! each_circuit.built?  and counter_z < 15     # Try up to 15 times to build the same circuit before giving up
            # each_circuit.launch if y != 0   # here I need to launch tor and polipo in this method with the right config file after adding tmpdir path and configs
            cirnum = each_circuit.extend
            if each_circuit.built?
                next
            else
                sleep( 2 * @circuitarray.index(each_circuit) + 2 )      # arithmetic progression time possible delay between each circuits [2,4,6,8,10,12]...
                next if each_circuit.built?
            end
            z += 1
        end

        if z == 15
            puts "Circuit failed \n" 
            return nil
        else
            cirnum
        end
    }
end