closing a socks/proxy connection in python - python

I am writing some code that uses poplib and imaplib to collect emails through a proxy server.
I use the following to set up a proxy connection:-
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,proxy_ip,port,True)
socket.socket = socks.socksocket
Which I got from the stackoverflow post:-
http://stackoverflow.com/questions/3386724/python-how-can-i-fetch-emails-via-pop-or-imap-through-a-proxy
Then I make my connection with the email server:-
server = poplib.POP3(self.host, self.port)
server.user(self.username)
server.pass_(self.password)
I am testing my code in a unittest and have encountered a problem that I believe relates to my connection with the proxy not closing down properly.
An example is:-
I have set up the proxy connection and am trying to establish a connection with the email server. As part of the unittest I intentionally use an incorrect email server password.
The poplib library throws an exception that it can't connect. I catch the exception in the unittest, then move on to the next unittest, trusting the poplib library would properly close my previous connection.
My understanding is that this is not a good thing and that I should be ensuring the email and proxy server connections are properly closed.
I know how to close the pop3 connection:-
server.quit()
But do not know how to close the connection with the proxy server or if I have to do so.
Could someone please help me with this question or with my understanding if that's where the problem lies :)

No special action is required. When you close the POP connection, the proxy connection will close automatically, since it's only needed while you are connected to something through the proxy.

Related

Unable to run a simple SMTP server

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
import smtplib
senders='jsujith1311#gmail.com'
password="ekjhgisxavzw"#Not Original Password
connection=smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user=senders,password=password)
connection.sendmail(from_addr=senders,to_addrs=senders,msg="HI")
connection.close()
Can anyone help with this
I need a simple smtp server running on my device initially, then i want to configutre it according to my needs.
You need to change your password to an app password. Follow the below link for that: https://support.google.com/mail/answer/185833?hl=en
So, the answer is to use
connection = smtplib.SMTP_SSL("smtp.gmail.com")
and we have to remove the tls connection,I don't know why.But it worked.

Python 3.8 TLSv1.3 socket close result in ConnectionResetError or ConnectionAbortedError

Client side:
data = b'\xff' * 1000000
ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
#context is created by ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ssock = context.wrap_socket(ssock, server_hostname='xd1337sv')
ssock.connect((SERVERADDR, SERVERPORT))
ssock.sendall(data)
#time.sleep(3)
ssock.close()
If I just use regular non-SSL socket, everything works correctly with the server receiving exact amount of data. If I use TLS socket, the behavior then depends on the version.
If I run either the server or client on Python 3.6 and therefore the TLSv1.2 will be used, there's no problem.
Problem arises only when TLSv1.3 is used and depends on the size of data and how soon client ssocket.close() line is executed.
If I put a right amount of time.sleep before ssocket.close() depending on the size of data, then I get no error. Otherwise, the server will get ConnectionResetError [WinError 10054] An existing connection was forcibly closed by the remote host and receive only part of the data, or throw ConnectionAbortedError [WinError 10053] An established connection was aborted by the software in your host machine and receive no data.
I'm testing both the server and client on my local machine with local address 192.168.1.2.
The difference is caused by TLS 1.3 sending a session ticket after the TLS handshake while with previous TLS versions the session ticket is send inside the TLS handshake. Thus, with TLS 1.3 data from the server (the session ticket) will arrive after the ssock.connect(...) is done. Since your application does not read any data after the connect it closes the socket while unread data are still inside the socket buffer of the underlying TCP socket. This will cause RST send to the server and cause there the connection reset error.
This is a known problems with applications which never attempt to read from the server. If the application would expect a response from the server and use recv to get it this would implicitly also read the session ticket.
To fix this situation when you don't expect the server to return any application data do a proper SSL shutdown of the socket before closing it. Since this will read the servers SSL shutdown message it will also implicitly read the session ticket send before by the server.
try:
ssock = ssock.unwrap()
except:
True
ssock.close()
For more information see also this issue and this documentation.
I was getting a similar problem when the application was running through gunicorn with certificates. The jsondecodeerror problem randomly came to the client, i.e. the response was empty. The only thing that TLS 1.2 was used.
The solution was simple, I deployed the application on uwsgi and the problem went away

How detect client "socket.close()" in tornado socket server

I am implementing a tornado socket server based on this code:
https://gist.github.com/robcowie/974695
Client is Python simple socket client. When I call socket.close() in client, nothing happens in server. I put full print traces in the server and closing is not detected nowhere.
I know I can detect the closure for example sending a string "CNNDEND" which means closing. But I wonder if there is any way to detect on server socket.close() from client.
in Connection __init__:
self.stream.set_close_callback(self.__onClose)
in Connection class:
def __onClose(self):
print 'close detected'

Send mail with smtplib using proxy

I have a very basic piece of Python code:
import smtplib
server = smtplib.SMTP(host, port)
problems = server.sendmail(from_addr, to_addr, message)
Is there solution to run it behind an HTTP proxy? I am using Python 3.4.1 on Linux with the http_proxy variable set.
Now I am getting a timeout from SMTP, but if I run this code from a proxy-free network, it works OK.
Is there solution to run it behind an HTTP proxy?
No, HTTP is a different protocol than SMTP and the proxy is for HTTP only. If you are very lucky you might be able to create a tunnel using the CONNECT command to the outside SMTP server, but usually the ports used for CONNECT are restricted so that you will not be able to create a tunnel to an outside host port 25 (i.e. SMTP).

'fail_if_no_peer_cert' not working as expected in rabbitmq

I am trying to validate the SSL connection between client and server.
I have two python scripts send.py for producer and receive.py for consumer.
I am using below code to make connection.
import pika
ssl_option = {'certfile': '/home/rmqca/client1/cert.pem', 'keyfile': '/home/rmqca/client1/key.pem'}
parameters = pika.ConnectionParameters(host='localhost', port=5671, ssl=True, ssl_options=ssl_option)
connection = pika.BlockingConnection(parameters)
ALso, in my rabbitmq.config, I am using the below parameters:
{ssl_listeners, [5671]},
{ssl_options, [{cacertfile, "/home/rmqca/testca/cacert.pem"},
{certfile, "/home/rmqca/server/cert.pem"},
{keyfile, "/home/rmqca/server/key.pem"},
{verify, verify_peer},
{fail_if_no_peer_cert, true}]}
This works fine when I try connecting through SSL.
But As I wanted to cover negative usecase, like if I make connection without ssl, like using code:
import pika
connection = pika.BlockingConnection()
then, as per my understanding, my client should not be able to connect to server. But currently it is connecting fine. I am not sure why this is happening. Am I doing anything wrong here?

Categories