rubtella 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/bin/rubtella +50 -0
- data/lib/config/config.rb +18 -2
- data/lib/rubtella/rubtella.rb +73 -48
- data/rubtella.gemspec +5 -5
- metadata +5 -5
- data/bin/rubtellad +0 -26
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/bin/rubtella
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubtella'
|
4
|
+
|
5
|
+
case ARGV[0]
|
6
|
+
when "start"
|
7
|
+
$0 = 'rubtella-base'
|
8
|
+
|
9
|
+
@p = fork do
|
10
|
+
$0 = 'rubtella'
|
11
|
+
Signal.trap("TERM") do
|
12
|
+
@@logger.info "terminating..."
|
13
|
+
File.delete Rubtella::Config::PID_FILE if File.exist? Rubtella::Config::PID_FILE
|
14
|
+
exit
|
15
|
+
end
|
16
|
+
|
17
|
+
Thread.new do
|
18
|
+
server = Rubtella::Listener.new
|
19
|
+
server.listen
|
20
|
+
end
|
21
|
+
|
22
|
+
Thread.new do
|
23
|
+
gnutella = Rubtella::Sender.new
|
24
|
+
gnutella.connect
|
25
|
+
gnutella.send_query("doors")
|
26
|
+
end
|
27
|
+
|
28
|
+
while true
|
29
|
+
sleep 100
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
#save pid
|
34
|
+
f = File.new Rubtella::Config::PID_FILE, File::CREAT|File::TRUNC|File::RDWR, 0644
|
35
|
+
f.puts @p
|
36
|
+
f.close
|
37
|
+
|
38
|
+
Process.detach(@p)
|
39
|
+
|
40
|
+
when "stop"
|
41
|
+
unless File.exist? Rubtella::Config::PID_FILE
|
42
|
+
puts 'No pid file found'
|
43
|
+
else
|
44
|
+
f = File.open Rubtella::Config::PID_FILE
|
45
|
+
pid = f.read.to_i
|
46
|
+
Process.kill "TERM", pid
|
47
|
+
end
|
48
|
+
else
|
49
|
+
puts "[start|stop]"
|
50
|
+
end
|
data/lib/config/config.rb
CHANGED
@@ -9,9 +9,25 @@ module Rubtella
|
|
9
9
|
guid
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
if File.exist?(ENV["HOME"] + "/.rubtella/config.rb")
|
13
|
+
load ENV["HOME"] + "/.rubtella/config.rb"
|
14
|
+
else
|
15
|
+
Dir.mkdir(ENV['HOME'] + "/.rubtella") unless File.exist?(ENV['HOME'] + "/.rubtella")
|
16
|
+
File.open(ENV["HOME"] + "/.rubtella/config.rb", 'w') do |f|
|
17
|
+
f.puts("#put your config here")
|
18
|
+
f.puts("#")
|
19
|
+
f.puts("# example below")
|
20
|
+
f.puts("#")
|
21
|
+
f.puts("#IP_ADDRESS = 127.0.0.1")
|
22
|
+
f.puts("#PORT = 54321")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
IP_ADDRESS = "127.0.0.1" unless defined? IP_ADDRESS
|
27
|
+
PORT = 54321 unless defined? PORT
|
28
|
+
|
14
29
|
GUID = generate_guid
|
30
|
+
PID_FILE = ENV["HOME"] + "/.rubtella/rubtella.pid"
|
15
31
|
|
16
32
|
|
17
33
|
|
data/lib/rubtella/rubtella.rb
CHANGED
@@ -31,30 +31,32 @@ module Rubtella
|
|
31
31
|
Thread.start do
|
32
32
|
|
33
33
|
begin
|
34
|
-
|
34
|
+
manage_connection session
|
35
35
|
rescue => e
|
36
|
-
session.puts "Rubtella
|
37
|
-
|
36
|
+
session.puts "Rubtella Listener Error: " + e.to_s
|
37
|
+
@@logger.info "Rubtella Server Error: " + e.to_s
|
38
38
|
ensure
|
39
39
|
session.close
|
40
40
|
end
|
41
41
|
|
42
42
|
end
|
43
43
|
end #end loop
|
44
|
-
|
45
44
|
end
|
45
|
+
|
46
|
+
def manage_connection stream
|
47
|
+
loop do
|
48
|
+
@@logger.info 'we\'re listening..'
|
49
|
+
resp = stream.recv 1000
|
50
|
+
if parse(resp) == "ping"
|
51
|
+
pong = TCPData::Builder::Pong.new
|
52
|
+
stream.send pong.build , 0
|
53
|
+
@@logger.info 'pong send..'
|
54
|
+
stream.close
|
55
|
+
@@logger.info 'connection closed'
|
56
|
+
break
|
57
|
+
end
|
58
|
+
end
|
46
59
|
|
47
|
-
def parse_request request
|
48
|
-
req = request.recv 1000
|
49
|
-
|
50
|
-
puts req
|
51
|
-
request.send Sender.pong, 0
|
52
|
-
puts 'response send!'
|
53
|
-
req = request.recv 1000
|
54
|
-
puts 'received content:'
|
55
|
-
|
56
|
-
puts req
|
57
|
-
puts 'received!'
|
58
60
|
end
|
59
61
|
|
60
62
|
end
|
@@ -64,44 +66,58 @@ module Rubtella
|
|
64
66
|
|
65
67
|
attr_accessor :peer, :connected
|
66
68
|
|
67
|
-
def initialize peer
|
68
|
-
@
|
69
|
+
def initialize peer = nil
|
70
|
+
@peers = Array.new
|
71
|
+
init_peers
|
72
|
+
|
73
|
+
if peer
|
74
|
+
@peers << peer
|
75
|
+
end
|
76
|
+
|
77
|
+
raise RubtellaError, "No peer address! Pleas add peers to hosts file!" if @peers.empty?
|
78
|
+
|
79
|
+
@peer = @peers.pop
|
80
|
+
|
69
81
|
@standard_headers = {"User-Agent" => "Rubtella",
|
70
82
|
"X-Ultrapeer" => "False",
|
71
83
|
"X-Query-Routing" => "0.1"}
|
84
|
+
|
85
|
+
rescue => e
|
86
|
+
@@logger.info e.to_s
|
72
87
|
end
|
73
88
|
|
74
89
|
def connect
|
75
|
-
|
76
|
-
|
90
|
+
@@logger.info "connecting to: #{@peer.ip}:#{@peer.port}"
|
91
|
+
stream = nil
|
92
|
+
timeout(5) do
|
77
93
|
stream = TCPSocket.new @peer.ip, @peer.port
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
@response = stream.recv 1000
|
94
|
+
stream.send handshake_req, 0
|
95
|
+
end
|
96
|
+
@response = stream.recv 1000
|
82
97
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
99
|
-
rescue Timeout::Error
|
100
|
-
@peer = resp.peers.shift
|
98
|
+
resp = HTTPData::Parser.new @response
|
99
|
+
stream.send handshake_resp, 0
|
100
|
+
|
101
|
+
if resp.ok?
|
102
|
+
#connection established
|
103
|
+
@@logger.info 'connection established'
|
104
|
+
@connected = @peer
|
105
|
+
@@logger.info "Connected with #{@connected.ip} #{@connected.port}"
|
106
|
+
@@logger.info "Connected with #{@connected.ip} #{@connected.port}"
|
107
|
+
|
108
|
+
manage_connection stream
|
109
|
+
else
|
110
|
+
@@logger.info 'failed to connect'
|
111
|
+
@peers.concat resp.peers
|
112
|
+
@peer = @peers.pop
|
101
113
|
connect
|
102
|
-
rescue => e
|
103
|
-
@@logger.info e.to_s
|
104
114
|
end
|
115
|
+
rescue Timeout::Error,Errno::ECONNREFUSED
|
116
|
+
@@logger.info "Timeout"
|
117
|
+
@peer = resp.peers.shift
|
118
|
+
connect
|
119
|
+
rescue => e
|
120
|
+
@@logger.info e.to_s
|
105
121
|
end
|
106
122
|
|
107
123
|
def handshake_req
|
@@ -116,14 +132,14 @@ module Rubtella
|
|
116
132
|
|
117
133
|
def manage_connection stream
|
118
134
|
loop do
|
119
|
-
|
135
|
+
@@logger.info 'we\'re listening..'
|
120
136
|
resp = stream.recv 1000
|
121
137
|
if parse(resp) == "ping"
|
122
138
|
pong = TCPData::Builder::Pong.new
|
123
139
|
stream.send pong.build , 0
|
124
|
-
|
140
|
+
@@logger.info 'pong send..'
|
125
141
|
stream.close
|
126
|
-
|
142
|
+
@@logger.info 'connection closed'
|
127
143
|
break
|
128
144
|
end
|
129
145
|
end
|
@@ -134,7 +150,7 @@ module Rubtella
|
|
134
150
|
|
135
151
|
def parse message
|
136
152
|
parsed = TCPData::Parser.new message
|
137
|
-
|
153
|
+
@@logger.info parsed.message
|
138
154
|
|
139
155
|
parsed.message
|
140
156
|
|
@@ -145,10 +161,19 @@ module Rubtella
|
|
145
161
|
query = TCPData::Builder::Query.new(:criteria => text)
|
146
162
|
@@logger.info "sending query - #{text}"
|
147
163
|
stream.send query.build, 0
|
148
|
-
|
164
|
+
@@logger.info 'we\'re listening..'
|
149
165
|
resp = stream.recv 1000
|
150
166
|
parse(resp)
|
151
167
|
end
|
168
|
+
|
169
|
+
def init_peers
|
170
|
+
if File.exist?(ENV["HOME"] + "/.rubtella/hosts")
|
171
|
+
hosts_file = File.open ENV["HOME"] + "/.rubtella/hosts"
|
172
|
+
hosts = hosts_file.read
|
173
|
+
hosts.split("\n")
|
174
|
+
hosts.each {|h| @peers << Peer.new(*(h.split(":")))}
|
175
|
+
end
|
176
|
+
end
|
152
177
|
end
|
153
178
|
|
154
179
|
end
|
data/rubtella.gemspec
CHANGED
@@ -5,15 +5,15 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rubtella}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mateusz Zawisza"]
|
12
|
-
s.date = %q{2010-01-
|
13
|
-
s.default_executable = %q{
|
12
|
+
s.date = %q{2010-01-19}
|
13
|
+
s.default_executable = %q{rubtella}
|
14
14
|
s.description = %q{Library for Gnuttale in Ruby}
|
15
15
|
s.email = %q{mateusz.zawisza@gmail.com}
|
16
|
-
s.executables = ["
|
16
|
+
s.executables = ["rubtella"]
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"README"
|
19
19
|
]
|
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
"README",
|
23
23
|
"Rakefile",
|
24
24
|
"VERSION",
|
25
|
-
"bin/
|
25
|
+
"bin/rubtella",
|
26
26
|
"lib/config/config.rb",
|
27
27
|
"lib/rubtella.rb",
|
28
28
|
"lib/rubtella/errors.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubtella
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mateusz Zawisza
|
@@ -9,14 +9,14 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
13
|
-
default_executable:
|
12
|
+
date: 2010-01-19 00:00:00 +01:00
|
13
|
+
default_executable: rubtella
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Library for Gnuttale in Ruby
|
17
17
|
email: mateusz.zawisza@gmail.com
|
18
18
|
executables:
|
19
|
-
-
|
19
|
+
- rubtella
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
@@ -26,7 +26,7 @@ files:
|
|
26
26
|
- README
|
27
27
|
- Rakefile
|
28
28
|
- VERSION
|
29
|
-
- bin/
|
29
|
+
- bin/rubtella
|
30
30
|
- lib/config/config.rb
|
31
31
|
- lib/rubtella.rb
|
32
32
|
- lib/rubtella/errors.rb
|
data/bin/rubtellad
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'rubtella'
|
4
|
-
|
5
|
-
$0 = 'rubtella-base'
|
6
|
-
|
7
|
-
@p = fork do
|
8
|
-
$0 = 'rubtella'
|
9
|
-
Thread.new do
|
10
|
-
server = Rubtella::Listener.new
|
11
|
-
server.listen
|
12
|
-
end
|
13
|
-
|
14
|
-
Thread.new do
|
15
|
-
gnutella = Rubtella::Sender.new Rubtella::Peer.new("70.188.16.73",38093)
|
16
|
-
gnutella.connect
|
17
|
-
gnutella.send_query("doors")
|
18
|
-
end
|
19
|
-
|
20
|
-
while true
|
21
|
-
sleep 100
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
Process.detach(@p)
|
26
|
-
|