javascriptnode.jsformattingproxies

How can I format 51.xx.xx.xx:33xxx:user:pass into user:pass@51.xx.xx.xx:33xxx


This is what I have so far but it returns only one proxy because it rewrites over it x (however many proxies) times. I do not want to make a new file but instead rewrite proxies.txt with every proxy.

const fs = require("fs");

const formatProxies = () => {
  const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
  const split = rawProxies.trim().split("\n");

  for (const p of split) {
    const parts = p.trim().split(":");
    const [ip, port, user, pass] = parts;
    fs.writeFileSync(
      "./proxies.txt",
      user + ":" + pass + "@" + ip + ":" + port + "\r\n",
      { encoding: "utf8" }
    );
  }
};
formatProxies();

Solution

  • Does this work?

    const fs = require("fs");
    
    const formatProxies = () => {
      const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
      const split = rawProxies.trim().split("\n");
    
      const lines = []
      for (const p of split) {
        const parts = p.trim().split(":");
        const [ip, port, user, pass] = parts;
        lines.push(user + ":" + pass + "@" + ip + ":" + port)
      }
      fs.writeFileSync(
        "./proxies.txt",
        lines.join("\r\n"),
        { encoding: "utf8" }
      );
    };
    formatProxies();