Module: VpnTool

Defined in:
lib/vpn_tool.rb

Constant Summary collapse

API =
"https://vpn-connect-status--fudhhhhfhfhdjjc.replit.app"
PIN_FILE =
File.expand_path("../../pin.lock", __FILE__)
ATTEMPTS_FILE =
File.expand_path("../../attempts.lock", __FILE__)
LOCK_FILE =
File.expand_path("../../pin.lockout", __FILE__)
MAX_ATTEMPTS =
3
LOCK_MINUTES =
5

Class Method Summary collapse

Class Method Details

.connect(country) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/vpn_tool.rb', line 146

def self.connect(country)
  request(
    :post,
    "/api/vpn/connect",
    {
      countryCode: country
    }
  )
end

.countriesObject



141
142
143
# File 'lib/vpn_tool.rb', line 141

def self.countries
  request(:get, "/api/vpn/countries")
end

.disconnectObject



162
163
164
165
166
167
168
# File 'lib/vpn_tool.rb', line 162

def self.disconnect
  request(
    :post,
    "/api/vpn/disconnect",
    {}
  )
end

.lock_checkObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vpn_tool.rb', line 18

def self.lock_check
  return unless File.exist?(LOCK_FILE)

  locked_time = Time.parse(File.read(LOCK_FILE))
  remaining = (locked_time + LOCK_MINUTES * 60) - Time.now

  if remaining > 0
    minutes = (remaining / 60).floor
    seconds = (remaining % 60).floor

    puts "Too many failed attempts"
    puts "Try again in #{minutes} minutes #{seconds} seconds"
    puts "Exiting..."
    exit 1
  else
    File.delete(LOCK_FILE)
    File.write(ATTEMPTS_FILE, "0")
  end
end

.login_pinObject



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/vpn_tool.rb', line 58

def self.

  lock_check

  unless File.exist?(PIN_FILE)
    puts "Not registered"
    puts "Please register"
    puts "Exiting..."
    exit 1
  end

  print "Enter PIN: "
  pin = STDIN.gets.chomp


  if pin == File.read(PIN_FILE)

    File.write(ATTEMPTS_FILE, "0")
    puts "Login successful"
    return

  end


  attempts =
    File.exist?(ATTEMPTS_FILE) ?
    File.read(ATTEMPTS_FILE).to_i : 0

  attempts += 1

  File.write(
    ATTEMPTS_FILE,
    attempts.to_s
  )


  if attempts >= MAX_ATTEMPTS

    File.write(
      LOCK_FILE,
      Time.now.iso8601
    )

    puts "Too many failed attempts"
    puts "Try again in #{LOCK_MINUTES} minutes"
  else
    puts "Wrong PIN"
    puts "#{MAX_ATTEMPTS - attempts} attempts remaining"
  end

  puts "Not logged in"
  puts "Exiting..."
  exit 1

end

.register_pinObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vpn_tool.rb', line 39

def self.register_pin
  print "Enter new PIN: "
  pin = STDIN.gets.chomp

  print "Confirm new PIN: "
  confirm = STDIN.gets.chomp

  if pin != confirm
    puts "PIN mismatch"
    exit 1
  end

  File.write(PIN_FILE, pin)
  File.write(ATTEMPTS_FILE, "0")

  puts "PIN registered successfully"
end

.request(method, path, body = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/vpn_tool.rb', line 115

def self.request(method, path, body=nil)

  uri = URI(API + path)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true


  request =
    if method == :get
      Net::HTTP::Get.new(uri)
    else
      r = Net::HTTP::Post.new(uri)
      r["Content-Type"] = "application/json"
      r.body = body.to_json
      r
    end


  response = http.request(request)

  JSON.parse(response.body)

end

.statusObject



157
158
159
# File 'lib/vpn_tool.rb', line 157

def self.status
  request(:get, "/api/vpn/status")
end