Module: VPN

Defined in:
lib/vpn.rb

Constant Summary collapse

BASE =
"https://vpn-gateway--jdjdjdjdjdh8288.replit.app"

Class Method Summary collapse

Class Method Details

.disconnectObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vpn.rb', line 27

def self.disconnect
  uri = URI("#{BASE}/api/vpn/disconnect")

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

  req = Net::HTTP::Post.new(uri)
  req["Content-Type"] = "application/json"
  req.body = {}.to_json

  res = http.request(req)
  puts res.body
end

.helpObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vpn.rb', line 55

def self.help
  puts <<~TXT
VPN CLI

Commands:
vpn list
vpn start US
vpn disconnect
vpn help
TXT
end

.listObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vpn.rb', line 41

def self.list
  uri = URI("#{BASE}/api/vpn/countries")

  res = Net::HTTP.get_response(uri)

  if res.is_a?(Net::HTTPSuccess)
    JSON.parse(res.body).each do |c|
      puts "#{c["code"]} #{c["flagEmoji"]} #{c["name"]}"
    end
  else
    puts "Error: #{res.code}"
  end
end

.run(args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vpn.rb', line 67

def self.run(args)
  case args[0]
  when "start"
    start(args[1])
  when "disconnect"
    disconnect
  when "list"
    list
  else
    help
  end
end

.start(country) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vpn.rb', line 8

def self.start(country)
  if country.nil?
    puts "Usage: vpn start <COUNTRY_CODE>"
    return
  end

  uri = URI("#{BASE}/api/vpn/connect")

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

  req = Net::HTTP::Post.new(uri)
  req["Content-Type"] = "application/json"
  req.body = { countryCode: country.upcase }.to_json

  res = http.request(req)
  puts res.body
end