strawman 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ examples/example.rb
5
+ lib/strawman.rb
6
+ lib/strawman/http_request.rb
7
+ lib/strawman/proxy.rb
8
+ lib/strawman/proxy_list.rb
9
+ lib/strawman/source.rb
10
+ strawman.gemspec
data/README.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = Strawman
2
+
3
+ A ruby gem which allows you to proxy EventMachine HTTP GET requests through
4
+ glype proxies on the net.
5
+
6
+ == Getting started
7
+
8
+ # make sure you have gemcutter.org repos enabled
9
+ gem install strawman
10
+
11
+ == A simple example
12
+
13
+ require 'rubygems'
14
+ require 'eventmachine'
15
+ require 'em-http'
16
+ $LOAD_PATH << "../lib/"
17
+ require 'strawman'
18
+
19
+ EventMachine.run {
20
+ proxy_list = Strawman::ProxyList.new
21
+ sources_set = proxy_list.set_sources([Strawman::TwitterSource.new("proxy_sites")])
22
+ sources_set.callback{
23
+ http = Strawman::HttpRequest.new(proxy_list, 'http://goingtorain.com/').get
24
+ http.callback {
25
+ p http.response
26
+ EventMachine.stop
27
+ }
28
+ }
29
+ }
30
+
31
+ == TODO
32
+
33
+ Patches happily accepted, please open a github ticket and attach the patch.
34
+
35
+ * specs need to be implemented
36
+ * POST doesn't work
37
+ * Cookies don't work
38
+ * SSL doesn't work
39
+ * Implement other sources
40
+
41
+ == Limitations
42
+
43
+ * PUT and DELETE can't work do to the way Glype is implemented
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('strawman', '0.1') do |p|
6
+ p.description = "Allows you fetch pages using glype proxies."
7
+ p.url = "http://github.com/mattcolyer/strawman"
8
+ p.author = "Matt Colyer"
9
+ p.email = "matt @nospam@ colyer.name"
10
+ p.ignore_pattern = []
11
+ p.development_dependencies = ["rspec"]
12
+ p.dependencies = ["eventmachine", "em-http-request", "json"]
13
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'eventmachine'
3
+ require 'em-http'
4
+ $LOAD_PATH << "../lib/"
5
+ require 'strawman'
6
+
7
+ EventMachine.run {
8
+ proxy_list = Strawman::ProxyList.new
9
+ sources_set = proxy_list.set_sources([Strawman::TwitterSource.new("proxy_sites")])
10
+ sources_set.callback{
11
+ http = Strawman::HttpRequest.new(proxy_list, 'http://goingtorain.com/').get
12
+ http.callback {
13
+ p http.response_headers.inspect
14
+ p http.response
15
+ EventMachine.stop
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,18 @@
1
+ module Strawman
2
+ class HttpRequest
3
+ def initialize(proxy_list, url)
4
+ @proxy = proxy_list.proxy
5
+ proxied_url = @proxy.proxy_url(url)
6
+ @request = EventMachine::HttpRequest.new(proxied_url)
7
+ end
8
+
9
+ def get
10
+ http = @request.get :head => {"referer" => @proxy.referer}
11
+ http.callback {
12
+ # TODO: Munge the return output so that the stuff added by Glype is
13
+ # removed
14
+ }
15
+ http
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ module Strawman
2
+ class Proxy
3
+ end
4
+
5
+ class GlypeProxy < Proxy
6
+ def initialize(url)
7
+ @root_url = url
8
+ @valid = false
9
+ end
10
+
11
+ def valid?
12
+ @valid
13
+ end
14
+
15
+ def validate
16
+ url = proxy_url("http://whatismyip.org")
17
+ # FIXME: This only validate proxies that don't require a unique session
18
+ # cookie which is retrieved by going to the root page and looking for the
19
+ # s cookie.
20
+ http = EventMachine::HttpRequest.new(url).get :head => {'referer' => @root_url}
21
+ http.callback {
22
+ @valid = true if http.response_header.status == 200
23
+ }
24
+
25
+ http
26
+ end
27
+
28
+ def to_s
29
+ "<GlypeProxy #{@root_url}>"
30
+ end
31
+
32
+ def referer
33
+ @root_url
34
+ end
35
+
36
+ def proxy_url(url)
37
+ URI.join @root_url, proxy_path(url)
38
+ end
39
+
40
+ def proxy_path(url)
41
+ encoded_url = CGI.escape(Base64.encode64(url[4..-1]))
42
+ "/browse.php?u=#{encoded_url}&f=norefer"
43
+ end
44
+ end
45
+
46
+ class PhpProxy < Proxy
47
+ def valid?
48
+ false
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,38 @@
1
+ module Strawman
2
+ class ProxyList
3
+ def initialize
4
+ @proxies = []
5
+ end
6
+
7
+ def set_sources(sources)
8
+ sources_ready = EventMachine::MultiRequest.new
9
+
10
+ sources.each do |source|
11
+ sources_ready.add(source)
12
+ end
13
+
14
+ proxies_ready = EventMachine::MultiRequest.new
15
+ sources_ready.callback do
16
+ sources.each do |source|
17
+ source.proxies.each do |proxy|
18
+ proxies_ready.add(proxy.validate)
19
+ end
20
+ end
21
+ end
22
+
23
+ proxies_ready.callback do
24
+ sources.each do |source|
25
+ source.proxies.each do |proxy|
26
+ @proxies << proxy if proxy.valid?
27
+ end
28
+ end
29
+ end
30
+
31
+ proxies_ready
32
+ end
33
+
34
+ def proxy
35
+ @proxies.choice
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,72 @@
1
+ module Strawman
2
+ class Source
3
+ end
4
+
5
+ class TwitterSource < Source
6
+ include EventMachine::Deferrable
7
+ ONE_HOUR = 60*60
8
+ attr_reader :proxies
9
+
10
+ def initialize(twitter_username)
11
+ @id = twitter_username
12
+
13
+ fetched = update_cache
14
+ fetched.callback do
15
+ @proxies = JSON.parse(read_cache).map do |status|
16
+ match = /.*(http:\/\/.*)/.match(status["text"])
17
+ if match
18
+ GlypeProxy.new(match[1])
19
+ else
20
+ nil
21
+ end
22
+ end.compact
23
+ set_deferred_status :succeeded
24
+ end
25
+ end
26
+
27
+ private
28
+ def cache_dir
29
+ "cache"
30
+ end
31
+
32
+ def cache_file_path
33
+ File.join cache_dir, "#{@id}.json"
34
+ end
35
+
36
+ def cache_file_url
37
+ "http://twitter.com/statuses/user_timeline/#{@id}.json"
38
+ end
39
+
40
+ def read_cache
41
+ File.read(cache_file_path)
42
+ end
43
+
44
+ def update_cache
45
+ begin
46
+ f = File.new(cache_file_path)
47
+ seconds_since_update = (Time.now - f.mtime)
48
+ if seconds_since_update > ONE_HOUR
49
+ fetch
50
+ else
51
+ d = EventMachine::Deferrable.new
52
+ d.set_deferred_success
53
+ end
54
+ rescue Exception
55
+ fetch
56
+ end
57
+ end
58
+
59
+ def fetch
60
+ http = EventMachine::HttpRequest.new(cache_file_url).get
61
+
62
+ http.callback do
63
+ FileUtils.mkdir(cache_dir) unless File.exist? cache_dir
64
+ open(cache_file_path, "w") do |f|
65
+ f.write(http.response)
66
+ end
67
+ end
68
+
69
+ http
70
+ end
71
+ end
72
+ end
data/lib/strawman.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'base64'
2
+ require 'json'
3
+ require 'cgi'
4
+ require 'strawman/http_request'
5
+ require 'strawman/proxy'
6
+ require 'strawman/proxy_list'
7
+ require 'strawman/source'
data/strawman.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{strawman}
5
+ s.version = "0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Matt Colyer"]
9
+ s.cert_chain = ["/home/mcolyer/.ssh/gem-public_cert.pem"]
10
+ s.date = %q{2010-02-13}
11
+ s.description = %q{Allows you fetch pages using glype proxies.}
12
+ s.email = %q{matt @nospam@ colyer.name}
13
+ s.extra_rdoc_files = ["README.rdoc", "lib/strawman.rb", "lib/strawman/http_request.rb", "lib/strawman/proxy.rb", "lib/strawman/proxy_list.rb", "lib/strawman/source.rb"]
14
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "examples/example.rb", "lib/strawman.rb", "lib/strawman/http_request.rb", "lib/strawman/proxy.rb", "lib/strawman/proxy_list.rb", "lib/strawman/source.rb", "strawman.gemspec"]
15
+ s.homepage = %q{http://github.com/mattcolyer/strawman}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Strawman", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{strawman}
19
+ s.rubygems_version = %q{1.3.5}
20
+ s.signing_key = %q{/home/mcolyer/.ssh/gem-private_key.pem}
21
+ s.summary = %q{Allows you fetch pages using glype proxies.}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<eventmachine>, [">= 0"])
29
+ s.add_runtime_dependency(%q<em-http-request>, [">= 0"])
30
+ s.add_runtime_dependency(%q<json>, [">= 0"])
31
+ s.add_development_dependency(%q<rspec>, [">= 0"])
32
+ else
33
+ s.add_dependency(%q<eventmachine>, [">= 0"])
34
+ s.add_dependency(%q<em-http-request>, [">= 0"])
35
+ s.add_dependency(%q<json>, [">= 0"])
36
+ s.add_dependency(%q<rspec>, [">= 0"])
37
+ end
38
+ else
39
+ s.add_dependency(%q<eventmachine>, [">= 0"])
40
+ s.add_dependency(%q<em-http-request>, [">= 0"])
41
+ s.add_dependency(%q<json>, [">= 0"])
42
+ s.add_dependency(%q<rspec>, [">= 0"])
43
+ end
44
+ end
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strawman
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Matt Colyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDLjCCAhagAwIBAgIBADANBgkqhkiG9w0BAQUFADA9MQ0wCwYDVQQDDARtYXR0
14
+ MRYwFAYKCZImiZPyLGQBGRYGY29seWVyMRQwEgYKCZImiZPyLGQBGRYEbmFtZTAe
15
+ Fw0xMDAyMTQwMTI5NDRaFw0x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-08:00
34
+ default_executable:
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: eventmachine
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ - !ruby/object:Gem::Dependency
47
+ name: em-http-request
48
+ type: :runtime
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ - !ruby/object:Gem::Dependency
57
+ name: json
58
+ type: :runtime
59
+ version_requirement:
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ - !ruby/object:Gem::Dependency
67
+ name: rspec
68
+ type: :development
69
+ version_requirement:
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ description: Allows you fetch pages using glype proxies.
77
+ email: matt @nospam@ colyer.name
78
+ executables: []
79
+
80
+ extensions: []
81
+
82
+ extra_rdoc_files:
83
+ - README.rdoc
84
+ - lib/strawman.rb
85
+ - lib/strawman/http_request.rb
86
+ - lib/strawman/proxy.rb
87
+ - lib/strawman/proxy_list.rb
88
+ - lib/strawman/source.rb
89
+ files:
90
+ - Manifest
91
+ - README.rdoc
92
+ - Rakefile
93
+ - examples/example.rb
94
+ - lib/strawman.rb
95
+ - lib/strawman/http_request.rb
96
+ - lib/strawman/proxy.rb
97
+ - lib/strawman/proxy_list.rb
98
+ - lib/strawman/source.rb
99
+ - strawman.gemspec
100
+ has_rdoc: true
101
+ homepage: http://github.com/mattcolyer/strawman
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options:
106
+ - --line-numbers
107
+ - --inline-source
108
+ - --title
109
+ - Strawman
110
+ - --main
111
+ - README.rdoc
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: "0"
119
+ version:
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: "1.2"
125
+ version:
126
+ requirements: []
127
+
128
+ rubyforge_project: strawman
129
+ rubygems_version: 1.3.5
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: Allows you fetch pages using glype proxies.
133
+ test_files: []
134
+
metadata.gz.sig ADDED
Binary file