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

Can’t get it working #1

Closed
simonpacis opened this issue Feb 10, 2020 · 8 comments
Closed

Can’t get it working #1

simonpacis opened this issue Feb 10, 2020 · 8 comments

Comments

@simonpacis
Copy link

Hey man! Thanks for this script. I can’t seem to get it working with ad-hoc on my Mac. Here’s what I do:

  1. Create network on Mac
  2. Join Mac’s new network from iPhone and the Mac itself
  3. Run the socks5 script.
  4. Enter the wdat url in Mac proxy settings.
  5. Open browser

Am I doing something wrong? Thanks for your help! I would love to make this work.

@nneonneo
Copy link
Owner

nneonneo commented Feb 10, 2020

Thanks for the bug report! I just tried your setup on my Mac using an ad-hoc (computer) network, and you're right, it doesn't work at all. I'm not sure what is going on, exactly - the Mac doesn't even attempt to retrieve the wpad.dat file when connected to the ad-hoc network; it won't even work if you set the SOCKS5 proxy manually. Something is preventing the Mac from believing that it has a network connection.

However, it works fine if you use Firefox on your Mac, as you can set proxy settings in Firefox independent of your system settings. I also just pushed a fix that makes the wpad work properly in Firefox (but before that fix, setting the SOCKS5 proxy URL manually also worked).

(The reason I didn't notice this before - I use Firefox almost exclusively, and the last time I tested with the macOS system proxy I was using a throttled mobile hotspot rather than an ad hoc network, so I didn't notice this problem).

@nneonneo
Copy link
Owner

nneonneo commented Feb 10, 2020

After some more testing, here's a workaround (I'll stress that this is a bug in macOS's network handling - not in my script):

  • Under the Network settings, go to Advanced -> TCP/IP.
  • Copy down the IPv4 address shown (for an ad hoc network, it should be of the form 169.254.x.x). - Change Configure IPv4 to Manually, then fill in the same IPv4 address from before, set the subnet mask to 255.255.0.0 (which was its previous value), and set Router to the same 169.254.x.x address.
  • Under DNS, add the IP address of your computer to the DNS Servers list.
  • Under Proxies, select SOCKS Proxy (do not select Automatic Proxy Configuration) and enter the IP of your phone (a different 169.254.x.x address) and the port 9876 (it's shown when the script boots up under "SOCKS Address").

In testing, this caused Safari and various system services to route through the proxy, as expected. (I didn't test Chrome, but since it uses the system proxy settings it would probably work too).


Technical detail: it appears that without a Router or DNS setting, macOS's URL APIs consider you offline, even though you can freely access anything under your subnet without a router (which would include the iPhone running the proxy). The Router setting is used for packets that need to be routed outside of your subnet, e.g. ones going to the Internet; if it's missing, you can't route packets directly to the Internet. I guess they didn't consider the case where you would exclusively proxy through a device on the local network...

(Note that using your own IP as a Router setting is technically invalid and definitely won't magically route your packets to the Internet, but it seems to satisfy macOS so that's enough.)

@simonpacis
Copy link
Author

Hey!

Thanks for the quick and detailed response. I got it working in Firefox and with Proxifier too. Would like to get System Services and Mail running, which Proxifier does not seem to be able to handle.

Either way, unfortunately your workaround did not work for me. I just lost all connectivity. Perhaps to be expected, if it's technically invalid.

Thank you anyway for trying!

  • Simon

@nneonneo
Copy link
Owner

nneonneo commented Feb 10, 2020

Hmm, interesting. I tested on macOS 10.14. Maybe it's changed in a different OS. One thing you could try is to set the Router to the IP of your iPhone rather than the IP of the computer itself. That won't route anything either (since the iPhone won't try to route your packets in an ad hoc network), but it might be less invalid...

@zbuc
Copy link

zbuc commented Jan 2, 2022

I am experiencing this issue as well. I'm able to ping the iPhone from the Mac on the ad hoc network and even manually open a socket connection to the SOCKS proxy, however Chrome/Safari/curl/etc. don't work after setting the SOCKS proxy setting in the network settings. I've tried with the router address set to both the laptop and the phone with no difference.

@zbuc
Copy link

zbuc commented Jan 2, 2022

If I manually specify the proxy on the command line to curl I get a little further but something is breaking:

INFO:root:169.254.80.43:53861: new connection
----------------------------------------
Exception happened during processing of request from ('169.254.80.43', 53861)
Traceback (most recent call last):
  File "/var/containers/Bundle/Application/B5EB9709-7018-4708-9B00-6E52F0C5F9E7/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/socketserver.py", line 640, in process_request_thread
    self.finish_request(request, client_address)
  File "/var/containers/Bundle/Application/B5EB9709-7018-4708-9B00-6E52F0C5F9E7/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/socketserver.py", line 362, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/var/containers/Bundle/Application/B5EB9709-7018-4708-9B00-6E52F0C5F9E7/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/socketserver.py", line 697, in __init__
    self.handle()
  File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/socks/iOS-SOCKS-Server-master/socks5.py", line 175, in handle
    version, cmd, _, address_type = readstruct(sockfile, "!BBBB")
  File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/socks/iOS-SOCKS-Server-master/socks5.py", line 114, in readstruct
    return struct.unpack(fmt, readall(f, struct.calcsize(fmt)))
  File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/socks/iOS-SOCKS-Server-master/socks5.py", line 108, in readall
    raise EOFError()
EOFError
----------------------------------------

@StefanCioata
Copy link

Same issue here on macOS Ventura Version 13.3.1 (22E261).
"ssh -o ProxyCommand='nc -X 5 -x :9876 %h %p' user@host"works but
I can't not get any of the browsers (Chroma & Safari) to work via the proxy.
I also noticed that the postings are rather old so I am wondering if this project has anymore traction/maintenance.

Thnak you

@nneonneo
Copy link
Owner

nneonneo commented May 29, 2023

Thanks for the ping. I believe I have a solution.

For some background: I have been using this script for the past few years, even as recently as a few weeks ago. I usually use it from a tethered-but-throttled connection, i.e. when my provider permits tethering but at speeds far below what my phone can achieve. In this scenario, macOS connects to the iPhone hotspot, so it correctly assesses the network as reachable.

However, I believe everyone in this thread has been using it in the non-tethering setup, where you e.g. create an ad-hoc network on your Mac, then connect to that network from the iPhone and launch the SOCKS proxy. In this scenario, macOS assesses the network as being unable to reach the internet, even when a proxy server is set. Unfortunately, this will prevent any application that relies on the macOS networking APIs from reaching Internet sites.

I usually use Firefox, which side-steps this issue because uses an independent proxy configuration and an independent network reachability check, so it will work properly if a proxy is set. However, this is not a complete solution.

I believe we can trick the reachability check. In #1 (comment) I mentioned setting the Router of the connection. That doesn't appear to be sufficient anymore.

What I did now was to also set the DNS Server to the phone. It doesn't matter that the phone has no DNS server running. The combination of setting the Router and the DNS Server seems to be enough to override the reachability check. I have verified this on macOS 12.6.5.

In detail, here's what you do:

  1. Create an ad-hoc network on your Mac (Wi-Fi Menu -> Create Network...)
  2. Join the network on your iPhone. As there is no DHCP server on this ad-hoc network, the status icon next to the WiFi network may spin indefinitely instead of becoming a checkmark. The WiFi icon may also fail to appear in the status area. This is normal.
  3. Launch Pythonista and the SOCKS5 script. If the ad-hoc network connection is working, you should see "Assuming proxy will be accessed over WiFi (en0) at 169.254.x.x" followed by "SOCKS Address: 169.254.x.x". Note down the full 169.254.x.x address. Note: if this says, instead, "Warning: could not get WiFi address", wait a few seconds, confirm that you're still connected to the ad-hoc network, and launch the script again - it may take up to a minute for your phone to "obtain" an IP address from the ad-hoc network.
  4. On macOS, head into System Preferences -> Network, select WiFi, and hit Advanced...
  5. In the TCP/IP tab, copy the IPv4 address as currently displayed (assuming you're using DHCP). Switch "Configure IPv4" to "Manually", put in the IPv4 address you just copied1, set Subnet Mask to 255.255.0.0, and set the Router to your iPhone's 169.254.x.x address.
  6. In the DNS tab, set your iPhone's 169.254.x.x address as the first (or only) DNS server2.
  7. In the Proxies tab, check SOCKS proxy and enter the iPhone's 169.254.x.x address, port 9876.

With any luck, Safari should start working. Please let me know if these instructions work for you.

(P.S. Make sure to undo those changes when you switch back to a regular WiFi connection!)

1: If no IPv4 address was displayed, you can just choose a random 169.254.y.z address (y, z between 1 and 254), as long as it isn't the address of your iPhone.
2: After some experimentation, it looks like you can actually set the router and DNS to roughly anything (including, for example, your Mac's own IP address); what basically matters is that they aren't blank.

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

No branches or pull requests

4 participants