-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyserver.rb
123 lines (111 loc) · 2.78 KB
/
keyserver.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require 'rubygems'
require 'sinatra'
require 'data_mapper'
require 'time_difference'
debug = true
# set up in-memory db
DataMapper.setup(:default, 'sqlite::memory:')
# Key class to store in db
class KeyInfo
include DataMapper::Resource
property :id, Integer, :key => true
property :state, String
property :timestamp, DateTime
end
DataMapper.finalize
KeyInfo.auto_upgrade!
def log_info
puts "Total records in DB: #{KeyInfo.count}"
puts "Available keys: #{KeyInfo.count(:state=>"available")}"
puts "Blocked keys: #{KeyInfo.count(:state=>"blocked")}"
puts "Purged keys: #{KeyInfo.count(:state=>"purged")}"
end
# Endpoint E1: Generate a random key
get '/generate' do
puts "Generating random key"
key = nil
while key == nil do
key = rand(1000000)
puts "Key is #{key}"
if KeyInfo.get(key)
puts "Key:#{key} already exists"
key = nil
else
KeyInfo.create(
:id => key,
:state => "available",
:timestamp => Time.now
)
end
end
log_info
"#{key}"
end
# Endpoint E2: Get available key
get '/getkey' do
puts "Getting a key"
key = KeyInfo.first(:state => "available")
if key == nil
# Show 404
error 404
end
key.update(:state => "blocked", :timestamp => Time.now)
log_info
"#{key.id}"
end
# Endpoint E3: Unblock a key
get '/unblock/:key_id' do |key_id|
puts "Unblocking a key: #{key_id}"
key = KeyInfo.get(key_id)
if key == nil
error 404
end
key.update(:state => "available", :timestamp => Time.now)
log_info
"#{key.id}"
end
# Endpoint E4: Delete
get '/delete/:key_id' do |key_id|
puts "Purging the key: #{key_id}"
key = KeyInfo.get(key_id)
if key == nil
error 404
end
key.update(:state => "purged", :timestamp => Time.now)
log_info
"#{key.id}"
end
# Endpoint E5: Keep alive
get '/keep_alive/:key_id' do |key_id|
puts "Keep alive for key: #{key_id}"
key = KeyInfo.get(key_id)
if key == nil || key.state != "available"
error 404
end
key.update(:timestamp => Time.now)
log_info
"#{key.id}"
end
# Updater thread
updater_thr = Thread.new {
while true do
KeyInfo.all.each do |key_record|
puts "KeyRecord Id: #{key_record.id} and state:#{key_record.state}" if debug
current_time = Time.now
if key_record.state == "available"
if TimeDifference.between(key_record.timestamp, current_time).in_seconds >= 300
puts "Purging the key #{key_record.id}"
# Purge this key
key_record.update(:state => "purged")
end
elsif key_record.state == "blocked"
if TimeDifference.between(key_record.timestamp, current_time).in_seconds >= 60
# Unblock this key
puts "Updating the key #{key_record.id}"
key_record.update(:state => "available")
end
end
end
sleep(1)
end
}