mirror of
https://github.com/YunoHost/check-http.git
synced 2024-09-03 19:56:42 +02:00
[enh] migrate check_smtp to asyncio
This commit is contained in:
parent
b9ce0ab1c4
commit
a3be8ca8fb
1 changed files with 21 additions and 7 deletions
|
@ -343,22 +343,35 @@ async def check_smtp(request):
|
||||||
return check_rate_limit_ip
|
return check_rate_limit_ip
|
||||||
|
|
||||||
if ":" in ip:
|
if ":" in ip:
|
||||||
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
futur = asyncio.open_connection(ip, 25, family=socket.AF_INET6)
|
||||||
else:
|
else:
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
futur = asyncio.open_connection(ip, 25, family=socket.AF_INET)
|
||||||
|
|
||||||
sock.settimeout(2)
|
try:
|
||||||
result = sock.connect_ex((ip, 25))
|
reader, writer = await asyncio.wait_for(futur, timeout=2)
|
||||||
if result != 0:
|
except (asyncio.TimeoutError, ConnectionRefusedError):
|
||||||
|
return json_response({
|
||||||
|
'status': "error_smtp_unreachable",
|
||||||
|
'content': "Could not open a connection on port 25, probably because of a firewall or port forwarding issue"
|
||||||
|
})
|
||||||
|
except Exception:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
return json_response({
|
return json_response({
|
||||||
'status': "error_smtp_unreachable",
|
'status': "error_smtp_unreachable",
|
||||||
'content': "Could not open a connection on port 25, probably because of a firewall or port forwarding issue"
|
'content': "Could not open a connection on port 25, probably because of a firewall or port forwarding issue"
|
||||||
})
|
})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
recv = sock.recv(1024).decode('utf-8')
|
recv = await asyncio.wait_for(reader.read(1024), timeout=200)
|
||||||
|
recv = recv.decode("Utf-8")
|
||||||
assert recv[:3] == "220"
|
assert recv[:3] == "220"
|
||||||
helo_domain = recv.split()[1].strip()
|
helo_domain = recv.split()[1].strip()
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
return json_response({
|
||||||
|
'status': "error_smtp_timeout_answer",
|
||||||
|
'content': "SMTP server took more than 2 seconds to answer."
|
||||||
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
@ -368,7 +381,8 @@ async def check_smtp(request):
|
||||||
'content': "SMTP server did not reply with '220 domain.tld' after opening socket ... Maybe another machine answered."
|
'content': "SMTP server did not reply with '220 domain.tld' after opening socket ... Maybe another machine answered."
|
||||||
})
|
})
|
||||||
finally:
|
finally:
|
||||||
sock.close()
|
writer.close()
|
||||||
|
await writer.wait_closed()
|
||||||
|
|
||||||
return json_response({'status': 'ok', 'helo': helo_domain})
|
return json_response({'status': 'ok', 'helo': helo_domain})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue