Search code examples
javaproxynio

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. 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. 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.

So, something worng with my code or its just closed opportunity to proxy by sites?

Update 1. I suppose, I found a problem. Cause I redirect request from localhost to remote server AS IS so in request in field HOST I have '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. So question is how to change field 'Host' in request on destination server name?


Solution

  • I found a problem. filed HOST after proxy contains 'localhost', so some sites accept it, other not. Replace value of this fields with real host fix the problem.