Skip to content

Instantly share code, notes, and snippets.

@pstaender
Last active October 28, 2018 18:13
Show Gist options
  • Save pstaender/e3aaaab29afd9cdd25b2d43d904c2f93 to your computer and use it in GitHub Desktop.
Save pstaender/e3aaaab29afd9cdd25b2d43d904c2f93 to your computer and use it in GitHub Desktop.
Open NordVPN easiliy via cli with country as argument and checking for newest server files
youremail
yourpassword
#!/usr/bin/env ruby
# usage (here with usa as example): ruby open_nord_vpn.rb us
require 'tmpdir'
require 'digest'
class NordVPNConnection
attr_accessor :country
def initialize(country)
self.country = country
ensure_nordvpn_dir_exists
ensure_checksumfile_exists
check_if_new_servers_are_available_and_update_config_files_if_so if update_required?
open_nordvpn_connection
end
private
def open_nordvpn_connection
tmp_file = "#{File.expand_path('~')}/.nordvpn.ovpn"
# copy config to tmp file
`cat #{openvpn_file} > #{tmp_file}`
# implement auth
open('.nordvpn.ovpn', 'a') { |f| f.puts "auth-user-pass #{File.expand_path('~')}/.nord_vpn_auth" }
# open vpn
exec("sudo openvpn #{tmp_file}")
end
def checksumfile
"#{Dir.home}\/.nordvpn_checksum"
end
def nordvpn_vpn_dir
"#{Dir.home}\/.nordvpn"
end
def openvpn_file
# use a random server
@openvpn_file ||= `find #{nordvpn_vpn_dir} | grep "^#{nordvpn_vpn_dir}\/ovpn_udp\/#{country}.*\.udp"`.lines.sample(1).first.strip
end
def ensure_nordvpn_dir_exists
`mkdir -p #{nordvpn_vpn_dir}`
end
def ensure_checksumfile_exists
File.write(checksumfile, '') unless File.file?(checksumfile)
end
def age_in_days
(Time.now.to_i - File.mtime(checksumfile).to_i).to_f / (60*60*24)
end
def check_if_new_servers_are_available_and_update_config_files_if_so
# download openvpn files: https://nordvpn.com/tutorials/linux/openvpn/
puts "Checking for updates…"
Dir.mktmpdir do |dir|
filepath = "#{dir}/ovpn.zip"
`curl -s https://downloads.nordcdn.com/configs/archives/servers/ovpn.zip > #{filepath}`
checksum = Digest::SHA256.file(filepath)
return if checksum == File.read(checksumfile)
puts "Updating…"
File.write(checksumfile, checksum)
`rm -rf #{nordvpn_vpn_dir}`
`mkdir -p #{nordvpn_vpn_dir}`
`unzip #{filepath} -d #{nordvpn_vpn_dir}`
end
end
def update_required?
age_in_days > 6 || Dir[File.join(nordvpn_vpn_dir, '**', '*')].count <= 0
end
end
NordVPNConnection.new(ARGV.first || 'ch')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment