简体   繁体   English

并非所有站点都可以由Java NIO代理。 为什么?

[英]Not all sites can be proxied by java NIO. Why?

I have implemented simple proxy server using Java NIO channels, but have a problem, some sites works perfectly, but other give an error about unknown path or redirect on technical page of its hoster with message the resource doesn't exist. 我已经使用Java NIO通道实现了简单的代理服务器,但是有一个问题,一些站点可以正常运行,但是其他站点在其主机的技术页面上给出了有关未知路径或重定向的错误,并显示消息资源不存在。 Is it my fault or may be some sites don`t allow proxy? 是我的错,还是某些网站不允许代理?

ProxyServer works as this: I enter 'localhost' and in browser I recive site that was set in code. ProxyServer的工作方式如下:输入“ localhost”,然后在浏览器中恢复在代码中设置的站点。 And request from browser I simply resend on target site at such way: 来自浏览器的请求我只是以这种方式重新发送到目标站点:

private void connect(SelectionKey key) throws IOException {
    SocketChannel channel = ((SocketChannel) key.channel());
    Attachment attachment = (Attachment) key.attachment();
    channel.write(attachment.buffer);
}

So 'key' - is SelectionKey of target site and in attachment.buffer I store request that was send to proxy server. 所以,“钥匙” - 是目标网站的SelectionKey和attachment.buffer我储存请求被发送到代理服务器。

So, something worng with my code or its just closed opportunity to proxy by sites? 那么,我的代码有什么用吗?或者它是封闭的站点代理机会?

Update 1. I suppose, I found a problem. 更新1.我想,我发现了一个问题。 Cause I redirect request from localhost to remote server AS IS so in request in field HOST I have 'localhost'. 因为我从本地主机到远程服务器重定向请求,因为在现场主持人的要求是让我有“localhost”的。 It seems like some sites ignore this fields, other try to use and redirect to 404 page, cause can't find 'localhost' I`m asking for. 似乎有些站点忽略了此字段,其他站点尝试使用并重定向到404页面,原因是找不到我要的“本地主机”。 So question is how to change field 'Host' in request on destination server name? 那么问题是如何在目标服务器名称的请求中更改字段“主机”?

The target server doesn't know anything about your NIO code, or whether you are a proxy or a real client. 目标服务器对您的NIO代码一无所知,或者您是代理还是真正的客户端。

If you got an error page, the proxy is working, and it is the resource being proxied that is the problem: it doesn't exist, you don't have access, etc. Nothing you can do about that in your code and no reason why you should worry. 如果出现错误页面,则说明代理正在工作,而正是被代理资源才是问题所在:它不存在,您无权访问等。您无法在代码中对此做任何事情,并且没有您应该担心的原因。 Just send the error page to the client, same as you would send anything else. 只需将错误页面发送到客户端,就像发送其他任何内容一样。

Why is that method called connect() when it doesn't connect and does do something else? 为什么该方法在未连接且执行其他操作时称为connect()?

I found a problem. 我发现了一个问题。 filed HOST after proxy contains 'localhost', so some sites accept it, other not. 代理包含“ localhost”后提交HOST,因此某些站点接受它,其他站点则不接受。 Replace value of this fields with real host fix the problem. 用实际主机替换此字段的值可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM