onion 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ = Onion
2
+
3
+ == Introduction
4
+
5
+ Onion is a library for interacting with Tor. Onion is in an alpha state and is
6
+ made availible for early adopters and contributors only.
7
+
8
+ == Functionality
9
+
10
+ * Parse router status entries per the directory protocol version 2.0.
11
+
12
+ == Authors
13
+
14
+ Onion was written by Tim Sally <poet@stack.io>. Please send bug reports and
15
+ suggestions to the author.
16
+
17
+ == License
18
+
19
+ (The MIT License)
20
+
21
+ Copyright (c) 2010 Tim Sally
22
+
23
+ Permission is hereby granted, free of charge, to any person obtaining
24
+ a copy of this software and associated documentation files (the
25
+ 'Software'), to deal in the Software without restriction, including
26
+ without limitation the rights to use, copy, modify, merge, publish,
27
+ distribute, sublicense, and/or sell copies of the Software, and to
28
+ permit persons to whom the Software is furnished to do so, subject to
29
+ the following conditions:
30
+
31
+ The above copyright notice and this permission notice shall be
32
+ included in all copies or substantial portions of the Software.
33
+
34
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
35
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
38
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
39
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
40
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ require 'rake/testtask'
2
+ require 'spec/rake/spectask'
3
+
4
+ task :default => :spec
5
+
6
+ Spec::Rake::SpecTask.new(:spec) do |t|
7
+ t.warning = true
8
+ t.spec_files = FileList["#{File.dirname(__FILE__)}/spec/**/*_spec.rb"]
9
+ t.spec_opts = %w(--backtrace --diff --color)
10
+ t.libs << "#{File.dirname(__FILE__)}/spec"
11
+ t.libs << "#{File.dirname(__FILE__)}/spec/onion"
12
+ end
13
+
14
+ desc "Build a gem file."
15
+ task :build do
16
+ system "gem build onion.gemspec"
17
+ end
18
+
19
+ # Load custom rake tasks.
20
+ Dir["#{File.dirname(__FILE__)}/lib/tasks/**/*.rake"].sort.each do |task|
21
+ load task
22
+ end
@@ -0,0 +1,19 @@
1
+ module Onion
2
+
3
+ require 'onion/elements'
4
+
5
+ parsers = %w[ router_lists ]
6
+ parsers.each do |parser|
7
+ begin
8
+ # If there is a pre-compiled parser, load it.
9
+ require 'treetop/runtime'
10
+ require "onion/parsers/#{parser}"
11
+ rescue LoadError
12
+ # Otherwise compiler with Treetop.
13
+ require 'treetop/runtime'
14
+ require 'treetop/compiler'
15
+ Treetop.load(File.join(File.dirname(__FILE__)) + "/onion/parsers/#{parser}")
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,4 @@
1
+ module Onion
2
+ autoload :Router, 'onion/elements/router'
3
+ autoload :RouterList, 'onion/elements/router_list'
4
+ end
@@ -0,0 +1,18 @@
1
+ module Onion
2
+ class Router
3
+ attr_reader :nick, :id_key_hash, :status_flags
4
+ def initialize(nick, id_key_hash, status_flags)
5
+ @nick = nick
6
+ @id_key_hash = id_key_hash
7
+ @status_flags = status_flags
8
+ end
9
+
10
+ def guard?
11
+ @status_flags and @status_flags.include?("Guard")
12
+ end
13
+
14
+ def exit?
15
+ @status_flags and @status_flags.include?("Exit")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Onion
2
+ class RouterList
3
+ attr_reader :routers
4
+ def initialize(text)
5
+ if text.blank?
6
+ @routers = []
7
+ return self
8
+ end
9
+ parser = Onion::RouterListsParser.new
10
+ if nodes = parser.parse(text)
11
+ @routers = nodes.routers
12
+ else
13
+ raise Exception "Couldn't parse #{text} b/c #{parser.failure_reason}."
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,133 @@
1
+ module Onion
2
+ grammar RouterLists
3
+
4
+ # --- Rule to start matching against. ----------------------------------- #
5
+
6
+ rule router_list
7
+ router_list_entry+ {
8
+ def routers
9
+ routers = []
10
+ self.elements.each do |e|
11
+ if e.respond_to? :s # Optional status flags are in the entry.
12
+ routers << Onion::Router.new(e.r.nick, e.r.id_hash, e.s.flags)
13
+ else
14
+ routers << Onion::Router.new(e.r_nos.nick, e.r_nos.id_hash, nil)
15
+ end
16
+ end
17
+ return routers
18
+ end
19
+ }
20
+ end
21
+
22
+ rule router_list_entry
23
+ r:router "\n" s:status "\n" / r_nos:router "\n"
24
+ end
25
+
26
+ # --- General use rules. ------------------------------------------------ #
27
+
28
+ rule base64_char
29
+ [A-Za-z0-9+/]
30
+ end
31
+
32
+ rule year
33
+ [0-9] [0-9] [0-9] [0-9]
34
+ end
35
+
36
+ rule month
37
+ [0] [1-9] / [1] [0-2]
38
+ end
39
+
40
+ rule day
41
+ [0-2] [0-9] / [3] [0-1]
42
+ end
43
+
44
+ rule hour
45
+ [0] [1-9] / [1] [0-9] / [2] [0-4]
46
+ end
47
+
48
+ rule minute
49
+ [0] [0-9] / [1-5] [0-9]
50
+ end
51
+
52
+ rule second
53
+ [0] [0-9] / [1-5] [0-9]
54
+ end
55
+
56
+ rule ip_addr_byte
57
+ [2] [5] [0-5] / [2] [0-4] [0-9] / [1] [0-9] [0-9] / [1-9] [0-9] / [0-9]
58
+ end
59
+
60
+ rule sp
61
+ " "+
62
+ end
63
+
64
+ # --- Rules needed to parse the router lines. --------------------------- #
65
+
66
+ rule router
67
+ "r " n:nick id:id_hash desc_hash desc_time ip_addr or_port dir_port {
68
+ def nick
69
+ n.text_value.strip
70
+ end
71
+
72
+ def id_hash
73
+ id.text_value.strip
74
+ end
75
+ }
76
+ end
77
+
78
+ rule nick
79
+ # TODO: This should be 1-19 alphanumeric chars, but this isn't easy
80
+ # to do in Treetop.
81
+ [a-zA-Z0-9]+ sp
82
+ end
83
+
84
+ rule id_hash
85
+ base64_char+ sp
86
+ end
87
+
88
+ rule desc_hash
89
+ base64_char+ sp
90
+ end
91
+
92
+ rule desc_time
93
+ year "-" month "-" day " " hour ":" minute ":" second sp
94
+ end
95
+
96
+ rule ip_addr
97
+ ip_addr_byte "." ip_addr_byte "." ip_addr_byte "." ip_addr_byte sp
98
+ end
99
+
100
+ rule or_port
101
+ [0-9]+ sp
102
+ end
103
+
104
+ rule dir_port
105
+ [0-9]+ sp?
106
+ end
107
+
108
+ # --- Rules needed to parse the status lines. --------------------------- #
109
+
110
+ rule status
111
+ "s " (valid:status_flag sp? / invalid_status_flag sp?)+ {
112
+ def flags
113
+ results = []
114
+ self.elements[1].elements.each do |e|
115
+ if e.respond_to? :valid
116
+ results << e.valid.text_value
117
+ end
118
+ end
119
+ return results
120
+ end
121
+ }
122
+ end
123
+
124
+ rule status_flag
125
+ "Authority" / "BadExit" / "BadDirectory" / "Exit" / "Fast"
126
+ / "Guard" / "Named" / "Stable" / "Running" / "Valid" / "V2Dir"
127
+ end
128
+
129
+ rule invalid_status_flag
130
+ [a-zA-Z]+
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,10 @@
1
+ namespace :tt do
2
+ desc "Generate Ruby parsers from Treetop grammars."
3
+ task :gen do
4
+ task_path = File.expand_path('../../lib/onion/parsers/*.treetop', __FILE__)
5
+ Dir.glob(task_path) do |filename|
6
+ # `tt #{filename}`
7
+ `bundle exec tt #{filename}`
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onion
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Poet (Tim Sally)
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-02 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: treetop
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 8
34
+ version: 1.4.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Interact with Tor.
38
+ email: poet@stack.io
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.rdoc
47
+ - Rakefile
48
+ - lib/onion/elements/router.rb
49
+ - lib/onion/elements/router_list.rb
50
+ - lib/onion/elements.rb
51
+ - lib/onion/parsers/router_lists.treetop
52
+ - lib/onion.rb
53
+ - lib/tasks/treetop.rake
54
+ has_rdoc: true
55
+ homepage: http://stack.io
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Onion is a library you can use to interact with Tor. It includes parsers and clients for Tor's protocols.
88
+ test_files: []
89
+