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

Need quick proxy switch #742

Open
berniedurfee opened this issue Aug 24, 2014 · 8 comments
Open

Need quick proxy switch #742

berniedurfee opened this issue Aug 24, 2014 · 8 comments

Comments

@berniedurfee
Copy link

I like Sublime Package Control. At work I need to configure a proxy server. Anywhere else I don't. I use Sublime Package Control everywhere.

It would be nice to have a quick way to switch the proxy on and off.

A) Just a setting like 'use_proxy' which can be true or false. That would be okay.
B) Some way that would allow me to configure a keyboard shortcut. That would be great.

If I get time I'll try to do a pull request, unless someone beats me to it?

@wbond
Copy link
Owner

wbond commented Sep 1, 2014

If you are going to work on something like this, make sure it works for all of the downloaders, not just one.

@berniedurfee
Copy link
Author

Can you give me a few sentences on where to get started implementing this? How you'd like to see it get done? I'm not familiar with the codebase, so don't know where to start.

@wbond
Copy link
Owner

wbond commented Sep 2, 2014

Ok, so at the HTTP level there are "downloaders". These are classes that interface with different APIs or external programs to actually download files from the internet.

The primary downloader uses the Python urllib/urllib2 module, and it used for non-secure downloads on OS X and Linux, plus secure downloads on OS X:
https://github.com/wbond/sublime_package_control/blob/master/package_control/downloaders/urllib_downloader.py

Sublime Text doesn't bundle the _ssl module on Linux, so there are two downloaders for secure connections on Linux, both which use external programs (curl and wget):
https://github.com/wbond/sublime_package_control/blob/master/package_control/downloaders/curl_downloader.py
https://github.com/wbond/sublime_package_control/blob/master/package_control/downloaders/wget_downloader.py

On Windows, all downloads (secure and not) are passed through the WinINet API so that Sublime Text uses the system-wide proxy settings:
https://github.com/wbond/sublime_package_control/blob/master/package_control/downloaders/wininet_downloader.py

Each downloader gets a settings dict passed to its constructor. Control of what settings are loaded from the settings file is performed in:
https://github.com/wbond/sublime_package_control/blob/master/package_control/package_manager.py#L65-L131

If you are going to create a new command, it should be placed in a file in https://github.com/wbond/sublime_package_control/tree/master/package_control/commands and added to the init.py. Then you'll need to add the name of the command to the list of commands exposed to the command palette: https://github.com/wbond/sublime_package_control/blob/master/Default.sublime-commands.

Good luck!

@schlamar
Copy link
Contributor

@berniedurfee There is a better solution to define settings based on your current machine/platform. The rough idea is to modify settings in a plugin. This is still no optimal solution but AFAIS Sublime is too limited for a really good synchronization across different machines/platforms.

So here is some pseudo code:

pc_settings = sublime.load_settings('Package Control.settings')
if socket.gesthostname() == 'MYWORKMACHINE':
    proxy = 'http://xxx:yy/'
else:
    proxy = ''
pc_settings.set('http_proxy', proxy)
pc_settings.save()

This should probably be but in a plugin which is loaded before Package Control, e.g. 00_preferences/preferences.py.

@FichteFoll
Copy link
Collaborator

FWIW, the pseudo code won't do since sublime.load_settings always returns None if the API is not ready yet, which is likely the case if you want this plugin to be loaded immediately after Default.

Instead you have to use the json module (and possibly account for comments in the file that you'll have to skip; see this).

@schlamar
Copy link
Contributor

Ah yes, but doing this in plugin_loaded (on ST3) should be working. So there is not need for manual JSON parsing...

@tauren
Copy link

tauren commented Jan 2, 2015

Syncing the same config between work system and personal system is great in theory, but the docs (https://packagecontrol.io/docs/syncing) won't work if you have to set an http_proxy on one system and not the other.

Rather than implementing a manual "quick proxy switch", perhaps it would be better to create a more general purpose plugin that can customize settings based on detected system information. This plugin could update the proxy and/or other settings based on machine name, wifi network name, IP address range, whether VPN is active, etc.

I haven't ever written a Sublime plugin, but it seems like this would be doable and more versatile. Perhaps PlatformSettings might be a good place to start looking for ideas?
https://packagecontrol.io/packages/PlatformSettings

@pomeh
Copy link

pomeh commented Jan 29, 2015

What about something like this in Package Control.sublime-settings:

{
    "http_proxy": "proxy.example.com",
    "https_proxy": "proxy.example.com",
    "proxy_username": "",
    "proxy_password": "",

    "machines": [
        // specific config for machine 1
        "machine1-name": {
            // eg: specify user info to use on this machine
            "proxy_username": "proxyuser",
            "proxy_password": "proxypassword",
        },

        // specific config for machine 2
        "machine2-name": {
            // eg: no proxy needed for this machine
            "http_proxy": "",
            "https_proxy": "",
        }

    ]
}

Or something like this (separate machine-specific config into its own file):

file Package Control.sublime-settings (default config)

{
    "http_proxy": "proxy.example.com",
    "https_proxy": "proxy.example.com",
    "proxy_username": "",
    "proxy_password": "",
}

file Package Control [machine1-name].sublime-settings

// specific config for machine 1
{
    // eg: specify user info to use on this machine
    "proxy_username": "proxyuser",
    "proxy_password": "proxypassword",
}

file Package Control [machine2-name].sublime-settings

// specific config for machine 2
{
    // eg: no proxy needed for this machine
    "http_proxy": "",
    "https_proxy": "",
}

With this kind of solution, you can override every setting you want for a particular machine. What do you think ? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants