-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.rb
65 lines (62 loc) · 1.7 KB
/
app.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
require 'sinatra'
require 'haml'
require 'open-uri'
require 'erb'
require 'ostruct'
configure do
Templates = Dir['templates/*.erb'].map do |path|
content = File.read path
{
title: path,
content: content,
permalink: "?template=#{URI.escape(content, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
}
end
Cache = OpenStruct.new(
data: nil,
version: Time.now
)
end
def compile
if (template = params[:template].to_s) != ''
templateEncoded = URI.escape template, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
@permalink = "http://chnroutesapp.micy.in/?template=#{templateEncoded}"
@permalink_raw = "http://chnroutesapp.micy.in/raw?template=#{templateEncoded}"
if Time.now - Cache.version > 60
Cache.data = nil
end
if Cache.data
data = Cache.data
else
Cache.version = Time.now
Cache.data = data = open('http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest').read.scan(/^apnic\|cn\|ipv4\|[\d\.]+\|\d+\|\d+\|a\w*$/im).map do |m|
m = m.split '|'
starting_ip = m[3]
num_ip = m[4].to_i
imask = 0xffffffff ^ (num_ip - 1)
imask = imask.to_s 16
mask = []
for i in 1..4
mask << imask.slice(-2 * i, 2).to_i(16)
end
mask = mask.reverse.join '.'
{
ip: starting_ip,
mask: mask,
cidr_mask: 32 - Math.log(num_ip, 2).to_i,
}
end
end
namespace = OpenStruct.new(routes: data)
@result = ERB.new(template).result(namespace.instance_eval { binding })
end
end
get '/' do
compile
@templates = Templates
haml :default
end
get '/raw' do
compile
[200, {"Content-Type" => "text/plain"}, @result.to_s]
end