Skip to content

Commit

Permalink
Refactors server code to cache servers
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Oct 22, 2024
1 parent dec7818 commit a774a0d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 86 deletions.
9 changes: 6 additions & 3 deletions lib/mullvadrb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
require 'mullvadrb/connection'
require 'mullvadrb/servers'

include Mullvadrb::Servers
@servers = load_servers

def main_menu
TTY::Prompt.new.select('Select', cycle: true, per_page: 10) do |menu|
menu.choice name: '📡 Status', value: 'status'
Expand All @@ -31,16 +34,16 @@ def main_menu
when 'disconnect'
Mullvadrb::Connection.disconnect
when 'country'
Mullvadrb::Servers.select_country
select_country
Mullvadrb::Connection.connect
when 'specific'
Mullvadrb::Servers.select_specific
Mullvadrb::Connection.connect
when 'random'
Mullvadrb::Servers.random
random
Mullvadrb::Connection.connect
when 'update_servers'
Mullvadrb::Servers.update
update
when 'account_login'
Mullvadrb::Account.login(
TTY::Prompt.new.ask('Please enter your account number:')
Expand Down
168 changes: 85 additions & 83 deletions lib/mullvadrb/servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,105 @@ module Mullvadrb
module Servers
SERVERS_FILE = File.expand_path('~/.local/share/mullvadrb.dump').freeze

class << self
def update
`mullvad relay update`
data = `mullvad relay list`
country, city, info, flag, value = nil
def update
`mullvad relay update`
data = `mullvad relay list`
country, city, info, flag, value = nil

# Each line is either a country, a city or a server
servers = data.split("\n").compact.reject(&:empty?).map do |s|
if s.start_with?("\t\t")
info = s.gsub("\t\t", '')
{ country: country, city: city, info: info, flag: flag, value: info.split(' ').first }
elsif s.start_with?("\t")
city = s.gsub("\t", '').split("(").first.strip
next
else
regexp = /\(([a-z]{2})\)/ # Country (xx) - Country name + code and group code
flag = s.match(regexp)[1]
.upcase
.codepoints
.map { |c| (c + 127397).chr('utf-8') }.join
country = s.gsub(regexp, '').strip
next
end
end.compact
# Each line is either a country, a city or a server
servers = data.split("\n").compact.reject(&:empty?).map do |s|
if s.start_with?("\t\t")
info = s.gsub("\t\t", '')
{ country: country, city: city, info: info, flag: flag, value: info.split(' ').first }
elsif s.start_with?("\t")
city = s.gsub("\t", '').split("(").first.strip
next
else
regexp = /\(([a-z]{2})\)/ # Country (xx) - Country name + code and group code
flag = s.match(regexp)[1]
.upcase
.codepoints
.map { |c| (c + 127397).chr('utf-8') }.join
country = s.gsub(regexp, '').strip
next
end
end.compact

save_servers(servers)
puts '🗃 Server list updated'
puts
end
save_servers(servers)
puts '🗃 Server list updated'
puts
end

def select_country
servers = load_servers
country = TTY::Prompt.new.select('Select country', countries(servers), cycle: true, per_page: 10, filter: true, symbols: { marker: '🛫' })
connection = servers.select do |s|
s[:country] == country
end.sample
puts `mullvad relay set location #{connection[:value]}`
end
def select_country
servers = @servers
country = TTY::Prompt.new.select('Select country', countries(servers), cycle: true, per_page: 10, filter: true, symbols: { marker: '🛫' })
connection = servers.select do |s|
s[:country] == country
end.sample
puts `mullvad relay set location #{connection[:value]}`
end

def random
server = all_connections.sample
puts "Connecting to #{server[:name]}"
puts `mullvad relay set location #{server[:value]}`
end
def random
server = all_connections.sample
puts "Connecting to #{server[:name]}"
puts `mullvad relay set location #{server[:value]}`
end

def select_specific
connections = all_connections
server = TTY::Prompt.new.select(
'Select configuration file',
connections,
cycle: true,
per_page: 10,
filter: true,
symbols: { marker: '🛫' }
)
puts `mullvad relay set location #{server}`
end
def select_specific
connections = all_connections
server = TTY::Prompt.new.select(
'Select server',
connections,
cycle: true,
per_page: 10,
filter: true,
symbols: { marker: '🛫' }
)
puts `mullvad relay set location #{server}`
end

def servers
@servers ||= load_servers
end

private
private

def countries(servers)
servers.uniq { |s| s[:country] }.map do |server|
{
name: "#{server[:flag]} #{server[:country]}",
value: server[:country]
}
end
def countries(servers)
servers.uniq { |s| s[:country] }.map do |server|
{
name: "#{server[:flag]} #{server[:country]}",
value: server[:country]
}
end
end

def all_connections
load_servers.map do |server|
{
name: "#{server[:flag]} #{server[:country]} - #{server[:city]}: #{server[:info]}",
value: server[:value]
}
end
def all_connections
servers.map do |server|
{
name: "#{server[:flag]} #{server[:country]} - #{server[:city]}: #{server[:info]}",
value: server[:value]
}
end
end

def emoji_from_code(code)
code.upcase
.codepoints
.map { |c| (c + 127397).chr('utf-8') }.join
end
def emoji_from_code(code)
code.upcase
.codepoints
.map { |c| (c + 127397).chr('utf-8') }.join
end

def load_servers
servers = File.expand_path(SERVERS_FILE)
if File.file?(servers)
Marshal.load(File.read(servers))
else
update
end
def load_servers
servers = File.expand_path(SERVERS_FILE)
if File.file?(servers)
Marshal.load(File.read(servers))
else
update
end
end

def save_servers(servers)
File.open(SERVERS_FILE, 'w+') do |f|
f.write(Marshal.dump(servers))
end
def save_servers(servers)
File.open(SERVERS_FILE, 'w+') do |f|
f.write(Marshal.dump(servers))
end
end
end
Expand Down

0 comments on commit a774a0d

Please sign in to comment.