ipfixer_client 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 902ee4cf6f34557696c5d28b0dcb07d57e6fb858
4
+ data.tar.gz: feb994876a3e6df61a710265638b0b99a7470efa
5
+ SHA512:
6
+ metadata.gz: e17e37fc0e074d4b0a6e97316fa6795b46628fed7ca6787e3b20f30fcea6a5126a34d6bf5df1d005811ee66ca44b0ce1d3ff6a24296d3b4755c324acff057002
7
+ data.tar.gz: 9c6ebaca0fed2c658024a2176bfe8dfae43ab48dce69f63331899be57879a85d67f5e926876e4e80f464dc1e90c050e5ce1147e446586bc64aaef009e21c7694
@@ -0,0 +1,3 @@
1
+ *.swp
2
+ pkg/
3
+ .project
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ipfixer_client (0.0.2)
5
+ daemons
6
+ thor
7
+ win32-service
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ coderay (1.0.8)
13
+ <<<<<<< HEAD
14
+ =======
15
+ columnize (0.3.6)
16
+ daemons (1.1.9)
17
+ debugger (1.3.0)
18
+ columnize (>= 0.3.1)
19
+ debugger-linecache (~> 1.1.1)
20
+ debugger-ruby_core_source (~> 1.1.7)
21
+ debugger-linecache (1.1.2)
22
+ debugger-ruby_core_source (>= 1.1.1)
23
+ debugger-ruby_core_source (1.1.7)
24
+ >>>>>>> 8732c7e2a3d79b98e2bc87b9a66bbf666c345865
25
+ diff-lcs (1.1.3)
26
+ ffi (1.9.0)
27
+ ffi (1.9.0-x86-mingw32)
28
+ method_source (0.8.1)
29
+ pry (0.9.11.4)
30
+ coderay (~> 1.0.5)
31
+ method_source (~> 0.8)
32
+ slop (~> 3.4)
33
+ pry (0.9.11.4-x86-mingw32)
34
+ coderay (~> 1.0.5)
35
+ method_source (~> 0.8)
36
+ slop (~> 3.4)
37
+ win32console (~> 1.3)
38
+ rake (0.9.2)
39
+ rspec (2.12.0)
40
+ rspec-core (~> 2.12.0)
41
+ rspec-expectations (~> 2.12.0)
42
+ rspec-mocks (~> 2.12.0)
43
+ rspec-core (2.12.2)
44
+ rspec-expectations (2.12.1)
45
+ diff-lcs (~> 1.1.3)
46
+ rspec-mocks (2.12.2)
47
+ slop (3.4.3)
48
+ thor (0.18.1)
49
+ win32-service (0.8.0)
50
+ ffi
51
+ win32console (1.3.2-x86-mingw32)
52
+
53
+ PLATFORMS
54
+ ruby
55
+ x86-mingw32
56
+
57
+ DEPENDENCIES
58
+ ipfixer_client!
59
+ pry
60
+ rake (= 0.9.2)
61
+ rspec
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb})
6
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/README ADDED
@@ -0,0 +1,64 @@
1
+ Ah, the old ipfixer client to be run on a windows machine...
2
+
3
+ Have you ever wanted to keep track of one or more remote machines? Yet their IP addresses are always subject to change? Well this gem is designed to work with the ipfixer_server to store it's latest IP address in a remotely accessible rails application.
4
+
5
+ The ipfixer_service runs on windows machines as a service (so it's always running as long as the PC is on, even if you're logged out). You can view a list of currently running services by right clicking "My Computer" and selecting "manage". Then go to...
6
+
7
+ Services and Applications -> Services
8
+
9
+ And you will see a list of services currently installed on your PC. Following the below procedure will install a service named "ipfixer_svc"
10
+
11
+
12
+
13
+ ==Fresh Install Sequence (~winxp or winvista)==
14
+
15
+ 1) Install git (just to download)
16
+ http://git-scm.com/download/win
17
+
18
+ 2) Install ruby 1.9.3 and then devkit (use the tdm devkit for 1.9.x!!)
19
+ http://rubyinstaller.org/downloads/
20
+
21
+
22
+ rubyinstaller.exe /verysilent /tasks="addtk,assocfiles,modpath"
23
+
24
+
25
+ Devkit: Extract to c:/devkit and then run c:/devkit/msys.bat
26
+
27
+
28
+ $ cd /c/devkit
29
+ $ ruby dk.rb init; ruby dk.rb install
30
+
31
+
32
+ 3) Clone the repo to desired location
33
+
34
+ $ cd dev # make a directory for development...
35
+ $ git clone https://github.com/TheNotary/ipfixer_client.git
36
+ $ cd ipfixer_client
37
+
38
+ 4) Install bundler, and then install the bundle for this gem
39
+
40
+ $ gem install ruby-debug19 # due to bugs, do this...
41
+ # it will fail, but that's fine and allows us to install debugger-linecache thereafter...
42
+ # maybe it just needs linecache19 and possibly... ruby-debug-base19-0.11.25??
43
+ $ gem install bundler
44
+ $ bundle install
45
+
46
+
47
+ 5) Install the gem intself
48
+
49
+ $ bundle exec rake install
50
+
51
+
52
+ 6) Install the windows service (run cmd as admin on vista... win_key -> 'cmd' -> Ctrl+shft+enter)
53
+
54
+ $ ipfixer install
55
+
56
+ It will prompt you to enter:
57
+ -An ipfixes server (I'll make the rails generator shortly and put it online)
58
+ -A port on which your server runs
59
+ -An optional dynamic dns URL that it will GET when ever it detects an IP change
60
+ -Someday, a security token that allows you to authenticate with your ipfixer server
61
+
62
+ A configuration file can be created... at `c:/it/ipfixer/ipfixer.conf` It can be modified at any time.
63
+
64
+ 7) Now start the service using computer management. (sry, i'm in a rush...)
@@ -0,0 +1,25 @@
1
+ require 'bundler'
2
+ include Rake::DSL
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require "rspec/core/rake_task" # RSpec 2.0
6
+
7
+ RSpec::Core::RakeTask.new(:unit) do |spec|
8
+ #spec.pattern = 'spec/*/*_spec.rb'
9
+ spec.pattern = 'spec/unit/*_spec.rb'
10
+ spec.rspec_opts = ['--backtrace']
11
+ end
12
+
13
+ RSpec::Core::RakeTask.new(:all) do |spec|
14
+ spec.pattern = 'spec/*/*_spec.rb'
15
+ spec.rspec_opts = ['--debug']
16
+ end
17
+
18
+ # this is for running tests that you've marked current... eg: it 'should work', :current => true do
19
+ RSpec::Core::RakeTask.new(:current) do |spec|
20
+ spec.pattern = 'spec/*/*_spec.rb'
21
+ spec.rspec_opts = ['--tag current']
22
+ end
23
+
24
+
25
+ task :default => :all
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require 'ipfixer_client'
5
+
6
+ class IpfixerClientRunner < Thor
7
+ default_task :help
8
+
9
+ desc "help", "Explains the gem's usage"
10
+ def help
11
+ IpfixerClient.help
12
+ end
13
+
14
+ desc "install", "Installs the client as a windows service"
15
+ def install
16
+ IpfixerClient.install
17
+ end
18
+
19
+ desc "uninstall", "Uninstalls the windows service"
20
+ def uninstall
21
+ IpfixerClient.uninstall
22
+ end
23
+
24
+ desc "client_svc", "This command should only be used by the windows service which allows the gem to be run"
25
+ def client_svc
26
+ IpfixerClient.client_svc
27
+ end
28
+
29
+ desc "start", "This command will start the service for you. Before it will work, you must install the service via `ipfixer install`"
30
+ def start
31
+ IpfixerClient.start
32
+ end
33
+ end
34
+
35
+ IpfixerClientRunner.start
@@ -0,0 +1,21 @@
1
+ ==Future version 0.0.?==
2
+ -Allow ipfixer to work on linux and mac machines too!!!
3
+ -Allow ipfixer_svc to cooperate with watchdog_listener for increased remote administration capabilities!
4
+ -Add a command to start the windows service you lazy bum!
5
+
6
+ ==Version 0.0.2==
7
+ Major:
8
+ -Added DEBUG_MODE to the conf
9
+ -Added an optional security_token to the conf
10
+
11
+
12
+ Minor:
13
+ -Made it work happily ipfix servers returning a status code of 201 or 200.
14
+ -Add error handling for when the IP Address isn't returning a valid IP (I think it just mysteriously crashes)
15
+ -Add error/ usability handling if PORT or SERVER isn't specified in the yaml.
16
+
17
+
18
+ ==version 0.0.1==
19
+
20
+ Built the app. This version spans many months, I think even years. I didn't think about change logs that far back, but am starting to now... The early version so of 0.0.1 didn't work; the latest version of 0.0.1, worked and was bumpted to version 0.0.2 to give me some practice...
21
+
@@ -0,0 +1,25 @@
1
+ ########### BASIC ###############
2
+
3
+
4
+ target_server: 192.168.0.11
5
+ port: 80
6
+
7
+
8
+
9
+
10
+
11
+ ########### ADVANCED ###############
12
+
13
+ # You can change which site you use to lookup your IP address
14
+ ip_lookup_url: http://api.externalip.net/ip/
15
+
16
+ # Place a url here and it will be run every time your dns changes. Afraid.org for
17
+ # example will let you change the ddns subdomain via a direct url you can place
18
+ # here...
19
+ # ddns_update_url: http://www.example.com/up_my_ip_stored_on_your_server
20
+
21
+ ddns_update_url:
22
+
23
+
24
+ # there's also the debug mode, set debug to true to see more output in the log file located at c:\it\logs\ipfixer.log
25
+ # debug: true
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ipfixer_client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ipfixer_client"
7
+ s.version = IpfixerClient::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["TheNotary"]
10
+ s.email = ["no@email.plz"]
11
+ s.homepage = ""
12
+ s.summary = %q{ This gem is used to keep track of remote infrastructure }
13
+ s.description = %q{ You install this service on a windows machine, and it will keep it's IP address posted to a rails application specified and also updates subdomain status at afraid.org. }
14
+
15
+ s.rubyforge_project = "ipfixer_client"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'thor'
23
+ s.add_dependency 'win32-service' if RUBY_PLATFORM =~ /mingw32/
24
+ s.add_development_dependency 'win32console' if RUBY_PLATFORM =~ /mingw32/
25
+ s.add_development_dependency 'wdm' if RUBY_PLATFORM =~ /mingw32/
26
+
27
+ s.add_dependency 'daemons' if RUBY_PLATFORM =~ /linux/
28
+
29
+
30
+ s.add_development_dependency('rake', '0.9.2')
31
+ s.add_development_dependency 'rspec'
32
+ s.add_development_dependency 'pry'
33
+
34
+ #s.add_development_dependency('rake-compiler', '0.7.9')
35
+ #s.add_development_dependency('archive-tar-minitar', '0.5.2') # this might be another secret dependency
36
+ #s.add_development_dependency('debugger-ruby_core_source', '1.1.3') # dep dep
37
+ #s.add_development_dependency('ruby_core_source', '0.1.5') # SECRET DEPENDENCY OF LINECACHE!!! MAYBE...
38
+ #s.add_development_dependency('ruby-debug19') # installing manually, this gem is able to install linecache19 successfully
39
+ #s.add_development_dependency('debugger-linecache', '1.1.2') # depdep
40
+
41
+ #s.add_development_dependency('linecache19', '0.5.12') # dep dep, very problematic one...
42
+ s.add_development_dependency 'debugger' if RUBY_PLATFORM =~ /linux/
43
+ #s.add_development_dependency 'guard'
44
+ #s.add_development_dependency 'guard-rspec'
45
+
46
+ #s.add_development_dependency 'autotest'
47
+ end
@@ -0,0 +1,81 @@
1
+ LOG_FILE = "C:\\it\\logs\\ipfixer.log"
2
+ LOG_FOLDER = File.dirname LOG_FILE
3
+ LONG_DURATION = 1000
4
+ STANDARD_INTERVAL = 500
5
+ DEBUG_MODE = true
6
+ WINDOWS = RUBY_PLATFORM =~ /mingw32/ ? true : false
7
+ LINUX = RUBY_PLATFORM =~ /linux/ ? true : false
8
+
9
+ SERVICE_NAME = 'ipfixer_svc'
10
+ SERVICE_DESC = 'A service that helps keep track of remote infrastructure.'
11
+
12
+
13
+ require 'ipfixer_client/config_stuff'
14
+ require 'ipfixer_client/logger'
15
+ require 'ipfixer_client/net_stuff'
16
+ require 'ipfixer_client/version'
17
+ require 'ipfixer_client/installer.rb'
18
+ require 'ipfixer_client/ipfixer_svc'
19
+
20
+
21
+ config = IpfixerClient.get_configuration_settings
22
+
23
+ # Point this at your hub
24
+ IP_FIXER_HUB = config["target_server"].nil? ? nil : config["target_server"].dup.gsub(/^https?:\/\//, '') # '192.168.1.1'
25
+ PORT = config["port"].nil? ? "80" : config["port"].to_s.dup # "3000"
26
+ IP_LOOKUP_URL = config["ip_lookup_url"].nil? ? nil : config["ip_lookup_url"].dup # 'http://automation.whatismyip.com/n09230945.asp'
27
+ DDNS_UPDATE_URL = config["ddns_update_url"].nil? ? nil : config["ddns_update_url"].dup
28
+ SECURITY_TOKEN = config["security_token"].nil? ? nil : config["security_token"].dup
29
+ #DEBUG_MODE = config["debug"].nil? || config["debug"] == false ? false : true
30
+
31
+
32
+ module IpfixerClient
33
+ extend Logger
34
+
35
+ def self.help
36
+ puts "This is ipfixer_client, a little client... see the docs"
37
+ end
38
+
39
+ def self.client_svc
40
+ DemoDaemon.mainloop
41
+ end
42
+
43
+ def self.install
44
+ i = Installer.new
45
+ i.install
46
+ end
47
+
48
+ def self.uninstall
49
+ i = Installer.new
50
+ i.uninstall
51
+ end
52
+
53
+ def self.start
54
+ if IpfixerClient::Installer.service_installed?
55
+ puts "We can start"
56
+ if WINDOWS
57
+ result = `sc start #{SERVICE_NAME}`
58
+ puts result
59
+ elsif LINUX
60
+ # FIXME: Write logic
61
+ end
62
+ else
63
+ puts "The service is not yet installed, please run the command `ipfixer install`"
64
+ end
65
+ end
66
+
67
+ def self.stop
68
+ if IpfixerClient::Installer.service_installed?
69
+ puts "We can stop"
70
+ if WINDOWS
71
+ result = `sc stop #{SERVICE_NAME}`
72
+ puts result
73
+ elsif LINUX
74
+ # FIXME: Write logic
75
+ end
76
+ else
77
+ puts "The service is not yet installed"
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,36 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+
4
+ module IpfixerClient
5
+
6
+ def self.get_configuration_settings(config_file_path = "C:\\it\\ipfixer\\conf\\config.yml")
7
+ return default_config_settings unless File.exists? config_file_path
8
+ config_file = get_config_file config_file_path
9
+
10
+ conf_contents = config_file.read
11
+
12
+ begin
13
+ config = YAML.load_file(config_file)
14
+ rescue Exception => e
15
+ File.open(LOG_FILE,'a+'){ |f| f.puts " ***YAML FILE PROBLEM DETECTED, The file was likely malformed... Check on it, or delete it and reinstall."; f.puts e.message }
16
+ exit!
17
+ end
18
+
19
+ return config
20
+ end
21
+
22
+ def self.get_config_file(file_path)
23
+ if File.exists?(file_path)
24
+ File.open(file_path, "rb")
25
+ else
26
+ StringIO.new('')
27
+ end
28
+ end
29
+
30
+ def self.default_config_settings
31
+ return {'target_server' => "192.168.0.11",
32
+ "port" => 80,
33
+ "ip_lookup_url" => "http://api.externalip.net/ip/",
34
+ "security_token" => "secret pass"}
35
+ end
36
+ end
@@ -0,0 +1,213 @@
1
+ module IpfixerClient
2
+ require 'yaml'
3
+ require 'rubygems'
4
+ require 'win32/service' if RUBY_PLATFORM =~ /mingw32/
5
+ require 'rbconfig'
6
+ require 'fileutils'
7
+
8
+
9
+ class Installer
10
+ include Win32 if RUBY_PLATFORM =~ /mingw32/
11
+
12
+ def initialize
13
+ end
14
+
15
+ def install(service_name = SERVICE_NAME, target_folder = 'c:\it\ipfixer')
16
+ uninstall(service_name) # uninstalls service if it's already installed
17
+
18
+ ruby = get_ruby_interpreter_path
19
+ gem_path = get_ipfixer_bin_path
20
+ service_command_line = construct_service_execution_string(ruby, gem_path)
21
+
22
+ config = prompt_for_information
23
+
24
+ create_installation_files(target_folder, config)
25
+
26
+ if WINDOWS
27
+ # Create a new service
28
+ Service.create({
29
+ :service_name => service_name,
30
+ :service_type => Service::WIN32_OWN_PROCESS,
31
+ :description => SERVICE_DESC,
32
+ :start_type => Service::AUTO_START,
33
+ :error_control => Service::ERROR_NORMAL,
34
+ :binary_path_name => service_command_line,
35
+ :load_order_group => 'Network',
36
+ :dependencies => ['W32Time','Schedule', 'RpcSs'],
37
+ :display_name => service_name
38
+ })
39
+ elsif LINUX
40
+ # FIXME: Add a linux routine
41
+
42
+ end
43
+
44
+
45
+ #`sc start ipfixer_svc`
46
+ end
47
+
48
+ def get_target_folder
49
+ return "" # FIXME: logic
50
+ end
51
+
52
+ def uninstall(service_name = SERVICE_NAME)
53
+ # stop the service if it's started
54
+ # stop if service_started? service_name
55
+ # if the service is marked for deletion, do nothing here
56
+ #return if service_marked_for_deletion? service_name
57
+ Service.delete(service_name) if service_installed? service_name if WINDOWS
58
+ # FIXME: write linux logic
59
+ end
60
+
61
+ def start(service_name = SERVICE_NAME)
62
+ Service.start(service_name)
63
+ end
64
+
65
+ def stop(service_name = SERVICE_NAME)
66
+ Service.stop(service_name)
67
+ end
68
+
69
+ def create_installation_files(target_folder, config = nil)
70
+ FileUtils.mkdir_p "#{target_folder}/conf"
71
+ FileUtils.cp "#{get_root_of_gem}/conf/config.yml", "#{target_folder}/conf/config.yml"
72
+ update_yml_file(target_folder, config)
73
+ end
74
+
75
+ def get_root_of_gem
76
+ dir_name = File.dirname(__FILE__)
77
+ File.expand_path Dir.new("#{dir_name}/../..").path
78
+ end
79
+
80
+
81
+ #private
82
+
83
+ # The finds out where the ruby interpretr is on the system.
84
+ def get_ruby_interpreter_path
85
+ File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"])
86
+ end
87
+
88
+ # This is how I get the path of the ipfixer script in the bin/ folder
89
+ def get_ipfixer_bin_path
90
+ Gem.bin_path('ipfixer_client', 'ipfixer').gsub(/.ipfixer$/,'')
91
+ end
92
+
93
+ # this string is the argument for the service
94
+ # Example: 'c:\Ruby\bin\ruby.exe -C c:\temp ruby_example_service.rb'
95
+ def construct_service_execution_string(ruby, gem_path)
96
+ "#{ruby} -C #{gem_path} ipfixer client_svc"
97
+ end
98
+
99
+ # this method prompts the user for a few pieces of information
100
+ def prompt_for_information
101
+ target_server = prompt_for_target_server
102
+ port = prompt_for_port((target_server =~ /^https:\/\//).nil? ? "80" : "443")
103
+ ddns_update_url = prompt_for_ddns_url
104
+ security_token = prompt_for_security_token
105
+
106
+ config = {'target_server' => target_server,
107
+ 'port' => port,
108
+ 'ddns_update_url' => ddns_update_url,
109
+ 'security_token' => security_token }
110
+ return config
111
+ end
112
+
113
+ def prompt_for_installation_folder
114
+ puts "Please specify an installation directory, or hit enter for default 'c:\\it\\ipfixer'"
115
+ input = STDIN.gets.chomp
116
+
117
+ if input == ""
118
+ return 'c:\it\ipfixer'
119
+ else
120
+ return input
121
+ end
122
+ end
123
+
124
+ def prompt_for_target_server
125
+ puts "Please specify a target server name"
126
+ puts "[192.168.0.11]"
127
+ input = STDIN.gets.chomp
128
+
129
+ if input == ""
130
+ return nil
131
+ else
132
+ return input
133
+ end
134
+ end
135
+
136
+ def prompt_for_port(default_port = "80")
137
+ puts "Please specify a port"
138
+ puts "[#{default_port}]"
139
+ input = STDIN.gets.chomp
140
+
141
+ if input == ""
142
+ return default_port
143
+ else
144
+ return input.to_i
145
+ end
146
+ end
147
+
148
+ def prompt_for_ddns_url
149
+ puts "If you have a ddns direct URL to set your IP address, you may specify that now, else hit enter"
150
+ puts "ex: www.example.com/ipsetter.php?lkajsdflkjsdf"
151
+ input = STDIN.gets.chomp
152
+
153
+ if input == ""
154
+ return nil
155
+ else
156
+ return input
157
+ end
158
+ end
159
+
160
+ def prompt_for_security_token
161
+ puts "If you'd like, you can specify a security token that ipfixer will use to authenticate with the ipfixes server"
162
+ puts "ex: 'secret pass'"
163
+ input = STDIN.gets.chomp
164
+ if input == ""
165
+ "secret pass"
166
+ else
167
+ input
168
+ end
169
+ end
170
+
171
+ def service_installed?(service_name)
172
+ if WINDOWS
173
+ return `sc query #{service_name}` =~ /FAILED 1060/i ? false : true
174
+ end
175
+
176
+ if LINUX # FIXME: write logic
177
+ return true
178
+ end
179
+ end
180
+
181
+ def service_started?(service_name)
182
+ return `sc query #{service_name} | grep STATE` =~ /STOPPED/i ? false : true if WINDOWS
183
+ return true if LINUX # FIXME: write logic
184
+ end
185
+
186
+ def service_marked_for_deletion?(service_name)
187
+ return `sc query #{service_name} | grep STATE` =~ /STOPPED/i ? false : true if WINDOWS
188
+ return true if LINUX # FIXME: write logic
189
+ end
190
+
191
+ # writes a new yaml file out of the data entered, unless there was no data entered,
192
+ # in which case it will leave that yaml file untouched and with full comments....
193
+ def update_yml_file(target_folder, config)
194
+ return if config.nil?
195
+
196
+
197
+ target_server = config['target_server']
198
+ port = config['port']
199
+ ddns_update_url = config['ddns_update_url']
200
+ require 'pry';binding.pry
201
+ config = YAML.load_file(target_folder + '/conf/config.yml') # "C:\\it\\ipfixer\\conf\\config.yml"
202
+
203
+ config['target_server'] = target_server unless target_server.nil?
204
+ config['port'] = port unless port.nil?
205
+ config['ddns_update_url'] = ddns_update_url unless ddns_update_url.nil?
206
+
207
+ File.open(target_folder + '/conf/config.yml', "w") {|f| f.write(config.to_yaml) }
208
+ end
209
+
210
+ end
211
+
212
+
213
+ end
@@ -0,0 +1,120 @@
1
+ require 'rubygems'
2
+ require 'win32/daemon' if RUBY_PLATFORM =~ /mingw32/
3
+ require 'daemons' if RUBY_PLATFORM =~ /linux/
4
+ if RUBY_PLATFORM =~ /linux/
5
+ class Daemon
6
+
7
+ end
8
+ end
9
+ require 'socket'
10
+ require 'net/http'
11
+ require 'fileutils'
12
+
13
+ module IpfixerClient
14
+ include Win32 if RUBY_PLATFORM =~ /mingw32/
15
+
16
+ begin
17
+ class DemoDaemon < Daemon # http://stackoverflow.com/questions/163497/running-a-ruby-program-as-a-windows-service
18
+
19
+ def service_main
20
+ host_name, last_ip = init_service
21
+
22
+ while running?
23
+ current_ip = IpfixerClient.get_ip_address(IP_LOOKUP_URL)
24
+ handle_invalid_ip(current_ip) and redo if IpfixerClient.invalid_ip?(current_ip)
25
+ ip_changed = current_ip != last_ip
26
+
27
+ if ip_changed
28
+
29
+ last_ip = handle_ip_change(host_name, current_ip, last_ip)
30
+ my_logger "got here"
31
+ else
32
+ my_logger "#{Time.now}: No change in IP, #{current_ip}" if DEBUG_MODE
33
+ end
34
+
35
+ #my_logger "Service is running #{Time.now}: #{host_name}"
36
+ sleep STANDARD_INTERVAL
37
+ end
38
+ end
39
+
40
+ def service_stop
41
+ File.open(LOG_FILE, "a"){ |f| f.puts "***Service stopped #{Time.now}" }
42
+ exit! # the guy I took the code from might have found the exit! command useful... I can't find a use for it. Wait.. I think it can't stop if you leave it out now...
43
+ end
44
+
45
+ # Does initialization preparations for the service.
46
+ # returns an array of [host_name, '']
47
+ def init_service
48
+ handle_missing_specifications and exit! if missing_specifications_detected
49
+
50
+ host_name = Socket.gethostname
51
+ last_ip = ''
52
+ [host_name, last_ip]
53
+ end
54
+
55
+ # To run, the daemon needs to have an IP Lookup address, and
56
+ # an IP_FIXER_HUB specified in the config. If those aren't
57
+ # found, we need to exit
58
+ def missing_specifications_detected
59
+ (IP_FIXER_HUB.nil? || IP_LOOKUP_URL.nil?) ? true : false
60
+ end
61
+
62
+ def handle_missing_specifications
63
+ File.open(LOG_FILE,'a+') do |f|
64
+ f.puts " ***YAML FILE PROBLEM DETECTED, IP_FIXER_HUB or IP_LOOKUP_URL was nil. You can't run this service with out a server to contact, or a way of looking up your IP: "
65
+ f.puts "#{IP_FIXER_HUB} ... #{IP_LOOKUP_URL}"
66
+ f.puts "Check your conf file at c:\\it\\ipfixer\\conf\\conf.yml"
67
+ end
68
+ true
69
+ end
70
+
71
+ def handle_invalid_ip(current_ip)
72
+ my_logger "**ERROR: Recieved an invalid ip."
73
+ my_logger " current_ip: #{current_ip}"
74
+ sleep STANDARD_INTERVAL
75
+ true
76
+ end
77
+
78
+ def handle_ip_change(host_name, current_ip, last_ip)
79
+ IpfixerClient.tell_ddns_our_new_ip(DDNS_UPDATE_URL) unless DDNS_UPDATE_URL.nil?
80
+
81
+ post_result = IpfixerClient.post_ip(host_name, current_ip, SECURITY_TOKEN)
82
+ if post_result == true
83
+ last_ip = current_ip
84
+ my_logger "Successfully posted IP to mother server... #{current_ip}"
85
+ else
86
+ handle_failed_ip_post(current_ip, last_ip, post_result)
87
+ end
88
+
89
+ last_ip
90
+ end
91
+
92
+ def handle_failed_ip_post(current_ip, last_ip, post_result)
93
+ my_logger "ERROR: Problem uploading new IP Address."
94
+ my_logger " current_ip: #{current_ip}, last_ip: #{last_ip}, post_result: #{post_result}"
95
+ sleep LONG_DURATION
96
+ end
97
+
98
+ def return_hello # for testing that funcs with the my_logger works... begging to be removed...
99
+ my_logger "hello"
100
+ sleep 1000
101
+ "hello"
102
+ end
103
+
104
+ def sleep(x) # for stub testing...
105
+ Kernel.sleep x
106
+ end
107
+
108
+ def my_logger(text)
109
+ IpfixerClient.my_logger(text)
110
+ end
111
+
112
+ end
113
+
114
+
115
+ rescue Exception => err
116
+ File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err} " }
117
+ raise
118
+ end
119
+
120
+ end
@@ -0,0 +1,58 @@
1
+ module IpfixerClient
2
+
3
+ def self.my_logger(text)
4
+ create_the_log_folder if !Dir.exists? LOG_FOLDER
5
+ File.open(LOG_FILE, "a"){ |f| f.puts text }
6
+ end
7
+
8
+ def self.create_the_log_folder
9
+ logs_folder = LOG_FOLDER
10
+ FileUtils.mkdir_p(logs_folder) unless File.directory?(logs_folder)
11
+ end
12
+
13
+ module Logger
14
+ =begin
15
+ def my_logger(text)
16
+ create_the_log_folder if !Dir.exists? LOG_FOLDER
17
+ File.open(LOG_FILE, "a"){ |f| f.puts text }
18
+ end
19
+
20
+ def create_the_log_folder
21
+ logs_folder = LOG_FOLDER
22
+ FileUtils.mkdir_p(logs_folder) unless File.directory?(logs_folder)
23
+ end
24
+ =end
25
+
26
+ # I moved this here so I'd have all debug output in the same file...
27
+ # But wouldn't it be better to have a module named Dbg.post_ip instead?
28
+ def dbg_post_ip(host_name, ip, response, body)
29
+ my_logger "DEBUGGER: In Post_IP"
30
+ my_logger " host_name: #{host_name}"
31
+ my_logger " ip: #{ip}"
32
+ my_logger " Response: #{response}"
33
+ my_logger " Req body: #{body}"
34
+ end
35
+
36
+ def dbg_tell_ddns_our_new_ip(code)
37
+ my_logger "Sent msg to ddns... response code was...#{code}. #{Time.now}"
38
+ end
39
+
40
+ def err_getting_ip(ip_lookup_url, message)
41
+ my_logger "**ERROR: Something went wrong trying to get current IP Address"
42
+ my_logger "ip_lookup_url: #{ip_lookup_url}"
43
+ my_logger "Exception: #{message}"
44
+ end
45
+
46
+ def err_tell_ddns_our_new_ip(ddns_update_url)
47
+ my_logger "Exception occured whil trying to tell_ddns_our_new_ip..."
48
+ my_logger "Url was #{ddns_update_url}"
49
+ end
50
+
51
+ def err_ddns_gave_wrong_response_code
52
+ my_logger "failed to properly tell ddns our new ip..."
53
+ false
54
+ end
55
+ end
56
+ end
57
+
58
+
@@ -0,0 +1,66 @@
1
+ module IpfixerClient
2
+ # You need to wait 300 seconds before using this function
3
+ def self.get_ip_address(ip_lookup_url)
4
+ begin
5
+ http_response = Net::HTTP.get_response(URI.parse(ip_lookup_url))
6
+ rescue Exception => e
7
+ err_getting_ip(ip_lookup_url, e.message)
8
+ return "failedToGetAddress"
9
+ end
10
+
11
+ if http_response.code == "200"
12
+ return http_response.body
13
+ else
14
+ return http_response.body + http_response.code # not very ellegant huh...
15
+ end
16
+ end
17
+
18
+ # Returns true if the string supplied wasn't actually a valid ip
19
+ def self.invalid_ip?(ip)
20
+ is_invalid = (ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/).nil? ? true : false
21
+ end
22
+
23
+ # POST /ipfixes
24
+ # { "ipfix": { "host":"Host Name", "ip":"192.168.0.1" } }
25
+ def self.post_ip(host_name, ip, security_token = nil)
26
+ security_token = security_token.nil? ? "secret pass" : security_token
27
+ path = '/ipfixes.json'
28
+
29
+ http = Net::HTTP.new(IP_FIXER_HUB, PORT)
30
+
31
+ req = Net::HTTP::Post.new(path)
32
+ req.content_type = 'application/json' # specify json
33
+
34
+ json_string = "{ 'ipfix': { 'host': '#{host_name}', 'ip': '#{ip}', 'security_token': '#{security_token}' } }"
35
+ req.body = json_string.gsub("'", '"') # json doesn't accept the single quote symbol
36
+
37
+
38
+ begin
39
+ response = http.start {|htp| htp.request(req) }
40
+ rescue
41
+ my_logger "FAILURE TO POST TO ADDRESS: Check if address and port are operational."
42
+ return false
43
+ end
44
+
45
+ dbg_post_ip(host_name, ip, response, req.body) if DEBUG_MODE
46
+
47
+ return true if response.code == "200" || response.code == "201"
48
+ return response.code
49
+ end
50
+
51
+ def self.tell_ddns_our_new_ip(ddns_update_url)
52
+ begin
53
+ http_response = Net::HTTP.get_response(URI.parse(ddns_update_url))
54
+ dbg_tell_ddns_our_new_ip(http_response.code) if DEBUG_MODE
55
+ rescue
56
+ err_tell_ddns_our_new_ip(ddns_update_url)
57
+ end
58
+
59
+ if http_response.code == "200"
60
+ true
61
+ else
62
+ err_ddns_gave_wrong_response_code
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,3 @@
1
+ module IpfixerClient
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+ require 'ipfixer_client'
4
+
5
+
6
+ describe "IpfixerClient" do
7
+
8
+ it "install should go fine" do
9
+ # IpfixerClient.install
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'ipfixer_client'
2
+
3
+ def windows_service_installed?(service_name)
4
+ response = `sc query #{service_name}` if WINDOWS
5
+ response = `ps -A |grep ` if LINUX # FIXME: Write working linux routine
6
+ return false if response =~ /FAILED 1060/i if WINDOWS
7
+ return true
8
+ end
9
+
10
+ def delete_all_testing_files(target_folder)
11
+ FileUtils.rm_rf target_folder # if RUBY_PLATFORM =~ /mingw32/
12
+ # FileUtils.rm_rf target_folder if RUBY_PLATFORM =~ /linux/
13
+ end
14
+
15
+ def get_project_dir_from_tests
16
+ File.expand_path Dir.new("#{File.dirname(__FILE__)}/..").path
17
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe "installation/ uninstallation" do
5
+ before :all do
6
+ @virtual_service_name = "aaa-deleteme-test-service-ipfixer"
7
+ @target_folder = "/tmp/it/ipfixer"
8
+ #FileUtils.mkdir_p @target_folder
9
+ delete_all_testing_files(@target_folder)
10
+ end
11
+ after :all do
12
+ delete_all_testing_files(@target_folder)
13
+ end
14
+
15
+
16
+ before :each do
17
+ @installer = IpfixerClient::Installer.new
18
+ @installer.stub!(:prompt_for_target_server).and_return(nil)
19
+ @installer.stub!(:prompt_for_port).and_return(nil)
20
+ @installer.stub!(:prompt_for_ddns_url).and_return(nil)
21
+ @installer.stub!(:prompt_for_security_token).and_return(nil)
22
+ end
23
+
24
+ after :each do
25
+ @installer.uninstall @virtual_service_name # just make sure we don't already have an accidental installation in place
26
+
27
+ @installer.unstub!(:prompt_for_target_server)
28
+ @installer.unstub!(:prompt_for_port)
29
+ @installer.unstub!(:prompt_for_ddns_url)
30
+ @installer.unstub!(:prompt_for_security_token)
31
+ end
32
+
33
+
34
+ it "should install the service into windows and also uninstall it...", :current => true do
35
+ @installer.install @virtual_service_name, @target_folder
36
+ windows_service_installed?(@virtual_service_name).should be_true
37
+
38
+ @installer.uninstall @virtual_service_name
39
+ windows_service_installed?(@virtual_service_name).should be_false
40
+ end
41
+
42
+ it "should setup the log folder and files" do
43
+ @installer.install @virtual_service_name, @target_folder
44
+ end
45
+
46
+ it "should be able to launch the daemon loop" do
47
+ # IpfixerClient.client_svc
48
+ end
49
+
50
+ it "should be able to find the root of the gem when it's installed" do
51
+ @installer.get_root_of_gem.should eq(get_project_dir_from_tests) # this test is a bit silly...
52
+ end
53
+
54
+ it "should be able to create it's log files" do
55
+ @installer.create_installation_files(@target_folder)
56
+
57
+ File.exists?(@target_folder+"/conf/config.yml").should be_true
58
+ end
59
+
60
+ describe "helper methods" do
61
+
62
+ it "should return a valid path" do
63
+ path = @installer.get_ipfixer_bin_path
64
+ lambda {
65
+ FileUtils.cd(path)
66
+ }.should_not raise_error
67
+ end
68
+
69
+
70
+ describe "when service is uninstalled" do
71
+ it "should know when the service is not installed" do
72
+ @installer.service_installed?(@virtual_service_name).should be_false
73
+ end
74
+ end
75
+
76
+ describe "when service is installed" do
77
+ before :each do
78
+ @installer.install @virtual_service_name, @target_folder
79
+ end
80
+ after :each do
81
+ @installer.uninstall @virtual_service_name
82
+ end
83
+
84
+ it "should know when the service is installed" do
85
+ @installer.service_installed?(@virtual_service_name).should be_true
86
+ end
87
+
88
+ it "should tell us when a service is STOPPED" do
89
+ @installer.service_started?(@virtual_service_name).should be_false
90
+ end
91
+ =begin
92
+ # SOmetimes... this test will fail... it's not predictable...
93
+ it ".start, .stop, .service_started?" do
94
+ @installer.start @virtual_service_name
95
+ @installer.service_started?(@virtual_service_name).should be_true
96
+
97
+ @installer.stop @virtual_service_name
98
+ # sleep 0.02 # you need to give it a moment to stop...
99
+ @installer.service_started?(@virtual_service_name).should be_false
100
+
101
+ sleep 0.5 # we need to sleep here so windows services can catch up
102
+ end
103
+ =end
104
+
105
+ end
106
+
107
+
108
+
109
+ end
110
+
111
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe "IpFixer Svc" do
4
+
5
+ before :each do
6
+ @d = IpfixerClient::DemoDaemon.new
7
+ @d.stub!(:sleep).and_return(nil)
8
+ @d.stub!(:my_logger).and_return(nil)
9
+ end
10
+
11
+ after :each do
12
+ @d.unstub!(:sleep)
13
+ @d.unstub!(:my_logger)
14
+ end
15
+
16
+ it "should work" do # for debuging the daemon apparently...
17
+ @d.return_hello.should eq "hello"
18
+ # d.service_main # only run this once... else it will run for ever and your tests can't pass
19
+ # binding.pry
20
+ end
21
+
22
+ it "should handle invalid IPs well" do
23
+ @d.handle_invalid_ip("i'm invalid").should eq true
24
+ end
25
+
26
+ it "should initialize the service, yielding the hostname" do
27
+ a = @d.init_service
28
+ a.first.should be_an_instance_of(String)
29
+ a.last.should be_an_instance_of(String)
30
+ a.length.should eq 2
31
+ end
32
+
33
+ it "should work" do
34
+ #binding.pry
35
+ end
36
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe "net stuff" do
4
+ before :each do
5
+ @ip_valid = "192.168.244.244"
6
+ @ip_with_letters = "192.168.244.24a"
7
+ @ip_too_big = "192.168.255.2555"
8
+ @ip_too_short = "1.1.2."
9
+ @ip_too_many_decimals = "192.168.2.2."
10
+ @ip_too_few_decimals = "192.168.2444"
11
+ end
12
+
13
+ describe "reject invalid IP addresses" do
14
+ it "should reject IP addresses with letters in them" do
15
+ IpfixerClient.invalid_ip?(@ip_with_letters).should be_true
16
+ end
17
+
18
+ it "should reject IP addresses that are way too long" do
19
+ IpfixerClient.invalid_ip?(@ip_too_big).should be_true
20
+ end
21
+
22
+ it "should reject IP addresses that are way too short" do
23
+ IpfixerClient.invalid_ip?(@ip_too_short).should be_true
24
+ end
25
+
26
+ it "should reject IP addresses that have too many decimal points" do
27
+ IpfixerClient.invalid_ip?(@ip_too_many_decimals).should be_true
28
+ end
29
+
30
+ it "should reject IP addresses that have too few decimal points" do
31
+ IpfixerClient.invalid_ip?(@ip_too_few_decimals).should be_true
32
+ end
33
+ end
34
+
35
+ it "should accept a valid IP address" do
36
+ IpfixerClient.invalid_ip?(@ip_valid).should be_false
37
+ end
38
+
39
+
40
+ it "should read a config file and return a config object" do
41
+ #IpfixerClient.get_configuration_settings.should eq true
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ipfixer_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - TheNotary
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: daemons
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: debugger
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ' You install this service on a windows machine, and it will keep it''s
98
+ IP address posted to a rails application specified and also updates subdomain status
99
+ at afraid.org. '
100
+ email:
101
+ - no@email.plz
102
+ executables:
103
+ - ipfixer
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - .gitignore
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - Guardfile
111
+ - README
112
+ - Rakefile
113
+ - bin/ipfixer
114
+ - change_log
115
+ - conf/config.yml
116
+ - ipfixer_client.gemspec
117
+ - lib/ipfixer_client.rb
118
+ - lib/ipfixer_client/config_stuff.rb
119
+ - lib/ipfixer_client/installer.rb
120
+ - lib/ipfixer_client/ipfixer_svc.rb
121
+ - lib/ipfixer_client/logger.rb
122
+ - lib/ipfixer_client/net_stuff.rb
123
+ - lib/ipfixer_client/version.rb
124
+ - spec/acceptance/ipfixer_client_spec.rb
125
+ - spec/spec_helper.rb
126
+ - spec/unit/installer_spec.rb
127
+ - spec/unit/ipfixer_svc_spec.rb
128
+ - spec/unit/net_stuff_spec.rb
129
+ homepage: ''
130
+ licenses: []
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project: ipfixer_client
148
+ rubygems_version: 2.1.8
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: This gem is used to keep track of remote infrastructure
152
+ test_files: []