Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save iMerica/dfb1be6e2f664c716756 to your computer and use it in GitHub Desktop.
Save iMerica/dfb1be6e2f664c716756 to your computer and use it in GitHub Desktop.
Quickly convert all of your Viscosity connections into OVPN configuration files for OpenVPN for iOS (bundles certificates and keys in the files too)
Dir.glob("#{ENV['HOME']}/Library/Application Support/Viscosity/OpenVPN/*/config.conf").each do |file|
certificate_files = ['ca', 'cert', 'key', 'tls-auth']
config_dir = File.dirname(file)
connection_name = nil
new_config = []
File.read(file).lines.each do |line|
line.strip!
if line.start_with?('#viscosity name')
connection_name = line.match(/^#viscosity name (.*)/)[1]
next
end
next if line.start_with?('#')
(key, value) = line.split(/\s+/, 2)
if certificate_files.include?(key)
# Special case for tls-auth which is "key direction"
if key == 'tls-auth'
# add direction to config
(value, direction) = value.split(/\s+/)
new_config << "key-direction #{direction}" unless direction.nil?
end
certificate = File.read("#{config_dir}/#{value}")
new_config << "<#{key}>"
new_config << certificate
new_config << "</#{key}>"
next
end
new_config << line
end
raise "Unable to find connection name in #{file}. Aborting." if connection_name.nil?
new_config.unshift("# OpenVPN Config for #{connection_name}")
out_file = "#{connection_name}.ovpn"
File.open(out_file, 'w') { |f| f.write(new_config.join("\n") + "\n") }
puts "wrote #{out_file}"
end
@nickjwebb
Copy link

Spectacular, thanks!

@johnnybfireman
Copy link

Hi and thanks for writing this code. Unfortunately, I am a nooooooob and am not sure how to make this work. I have downloaded the zip and unzipped it to the directory the contains my visc file. I've tried to run it from cli. No luck. I have changed it to executable and it throws an error. Any advice on how to run this to convert my file will be greatly appreciated. I am running it on a Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment