-
Notifications
You must be signed in to change notification settings - Fork 2
/
go.rb
executable file
·296 lines (247 loc) · 6.81 KB
/
go.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/ruby -Ilib
require 'base64'
require 'optparse'
require 'yaml'
require 'rubygems'
require 'highline/import'
require 'mechanize'
require 'scrapethissite'
def get_password(options)
password = options[:password] || nil
filename = options[:filename]
if File.size?(filename).nil?
password1 = password || '1'
password2 = password || '2'
while password1 != password2
password1 = ask("Choose a password for ScrapeThis|Site: ") { |q| q.echo = '*' }
password2 = ask("Confirm your ScrapeThis|Site password: ") { |q| q.echo = '*' }
end
password = password1
else
options.merge! YAML.load(File.read(filename))
begin
password = options[:password] ||
ask("Enter your ScrapeThis|Site password: ") { |q|
q.echo = '*'
}
begin
options[:password] = password
decrypt(options)
rescue OpenSSL::Cipher::CipherError => e
puts " Wrong password!"
options[:password] = nil
end
end while options[:password].nil?
end
end
def make_key(options)
pbkdf2 = options[:encryption][:pbkdf2]
if pbkdf2[:digest] == 'SHA1'
return OpenSSL::PKCS5.pbkdf2_hmac_sha1(
options[:password],
Base64.decode64(pbkdf2[:salt]),
pbkdf2[:iter],
options[:encryption][:alg].split(/-/)[1].to_i
)
else
return OpenSSL::PKCS5.pbkdf2_hmac(
options[:password],
Base64.decode64(pbkdf2[:salt]),
pbkdf2[:iter],
options[:encryption][:alg].split(/-/)[1].to_i,
pbkdf2[:digest]
)
end
end
def encrypt(options)
encrypter = OpenSSL::Cipher.new options[:encryption][:alg]
encrypter.encrypt
encrypter.key = make_key options
encrypter.random_iv
output = {
:encryption => {
:alg => options[:encryption][:alg],
:iv => nil,
:pbkdf2 => {
:salt => options[:encryption][:pbkdf2][:salt],
:iter => options[:encryption][:pbkdf2][:iter],
:digest => options[:encryption][:pbkdf2][:digest]
}
},
:encrypted => nil
}
encrypter = OpenSSL::Cipher.new options[:encryption][:alg]
encrypter.encrypt
encrypter.key = make_key options
output[:encryption][:iv] = Base64.encode64 encrypter.random_iv
output[:encrypted] = Base64.encode64(
encrypter.update(options[:decrypted].to_yaml) + encrypter.final
)
filename = options[:filename]
File.open(".#{filename}", 'w') { |file|
file.write output.to_yaml
}
File.rename(".#{filename}", filename)
end
def decrypt(options)
decrypter = OpenSSL::Cipher.new options[:encryption][:alg]
decrypter.decrypt
decrypter.key = make_key options
decrypter.iv = Base64.decode64 options[:encryption][:iv]
options[:decrypted] = YAML.load(
decrypter.update(
Base64.decode64(options[:encrypted])
) + decrypter.final
)
end
def list(options)
filename = options[:filename]
if File.size?(filename).nil?
STDERR.puts("you need to --add some sites first!")
exit 1
end
get_password(options)
decrypt(options)
pp(options[:decrypted])
exit 0
end
def die_add(name, sources, sinks)
if name.is_a?(String)
STDERR.puts "Unknown source or sink: #{name}"
STDERR.puts
end
STDERR.puts 'ScrapeThis|Site supports the following sources:'
sources.keys.sort.each { |key|
STDERR.puts " * #{key} - #{sources[key].url}"
}
STDERR.puts
STDERR.puts 'ScrapeThis|Site supports the following sinks:'
sinks.keys.sort.each { |key|
STDERR.puts " * #{key} - #{sinks[key].url}"
}
STDERR.puts
exit 1
end
def add(options)
name = options[:add]
sources = ScrapeThisSite::Sources.all
sinks = ScrapeThisSite::Sinks.all
clazz = sources[name] || sinks[name]
die_add(name, sources, sinks) if clazz.nil?
get_password(options)
filename = options[:filename]
if File.size?(filename).nil?
options[:decrypted] = {'sources' => [], 'sinks' => []}
else
options.merge! YAML.load(File.read(filename))
decrypt(options)
end
settings = {}
service = {
'class' => clazz.name,
'settings' => settings
}
puts "Please answer the following questions to configure access to #{name}:"
clazz.questions.each { |question|
answer = ask(" #{question.prompt} ") { |q|
q.echo = '*' if question.sensitive?
}
settings[question.key] = answer
}
# FIXME Test credentials before saving them
options[:decrypted]['sources'] << service if clazz.source?
options[:decrypted]['sinks'] << service if clazz.sink?
encrypt(options)
exit 0
end
def run(options)
filename = options[:filename]
if File.size?(filename).nil?
STDERR.puts("you need to --add some sites first!")
exit 1
end
get_password(options)
decrypt(options)
sinks = []
options[:decrypted]['sinks'].each { |c|
clazz = c['class'].sub(/.*:/, '')
begin
sinks << ScrapeThisSite::Sinks.const_get(clazz).new(c['settings'])
rescue NameError => e
puts "unsupported sink: #{clazz}"
puts e.inspect
end
}
options[:decrypted]['sources'].each { |c|
mech = Mechanize.new { |agent|
agent.user_agent_alias = 'Linux Mozilla'
}
clazz = c['class'].sub(/.*:/, '')
source = nil
begin
source = ScrapeThisSite::Sources.const_get(clazz).new(mech, c['settings'])
rescue NameError => e
puts "unsupported source: #{clazz}"
puts e.inspect
next
end
history = c['history'] || []
(source.statements - history).each { |stmt|
puts stmt
sinks.each { |sink|
sink.save( source.statement(stmt) )
}
history << stmt
c['history'] = history
encrypt(options)
}
}
end
options = {
:filename => 'sts.cf',
:encryption => {
:alg => 'AES-256-CBC',
:iv => nil,
:pbkdf2 => {
:salt => Base64.encode64(Random.new.bytes(8)),
:iter => 5000,
:digest => 'SHA1'
}
}
}
opts = OptionParser.new { |opts|
opts.banner = "usage: #{$0} [options]"
opts.separator ''
opts.separator 'Modes:'
opts.on('-r', '--run', 'runs ScrapeThis|Site') {
options[:run] = true
}
opts.on('-l', '--list', 'prints the current configuration') {
options[:list] = true
}
opts.on('-a', '--add [WEBSITE]', 'adds a new source or sink') { |s|
options[:add] = s
}
opts.separator ''
opts.separator 'Shared options:'
opts.on('-h', '--help', 'prints this usage statement') {
puts opts
exit 0
}
opts.on('-f', '--filename', "specifies the config file to use,", "defaults to #{options[:filename]}") { |filename|
options[:filename] = filename
}
opts.on('-p', '--password PASSWORD', "your ScrapeThis|Site password") { |password|
options[:password] = password
}
}
opts.parse!
if options[:list]
list(options)
elsif options.has_key?(:add)
add(options)
elsif options[:run]
run(options)
end
STDERR.puts opts.help
exit 1