cdmon_updater 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +76 -0
- data/Rakefile +8 -0
- data/VERSION +1 -0
- data/bin/cdmon_config +129 -0
- data/bin/cdmon_updater +14 -0
- data/cdmon_updater.gemspec +20 -0
- data/config/cdmon.yml +19 -0
- data/config/schedule.rb +22 -0
- data/lib/cdmon.rb +5 -0
- data/lib/cdmon_updater.rb +52 -0
- data/lib/cdmon_updater/config.rb +69 -0
- data/lib/cdmon_updater/mailer.rb +93 -0
- data/lib/cdmon_updater/version.rb +3 -0
- data/lib/ip_updater.rb +64 -0
- data/spec/ip_updater_spec.rb +15 -0
- data/spec/mailer_spec.rb +40 -0
- data/spec/spec_helper.rb +1 -0
- metadata +93 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.3@cdmon_updater --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Leonardo Mateo
|
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
@@ -0,0 +1,76 @@
|
|
1
|
+
# CdmonUpdater
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cdmon_updater'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cdmon_updater
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
After installation you'll have the command cdmon_config.
|
21
|
+
Use it with -g or --generate to help you generate your config file.
|
22
|
+
|
23
|
+
It will ask your user data and generate the file /etc/cdmon.yml.
|
24
|
+
If you need to change the settings you can modify the config file
|
25
|
+
later with any text editor.
|
26
|
+
The config file has the following structure:
|
27
|
+
|
28
|
+
general:
|
29
|
+
dns: dinamic1.cdmon.net
|
30
|
+
email: root@server
|
31
|
+
log_level: DEBUG
|
32
|
+
service_url: https://dinamico.cdmon.org/onlineService.php
|
33
|
+
send_mail_on_error: no
|
34
|
+
|
35
|
+
users:
|
36
|
+
cdmon_user:
|
37
|
+
hosts: kandalf.com.ar blog.kandalf.com.ar
|
38
|
+
md5pass: 1278323gfb37875f2749ju6cabd8io89
|
39
|
+
|
40
|
+
mailer:
|
41
|
+
address: relay.someserver.com.ar
|
42
|
+
port: 25
|
43
|
+
domain: kandalf.com.ar
|
44
|
+
user_name: someuser #without @domain
|
45
|
+
password: plain_password
|
46
|
+
|
47
|
+
The cdmon_user is the user you have associated in your CDMon control panel with your domains.
|
48
|
+
As you can have multiple users in CDMon, you can add as many user sections as you need here.
|
49
|
+
|
50
|
+
The md5pass should be the user's password crypted as an MD5 hash.
|
51
|
+
The cdmon_config script comes will encode your password for you
|
52
|
+
when generating the config file. If you later change your password
|
53
|
+
on the cdmon control panel, you can encode it using cdmon_config -e or --encode
|
54
|
+
and then copy/paste the encoded string into the /etc/cdmon.yml config file.
|
55
|
+
|
56
|
+
For mail notification you must activate the the send_mail_on_error feature on
|
57
|
+
the general section and properly configure your mail server.
|
58
|
+
Also, you have to define a valid email address for the email feature on the general section.
|
59
|
+
|
60
|
+
When these things are set up, an email will be sent to the email address defined on
|
61
|
+
the general section with the message of the error and the time it was raised and
|
62
|
+
the subject of "CDMon Updater ERROR".
|
63
|
+
|
64
|
+
After you have your settings, you can run the client manually or you can add the following line to your crontab
|
65
|
+
|
66
|
+
0/3 * * * * /usr/bin/cdmon_updater
|
67
|
+
|
68
|
+
For it to run every 3 minutes.
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.6
|
data/bin/cdmon_config
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
options = {}
|
6
|
+
@config_options = {}
|
7
|
+
|
8
|
+
optparse = OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage cdmon_config [options]
|
10
|
+
If no options are given, current configuration will be shown."
|
11
|
+
|
12
|
+
opts.on('-g', '--generate', 'Generates configuration') do
|
13
|
+
options[:generate] = true
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on('-e', '--encode', 'Encode password with MD5') do
|
17
|
+
options[:encode] = true
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on( "-h", "--help", "Display this screen") do |opt|
|
21
|
+
puts opts
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
optparse.parse!
|
27
|
+
|
28
|
+
def show_config
|
29
|
+
puts File.read("/etc/cdmon.yml")
|
30
|
+
end
|
31
|
+
|
32
|
+
def encode_password
|
33
|
+
require 'digest/md5'
|
34
|
+
|
35
|
+
puts 'Enter your password. (Your input will not be shown)'
|
36
|
+
`stty -echo`
|
37
|
+
password = gets.strip
|
38
|
+
@config_options[:password] = Digest::MD5.hexdigest password
|
39
|
+
puts
|
40
|
+
`stty echo`
|
41
|
+
puts @config_options[:password]
|
42
|
+
end
|
43
|
+
|
44
|
+
def generate_config_file
|
45
|
+
raise 'Empty config options' if @config_options.empty?
|
46
|
+
|
47
|
+
config = <<-EOS
|
48
|
+
general:
|
49
|
+
dns: dinamic1.cdmon.net
|
50
|
+
email: root@server
|
51
|
+
log_level: DEBUG
|
52
|
+
service_url: https://dinamico.cdmon.org/onlineService.php
|
53
|
+
send_mail_on_error: #{@config_options[:send_mail]}
|
54
|
+
users:
|
55
|
+
#{@config_options[:username]}:
|
56
|
+
hosts: #{@config_options[:hosts]}
|
57
|
+
md5pass: #{@config_options[:password]}
|
58
|
+
|
59
|
+
mailer:
|
60
|
+
address: #{@config_options[:mailer_address]}
|
61
|
+
port: #{@config_options[:mailer_port]}
|
62
|
+
domain: #{@config_options[:mailer_domain]}
|
63
|
+
user_name: #{@config_options[:mailer_username]} #without @domain
|
64
|
+
password: #{@config_options[:mailer_password]}
|
65
|
+
EOS
|
66
|
+
|
67
|
+
begin
|
68
|
+
File.open("/etc/cdmon.yml", "w") do |f|
|
69
|
+
f.puts config.strip
|
70
|
+
end
|
71
|
+
show_config
|
72
|
+
rescue Errno::EACCES
|
73
|
+
puts "Can't open /etc/cdmon.yml for writing. Copy and paste this into /etc/cdmon.yml as root"
|
74
|
+
puts config
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
if options.empty?
|
80
|
+
show_config
|
81
|
+
exit
|
82
|
+
end
|
83
|
+
|
84
|
+
if options[:encode]
|
85
|
+
encode_password
|
86
|
+
exit
|
87
|
+
end
|
88
|
+
|
89
|
+
if options[:generate]
|
90
|
+
puts 'Enter your CDMON username'
|
91
|
+
@config_options[:username] = gets.strip
|
92
|
+
encode_password
|
93
|
+
|
94
|
+
puts 'Enter your hosts (white space separated)'
|
95
|
+
@config_options[:hosts] = gets.strip
|
96
|
+
|
97
|
+
puts 'Send mail on error? (yes/no)'
|
98
|
+
send_mail = gets.strip
|
99
|
+
@config_options[:send_mail] = (send_mail.downcase == 'yes') ? 'yes' : 'no'
|
100
|
+
|
101
|
+
if @config_options[:send_mail] == 'yes'
|
102
|
+
puts 'Enter your mail server address'
|
103
|
+
@config_options[:mailer_address] = gets.strip
|
104
|
+
|
105
|
+
puts 'Enter your mail server port (25)'
|
106
|
+
port = gets.strip
|
107
|
+
@config_options[:mailer_port] = (port.empty?) ? '25' : port
|
108
|
+
|
109
|
+
puts 'Enter your domain'
|
110
|
+
@config_options[:mailer_domain] = gets.strip
|
111
|
+
|
112
|
+
puts 'Enter your mailer user name (without the domain)'
|
113
|
+
@config_options[:mailer_username] = gets.strip
|
114
|
+
|
115
|
+
puts 'Enter your mailer password (input will not be shown)'
|
116
|
+
`stty -echo`
|
117
|
+
@config_options[:mailer_password] = gets.strip
|
118
|
+
`stty echo`
|
119
|
+
else
|
120
|
+
@config_options[:mailer_address] = 'relay.someserver.com.ar'
|
121
|
+
@config_options[:mailer_port] = '25'
|
122
|
+
@config_options[:mailer_domain] = 'example.com'
|
123
|
+
@config_options[:mailer_username] = 'someuser'
|
124
|
+
@config_options[:mailer_password] = 'secret'
|
125
|
+
end
|
126
|
+
generate_config_file
|
127
|
+
end
|
128
|
+
|
129
|
+
|
data/bin/cdmon_updater
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/ip_updater'
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
if File.exists?("/etc/cdmon.yml")
|
7
|
+
config_file = "/etc/cdmon.yml"
|
8
|
+
else
|
9
|
+
config_file = "#{Gem.dir}/gems/cdmon_updater-#{CDMonUpdater::VERSION}/config/cdmon.yml"
|
10
|
+
end
|
11
|
+
|
12
|
+
updater = CDMonUpdater::IPUpdater.new(config_file)
|
13
|
+
|
14
|
+
updater.update
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/cdmon_updater/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Leonardo Mateo"]
|
6
|
+
gem.email = ["leonardomateo@gmail.com"]
|
7
|
+
gem.description = %q{Just another client for CDMon (http://www.cdmon.com) dynamic DNS.}
|
8
|
+
gem.summary = %q{CDMon dynamic DNS client}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "cdmon_updater"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = CDMonUpdater::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
gem.add_development_dependency 'rspec'
|
20
|
+
end
|
data/config/cdmon.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
general:
|
2
|
+
dns: dinamic1.cdmon.net
|
3
|
+
email: root@server
|
4
|
+
log_level: DEBUG
|
5
|
+
service_url: https://dinamico.cdmon.org/onlineService.php
|
6
|
+
send_mail_on_error: no
|
7
|
+
|
8
|
+
users:
|
9
|
+
cdmon_user:
|
10
|
+
hosts: kandalf.com.ar blog.kandalf.com.ar
|
11
|
+
md5pass: 1278323gfb37875f2749ju6cabd8io89
|
12
|
+
|
13
|
+
mailer:
|
14
|
+
address: relay.someserver.com.ar
|
15
|
+
port: 25
|
16
|
+
domain: kandalf.com.ar
|
17
|
+
user_name: someuser #without @domain
|
18
|
+
password: plain_password
|
19
|
+
|
data/config/schedule.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Use this file to easily define all of your cron jobs.
|
2
|
+
#
|
3
|
+
# It's helpful, but not entirely necessary to understand cron before proceeding.
|
4
|
+
# http://en.wikipedia.org/wiki/Cron
|
5
|
+
|
6
|
+
# Example:
|
7
|
+
#
|
8
|
+
# set :cron_log, "/path/to/my/cron_log.log"
|
9
|
+
#
|
10
|
+
# every 2.hours do
|
11
|
+
# command "/usr/bin/some_great_command"
|
12
|
+
# runner "MyModel.some_method"
|
13
|
+
# rake "some:great:rake:task"
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# every 4.days do
|
17
|
+
# runner "AnotherModel.prune_old_records"
|
18
|
+
# end
|
19
|
+
every 3.minutes do
|
20
|
+
command "/usr/bin/cdmon_updater"
|
21
|
+
end
|
22
|
+
# Learn more: http://github.com/javan/whenever
|
data/lib/cdmon.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/cdmon_updater/version"
|
2
|
+
require File.dirname(__FILE__) + "/cdmon_updater/config"
|
3
|
+
require File.dirname(__FILE__) + "/cdmon_updater/mailer"
|
4
|
+
require "logger"
|
5
|
+
|
6
|
+
module CDMonUpdater
|
7
|
+
def self.logger
|
8
|
+
@@logger ||=
|
9
|
+
begin
|
10
|
+
@@logger = Logger.new("/var/log/cdmon.log", 10, 1024000)
|
11
|
+
rescue Errno::EACCES
|
12
|
+
@@logger = Logger.new("#{ENV["HOME"]}/.cdmon.log")
|
13
|
+
end
|
14
|
+
@@logger
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.log_level=(level)
|
18
|
+
case level
|
19
|
+
when "WARN"
|
20
|
+
logger.level = Logger::WARN
|
21
|
+
when "INFO"
|
22
|
+
logger.level = Logger::INFO
|
23
|
+
when "ERROR"
|
24
|
+
logger.level = Logger::ERROR
|
25
|
+
when "FATAL"
|
26
|
+
logger.level = Logger::FATAL
|
27
|
+
when "DEBUG"
|
28
|
+
logger.level = Logger::DEBUG
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.log_all(message)
|
33
|
+
logger.info(message)
|
34
|
+
logger.debug(message)
|
35
|
+
logger.warn(message)
|
36
|
+
logger.error(message)
|
37
|
+
self.send_error_mail(message) if Config.mail_on_error?
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
def self.send_error_mail(message)
|
42
|
+
mailer = Mailer.new({"server" => Config.mailer})
|
43
|
+
mailer.from = "cdmon_updater@#{Mailer.domain}"
|
44
|
+
mailer.to = Config.mail_to
|
45
|
+
mailer.subject = "CDMon Updater ERROR"
|
46
|
+
mailer.message = message
|
47
|
+
unless mailer.send_mail
|
48
|
+
logger.debug("Cannot send mail")
|
49
|
+
logger.info("Cannot send mail")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module CDMonUpdater
|
4
|
+
module Config
|
5
|
+
USER = "cdmon_host_user"
|
6
|
+
MD5PASS = "cdmon_crypted_user_pass"
|
7
|
+
EMAIL = "root@server"
|
8
|
+
HOSTS = %w"kandalf.com.ar blog.kandalf.com.ar"
|
9
|
+
DNS_NAMES = ["dinamic1.cdmon.net"]
|
10
|
+
|
11
|
+
CDMON_OK_IP = "customok"
|
12
|
+
CDMON_BAD_IP = "badip"
|
13
|
+
CDMON_ERROR_LOGIN = "errorlogin"
|
14
|
+
|
15
|
+
@@config = {}
|
16
|
+
|
17
|
+
def self.load(config_file)
|
18
|
+
@@config = YAML::load_file(config_file)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.log_level
|
22
|
+
@@config["general"]["log_level"] if @@config
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.users
|
26
|
+
@@config["users"]
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.mailer
|
30
|
+
@@config["mailer"]
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.mail_to
|
34
|
+
@@config["general"]["email"] if @@config
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.md5_password_for(user)
|
38
|
+
@@config["users"][user.to_s]["md5pass"] if @@config
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.hosts_for(user)
|
42
|
+
@@config["users"][user.to_s]["hosts"].split(" ") if @@config
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.dns
|
46
|
+
@@config["general"]["dns"] if @@config
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.mail_on_error?
|
50
|
+
@@config["general"]["send_mail_on_error"]
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.hosts
|
54
|
+
HOSTS
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.service_url
|
58
|
+
@@config["general"]["service_url"] if @@config
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.get_cdmon_ip_path_for(user)
|
62
|
+
"/onlineService.php?enctype=MD5&n=#{user.to_s}&p=#{self.md5_password_for(user)}"
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.cdmon_ip_path_for(user, address)
|
66
|
+
"/onlineService.php?enctype=MD5&n=#{user.to_s}&p=#{self.md5_password_for(user)}&cip=#{address}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'net/smtp'
|
2
|
+
require File.dirname(__FILE__) + '/config'
|
3
|
+
|
4
|
+
module CDMonUpdater
|
5
|
+
class Mailer < Net::SMTP
|
6
|
+
attr_accessor :subject, :message, :from, :to
|
7
|
+
|
8
|
+
if CDMonUpdater::Config.mailer
|
9
|
+
@@address = CDMonUpdater::Config.mailer["address"]
|
10
|
+
@@port = CDMonUpdater::Config.mailer["port"]
|
11
|
+
@@domain = CDMonUpdater::Config.mailer["domain"]
|
12
|
+
@@credentials[:user_name] = CDMonUpdater::Config.mailer["user_name"]
|
13
|
+
@@credentials[:password] = CDMonUpdater::Config.mailer["password"]
|
14
|
+
else
|
15
|
+
@@address = nil
|
16
|
+
@@port = nil
|
17
|
+
@@credentials = {:user_name => nil, :password => nil}
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(options = {})
|
21
|
+
if options.has_key?("server") && options["server"].is_a?(Hash) && options["server"].keys.any?
|
22
|
+
server_options = options.delete("server")
|
23
|
+
|
24
|
+
@@address = server_options["address"]
|
25
|
+
@@port = server_options["port"]
|
26
|
+
@@domain = server_options["domain"]
|
27
|
+
@@credentials[:user_name] = server_options["user_name"]
|
28
|
+
@@credentials[:password] = server_options["password"]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#class methods
|
33
|
+
def self.address
|
34
|
+
@@address
|
35
|
+
end
|
36
|
+
def self.address=(address)
|
37
|
+
@@address = address
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.port
|
41
|
+
@@port
|
42
|
+
end
|
43
|
+
def self.port=(port)
|
44
|
+
@@port = port
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.domain
|
48
|
+
@@domain
|
49
|
+
end
|
50
|
+
def self.domain=(domain)
|
51
|
+
@@domain = domain
|
52
|
+
end
|
53
|
+
def self.credentials
|
54
|
+
@@credentials
|
55
|
+
end
|
56
|
+
def self.credentials=(data = {:user_name => nil, :password => nil})
|
57
|
+
@@credentials = data
|
58
|
+
end
|
59
|
+
|
60
|
+
#instance methods
|
61
|
+
def send_mail
|
62
|
+
body = mail_headers
|
63
|
+
body << "#{Time.now.to_s} - #{@message}"
|
64
|
+
|
65
|
+
begin
|
66
|
+
@to.split(",").each do | to |
|
67
|
+
|
68
|
+
Net::SMTP.start(@@address, @@port, @from, @@credentials[:user_name], @@credentials[:password], :login) do |smtp|
|
69
|
+
|
70
|
+
smtp.send_message(body, @from, to.strip)
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
true
|
75
|
+
rescue
|
76
|
+
CDMon.log_all("Cannot send email")
|
77
|
+
false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
def mail_headers
|
83
|
+
body = "From: #{@from} <#{@from}>\n"
|
84
|
+
body << "To: #{@to}<#{@to}>\n"
|
85
|
+
body << "Subject: #{@subject}\n"
|
86
|
+
body << "Date: #{Time.now}\n"
|
87
|
+
body << "Importance:high\n"
|
88
|
+
body << "MIME-Version:1.0\n"
|
89
|
+
body << "\n\n\n"
|
90
|
+
body
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/ip_updater.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'resolv'
|
5
|
+
require_relative 'cdmon_updater/config'
|
6
|
+
require_relative 'cdmon_updater'
|
7
|
+
|
8
|
+
module CDMonUpdater
|
9
|
+
class IPUpdater
|
10
|
+
def initialize(config_file = "../config/cdmon.yml")
|
11
|
+
Config.load(config_file)
|
12
|
+
@resolver = Resolv::DNS.new(:nameserver => Config.dns, :search => ["localhost"], :dots => 1)
|
13
|
+
CDMonUpdater.log_level = Config.log_level
|
14
|
+
end
|
15
|
+
|
16
|
+
def update
|
17
|
+
url = URI.parse(Config.service_url)
|
18
|
+
http = Net::HTTP.new(url.host, url.port)
|
19
|
+
http.use_ssl = (url.scheme == 'https')
|
20
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
21
|
+
|
22
|
+
#require 'debug'
|
23
|
+
begin
|
24
|
+
Config.users.keys.each do |usr|
|
25
|
+
request = Net::HTTP::Get.new(Config.get_cdmon_ip_path_for(usr))
|
26
|
+
Config.hosts_for(usr).each do |host|
|
27
|
+
host_ip_by_cdmon = @resolver.getaddress(host).to_s
|
28
|
+
values = parse_response(http.request(request).body)
|
29
|
+
current_cdmon_ip = values["newip"] if values.has_key?("newip")
|
30
|
+
unless host_ip_by_cdmon == current_cdmon_ip
|
31
|
+
request = Net::HTTP::Get.new(Config.cdmon_ip_path_for(usr, current_cdmon_ip))
|
32
|
+
response = parse_response(http.request(request).body)
|
33
|
+
case response["resultat"]
|
34
|
+
when Config::CDMON_OK_IP
|
35
|
+
CDMonUpdater.log_all("IP Succesfully updated")
|
36
|
+
when Config::CDMON_BAD_IP
|
37
|
+
CDMonUpdater.log_all("Bad IP Provided")
|
38
|
+
when Config::CDMON_ERROR_LOGIN
|
39
|
+
CDMonUpdater.log_all("Login Error")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
rescue SocketError
|
45
|
+
CDMonUpdater.log_all("SocketError: Probably the internet connection is broken")
|
46
|
+
rescue Resolv::ResolvError
|
47
|
+
CDMonUpdater.log_all("ResolvError: Cannot get DNS results")
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def parse_response(response)
|
54
|
+
values = response.split("&")
|
55
|
+
pairs = {}
|
56
|
+
values.each do |val|
|
57
|
+
key_val = val.split("=")
|
58
|
+
pairs[key_val[0]] = key_val[1]
|
59
|
+
end
|
60
|
+
pairs
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ip_updater'
|
3
|
+
|
4
|
+
describe CDMonUpdater::IPUpdater do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
@updater = CDMonUpdater::IPUpdater.new(File.dirname(__FILE__) + "/../config/cdmon.yml")
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should update the IP" do
|
12
|
+
"you should have your settings on /etc/cdmon.yml".should_not be_nil
|
13
|
+
#@updater.update
|
14
|
+
end
|
15
|
+
end
|
data/spec/mailer_spec.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/cdmon_updater/mailer'
|
3
|
+
|
4
|
+
describe CDMonUpdater::Mailer do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
server_options = {"server" => {"address" => "some.smtp.server", "port" => 25, "user_name" => "user", "password" => "smtp_password"}}
|
8
|
+
|
9
|
+
@mailer = CDMonUpdater::Mailer.new(server_options)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have class options set" do
|
13
|
+
CDMonUpdater::Mailer.address.should == "some.smtp.server"
|
14
|
+
CDMonUpdater::Mailer.port.should == 25
|
15
|
+
CDMonUpdater::Mailer.credentials[:user_name].should == "user"
|
16
|
+
CDMonUpdater::Mailer.credentials[:password].should == "smtp_password"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have instance attributes" do
|
20
|
+
@mailer.subject = "Test Mail"
|
21
|
+
@mailer.message = "This is the body of the message"
|
22
|
+
@mailer.from = "someuser@someserver.com"
|
23
|
+
@mailer.to = "someotheruser@someserver.com"
|
24
|
+
@mailer.stub!(:send_mail).and_return(true)
|
25
|
+
@mailer.send_mail.should be_true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should send mail" do
|
29
|
+
"This is a stub left in blank since I can't publish my smtp access data".should_not be_nil
|
30
|
+
"You should fill your own data on ../config/cdmon.yml".should_not be_nil
|
31
|
+
#Config.load("../config/cdmon.yml")
|
32
|
+
|
33
|
+
#@mailer = CDMon::Mailer.new({"server" => Config.mailer})
|
34
|
+
#@mailer.subject = "Test Mail"
|
35
|
+
#@mailer.message = "This is the body of the message"
|
36
|
+
#@mailer.from = "cdmon_updater@kandalf.com.ar"
|
37
|
+
#@mailer.to = "some_real_address@some_server.com"
|
38
|
+
#@mailer.send_mail.should be_true
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
$: << "../lib" << "lib"
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cdmon_updater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Leonardo Mateo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &15282360 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *15282360
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &15281800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *15281800
|
36
|
+
description: Just another client for CDMon (http://www.cdmon.com) dynamic DNS.
|
37
|
+
email:
|
38
|
+
- leonardomateo@gmail.com
|
39
|
+
executables:
|
40
|
+
- cdmon_config
|
41
|
+
- cdmon_updater
|
42
|
+
extensions: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- .rvmrc
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- bin/cdmon_config
|
53
|
+
- bin/cdmon_updater
|
54
|
+
- cdmon_updater.gemspec
|
55
|
+
- config/cdmon.yml
|
56
|
+
- config/schedule.rb
|
57
|
+
- lib/cdmon.rb
|
58
|
+
- lib/cdmon_updater.rb
|
59
|
+
- lib/cdmon_updater/config.rb
|
60
|
+
- lib/cdmon_updater/mailer.rb
|
61
|
+
- lib/cdmon_updater/version.rb
|
62
|
+
- lib/ip_updater.rb
|
63
|
+
- spec/ip_updater_spec.rb
|
64
|
+
- spec/mailer_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.17
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: CDMon dynamic DNS client
|
90
|
+
test_files:
|
91
|
+
- spec/ip_updater_spec.rb
|
92
|
+
- spec/mailer_spec.rb
|
93
|
+
- spec/spec_helper.rb
|