findme 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +4 -0
- data/Rakefile +2 -0
- data/findme.gemspec +17 -0
- data/lib/findme.rb +97 -0
- data/lib/findme/version.rb +3 -0
- data/test/test.rb +7 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 c2h2
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/findme.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/findme/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["c2h2"]
|
6
|
+
gem.email = ["yiling.cao@gmail.com"]
|
7
|
+
gem.description = "find neighborhood host and services."
|
8
|
+
gem.summary = "find neighborhood host and services."
|
9
|
+
gem.homepage = "https://github.com/c2h2/findme"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "findme"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Findme::VERSION
|
17
|
+
end
|
data/lib/findme.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
class Findme
|
2
|
+
|
3
|
+
AVAHI_SERVICE_DIR = "/etc/avahi/services/"
|
4
|
+
AVAHI_BROWSE = "/usr/bin/avahi-browse"
|
5
|
+
|
6
|
+
|
7
|
+
#return all the services running by avahi
|
8
|
+
def self.services
|
9
|
+
files_with_path = Dir.glob(AVAHI_SERVICE_DIR + "*.service")
|
10
|
+
files = files_with_path.map{|fwp| fwp.split("/").last}
|
11
|
+
end
|
12
|
+
|
13
|
+
#clean up all the services this gem registered.
|
14
|
+
def self.cleanup
|
15
|
+
findme_services = []
|
16
|
+
files_with_path = Dir.glob(AVAHI_SERVICE_DIR + "*.service")
|
17
|
+
files_with_path.each do |fwp|
|
18
|
+
File.open(fwp, 'r') do |infile|
|
19
|
+
while (line = infile.gets)
|
20
|
+
if line.strip == "<!-- findme generated -->"
|
21
|
+
findme_services << fwp
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
findme_services.each do |fs|
|
27
|
+
puts "Removing #{fs}"
|
28
|
+
File.delete fs
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.discover
|
33
|
+
res = `avahi-browse -arpt`
|
34
|
+
services = []
|
35
|
+
lines = res.split("\n")
|
36
|
+
lines.each do |line|
|
37
|
+
if line[0] == "="
|
38
|
+
elems=line.split(";")
|
39
|
+
as = AvahiService.new
|
40
|
+
as.eth = elems[1]
|
41
|
+
as.ipv4 = elems[2]
|
42
|
+
as.hosttxt = elems[3]
|
43
|
+
as.service = elems[4]
|
44
|
+
as.hostname = elems[6]
|
45
|
+
as.ip = elems[7]
|
46
|
+
as.port = elems[8]
|
47
|
+
as.txt = elems[9]
|
48
|
+
services << as
|
49
|
+
end
|
50
|
+
end
|
51
|
+
services
|
52
|
+
end
|
53
|
+
|
54
|
+
#register a new service to the folder
|
55
|
+
def self.register service_name, port, type = '_tcp'
|
56
|
+
xml = '<?xml version="1.0" standalone=\'no\'?><!--*-nxml-*-->
|
57
|
+
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
|
58
|
+
|
59
|
+
<!-- findme generated -->
|
60
|
+
<!-- This file is part of udisks -->
|
61
|
+
|
62
|
+
<service-group>
|
63
|
+
<name replace-wildcards="yes">%h</name>
|
64
|
+
|
65
|
+
<service>
|
66
|
+
<type>'+ "_#{service_name}.#{type}" +'</type>
|
67
|
+
<port>' + port.to_s + '</port>
|
68
|
+
</service>
|
69
|
+
</service-group>'
|
70
|
+
|
71
|
+
File.open(AVAHI_SERVICE_DIR + service_name + ".service", "w" ){|f| f.write xml}
|
72
|
+
end
|
73
|
+
|
74
|
+
#find the mac address
|
75
|
+
def self.mac
|
76
|
+
return @mac_address if defined? @mac_address
|
77
|
+
re = %r<(?:hwaddr|:)\s+((?:[0-9a-f]{1,2}[-:]){5}[0-9a-f]{1,2})\s*$>i
|
78
|
+
lines =
|
79
|
+
begin
|
80
|
+
IO.popen('ifconfig'){|fd| fd.readlines}
|
81
|
+
rescue
|
82
|
+
IO.popen('ipconfig /all'){|fd| fd.readlines}
|
83
|
+
end
|
84
|
+
candidates = lines.map{|l| re.match( l )[1] rescue nil }.compact
|
85
|
+
@mac_address = candidates.first
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.mac_short
|
89
|
+
self.mac.split(":").join("")
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
class AvahiService
|
95
|
+
attr_accessor :eth, :ipv4, :ip, :hosttxt, :hostname, :port, :txt, :service
|
96
|
+
|
97
|
+
end
|
data/test/test.rb
ADDED
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: findme
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- c2h2
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-02 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: find neighborhood host and services.
|
15
|
+
email:
|
16
|
+
- yiling.cao@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- findme.gemspec
|
27
|
+
- lib/findme.rb
|
28
|
+
- lib/findme/version.rb
|
29
|
+
- test/test.rb
|
30
|
+
homepage: https://github.com/c2h2/findme
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: find neighborhood host and services.
|
54
|
+
test_files:
|
55
|
+
- test/test.rb
|