luvit / lit

Toolkit for developing, sharing, and running luvit/lua programs and libraries.
http://lit.luvit.io/
Apache License 2.0
249 stars 58 forks source link

Is there any option for HTTPS proxy? #168

Open 9chu opened 8 years ago

9chu commented 8 years ago

It's hard for my network to access to "github-cloud.s3.amazonaws.com", which causes the command "lit make lit://luvit/luvit" fail. And it seems that the environment variable "HTTPS_PROXY" doesn't work for lit. So, is there any solution to make lit using HTTPS proxy? thanks.

creationix commented 8 years ago

This feature can be added, what kind of proxy is it?

9chu commented 8 years ago

Just an HTTP/HTTPS proxy. I am now using proxychains to solve this problem under ubuntu. It's better if lit could pull from a Git repository through an HTTP proxy directly :)

creationix commented 8 years ago

ok, I'll see about adding support for HTTPS_PROXY, if you have links for the protocol that would help (or at least an example of what the value contains with your auth details scrubbed out)

lduboeuf commented 7 years ago

like @9chu , would be great to make lit work behind a corporate proxy ;-)

9chu commented 7 years ago

That's great! Thanks a lot.

creationix commented 7 years ago

So is this a proxy using CONNECT in http and the the proxy doing plain TCP proxy? https://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_tunneling

What does the value of the HTTPS_PROXY environment variable look like? I assume it's a local url of some sort (or at least host and optional port)

lduboeuf commented 7 years ago

https_proxy and http_proxy are set to http://proxyurl:8080 scheme but are not used by lit i guess when doing lit install works fine if use proxychain

creationix commented 7 years ago

thanks, I'll see what it takes to implement this. It shouldn't be too hard.

squeek502 commented 7 years ago

I attempted a proxy implementation a while back over the top of coro-http. It might not be the best way to do it, but I figure it might be of some potential use as a reference.

For some reason it's getting a 400 response when trying to connect to https://luvit.io through a proxy now (it used to work), but it still works for other sites (e.g. https://www.google.com)

--[[lit-meta
  name = "squeek502/coro-http-proxy"
  version = "1.0.0"
  dependencies = {
    "creationix/coro-http@2.1.1",
    "luvit/secure-socket@1.0.0",
  }
]]

local http = require('coro-http')

local function request(method, url, headers, body, proxy)
  local requestURI = http.parseUrl(url)

  if proxy then
    local proxyURI = http.parseUrl(proxy)
    proxyURI.host = proxyURI.host:match("^[^:]+")
    local connection = http.getConnection(proxyURI.host, proxyURI.port, proxyURI.tls)
    local read, write = connection.read, connection.write

    local authorityForm = requestURI.host .. ':' .. requestURI.port
    local req = {
      method = 'CONNECT',
      path = authorityForm,
      {"Host", authorityForm}
    }

    write(req)

    local res = read()
    if not res then
      if not connection.socket:is_closing() then
        connection.socket:close()
      end
      -- If we get an immediate close on a reused socket, try again with a new socket.
      -- TODO: think about if this could resend requests with side effects and cause
      -- them to double execute in the remote server.
      if connection.reused then
        return request(method, url, headers, body, proxy)
      end
      error("Connection closed")
    end

    if res.code == 200 then
      connection.host = requestURI.host
      connection.port = requestURI.port
      connection.tls = requestURI.tls
      connection.reset()
      http.saveConnection(connection)
    else
      error("Tunnel socket recieved unexpected response code: " .. res.code)
    end
  end

  return http.request(method, url, headers, body)
end

return {
  request = request,
}

The test (the proxy is just some random one from a public proxy list):

local httpProxy = require('coro-http-proxy')
local http = require('coro-http')

require('tap')(function(test)

  test("proxy request", function()
    coroutine.wrap(function()
      local res, body = httpProxy.request("GET", "https://www.google.com", nil, nil, "http://97.77.104.22:3128")
      assert(res.code == 200, res.code)
      assert(body ~= nil)

      if res.keepAlive then
        local connection = http.getConnection("www.google.com", 443, true)
        assert(connection.reused)
        connection.socket:close()
      end
    end)()
  end)

end)
tbmale commented 6 years ago

Hi all, Are there any news regarding the issue of using lit behind a http proxy ?