gem [Ruby] 通过网络代理连接 [windows]

发布于 2024-10-10 13:04:11 字数 2060 浏览 0 评论 0原文

我问这个古老的问题,关于在使用 ruby​​ gem 时如何通过代理。

我尝试设置环境变量HTTP_PROXY

HTTP_PROXY=myusername:[email protected]:8080

但这不起作用。

> gem install rails
ERROR:  While executing gem ... (URI::InvalidURIError)
    bad URI(is not URI?): http://myusername:[email protected]:8080

我尝试在开头添加 tcp:// 但它在错误消息中显示 http://tcp:// ,所以我认为这也是错误的。

所以,我尝试使用以下代码通过 php 自动连接:

<?php
$path = 'http://rubygems.org/';

$auth = base64_encode('myusername:password');

file_put_contents('proxy.log', 'POST::', FILE_APPEND);
file_put_contents('proxy.log', print_r($_POST, true), FILE_APPEND);
file_put_contents('proxy.log', 'GET::', FILE_APPEND);
file_put_contents('proxy.log', print_r($_GET, true), FILE_APPEND);

$aContext = array(
    'http' => array(
        'method' => 'GET',
        'content' => http_build_query($_GET),
        'proxy' => 'tcp://proxy.com:8080',
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth"
    )
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents($path, false, $cxContext);

header('Content-type: application/gzip');
echo $sFile;

如果它试图获取特定文件,我使用 apache 处理它。

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L] 

    RewriteCond $1 !^$
    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
# Options -Indexes

但我仍然收到此错误。

> gem sources -ahttp://mylocalhost/ror-proxy/
ERROR:  While executing gem ... (Zlib::GzipFile::Error)
not in gzip format

有什么想法吗?

I'm asking this age-old question on how go through your proxy when using ruby gem.

i tried setting the env variable HTTP_PROXY:

HTTP_PROXY=myusername:[email protected]:8080

but this does not work.

> gem install rails
ERROR:  While executing gem ... (URI::InvalidURIError)
    bad URI(is not URI?): http://myusername:[email protected]:8080

i tried using adding the tcp:// at the start but it shows http://tcp:// in the error message, so i assumed that was wrong too.

So, i tried an automatic connect via php using this code:

<?php
$path = 'http://rubygems.org/';

$auth = base64_encode('myusername:password');

file_put_contents('proxy.log', 'POST::', FILE_APPEND);
file_put_contents('proxy.log', print_r($_POST, true), FILE_APPEND);
file_put_contents('proxy.log', 'GET::', FILE_APPEND);
file_put_contents('proxy.log', print_r($_GET, true), FILE_APPEND);

$aContext = array(
    'http' => array(
        'method' => 'GET',
        'content' => http_build_query($_GET),
        'proxy' => 'tcp://proxy.com:8080',
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth"
    )
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents($path, false, $cxContext);

header('Content-type: application/gzip');
echo $sFile;

and incase it is trying to get a specific file, i handled it using apache.

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L] 

    RewriteCond $1 !^$
    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
# Options -Indexes

but still i get this error.

> gem sources -ahttp://mylocalhost/ror-proxy/
ERROR:  While executing gem ... (Zlib::GzipFile::Error)
not in gzip format

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-10-17 13:04:11

代理服务器,特别是身份验证代理服务器可能会给各种自动更新服务带来麻烦。不幸的是,gem 无法正确处理 HTTP_PROXY 环境变量。简而言之,它不知道如何处理来自服务器的 BasicAuthentication 响应——即使它尝试了。

长话短说,您必须设置一个本地代理服务器来负责通过代理进行导航。它不能执行任何转换(例如解压缩压缩流)。

myusername:[电子邮件受保护]:8080构造不是有效的 URL。我知道这是 Windows 验证代理服务器的快捷方式,但只有 @ 符号后面的部分才是有效的 URL。

查看此常见问题解答:http://docs.rubygems.org/read/chapter/15

这个问题很常见。

Proxy servers, particularly authenticating proxy servers can throw a monkey wrench in all sorts of automatic update services. Unfortunately, gem does not properly deal with the HTTP_PROXY environment variable. In short, it doesn't know what to do with a BasicAuthentication response from a server--even though it tries.

Long story short, you'll have to set up a local proxy server that takes care of navigating through the proxy. It can't do anything that performs any transformations (like decompressing a compressed stream).

The myusername:[email protected]:8080 construct is not a valid URL. I know that it is a shortcut for windows authenticating proxy servers, but only the portion after the @ symbol is a valid URL.

Check out this FAQ: http://docs.rubygems.org/read/chapter/15

The problem is common enough.

千柳 2024-10-17 13:04:11

现在修复了 PHP 代理。

添加新的源 gem 时发出

规格_4_8_gz

请注意,_ 代表 .(点)。您所需要做的就是更换它。请注意,此操作仅执行一次。其他宝石将以其正确的文件名发送出去。

应该看起来像这样。

if (empty($_GET['_p'])) {
    header('Content-type: text/html');
} else {
    header('Content-type: application/gzip');

    $item = $_GET['_p'];
    $item = '.gz' == substr($item, -3) ? $item: str_replace('_', '.', $item);
    header('Content-Disposition: filename="' . $item . '"');
    $path .= $item;
}

我的完整脚本将发布在 github 上。

Fixed PHP proxy now.

when adding a new source gem sends out

specs_4_8_gz

notice that it that _ represents .(dot). all you need to do is replace it. note that this is only done once. other gems will be sent out as their proper file name.

should look something like this.

if (empty($_GET['_p'])) {
    header('Content-type: text/html');
} else {
    header('Content-type: application/gzip');

    $item = $_GET['_p'];
    $item = '.gz' == substr($item, -3) ? $item: str_replace('_', '.', $item);
    header('Content-Disposition: filename="' . $item . '"');
    $path .= $item;
}

my full script will be posted on github.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文