showkey
showkey

Reputation: 320

Network is unreachable for hg clone

Autoproxy was installed on my firefox,and every web can be browsed via socket5 proxy service after buying vps service.

apt-get install mercurial  #on my local pc
hg clone https://vim.googlecode.com/hg/   /tmp/vim  #on my local pc
abort: error: Network is unreachable

https://vim.googlecode.com/hg/ can be visited via autoproxy in Firefox.
How to build some kind of proxy service on my remote VPS to make the hg clone command to reach https://vim.googlecode.com/hg/?

Not all webs can be visited here, I have build a ladder to reach them via Firefox.

How to make another ladder on my VPS (on the remote PC) to make the hg clone command run on my local PC?

Every time I make the ladder with my VPS and Firefox this way:

  1. To input ssh command in my local pc

    ssh -D 127.0.0.1:1080  -p 1234 [email protected]
    
  2. Click the Autoproxy plugin into Global mode. It was set in good status in Socket 5 proxy.

  3. Now i can visit what I want to visit.

How to write the hg clone command?

Upvotes: 3

Views: 1238

Answers (2)

kenorb
kenorb

Reputation: 166843

To use proxy for Mercurial you can prefix your command with http_proxy:

http_proxy=http://proxy-server:8080 hg clone https://vim.googlecode.com/hg

Or configure in your ~/.hgrc as:

[http_proxy]
host=proxy-host:port
user=username
passwd=password

Since you're using a SOCKS5 proxy, this is not yet supported by Mercurial (3.7.2 at time of writing), however there is an extension for it based on the patch below.

Extension

You can download a small extension for the Mercurial DVCS to enable the use of a SOCKS5 proxy: https://bitbucket.org/bugheisen/socks_proxy

So can configure your proxy in your ~/.hgrc as:

[extensions]
socks_proxy = /path/to/socks_proxy.py

[socks_proxy]
host = localhost:9150

To experiment with a SOCKS5 proxy you can use the -D option of SSH, which creates such a proxy that runs over the SSH connection. Alternatively you can run Tor, which also starts a SOCKS5 proxy (probably either on port 9050 or port 9150).

Patch

The above extension is based on the original patch posted by Bug Heisen and you can apply by downloading the source code of Mercurial and applying this patch based on the code below:

diff -r 12f161f08d74 -r 3f58227755ed mercurial/url.py
--- a/mercurial/url.py  Tue Apr 01 23:41:32 2014 -0700
+++ b/mercurial/url.py  Sun Apr 06 16:16:02 2014 +0200
@@ -63,6 +63,44 @@

 class proxyhandler(urllib2.ProxyHandler):
     def __init__(self, ui):
+
+        # First, check if a SOCKS5 proxy is set, using a line 'host = ...' in 
+        # the [socks_proxy] section of the config file.
+
+        proxyurl = ui.config("socks_proxy", "host")
+
+        if proxyurl:
+            idx = proxyurl.find(":")
+            if idx < 0:
+                raise util.Abort(_("host in socks_proxy should be "
+                                   "hostname:port"))
+
+            host = proxyurl[:idx]
+            portstr = proxyurl[idx+1:]
+            try:
+                port = int(portstr)
+            except ValueError:
+                raise util.Abort(_("Cannot interpret '%s' in the socks_proxy "
+                                   "host line as an integer port number") 
+                                   % portstr)
+
+            if port <= 0 or port > 65535:
+                raise util.Abort(_("Port number in socks_proxy host line "
+                                   "must lie between 1 and 65535, but is "
+                                   "%d") % port)
+
+            ui.note("Setting SOCKS5 proxy to %s:%d\n" % (host, port))
+                    
+            try:
+                import socks
+                socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, host, port)
+                socket.socket = socks.socksocket
+            except ImportError:
+                raise util.Abort(_("The SocksiPy socks module is needed for "
+                                   "SOCKS support"))
+
+        # Then check for a http/https proxy
+        
         proxyurl = ui.config("http_proxy", "host") or os.getenv('http_proxy')
         # XXX proxyauthinfo = None

then in your ~/.hgrc use socks_proxy instead of http_proxy (as shown above).

Original notes regarding this patch:

This patch adds support for a SOCKS5 proxy, using the 'socks' module from the SocksiPy projects (needs to be installed separately). Because it is the 'proxyhandler' function that is modified, it will only work for HTTP/HTTPS protocols. For the SSH protocol, using a SOCKS proxy can easily be done outside of Mercurial, by specifying the ProxyCommand option.

The patch has the SOCKS5 version hardcoded in it, but the socks module should support other versions as well. So other SOCKS versions are possible in the same way, but the code would require modification to support them.

For testing purposes, it's good to know that SSH can create a SOCKS5 proxy using the -D command line flag. Using Tor also generates such a proxy, typically either on port 9050 or 9150.

Upvotes: 3

planetmaker
planetmaker

Reputation: 6044

I suggest to start with configuring the proxy settings for your local mercurial instance. Check hg help config and look for proxy there.

"http_proxy"
------------

Used to access web-based Mercurial repositories through a HTTP proxy.

"host"
    Host name and (optional) port of the proxy server, for example
    "myproxy:8000".

"no"
    Optional. Comma-separated list of host names that should bypass the
    proxy.

"passwd"
    Optional. Password zur Authentifikation mit einem Proxy-Server.

"user"
    Optional. User name to authenticate with at the proxy server.

"always"
    Optional. Always use the proxy, even for localhost and any entries in
    "http_proxy.no". (default: False)

Upvotes: -1

Related Questions