Coder Social home page Coder Social logo

ablyes / proxy-settings-osx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kontrafiktion/proxy-settings-osx

0.0 2.0 0.0 174 KB

OS X: scripts to set the proxy configuration for mvn, git, curl, npm and svn

Shell 85.55% Python 2.97% Perl 11.47%

proxy-settings-osx's Introduction

Switching proxy settings for Maven, git, etc. (on OS X)

I am currently working for two customers at three different locations (and at home). And the proxy settings differ at every location! Sometimes I have to switch from the internal network (with proxy) to my iPhone HotSpot and then back again.

Each time I have to change my proxy settings for

  • git
  • Maven
  • homebrew/curl/...

I tried to use a local proxy, but had some problems with Outlook (for OS X).

So I ended up creating a small shell script that checks for the proxy settings in OS X and patch Maven's settings.xml and the git configuration (as well as setting the environment variable http_proxy). It has served me well in the last weeks, so I wanted to share it.

Determining the current default network

You can have multiple active network interfaces (e.g. "Wi-Fi" and your iPhone), but one is the 'default' interface, which you can determine with:

route -n get default

this gives you the network interface, e.g. 'en0'. But for the following steps I need the network service name, i.e. "Wi-Fi", so we need to call

networksetup -listnetworkserviceorder

which produces something like:

(1) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

(2) iPhone
(Hardware Port: iPhone USB, Device: en5)

I have to grep for the "Device" (and then get the previous line, etc.).

After all this I have the default network service name.

Determining the proxy

explicit proxy settings

If the host and port of the proxy are set explicitely, I can retrieve the sttings with

networksetup -getwebproxy "<network service name>"

The above statement prints something like:

Enabled: Yes
Server: 10.2.58.17
Port: 8080
Authenticated Proxy Enabled: 0

proxy.pac

But if I have a proxy.pac configured, I need to call

networksetup -getautoproxyurl Wi-Fi

which returns the URL for the proxy.pac file. This file is a simple JavaScript file that can be parsed with the help of pacparser

Configuring Git, Maven, etc.

Git config

When I have the PROXY_HOST and the PROXY_PORT, I can set some environment variables and tell git to use the proxy:

git config --global http.proxy "http://$PROXY_HOST:$PROXY_PRT"

Maven proxy settings

The proxy settings for Maven are located in $HOME/.m2/settings.xml. I have created a simple entry for the proxy, the id must be "env-proxy":

<proxies>
  <proxy>
    <id>env-proxy</id>
    <active>false</active>
    <protocol>http</protocol>
    <host>proxy</host>
    <port>8080</port>
  </proxy>
</proxies>

This whole block is replaced by the correct settings with the active-flag switched to true.

Un-setting

Invoking the script when no proxy can be determined removes/deactivates the proxy settings (git, Maven's settings.xml and environment variables)

Install

  1. install pacparser
  2. clone https://github.com/vrvolle/proxy-settings-osx.git
  3. put the scripts in a directory in your PATH
  4. make them executable
  5. add a proxy entry "env-proxy" to your settings.xml

Invoke

To invoke the script, you must source it. Otherwise the environment variables will not be set:

source <path-to-script>/set-http-proxy

proxy-settings-osx's People

Contributors

kontrafiktion avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.