i2plookuper 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/i2plookuper.rb +141 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2d87f2e51553dea12ae5cd2174bc904f1b4ce9b83bfd950e102973e86f7d4851
4
+ data.tar.gz: ad98868d826e8ecec97c8a474e4b7a77e3802f3a4c02c6a87d308b575cf1817f
5
+ SHA512:
6
+ metadata.gz: dc98090b7690e65256d594190bd2de8fa04559e91542047ac71314562b965ce31484f8595edbb9ab8f9c1759856715268d95a7c906bdd06ecc92a9cc50202ea7
7
+ data.tar.gz: 612764561742a570e3e2bfff34f142dde8c5fe75f46b8817ee5c234c5f1624bae7b51032c48f19add4b6465a8dd876e1b8f7d0981829080538f402d42fafc57a
@@ -0,0 +1,141 @@
1
+ =begin
2
+ Copyright (c) 2020 Marek Küthe
3
+ This program is free software. It comes without any warranty, to
4
+ the extent permitted by applicable law. You can redistribute it
5
+ and/or modify it under the terms of the Do What The Fuck You Want
6
+ To Public License, Version 2, as published by Sam Hocevar. See
7
+ http://www.wtfpl.net/ for more details.
8
+ =end
9
+
10
+ module I2PLookuper
11
+
12
+ require "socket"
13
+
14
+ module SAMHelper
15
+ def self.samcmd fircmd, seccmd = nil, args = {}
16
+ cmd = "#{fircmd}#{seccmd ? " #{seccmd}" : ""}"
17
+ args.each_pair { |arg, value|
18
+ cmd << " #{arg}=\"#{value.to_s}\""
19
+ }
20
+ return cmd
21
+ end
22
+
23
+ def self.parsesamcmd ans
24
+ ans.chomp!
25
+ ans += " "
26
+ f = ans.index " "
27
+ s = ans.index " ", f + 1
28
+ w = ans[f+1...s]
29
+
30
+ args = Hash.new
31
+ loop do
32
+ g1 = ans.index "=", s+1
33
+ break if g1 == nil
34
+ g2 = nil
35
+ if ans[g1+1] == "\""
36
+ g2 = ans.index "\"", g1+2
37
+ args[ans[s+1..g1-1]] = ans[g1+2..g2-1]
38
+ else
39
+ g2 = ans.index " ", g1+1
40
+ args[ans[s+1..g1-1]] = ans[g1+1..g2-1]
41
+ end
42
+ s = g2
43
+ end
44
+
45
+ return [ans[0...f], w, args]
46
+ end
47
+ end
48
+
49
+ class BOBLookuper
50
+
51
+ attr_reader :version, :status
52
+
53
+ # @status:
54
+ # :connected
55
+ # :connectedand[bob initialize status, eg. "ok" or "error"]
56
+ # :disconnected
57
+
58
+
59
+ def initialize host = "127.0.0.1", port = 2827
60
+ @sock = TCPSocket.new host, port
61
+ @status = :connected
62
+
63
+ ver = @sock.gets.chomp
64
+ status = @sock.gets.chomp
65
+
66
+ @status = "connectedand#{status.downcase}".to_sym
67
+ @version = ver.split[1]
68
+
69
+ at_exit { self.close }
70
+ end
71
+
72
+ def close
73
+ if @status != :disconnected
74
+ @sock.puts "quit"
75
+ @sock.close
76
+ @status = :disconnected
77
+ end
78
+ end
79
+
80
+ def lookup name
81
+ oldstatus = @status
82
+ @status = :lookup
83
+
84
+ @sock.puts "lookup #{name}"
85
+
86
+ res = @sock.gets.chomp
87
+ space = res.index " "
88
+
89
+ @status = oldstatus
90
+
91
+ return res[0...space].downcase.to_sym, res[space+1..-1]
92
+ end
93
+
94
+ end
95
+
96
+ class SAMLookuper
97
+
98
+ attr_reader :status, :version
99
+
100
+ def initialize host = "127.0.0.1", port = 7656, samhelper = SAMHelper
101
+ @samhelper = SAMHelper
102
+ @sock = TCPSocket.new host, port
103
+ @status = :connected
104
+
105
+ @sock.puts @samhelper.samcmd "HELLO", "VERSION", {
106
+ "MIN" => "1.0"
107
+ }
108
+ res = @samhelper.parsesamcmd @sock.gets.chomp
109
+ @status = "connectedand#{res[2]["RESULT"].downcase}".to_sym
110
+ @version = res[2]["VERSION"]
111
+
112
+ at_exit { self.close }
113
+ end
114
+
115
+ def close
116
+ if @status != :disconnected
117
+ @sock.puts @samhelper.samcmd "QUIT"
118
+ @sock.close
119
+ @status = :disconnected
120
+ end
121
+ end
122
+
123
+ def lookup name
124
+ oldstatus = @status
125
+ @status = :lookup
126
+
127
+ @sock.puts @samhelper.samcmd "NAMING", "LOOKUP", {
128
+ "NAME" => name
129
+ }
130
+
131
+ res = @samhelper.parsesamcmd @sock.gets
132
+
133
+ @status = oldstatus
134
+
135
+ return res[2]["RESULT"].downcase.to_sym, res[2].has_key?("VALUE") ? res[2]["VALUE"] : res[2]["RESULT"].tr("_", " ")
136
+ end
137
+
138
+ end
139
+ end
140
+
141
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i2plookuper
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek Küthe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: m.k@mk16.de
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/i2plookuper.rb
20
+ homepage: https://mk16.de/
21
+ licenses:
22
+ - WTFPL
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.1.4
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: This gem contains the module I2PLookuper, which in turn includes the classes
43
+ SAMLookuper and BOBLookuper.
44
+ test_files: []