forked from refractalize/nginx_mod_akamai_g2o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl_g2o
executable file
·93 lines (75 loc) · 2.16 KB
/
curl_g2o
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
#!/usr/bin/env ruby
require 'net/http'
require 'digest'
require 'base64'
require 'openssl'
class ArgumentsError < Exception
def initialize(msg)
super(msg)
end
def explain
$stderr.puts message
$stderr.puts "usage: curl_g2o [curl options] --g2o-token token --g2o-password password url"
$stderr.puts "url must be a fully qualified url, starting with HTTP or HTTPS"
end
end
def parse_token_from_arguments(argv)
args = {:args => argv}
parse_argument(args, 'token', '--g2o-token')
parse_argument(args, 'password', '--g2o-password')
args[:url] = parse_url_from_arguments(args[:args])
args
end
def parse_argument(args, arg_description, arg)
index = args[:args].index(arg)
if index
value = args[:args][index + 1]
args[:args].delete_at(index)
args[:args].delete_at(index)
args[arg_description.intern] = value
else
raise ArgumentsError.new("no #{arg_description} found")
end
end
def parse_url_from_arguments(args)
uri_string = args[-1]
begin
uri = URI.parse(uri_string)
raise URI::InvalidURIError unless uri.host
raise URI::InvalidURIError unless uri.path
uri
rescue URI::InvalidURIError
raise ArgumentsError.new("invalid url: `#{uri_string}'")
end
end
def g2o_data_header(token)
"3, 69.31.17.132, 80.169.32.154, #{Time.now.to_i}, 13459971.1599924223, #{token}"
end
def get(uri)
Net::HTTP.start(uri.host, uri.port) do |http|
headers = {}
data = g2o_data_header
headers["X-Akamai-G2O-Auth-Data"] = data
headers["X-Akamai-G2O-Auth-Sign"] = sign_data(uri, data)
http.get(uri.path, headers)
end
end
def sign_data(uri, data, options = {})
key = (options[:key] or 'some password')
digest = OpenSSL::HMAC.digest('md5', key, data + uri.path)
Base64.encode64(digest).chomp
end
def exec_curl(data, sign, other_arguments)
p sign
cmd = %{curl -H "X-Akamai-G2O-Auth-Data: #{data}" -H "X-Akamai-G2O-Auth-Sign: #{sign}" #{other_arguments}}
puts cmd
exec cmd
end
begin
args = parse_token_from_arguments(ARGV)
data = g2o_data_header(args[:token])
sign = sign_data(args[:url], data)
exec_curl(data, sign, args[:args].join(' '))
rescue ArgumentsError => e
e.explain
end