Class: DNSUpdater::Updaters::HTTP

Inherits:
Updater
  • Object
show all
Defined in:
lib/dnsupdater/updaters/http.rb

Overview

DNS updater over HTTP

Constant Summary collapse

AUTH_NAME =

Name of authentication - our own custom

'DNSUpdate'
AUTH_VALID_SECONDS =

Time how long authentication key is valid for

60 * 10
DEFAULT_SETTINGS =

Default settings

{ 'Host' => '127.0.0.1', 'Port' => 8245 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Updater

#initialize

Constructor Details

This class inherits a constructor from DNSUpdater::Updaters::Updater

Class Method Details

.buildAuthHMAC(secret, method, path, query = '', authValidTime = nil) ⇒ Object

Build authentication HMAC, taking into parameters and time If authValidTime is passed it will be decreased for use of retry



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dnsupdater/updaters/http.rb', line 77

def self.buildAuthHMAC(secret, method, path, query = '', authValidTime = nil)
    if authValidTime
        authValidTime -= 1
    else
        # Will be valid only for limited time
        authValidTime = Time.now.to_i / AUTH_VALID_SECONDS
    end
    data = buildAuthString(authValidTime, method, path, query)

    [OpenSSL::HMAC.digest('SHA256', secret, data), authValidTime]
end

.buildAuthString(*params) ⇒ Object



89
90
91
# File 'lib/dnsupdater/updaters/http.rb', line 89

def self.buildAuthString(*params)
    params.join(':')
end

.formatResponse(code, message, extraHeaders = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/dnsupdater/updaters/http.rb', line 60

def self.formatResponse(code, message, extraHeaders = {})
    headers = { 'Content-Type' => 'application/json; charset=UTF-8' }
    headers.merge!(extraHeaders)
    data = { success: code == 200, message: message }
    [code, headers, [JSON.generate(data)]]
end

.getHostPort(config) ⇒ Object



68
69
70
71
72
73
# File 'lib/dnsupdater/updaters/http.rb', line 68

def self.getHostPort(config)
    [
        config['HTTP']['Host'],
        config['HTTP']['Port']
    ]
end

.getParams(target) ⇒ Object



53
54
55
56
57
58
# File 'lib/dnsupdater/updaters/http.rb', line 53

def self.getParams(target)
    params = DNSUpdater.buildParams
    DNSUpdater.fillPathParams(target, params)

    params
end

Instance Method Details

#call(env) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dnsupdater/updaters/http.rb', line 38

def call(env)
    @ENV = env

    path = @ENV['PATH_INFO']
    if isAuthenticated(@ENV['HTTP_AUTHORIZATION'], @ENV['REQUEST_METHOD'], path, @ENV['QUERY_STRING'])
        handleRequest(@ENV['REQUEST_METHOD'], path, CGI.parse(@ENV['QUERY_STRING']))
    else
        authName = AUTH_NAME
        authName = 'Basic' if isDynDNS(path, CGI.parse(@ENV['QUERY_STRING']))
        self.class.formatResponse(401, 'Unauthorized!', 'WWW-Authenticate' => authName)
    end
rescue Error => e
    self.class.formatResponse(400, e.message)
end

#update(params) ⇒ Object

See Also:



27
28
29
30
31
32
33
34
35
36
# File 'lib/dnsupdater/updaters/http.rb', line 27

def update(params)
    @ENV = nil
    fillParams(params)

    path = [nil, params[:Domain], params[:IPs].join(',')].join(Addressable::URI::SLASH)
    uri = Addressable::URI.new(scheme: params[:Protocol].to_s, host: params[:Server], port: params[:Port], path: path).normalize
    request = Net::HTTP::Post.new(uri.path)
    addAuthHeader(request, uri.path)
    sendRequest(uri, request)
end