Module: VPNClistart

Defined in:
lib/vpn_clistart.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.disconnectObject



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

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



23
24
25
26
27
28
29
30
31
32
# File 'lib/vpn_clistart.rb', line 23

def self.help
  puts <<~TXT
VPN CLI Commands:

vpn list
vpn start US
vpn disconnect
vpn help
TXT
end

.listObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vpn_clistart.rb', line 34

def self.list
  res = Net::HTTP.get_response(URI("#{BASE}/api/vpn/countries"))

  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(argv) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vpn_clistart.rb', line 8

def self.run(argv)
  case argv[0]
  when "help", nil
    help
  when "list"
    list
  when "start"
    start(argv[1])
  when "disconnect"
    disconnect
  else
    puts "Unknown command. Use: vpn help"
  end
end

.start(country) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vpn_clistart.rb', line 46

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