Commit 8c8fef11 authored by Romain Courteaud's avatar Romain Courteaud

Check tcp port

parent be9ff690
...@@ -5,6 +5,7 @@ from urlchecker_platform import logPlatform ...@@ -5,6 +5,7 @@ from urlchecker_platform import logPlatform
from urlchecker_status import logStatus from urlchecker_status import logStatus
from urlchecker_dns import getResolverDict, expandDomainList, getServerIpDict from urlchecker_dns import getResolverDict, expandDomainList, getServerIpDict
from urlchecker_http import getUrlHostname from urlchecker_http import getUrlHostname
from urlchecker_network import isTcpPortOpen
__version__ = "0.0.3" __version__ = "0.0.3"
...@@ -46,14 +47,21 @@ class WebBot: ...@@ -46,14 +47,21 @@ class WebBot:
domain_list = expandDomainList(domain_list) domain_list = expandDomainList(domain_list)
print(domain_list) print(domain_list)
# Check if domain have an A record # Get the list of server to check
server_id_dict = getServerIpDict( server_ip_dict = getServerIpDict(
self._db, status_id, resolver_dict, domain_list, "A" self._db, status_id, resolver_dict, domain_list, "A"
) )
print(server_id_dict)
# Check TCP port for the list of IP found # Check TCP port for the list of IP found
# If https ok, check SSL certificate # XXX For now, check http/https only
server_ip_list = [x for x in server_ip_dict.keys()]
for server_ip in server_ip_list:
isTcpPortOpen(self._db, server_ip, 80, status_id)
isTcpPortOpen(self._db, server_ip, 443, status_id)
print(server_ip_dict)
# XXX If https ok, check SSL certificate
# Check HTTP Status # Check HTTP Status
def stop(self): def stop(self):
......
import socket
import errno
TIMEOUT = 2
def logNetwork(db, ip, transport, port, state, status_id): def logNetwork(db, ip, transport, port, state, status_id):
with db._db.atomic(): with db._db.atomic():
...@@ -25,3 +31,28 @@ def logNetwork(db, ip, transport, port, state, status_id): ...@@ -25,3 +31,28 @@ def logNetwork(db, ip, transport, port, state, status_id):
state=state, state=state,
) )
return previous_entry.id return previous_entry.id
def isTcpPortOpen(db, ip, port, status_id):
is_open = False
sock = socket.socket()
sock.settimeout(TIMEOUT)
try:
sock.connect((ip, port))
state = "open"
is_open = True
except ConnectionRefusedError:
state = "closed"
except socket.timeout:
state = "filtered"
except OSError as e:
if e.errno == errno.EHOSTUNREACH:
# OSError: [Errno 113] No route to host
state = "filtered"
else:
raise
sock.close()
logNetwork(db, ip, "TCP", port, state, status_id)
return is_open
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment