-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mailbox.MailBox splitted to: BaseMailBox, MailBox, MailBoxUnencrypted
MailBox ssl argument deleted mailbox.MessageFlags class moved to utils.MessageFlags Add PySocks proxy examples
- Loading branch information
Showing
6 changed files
with
253 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
""" | ||
MailBox traffic through proxy servers using https://github.com/Anorov/PySocks | ||
NOTE: examples NOT checked! | ||
""" | ||
import ssl | ||
import socks | ||
from imaplib import IMAP4 | ||
|
||
|
||
class Imap4Proxy(IMAP4): | ||
def __init__(self, | ||
host: str = "", | ||
port: int = 143, | ||
p_timeout: int = None, | ||
p_source_address: tuple = None, | ||
p_proxy_type: socks.PROXY_TYPES = "HTTP", | ||
p_proxy_addr: str = None, | ||
p_proxy_port: int = None, | ||
p_proxy_rdns=True, | ||
p_proxy_username: str = None, | ||
p_proxy_password: str = None, | ||
p_socket_options: iter = None, | ||
): | ||
self._host = host | ||
self._port = port | ||
self._p_timeout = p_timeout | ||
self._p_source_address = p_source_address | ||
self._p_proxy_type = p_proxy_type | ||
self._p_proxy_addr = p_proxy_addr | ||
self._p_proxy_port = p_proxy_port | ||
self._p_proxy_rdns = p_proxy_rdns | ||
self._p_proxy_username = p_proxy_username | ||
self._p_proxy_password = p_proxy_password | ||
self._p_socket_options = p_socket_options | ||
super().__init__(host, port) | ||
|
||
def _create_socket(self): | ||
return socks.create_connection( | ||
dest_pair=(self._host, self._port), | ||
timeout=self._p_timeout, | ||
source_address=self._p_source_address, | ||
proxy_type=self._p_proxy_type, | ||
proxy_addr=self._p_proxy_addr, | ||
proxy_port=self._p_proxy_port, | ||
proxy_rdns=self._p_proxy_rdns, | ||
proxy_username=self._p_proxy_username, | ||
proxy_password=self._p_proxy_password, | ||
socket_options=self._p_socket_options, | ||
) | ||
|
||
|
||
class Imap4SslProxy(Imap4Proxy): | ||
def __init__(self, | ||
host: str = "", | ||
port: int = 993, | ||
keyfile=None, | ||
certfile=None, | ||
ssl_context=None, | ||
p_timeout: int = None, | ||
p_source_address: tuple = None, | ||
p_proxy_type: socks.PROXY_TYPES = "HTTP", | ||
p_proxy_addr: str = None, | ||
p_proxy_port: int = None, | ||
p_proxy_rdns=True, | ||
p_proxy_username: str = None, | ||
p_proxy_password: str = None, | ||
p_socket_options: iter = None, | ||
): | ||
self._host = host | ||
self._port = port | ||
self._p_timeout = p_timeout | ||
self._p_source_address = p_source_address | ||
self._p_proxy_type = p_proxy_type | ||
self._p_proxy_addr = p_proxy_addr | ||
self._p_proxy_port = p_proxy_port | ||
self._p_proxy_rdns = p_proxy_rdns | ||
self._p_proxy_username = p_proxy_username | ||
self._p_proxy_password = p_proxy_password | ||
self._p_socket_options = p_socket_options | ||
|
||
if ssl_context is not None and keyfile is not None: | ||
raise ValueError("ssl_context and keyfile arguments are mutually exclusive") | ||
if ssl_context is not None and certfile is not None: | ||
raise ValueError("ssl_context and certfile arguments are mutually exclusive") | ||
if keyfile is not None or certfile is not None: | ||
import warnings | ||
warnings.warn("keyfile and certfile are deprecated, use ssl_context instead", DeprecationWarning, 2) | ||
|
||
if ssl_context is None: | ||
ssl_context = ssl._create_stdlib_context(certfile=certfile, keyfile=keyfile) # noqa | ||
|
||
self.keyfile = keyfile | ||
self.certfile = certfile | ||
self.ssl_context = ssl_context | ||
|
||
super().__init__(host, port, p_timeout, p_source_address, p_proxy_type, p_proxy_addr, p_proxy_port, | ||
p_proxy_rdns, p_proxy_username, p_proxy_password, p_socket_options) | ||
|
||
def _create_socket(self): | ||
sock = super()._create_socket() | ||
server_hostname = self.host if ssl.HAS_SNI else None | ||
return self.ssl_context.wrap_socket(sock, server_hostname=server_hostname) | ||
|
||
def open(self, host='', port=993): | ||
super().open(host, port) | ||
|
||
|
||
class MailBoxUnencryptedProxy: | ||
"""Working with the email box through IMAP4 through proxy""" | ||
|
||
def __init__(self, | ||
host: str = "", | ||
port: int = 143, | ||
p_timeout: int = None, | ||
p_source_address: tuple = None, | ||
p_proxy_type: socks.PROXY_TYPES = "HTTP", | ||
p_proxy_addr: str = None, | ||
p_proxy_port: int = None, | ||
p_proxy_rdns=True, | ||
p_proxy_username: str = None, | ||
p_proxy_password: str = None, | ||
p_socket_options: iter = None, | ||
): | ||
self._host = host | ||
self._port = port | ||
self._p_timeout = p_timeout | ||
self._p_source_address = p_source_address | ||
self._p_proxy_type = p_proxy_type | ||
self._p_proxy_addr = p_proxy_addr | ||
self._p_proxy_port = p_proxy_port | ||
self._p_proxy_rdns = p_proxy_rdns | ||
self._p_proxy_username = p_proxy_username | ||
self._p_proxy_password = p_proxy_password | ||
self._p_socket_options = p_socket_options | ||
super().__init__() | ||
|
||
def _get_mailbox_client(self): | ||
return Imap4Proxy( | ||
self._host, self._port, | ||
self._p_timeout, self._p_source_address, self._p_proxy_type, self._p_proxy_addr, self._p_proxy_port, | ||
self._p_proxy_rdns, self._p_proxy_username, self._p_proxy_password, self._p_socket_options) | ||
|
||
|
||
class MailBoxProxy: | ||
"""Working with the email box through IMAP4 over SSL connection through proxy""" | ||
|
||
def __init__(self, | ||
host: str = "", | ||
port: int = 993, | ||
keyfile=None, | ||
certfile=None, | ||
ssl_context=None, | ||
p_timeout: int = None, | ||
p_source_address: tuple = None, | ||
p_proxy_type: socks.PROXY_TYPES = "HTTP", | ||
p_proxy_addr: str = None, | ||
p_proxy_port: int = None, | ||
p_proxy_rdns=True, | ||
p_proxy_username: str = None, | ||
p_proxy_password: str = None, | ||
p_socket_options: iter = None, | ||
): | ||
self._host = host | ||
self._port = port | ||
self._keyfile = keyfile | ||
self._certfile = certfile | ||
self._ssl_context = ssl_context | ||
self._p_timeout = p_timeout | ||
self._p_source_address = p_source_address | ||
self._p_proxy_type = p_proxy_type | ||
self._p_proxy_addr = p_proxy_addr | ||
self._p_proxy_port = p_proxy_port | ||
self._p_proxy_rdns = p_proxy_rdns | ||
self._p_proxy_username = p_proxy_username | ||
self._p_proxy_password = p_proxy_password | ||
self._p_socket_options = p_socket_options | ||
super().__init__() | ||
|
||
def _get_mailbox_client(self): | ||
return Imap4SslProxy( | ||
self._host, self._port, self._keyfile, self._certfile, self._ssl_context, | ||
self._p_timeout, self._p_source_address, self._p_proxy_type, self._p_proxy_addr, self._p_proxy_port, | ||
self._p_proxy_rdns, self._p_proxy_username, self._p_proxy_password, self._p_socket_options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
from .folder import * | ||
from .utils import * | ||
|
||
__version__ = '0.14.3' | ||
__version__ = '0.15.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
try to imitate long poll by NOOP | ||
|
||
check https://docs.python.org/release/3.8.1/library/email.utils.html | ||
|
||
check possibility to implement SOCKS without/with dependencies |