Class: TunnelBlick::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnel_blick/tunnel.rb,
lib/tunnel_blick/ip_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTunnel

Returns a new instance of Tunnel.



10
11
12
13
14
# File 'lib/tunnel_blick/tunnel.rb', line 10

def initialize
  @tunnel = Appscript.app('Tunnelblick.app')
  TunnelBlick.conn_sleep = 3.5 if TunnelBlick.conn_sleep.nil?
  shortlist_configs
end

Instance Attribute Details

#tunnelObject

Returns the value of attribute tunnel.



8
9
10
# File 'lib/tunnel_blick/tunnel.rb', line 8

def tunnel
  @tunnel
end

Instance Method Details

#change_ip(allow_bare = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tunnel_blick/ip_methods.rb', line 34

def change_ip (allow_bare = false)
  start_ip = TunnelBlick.my_ip

  if connected?
    disconnect
    sleep(3)

    if allow_bare
      return TunnelBlick.my_ip
    end
  end

  new_ip = start_ip.dup
  until new_ip != start_ip
    disconnect
    new_ip = connect_cycle
  end
  new_ip
end

#change_ip_unique(reset = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tunnel_blick/ip_methods.rb', line 54

def change_ip_unique (reset = false)

  TunnelBlick.set_unique_attempts(6) if TunnelBlick.uniq_tries.nil?
  @checked_ips = [] if @checked_ips.nil? || reset
  my_ip = change_ip
  counter = 1

  while @checked_ips.include?(my_ip) && counter < TunnelBlick.uniq_tries
    my_ip = change_ip
    counter += 1
  end
  @checked_ips << my_ip

  if counter == TunnelBlick.uniq_tries
    sputs "unique ip change failed after #{counter} tries"
    false
  else
    my_ip
  end

end

#connect(num = nil, logging = true) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tunnel_blick/tunnel.rb', line 47

def connect (num = nil, logging = true)

  if num.nil?
    target = @shortlist.sample
  else
    target = @shortlist[num]
  end

  TunnelBlick.ip_db.disconnect
  Sequel::Model.db.disconnect
  print '*'

  tunnel.connect(target)
  until connected?
    sleep(1)
    print '.'
  end
  puts '$'
  sleep(TunnelBlick.conn_sleep)

  details = get_config_info(target)
  TunnelBlick.details = details
  my_ip = TunnelBlick.my_ip

  if logging

    info = details.dup
    info.public_ip = my_ip

    test_okay = false
    until test_okay
      sleep(3)
      conn1_okay = TunnelBlick.ip_db.test_connection
      sleep(1)
      conn2_okay = Sequel::Model.db.test_connection
      print '.'
      test_okay = conn1_okay && conn2_okay
    end
    puts '$'
    # Sequel::Model.db = TunnelBlick.ip_db
    TunnelBlick.try_log_info(info)
  end

  my_ip
end

#connect_cycle(reset = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tunnel_blick/ip_methods.rb', line 17

def connect_cycle (reset = false)

  last_num = @shortlist.size - 1

  if reset
    @cycle_num = 0
  else
    @cycle_num += 1
  end

  if @cycle_num > last_num
    @cycle_num = 0
  end
  disconnect if connected?
  connect(@cycle_num)
end

#connect_sameObject



8
9
10
11
12
13
14
15
# File 'lib/tunnel_blick/ip_methods.rb', line 8

def connect_same

  if @cycle_num == -1
    @cycle_num = 0
  end

  connect(@cycle_num)
end

#connected?Boolean

Returns:

  • (Boolean)


36
# File 'lib/tunnel_blick/tunnel.rb', line 36

def connected?; vpn_states.include?('CONNECTED') end

#current_configObject



38
39
40
41
42
43
44
45
# File 'lib/tunnel_blick/tunnel.rb', line 38

def current_config
  if connected?
    position = vpn_states.index('CONNECTED')
    vpn_configs[position]
  else
    nil
  end
end

#disconnectObject



93
# File 'lib/tunnel_blick/tunnel.rb', line 93

def disconnect; tunnel.disconnect_all if connected? end

#get_config_info(config) ⇒ Object



16
17
18
# File 'lib/tunnel_blick/tunnel.rb', line 16

def get_config_info (config)
  match_provider(config).parse_config(config)
end

#match_provider(config_name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/tunnel_blick/tunnel.rb', line 95

def match_provider (config_name)

  case config_name
    when /\Amy_expressvpn.+/
      TunnelBlick::ExpressVPN
    else
      raise ArgumentError('VPN provider not recognized.')
  end

end

#shortlist_configsObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tunnel_blick/tunnel.rb', line 20

def shortlist_configs
  if TunnelBlick.countries.nil?
    @shortlist = vpn_configs
  else
    @shortlist = vpn_configs.select { |config|
      details = get_config_info(config)
      TunnelBlick.countries.include?(details.country)
    }
  end
  @cycle_num = -1
end

#vpn_configsObject



34
# File 'lib/tunnel_blick/tunnel.rb', line 34

def vpn_configs; tunnel.configurations.name.get end

#vpn_statesObject



32
# File 'lib/tunnel_blick/tunnel.rb', line 32

def vpn_states; tunnel.configurations.state.get end