simonrob / email-oauth2-proxy

An IMAP/POP/SMTP proxy that transparently adds OAuth 2.0 authentication for email clients that don't support this method.
Apache License 2.0
825 stars 89 forks source link

Issues with SMTP Connection Proxy #85

Closed computersg closed 1 year ago

computersg commented 1 year ago

I have been able to use your script quite successfully for proxying SMTP transactions through O365 on multiple windows platforms (server 2008R2 - Server 2022 and Windows 10/11). Recently some of the older systems [in this particular case Server 2012 R2] have stopped working. I have checked all of the settings and a fairly certain everything is correct. The issue appears to be that once I generate the initial SMTP authentication request and click on the tray icon to authorize the account (regardless of if I use "External Authorization Mode" or not) nothing happens and if I am running the script in a command prompt I get an error message like this:

2022-10-25 11:25:10: Authorisation request received for **<Email redacted>** (inte
ractive mode)
An error occurred when calling message handler
Traceback (most recent call last):
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\pystray\_win32.py", line 401, in _dispatcher
    return int(icon._message_handlers.get(
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\pystray\_win32.py", line 213, in _on_notify
    descriptors[index - 1](self)
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\pystray\_base.py", line 324, in inner
    callback(self)
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\pystray\_base.py", line 449, in __call__
    return self._action(icon, self)
  File "C:\email-oauth2-proxy-main\emailproxy.py", line 1975, in authorise_account
    webview.start(gui=forced_gui, debug=Log.get_level() == logging.DEBUG)
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\__init__.py", line 122, in start
    guilib = initialize(gui)
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\guilib.py", line 101, in initialize
    if not try_import(guis):
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\guilib.py", line 62, in try_import
    if import_func():
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\guilib.py", line 52, in import_winforms
    import webview.platforms.winforms as guilib
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\platforms\winforms.py", line 31, in <module>
    import clr
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\clr.py", line 6, in <module>
    load()
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\pythonnet\__init__.py", line 43, in load
    if func(''.encode("utf8")) != 0:
  File "C:\Users\administrator.HOFFMANTOWN\AppData\Local\Programs\Python\Python38\lib\site-packages\clr_loader\wrappers.py", line 20, in __call__
    return self._callable(ffi.cast("void*", buf_arr), len(buf_arr))
RuntimeError: cannot call null pointer pointer from cdata 'int(*)(void *, int)'

Please let me know what I can do to fix the issue.

So far I am not having this issue with Server 2019 or Windows 10/11 and for clarity, to the best of my knowledge this was working on the Server 2012 R2 box for a few weeks before this issue. I am primarily using it to add OAuth to SMTP email alerts generated from various programs when sending through O365.

Thank you.

simonrob commented 1 year ago

I'd be surprised if this had happened without any changes to proxy or operating environment. Have you recently updated python or any packages on these platforms?

The issue is probably related to pywebview and pythonnet rather than the proxy. Please could you try pywebview's getting started example to see whether you are able to get a browser window to display?

In the meantime you could use the --local-server-auth proxy option to get you past the authentication request.

computersg commented 1 year ago

IN answer to your question: Have you recently updated python or any packages on these platforms?

I have not performed any python updates nor installed any packages prior to this issue occurring. To the best of my knowledge your script it the only reason python is installed on this server.

In the process or attempting to fix the issue on this particular server I tried:

Uninstalling Python Re-installing Python (In this case version 3.8.10) -> As a side note I also tried the same steps below with Python version 3.11.0 Removing the packages using: Python.exe -m pip uninstall -r C:\email-oauth2-proxy-main\requirements.txt Installing the packages again via: Python.exe -m pip install -r C:\email-oauth2-proxy-main\requirements.txt Deleting and recreating the emailproxy.config file with the appropriate info Downloaded the latest version of the email-oauth2-proxy-main.zip and set everything up from scratch Tried setting up everything from scratch again with an older download of your email-oauth2-proxy-main.zip from about 1 month ago that had worked just in case anything changed in the GitHub repository

After each step I would run the script: python.exe C:\email-oauth2-proxy-main\emailproxy.py in a CMD prompt window and test by:

Using PowerShell to create a SMTP request using the "Send-MailMessage" command with the appropriate server addresses and credentials for testing purposes. The pop-up in the system tray indicating an account needing verified would appear and if I either attempted to verify the account directly or selected external verification and attempting to verify both would cause the the error messages in the previous post to appear in the CMD prompt window and the system would not complete the authorization eventually timing out on the SMTP request.

I will also note that at one time in the past I had similar issues on other Windows systems (at that time I thought it was a W10 update causing the issue) and was able to get past them by installing these two python packages: Python.exe -m pip install clr-loader==0.1.7 Python.exe -m pip install pythonnet==3.0.0a2

This did not resolve the issue for this system and I removed these packages after trying them as well as verified they were removed when I re-installed both the 3.8.10 & 3.11.0 versions if python.

simonrob commented 1 year ago

Please could you try pywebview's getting started example to see whether you are able to get a browser window to display?

Please try this. If pywebview itself is not installed properly for whatever reason then the proxy will not be able to display the authentication popup.

computersg commented 1 year ago

I am testing that now on the affected server and should be able to let you know shortly

computersg commented 1 year ago

I checked to make sure pywebview was installed on the system as instructed: python.exe -m pip install pywebview

It came back with the result: Requirement already satisfied: pywebview in c:\users**user redacted\appdata\local\programs\python\python311\lib\site-packages (3.6.3) Requirement already satisfied: proxy-tools in c:\users\user redacted\appdata\local\programs\python\python311\lib\site-packages (from pywebview) (0.1.0) Requirement already satisfied: pythonnet in c:\users\user redacted\appdata\local\programs\python\python311\lib\site-packages (from pywebview) (3.0.0a2) Requirement already satisfied: clr-loader in c:\users\user redacted\appdata\local\programs\python\python311\lib\site-packages (from pythonnet->pywebview) (0.1.7) Requirement already satisfied: cffi>=1.13 in c:\users\user redacted\appdata\local\programs\python\python311\lib\site-packages (from clr-loader->pythonnet->pywebview) (1.15.1) Requirement already satisfied: pycparser in c:\users\user redacted**\appdata\local\programs\python\python311\lib\site-packages (from cffi>=1.13->clr-loader->pythonnet->pywebview) (2.21)

Then I created a file called welcome.py and place the coommands in it: import webview webview.create_window('Hello world', 'https://pywebview.flowrl.com/') webview.start()

I then attempted to run the script: python.exe welcome.py

And received the following error messages in return with no pywebview windows appearing: Traceback (most recent call last): File "C:\Users**user redacted\AppData\Local\Programs\Python\Python311\welcome.py", line 3, in webview.start() File "C:\Users\user redacted\AppData\Local\Programs\Python\Python311\Lib\site-packages\webview__init__.py", line 122, in start guilib = initialize(gui) ^^^^^^^^^^^^^^^ File "C:\Users\user redacted\AppData\Local\Programs\Python\Python311\Lib\site-packages\webview\guilib.py", line 101, in initialize if not try_import(guis): ^^^^^^^^^^^^^^^^ File "C:\Users\user redacted\AppData\Local\Programs\Python\Python311\Lib\site-packages\webview\guilib.py", line 62, in try_import if import_func(): ^^^^^^^^^^^^^ File "C:\Users\user redacted\AppData\Local\Programs\Python\Python311\Lib\site-packages\webview\guilib.py", line 52, in import_winforms import webview.platforms.winforms as guilib File "C:\Users\user redacted\AppData\Local\Programs\Python\Python311\Lib\site-packages\webview\platforms\winforms.py", line 31, in import clr File "C:\Users\user redacted\AppData\Local\Programs\Python\Python311\Lib\site-packages\clr.py", line 6, in load() File "C:\Users\user redacted\AppData\Local\Programs\Python\Python311\Lib\site-packages\pythonnet__init__.py", line 43, in load if func(''.encode("utf8")) != 0: ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user redacted*\AppData\Local\Programs\Python\Python311\Lib\site-packages\clr_loader\wrappers.py", line 20, in call return self._callable(ffi.cast("void", buf_arr), len(buf_arr)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: cannot call null pointer pointer from cdata 'int()(void , int)'

For the sake of completeness I am testing the pywebview on a W11 PC to see if I can make it work just in case I am executing the process incorrectly. I will report back what occurs shortly

computersg commented 1 year ago

OK, on the W11 machine I ran into a bit of a complex issue. If you install Python 3.11 and try to install pywebview it errors out on the pyhtonnet installer and references this article: https://github.com/pypa/pip/issues/8559 thath suggests work-arounds that have since been deprecated. Long story short, you can't use python 3.11 but have to install 3.8. Once 3.8 is install you can install you can install pywebview. However, when I created the .py scripts using code from [https://pywebview.flowrl.com/ & https://github.com/r0x0r/pywebview/blob/docs/examples/todos/start.py <- as an alternate test] and ran them I got the following error:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Forms.Control.get_CacheTextInternal() at System.Windows.Forms.Control.get_Text() at System.Windows.Forms.Control.set_Text(String value) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Python.Runtime.PythonException.ThrowLastAsClrException() at Python.Runtime.Dispatcher.TrueDispatch(Object[] args) at Python.Runtime.Dispatcher.Dispatch(Object[] args) at __System_Threading_ThreadStartDispatcher.Invoke() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

I looked that up and found an article indicating that I should install an older version of pyhtonnet [https://stackoverflow.com/questions/70640459/python-pywebview-webview-start-system-nullreferenceexception-object-reference], this is referencing what I mentioned earlier while installing the proxy on W10 PC's. I executed that but ended up getting similar errors to the ones on the Server 2012 R2 system when attempting to call the pyweb:

C:\Users**user redacted\AppData\Local\Programs\Python\Python38>python.exe welcome.py Traceback (most recent call last): File "welcome.py", line 3, in webview.start() File "C:\Users\user redacted\AppData\Local\Programs\Python\Python38\lib\site-packages\webview__init__.py", line 122, in start guilib = initialize(gui) File "C:\Users\user redacted\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\guilib.py", line 101, in initialize if not try_import(guis): File "C:\Users\user redacted\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\guilib.py", line 62, in try_import if import_func(): File "C:\Users\user redacted\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\guilib.py", line 52, in import_winforms import webview.platforms.winforms as guilib File "C:\Users\user redacted\AppData\Local\Programs\Python\Python38\lib\site-packages\webview\platforms\winforms.py", line 31, in import clr File "C:\Users\user redacted\AppData\Local\Programs\Python\Python38\lib\site-packages\clr.py", line 6, in load() File "C:\Users\user redacted**\AppData\Local\Programs\Python\Python38\lib\site-packages\pythonnet__init__.py", line 42, in load func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"] TypeError: 'Assembly' object is not subscriptable

Sorry to make this difficult I am more of a sysadmin vs a programmer so hopefully I am not missing something obvious

simonrob commented 1 year ago

I'm afraid this is not something I can resolve given that pywebview is not working. It might be worth opening an issue on that project's repository.

computersg commented 1 year ago

Quick update, I searched the "'Assembly' object is not subscriptable" error and that lead me back to another article that indicated I needed to install clr-loader==0.1.7 as well [https://stackoverflow.com/questions/74030626/python-pywebview-trouble-with-pythonnet-unbehandelte-ausnahme-system-nullref]. I did that and now I am able to call the pywebview control on the W11 PC: image

So I believe that my procedure is correct. I will double check that the: pip install pythonnet==3.0.0a2 pip install clr-loader==0.1.7

Commands have been run on the affected Server 2012 PC and let you know the result.

computersg commented 1 year ago

Unfortunately, I confirmed that those remediations have been done on the Server 2012 R2 PC. So, no joy. Hopefully it helps someone if they are having the issue on W10/W11. I will take your advice and open an issue with pywebview