-
Notifications
You must be signed in to change notification settings - Fork 38
/
webmention.rb
294 lines (249 loc) · 8.74 KB
/
webmention.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
class Controller < Sinatra::Base
# Chances are some people will click the links in the href tags, so show a nice message here
get '/:username/xmlrpc' do |username|
title "Hosted Pingback Service"
@username = username
error 200, erb(:endpoint)
end
get '/:username/webmention' do |username|
title "Hosted Webmention Service"
@username = username
error 200, erb(:endpoint)
end
# Webmention status page
get '/:username/webmention/:token' do |username, token|
status = @redis.get "webmention:status:#{token}"
if status
json_response 200, JSON.parse(status)
else
json_response 404, {
:error => 'not_found',
}
end
end
# Receive Webmentions
post '/:username/webmention' do |username|
validate_parameters params[:source], params[:target]
# First check that the domain of the target exists on this account
account = Account.first :domain => username
if account.nil?
json_response 404, {
:error => 'not_found',
:error_description => "account #{username} not found"
}
end
target_uri = URI.parse(URI.escape(params[:target]))
target_domain = target_uri.host
# Check that the domain of the target URL is a registered site on this account
site = Site.first :account => account, :domain => target_domain
if site.nil?
json_response 404, {
:error => 'invalid_target',
:error_description => "target domain not found on this account"
}
end
process_webmention(username, 'account')
end
post '/d/:domain/webmention' do |domain|
validate_parameters params[:source], params[:target]
# First check that the domain of the target exists
site = Site.first :domain => domain
if site.nil?
json_response 404, {
:error => 'not_found',
:error_description => "site #{domain} not found"
}
end
target_uri = URI.parse(URI.escape(params[:target]))
target_domain = target_uri.host
# Check that the target domain of the webmention matches the domain of the endpoint
if target_domain != site.domain
json_response 400, {
:error => 'invalid_target',
:error_description => "Target domain (#{target_domain}) does not match the domain of this webmention endpoint (#{domain})"
}
end
process_webmention(site.account.username, 'site')
end
def process_webmention(username, endpoint_type)
hash = OpenSSL::Digest::MD5.hexdigest("s=#{params[:source]};t=#{params[:target]}")
if @redis.get "webmention:ratelimit:#{hash}"
json_response 429, {
:error => 'rate_limit_exceeded',
:error_description => 'Only one request per source and target combination is allowed every 30 seconds'
}
end
@redis.setex "webmention:ratelimit:#{hash}", 30, 1
token = SecureRandom.urlsafe_base64 15
status_url = "#{SiteConfig.base_url}/#{username}/webmention/#{token}"
# Swap Twitter URLs for Bridgy proxy URLs
if m=params[:source].match(/https?:\/\/(?:www\.)?twitter\.com\/(.+)\/status(?:es)?\/([0-9]+)/)
params[:source] = "https://brid.gy/post/twitter/#{m[1]}/#{m[2]}"
end
puts "#{DateTime.now} WM: source=#{params[:source]} target=#{params[:target]}#{params[:code] ? ' private' : ''} ip=#{request.ip} status=#{status_url}"
begin
result = process_mention(username, params[:source], params[:target], 'webmention', token, params[:code], params[:debug], endpoint_type)
rescue => e
puts "!!!!!!!!!!!!!!!!!!!!!"
puts "INTERNAL SERVER ERROR"
puts e.inspect
puts e.backtrace
json_response 500, {
:error => 'internal_server_error',
:error_description => e.message
}
end
case result
when 'queued'
code = accept_html ? 303 : 201 # send browsers a 303 so they get redirected to the status page
json_response code, {
:status => 'queued',
:summary => 'Webmention was queued for processing',
:location => status_url,
:source => params[:source],
:target => params[:target]
}, {
'Location' => status_url
}
when 'success'
json_response 200, {
:status => 'success',
:summary => 'Webmention was successful'
}
when 'source_not_found'
json_response 400, {
:error => result,
:error_description => 'The source URI does not exist'
}
when 'invalid_target'
json_response 400, {
:error => result,
:error_description => 'The target is not a valid URI'
}
when 'target_not_found'
json_response 400, {
:error => result,
:error_description => 'The target URI does not exist'
}
when 'target_not_supported'
json_response 400, {
:error => result,
:error_description => 'The specified target URI is not a Webmention-enabled resource'
}
when 'no_link_found'
json_response 400, {
:error => result,
:error_description => 'The source URI does not contain a link to the target URI'
}
end
end
# Receive Pingbacks
post '/:username/xmlrpc' do |username|
account = Account.first :username => username
if account.nil?
rpc_error 404, 0, "Not found"
end
if account.pingback_enabled == false
rpc_error 401, 0, "Inactive account"
end
#puts "RECEIVED PINGBACK REQUEST"
utf8 = request.body.read.force_encoding "UTF-8"
# puts utf8
if utf8.valid_encoding?
xml = utf8
else
puts "Invalid string encoding"
rpc_error 400, 0, "Invalid string encoding"
end
begin
method, arguments = XMLRPC::Marshal.load_call(xml)
rescue
rpc_error 400, 0, "Invalid request"
end
method.gsub! /\./, '_'
if method == 'pingback_ping'
content_type("text/xml", :charset => "utf-8")
source, target = arguments
puts "#{DateTime.now} PB s=#{source} t=#{target} ip=#{request.ip}"
validate_parameters source, target
token = SecureRandom.urlsafe_base64 15
begin
result = process_mention(username, source, target, 'pingback', token, nil)
rescue => e
puts "!!!!!!!!!!!!!!!!!!!!!"
puts "INTERNAL SERVER ERROR"
puts e.inspect
rpc_error 500, 0, "Internal Server Error: #{e.message}"
end
case result
when 'queued'
status_url = "#{SiteConfig.base_url}/#{username}/webmention/#{token}"
rpc_respond 200, "Pingback was queued for processing: #{status_url}"
when 'success'
rpc_respond 200, "Pingback from #{source} to #{target} was successful! Keep the web talking!"
when 'source_not_found'
rpc_error 200, 0x0010, "The source URI does not exist"
when 'invalid_target'
rpc_error 200, 0x0021, "The target is not a valid URI"
when 'target_not_found'
rpc_error 200, 0x0020, "The target URI does not exist"
when 'target_not_supported'
rpc_error 200, 0x0021, "The specified target URI is not a Pingback-enabled resource"
when 'no_link_found'
rpc_error 200, 0x0011, "There appears to be no link to this page!"
when 'already_registered'
rpc_error 200, 0x0030, "The pingback has already been registered"
end
else
rpc_error 404, 0, "Not Found"
end
end
def validate_parameters(source, target)
if source.empty? or target.empty?
json_response 400, {
:error => 'invalid_request',
:error_description => 'source or target were missing'
}
end
begin
source = URI.parse(URI.escape(source))
target = URI.parse(URI.escape(target))
if source.host.nil? or target.host.nil?
raise "missing host"
end
if !['http','https'].include?(source.scheme) or !['http','https'].include?(target.scheme)
raise "invalid protocol"
end
rescue => e
json_response 400, {
:error => 'invalid_request',
:error_description => "source or target were invalid",
:error_details => e.message
}
end
end
def process_mention(username, source, target, protocol, token, code, debug=false, endpoint_type='account')
WebmentionProcessor.update_status @redis, token, {
:status => 'pending',
:source => source,
:target => target,
:private => code ? true : false,
:summary => "The webmention is currently being processed",
:data => {}
}
if debug
WebmentionProcessor.new.process_mention username, source, target, protocol, token, code, endpoint_type
else
WebmentionProcessor.new.async.perform(
:username => username,
:source => source,
:target => target,
:protocol => protocol,
:token => token,
:code => code,
:endpoint_type => endpoint_type
)
'queued'
end
end
end