Class: String

Inherits:
Object show all
Defined in:
lib/net/extended_classes.rb

Instance Method Summary collapse

Instance Method Details

#blacklisted?(dx) ⇒ Boolean

returns true if the IP is blacklisted; otherwise false examples: barracuda = ‘b.barracudacentral.org’.blacklisted?(ip) spamhaus = ‘zen.spamhaus.org’.blacklisted?(ip)

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/net/extended_classes.rb', line 117

def blacklisted?(dx)
  domain = dx.split('.').reverse.join('.')+"."+self
  a = []
  Resolv::DNS.open do |dns|
    begin
      a = dns.getresources(domain, Resolv::DNS::Resource::IN::A)
    rescue Resolv::NXDomainError
      a=[]
    end
  end
  if a.size>0 then true else false end
end

#dig_aObject

returns list of IPV4 addresses, or nil (there should only be one IPV4 address)



70
71
72
73
74
75
# File 'lib/net/extended_classes.rb', line 70

def dig_a
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::A).collect { |r| r.address.to_s }
    if txts.empty? then nil else txts[0] end
  end
end

#dig_aaaaObject

returns list of IPV6 addresses, or nil (there should only be one IPV6 address)



79
80
81
82
83
84
# File 'lib/net/extended_classes.rb', line 79

def dig_aaaa
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::AAAA).collect { |r| r.address.to_s.downcase }
    if txts.empty? then nil else txts[0] end
  end
end

#dig_dkObject

returns a publibdomainkey, or nil (there should only be one DKIM public key)



97
98
99
100
101
102
# File 'lib/net/extended_classes.rb', line 97

def dig_dk
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::TXT).collect { |r| r.strings }
    if txts.empty? then nil else txts[0][0] end
  end
end

#dig_mxObject

returns list of MX names, or nil (there may be multiple MX names for a domain)



88
89
90
91
92
93
# File 'lib/net/extended_classes.rb', line 88

def dig_mx
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::MX).collect { |r| r.exchange.to_s }
    if txts.empty? then nil else txts end
  end
end

#dig_ptrObject

returns a reverse DNS hostname or nil



105
106
107
108
109
110
111
# File 'lib/net/extended_classes.rb', line 105

def dig_ptr
  begin
    Resolv.new.getname(self.downcase)
  rescue Resolv::ResolvError
    nil
  end
end

#mta_live?(port) ⇒ Boolean

opens a socket to the IP/port to see if there is an SMTP server there - returns “250 …” if the server is there, or times out in 5 seconds to prevent hanging the process

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/net/extended_classes.rb', line 140

def mta_live?(port)
  tcp_socket = nil
  welcome = nil
  begin
    Timeout.timeout(LiveServerTestTimeout) do
      begin
        tcp_socket = TCPSocket.open(self,port)
      rescue Errno::ECONNREFUSED => e
        return "421 Service not available (port closed)"
      end
      begin
        welcome = tcp_socket.gets
        return welcome if welcome[1]!='2'
        tcp_socket.write("QUIT\r\n")
        line = tcp_socket.gets
        return line if line[1]!='2'
      ensure
        tcp_socket.close if tcp_socket
      end
    end
    return "250 #{welcome.chomp[4..-1]}"
  rescue SocketError => e
    return "421 Service not available (#{e.to_s})"
  rescue Timeout::Error => e
    return "421 Service not available (#{e.to_s})"
  end
end

#utf8Object

returns a UTF-8 encoded string – be carefule using this with email: email has to be received and transported with NO changes, except the addition of extra headers at the beginning (before any DKIM headers)



133
134
135
# File 'lib/net/extended_classes.rb', line 133

def utf8
  self.encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '?')
end

#validate_plainObject

this validates a password with the base64 plaintext in an AUTH command encoded -> AGNvY29AY3phcm1haWwuY29tAG15LXBhc3N3b3Jk => [“[email protected]”, “my-password”] “my-password” –> CRYPTIwYH/ZXeR8vUM “AGNvY29AY3phcm1haWwuY29tAG15LXBhc3N3b3Jk”.validate_plain { “CRYPTIwYH/ZXeR8vUM” } => “[email protected]”, true “AGNvY29AY3phcm1haWwuY29tAHh4LXBhc3N3b3Jk”.validate_plain { “CRYPTIwYH/ZXeR8vUM” } => “[email protected]”, false



173
174
175
176
177
178
179
180
181
# File 'lib/net/extended_classes.rb', line 173

def validate_plain
  # decode and split up the username and password)
  username, password = Base64::decode64(self).split("\x00")[1..-1]
  return "", false if username.nil? || password.nil?
  passwd_hash = yield(username) # get the hash
  return "", false if passwd_hash.nil?
  m = passwd_hash.match(/^{(.*)}(.*)$/)
  return username, UnixCrypt.valid?(password, m[2])
end