Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic SOCKS proxy support #195

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion build-aux/flatpak/pypi-dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,21 @@
"sha256": "1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c"
}
]
}
},
{
"name": "python3-PySocks",
"buildsystem": "simple",
"build-commands": [
"pip3 install --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"PySocks\""
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/8d/59/b4572118e098ac8e46e399a1dd0f2d85403ce8bbaad9ec79373ed6badaf9/PySocks-1.7.1-py3-none-any.whl",
"sha256": "2725bd0a9925919b9b51739eea5f9e2bae91e83288108a9ad338b2e3a4435ee5"
}
]
}
]
}

38 changes: 35 additions & 3 deletions dialect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# SPDX-License-Identifier: GPL-3.0-or-later

# Initial setup
import socket
import socks
import sys
from gettext import gettext as _
from os import environ

import gi
gi.require_version('Gdk', '3.0')
Expand Down Expand Up @@ -35,6 +38,8 @@ def __init__(self, version):
self.launch_text = ''
self.launch_langs = {}

self.proxy_status = setup_proxy()

# Add command line options
self.add_main_option('text', b't', GLib.OptionFlags.NONE,
GLib.OptionArg.STRING, 'Text to translate', None)
Expand All @@ -47,7 +52,7 @@ def __init__(self, version):

def do_activate(self):
self.window = self.props.active_window

if not self.window:
width, height = Settings.get().window_size
self.window = DialectWindow(
Expand All @@ -60,7 +65,7 @@ def do_activate(self):
langs=self.launch_langs
)
self.setup_actions_signals()

self.window.present()

def do_command_line(self, command_line):
Expand All @@ -80,7 +85,7 @@ def do_command_line(self, command_line):
langs['dest'] = options['dest']

if self.window is not None:
self.window.translate(text, langs['src'], langs['dest'])
self.window.translate(text, langs['src'], langs['dest'])
else:
self.launch_text = text
self.launch_langs = langs
Expand Down Expand Up @@ -218,6 +223,33 @@ def on_quit(self, _action, _param):
self.quit()


def setup_proxy():
""" Use proxy settings from GNOME """
try:
proxy = Gio.Settings.new("org.gnome.system.proxy")
mode = proxy.get_value("mode").get_string()
if mode == "manual":
# Fetch socks proxy settings
socks_settings = Gio.Settings.new("org.gnome.system.proxy.socks")
host = socks_settings.get_value("host").get_string()
port = socks_settings.get_value("port").get_int32()

if host != "" and port != 0:
# Reset proxy env vars
environ["all_proxy"] = ""
environ["ALL_PROXY"] = ""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do this because something kept reading it from the environment variable and did not allow the app to work. But from my experimentation using NetHogs, the proxy seems to work just fine, so it should be okay.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not work here (timeout on search). Looks like settings variable is mandatory.

Tried but your right, something manually tries to understand proxy env :(

Copy link
Member Author

@mufeedali mufeedali Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see :(

Too bad. Well... proper proxy support and a bunch of other large changes will be coming eventually, so maybe we will have to put this off until then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, converting to draft and will experiment again soon if possible. But unfortunately... it works for me now. So I don't know how to proceed 😅 .

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try to debug

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be awesome! Thanks!


# Try to set socks proxy by monkey patching
socks.set_default_proxy(socks.SOCKS4, host, port)
socket.socket = socks.socksocket

return True
except Exception as e:
print(f"Proxy setup failed: {e}")

return False


def main(version):
# Run the Application
app = Dialect(version)
Expand Down
7 changes: 3 additions & 4 deletions dialect/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,11 @@ def send_notification(self, text, timeout=5):
self.notification_label.set_text(text)
self.notification_revealer.set_reveal_child(True)

timer = threading.Timer(
GLib.timeout_add_seconds(
timeout,
GLib.idle_add,
args=[self.notification_revealer.set_reveal_child, False]
self.notification_revealer.set_reveal_child,
False
)
timer.start()

def toggle_voice_spinner(self, active=True):
if active:
Expand Down