-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.rb
63 lines (48 loc) · 1.32 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# encoding: utf-8
query = "{query}"
require "openssl"
require "base64"
require "net/http"
require "json"
load "alfred_workflow.rb"
load "crypt.rb"
keepass_server = "http://localhost:19455"
key_name = "MBP-Chrome"
aes_key = "DBn3Wrqd7rFHu8cgPXCnEzIWgaUXcRXzOplkskcy9fo="
# url = "http://" + query
url = ARGV[0]
iv = OpenSSL::Cipher::Cipher.new("AES-256-CBC").random_iv
nonce = b64enc(iv)
veri = encrypt(nonce, aes_key, iv)
url = encrypt(url, aes_key, iv)
query_body = {
RequestType: "get-logins",
Id: key_name,
Nonce: nonce,
Verifier: veri,
Url: url,
}.to_json
# puts query_body
uri = URI(keepass_server)
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.body = query_body
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
# puts res.body
body = JSON.parse(res.body)
resp_nonce = body["Nonce"]
# puts body["Entries"]
alfred_xml = AlfredXmlFormatter.new
entries = body["Entries"]
entries.each do |entry|
title = entry["Name"]
username = entry["Login"]
password = entry["Password"]
title_plain = decrypt(title, aes_key, resp_nonce)
username_plain = decrypt(username, aes_key, resp_nonce)
password_plain = decrypt(password, aes_key, resp_nonce)
kp = KeepassItem.new(title_plain, username_plain, password_plain)
alfred_xml.add_kp(kp)
end
puts alfred_xml.to_string