Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
Have net.Socket redirect the client to new URL in forward proxy

I'm trying to create a forward proxy that redirects certain web requests to another site.

For example, if you were to search on Google, it would redirect that search to another service like DuckDuckGo/Bing/Yahoo.

I have the proxy listening in on requests but can't quite get it to redirect:

import { createServer, createConnection, Socket } from 'net'

const server = createServer((clientToProxySocket: Socket) => {
    clientToProxySocket.once("data", (data) => {
        let isTLSConnection = data.toString().indexOf("CONNECT") !== -1;

        let port = 80;
        let host;
        if (isTLSConnection) {
            port = 443;
            host = data.toString().split("CONNECT")[1].split(" ")[1].split(":")[0];
        } else
            host = data.toString().split("Host: ")[1].split("\r\n")[0];

        let proxyToServerSocket: Socket = createConnection({ host, port },() => console.log("Proxy to server set up"));

        if (isTLSConnection)
            clientToProxySocket.write("HTTP/1.1 200 OK\r\n\r\n");
        else
            proxyToServerSocket.write(data);

        clientToProxySocket.pipe(proxyToServerSocket);
        proxyToServerSocket.pipe(clientToProxySocket);

        proxyToServerSocket.on("error", console.error);
        clientToProxySocket.on("error", console.error);
    });
});
server.on("error", console.error);
server.on("close", () => console.log("Client disconnected"));
server.listen(8080, () => console.log("Server listening on 0.0.0.0:8080"));

I tried to add this in place of the other writes to redirect, but it just returns an ERR_TUNNEL_CONNECTION_FAILED in Chrome:

if (host.includes('google.com')) {
    clientToProxySocket.write(
        "HTTP/1.1 301 Moved Permanently\t\r\n" +
        "Location: https://bing.com\r\n\r\n"
    );
}

Any help is greatly appreciated!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda