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

Add a command line option for what interface to listen on #34

Merged
merged 2 commits into from
Aug 11, 2018

Conversation

tkluck
Copy link
Owner

@tkluck tkluck commented Aug 10, 2018

This may be useful for exposing pac4cli to containers (cf. #33).

Choosing -s for the short form as that agrees with netcat.

This is WIP, so let's not merge until we hear back from the issue author.

@badp
Copy link

badp commented Aug 10, 2018

I couldn't manage to reach happiness, but it might be due to either me setting up the program incorrectly, or my bug report having fundamentally misunderstood how containers work.

$ git clone --recursive https://github.com/pacparser/pacparser.git
$ cd pac4cli
$ git checkout add-interface-option
$ pipenv install -r requirements.txt
$ pipenv run python main.py -p 3129 -s docker0
Traceback (most recent call last):
  File "main.py", line 17, in <module>
    import pacparser
ModuleNotFoundError: No module named 'pacparser'
$ git clone https://github.com/pacparser/pacparser.git
$ pipenv run python main.py -p 3129 -s docker0 &
ERROR [20682]: pac4cli: Problem starting the server
Traceback (most recent call last):
  File "main.py", line 95, in main
    pacparser.init()
AttributeError: module 'pacparser' has no attribute 'init'
# the proxy seems to be up and running now?
$ PROXY_IP_ADDRESS=172.17.0.1
$ jq '. + {"proxies": {"default": {"httpProxy": "http://'$PROXY_IP_ADDRESS':3129/", "httpsProxy": "http://'$PROXY_IP_ADDRESS':3129/"}}}' ~/.docker/config.json | tee tmp.$$.json && mv tmp.$$.json ~/.docker/config.json
{
  "auths": {...},
  "HttpHeaders": {
    "User-Agent": "Docker-Client/17.12.1-ce (linux)"
  },
  "proxies": {
    "default": {
      "httpProxy": "http://172.17.0.1:3129/",
      "httpsProxy": "http://172.17.0.1:3129/"
    }
  }
}
$ docker run -it python env | grep http
http_proxy=http://172.17.0.1:3129/
HTTPS_PROXY=http://172.17.0.1:3129/
https_proxy=http://172.17.0.1:3129/
HTTP_PROXY=http://172.17.0.1:3129/
$ docker run -it python pip install pipenv
Collecting pipenv
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7f4d08bfd470>: Failed to establish a new connection: [Errno 111] Connection refused'))': /simple/pipenv/

Indeed:

$ pipenv run python main.py -p 3129 -s 0.0.0.0 &
$ docker run -it python pip install pipenv
Collecting pipenv
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7f8f54967748>: Failed to establish a new connection: [Errno 111] Connection refused'))': /simple/pipenv/

@tkluck
Copy link
Owner Author

tkluck commented Aug 10, 2018

@badp that looks like an issue setting up the environment, indeed: there's an additional step to install pacparser (which has a build step) into the environment. If you prefer pipenv over virtualenv, then be sure to also suitably adjust the build step:
https://github.com/tkluck/pac4cli/blob/add-interface-option/Makefile#L38

@badp
Copy link

badp commented Aug 10, 2018

okay, that git clone https://github.com/pacparser/pacparser.git is where I took a wrong turn. Trying again...

@badp
Copy link

badp commented Aug 10, 2018

Starting over (I happened to have all of the required packages installed with apt anyway -- I did install the software!)

$ git clone https://github.com/pacparser/pacparser.git
$ cd pacparser
$ make -c src/
$ sudo make -C src install
$ make -C src pymod
$ sudo make -C src install-pymod
$ cd pac4cli
$ python3 main.py -p 3129 -s docker0
INFO [16176]: pac4cli: Updating WPAD configuration...
INFO [16176]: pac4cli: Trying to get wpad url from NetworkManager DHCP...
INFO [16176]: pac4cli: Trying to get wpad url from NetworkManager domains...
INFO [16176]: pac4cli: Trying http://wpad/wpad.dat...
INFO [16176]: pac4cli: ...found. Parsing configuration...
INFO [16176]: pac4cli: Updated configuration
INFO [16176]: pac4cli: Starting proxy server on docker0:3129
ERROR [16176]: pac4cli: Problem starting the server
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/twisted/internet/defer.py", line 1386, in _inlineCallbacks
    result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/twisted/internet/tcp.py", line 978, in startListening
    skt.bind(addr)
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 104, in main
    yield start_server(args.interface, args.port, reactor)
twisted.internet.error.CannotListenError: Couldn't listen on docker0:3129: [Errno -2] Name or service not known.
$ python3 main.py -p 3129 -s 172.17.0.1 &
$ docker run -it python pip install pipenv
Downloading https://files.pythonhosted.org/packages/eb/64/9b2747d54f2008ac3dfe86c0b1c8ec126042726fd8a540d5208d26732701/pipenv-2018.7.1-py3-none-any.whl (5.0MB)
    100% |████████████████████████████████| 5.0MB 6.0MB/s 

By the name of the parameter, "interface", I expected to be able to say "docker0" and have it work, but I don't suppose it's a huge deal breaker.

Stack Overflow says you may be able to get the IP address for a given interface with this:

 import netifaces; 
 ip = netifaces.ifaddresses('docker0')[netifaces.AF_INET][0]['addr']

Of course, that [0] is pretty scary hardcoding, and (more importantly) that's an additional dependency for your project, and your packaging, PPA, etc., so I'm happy to take the patch as-is and kludge the IP address myself with some ip address show dev docker0 | grep -o -P 'inet ([^ ]+)' | cut -d' ' -f2 | head -n1 incantation.

@tkluck
Copy link
Owner Author

tkluck commented Aug 10, 2018

Thanks @badp ! Maybe @lucrocha or @kdehairy can do a review as maintainers?

@tkluck
Copy link
Owner Author

tkluck commented Aug 10, 2018

@badp heads-up: I just changed the name for this option to -b, --bind to avoid the confusion you suffered. I stole that from python3 http.server.

@tkluck tkluck changed the title WIP: Add a command line option for what interface to listen on Add a command line option for what interface to listen on Aug 11, 2018
This may be useful for exposing pac4cli to containers (cf. #33).

Choosing `-s` for the short form as that agrees with `netcat`.
The original reporter was confused, and this name is the same
as what python's `http.server` module uses.
@kdehairy kdehairy merged commit b9d8e18 into master Aug 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants