alfred-gmail icon indicating copy to clipboard operation
alfred-gmail copied to clipboard

Detect and use system proxy

Open sepehr opened this issue 8 years ago • 14 comments

Currently, the workflow's python script does not respect the globally set proxy:

20:25:11 workflow.py:2133 DEBUG    Last run version : 1.2.0
20:25:11 workflow.py:2181 DEBUG    update_data : None
20:25:11 workflow.py:2350 DEBUG    Got password : com.fniephaus.gmail:gmail_credentials
Traceback (most recent call last):
  File "gmail.py", line 271, in <module>
    main()
  File "gmail.py", line 133, in main
    refresh_cache([label])
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/gmail_refresh.py", line 122, in refresh_cache
    gmail_service = build('gmail', 'v1', http=http)
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/apiclient/discovery.py", line 198, in build
    raise HttpError(resp, content, uri=requested_url)
apiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest returned "This service is not available from Iran. If you believe the country of origin was incorrectly identified, please report it at https://support.google.com/websearch/contact/ip.">

That's unfortunate to say that Google blocks access to its Gmail API from Iranian IPs. So, basically, everyone needs to use some kinda VPN or proxy to access it here.

That'd be great (and much appreciated) the workflow could detect and use the system's set proxy.

system socks proxy

sepehr avatar Apr 18 '17 15:04 sepehr

Hi @sepehr, Thanks for reporting this. I was assuming that Python would already automatically respect your proxy settings. I will look into a solution for this...

fniephaus avatar Apr 18 '17 20:04 fniephaus

Hi @sepehr, So it looks like that the Google Python client that I bundled with the workflow didn't support SOCKS proxies. Could you please download and try out the following updated version of the workflow: Gmail-Client-for-Alfred.alfredworkflow.zip. Let me know if that works for you, otherwise I need to dig deeper.

fniephaus avatar Apr 18 '17 20:04 fniephaus

Hey @fniephaus,

Thank you so much for looking into this 🙏

I just installed it, now it does not even list the primary actions. Here's the debug log:

[2017-04-19 17:07:35][ERROR: input.scriptfilter] Code 1: Traceback (most recent call last):
  File "gmail.py", line 2, in <module>
    from gmail_refresh import refresh_cache
  File "/Users/Sepehr/Dropbox/dev/dotfiles/conf/app/alfred/Alfred.alfredpreferences/workflows/user.workflow.A06A4C55-CF30-4DAA-9ED8-A6C15CA10B7F/gmail_refresh.py", line 10, in <module>
    from oauth2client.tools import run
ImportError: cannot import name run

I fixed the exception in the gmail_refresh.py:

from oauth2client.tools import run_flow as run

Now, it shows me a "No mails found!" message while there is:

screen shot 2017-04-19 at 5 11 57 pm

Here's the debug log with the updated python file:

[2017-04-19 17:12:39][STDERR: input.scriptfilter] 17:12:38 workflow.py:1628 DEBUG    Reading settings from `/Users/Sepehr/Library/Application Support/Alfred 3/Workflow Data/com.fniephaus.gmail/settings.json` ...
17:12:38 workflow.py:2394 DEBUG    Last run version : 1.2.0
17:12:38 workflow.py:2394 DEBUG    Last run version : 1.2.0
17:12:38 workflow.py:2444 DEBUG    update_data : None
17:12:38 workflow.py:1859 DEBUG    Loading cached data from : /Users/Sepehr/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/com.fniephaus.gmail/gmail_inbox.cpickle
[2017-04-19 17:12:39][input.scriptfilter] <?xml version="1.0" encoding="utf-8"?>
<items><item autocomplete="" valid="no"><title>No mails found!</title><subtitle /><icon>icons/alert-dark.png</icon></item><item autocomplete="" valid="no"><title>...</title><subtitle /><icon>icons/mail-reply-dark.png</icon></item></items>

sepehr avatar Apr 19 '17 12:04 sepehr

Whoops, thanks @sepehr. I still had the old .pyc on my filesystem for some reason, but I'll update gmail_refresh.py with your fix the next time I push to the repo. Well, we are not sure if the proxy issue really is gone until you see your emails. Do you have Python scripts that work with your SOCKS proxy settings? httplib2, which Google's Python API client uses, also comes with SocksiPy, a Python SOCKS module. So I hope this respects your SOCKS settings. Let me know if you find out more...

fniephaus avatar Apr 19 '17 13:04 fniephaus

Here's the situation with the new workflow:

  • I get the same 403 without the socks proxy
  • I get a "No mails found!" message when using the socks proxy
  • I get the same "No mails found!" message even though I use global openvpn connections (this was working before)

So, I'm sure there's a problem with the new workflow. Is it working for you?

P. S. No, unfortunately, I don't have any existing scripts to test this out. But I look into it.

sepehr avatar Apr 19 '17 13:04 sepehr

It is working for me. Maybe reauthenticate the workflow and try again. You can start from scratch after typing in gmail wf:deauth which deletes your access token.

fniephaus avatar Apr 19 '17 13:04 fniephaus

Re-authed the application, still the "No mails found!" message... everywhere.

No mails found! everywhere

sepehr avatar Apr 19 '17 14:04 sepehr

Maybe have a look at the gmail_refresh.py and see how the client works. Then, try to build a simple script that for example fetches entries from your inbox and prints them to stdout. Let me know if you need help. I have the slight feeling that there is something wrong with your setup or the Python client...

fniephaus avatar Apr 19 '17 14:04 fniephaus

Thank you @fniephaus. Will do.

Would you please gimme a diff of what you've done with the python script to support the socks proxy?

sepehr avatar Apr 19 '17 14:04 sepehr

No worries, @sepehr. I haven't really done anything but update all dependencies including httplib2 and the Google Python client. Maybe start with the client first and then try to get one of the examples working.

fniephaus avatar Apr 19 '17 14:04 fniephaus

So, basically its the current HEAD? right?

sepehr avatar Apr 19 '17 15:04 sepehr

Yes, that's correct.

fniephaus avatar Apr 19 '17 15:04 fniephaus

I'm pretty sure that the recent commits have introduced a new bug. I ran through the history with git bisect and found out that since 5b9e3ba, the workflow is returning that "No mails found!" message no matter what. The latest working snapshot for me is 344b42c.

And I think I found the issue. I'll open a pull request to address it. Please see #19:

With this fix, now I've returned to that "This service is not available from Iran" error when using the socks proxy. I'll look into it as soon as I can find some time.

sepehr avatar Apr 23 '17 11:04 sepehr

Thanks!

fniephaus avatar Apr 23 '17 11:04 fniephaus