OpenWrt LuCI login through proxy failing

I have a location with a server and proxies. Sometimes I reboot them doing it manually by Web interface. I'm tired of this, so decided to write a code, at first, to just log in.

I'm trying to access my OpenWrt router's LuCI web interface remotely through a proxy server. I'm using Node.js with Axios to achieve this, but my code isn't working. I suspect the problem might be related to either:

  • CSRF token: as I understoond LuCI uses CSRF protection, which requires a valid token to be included in the request so I'm unsure how to properly implement this in my code

  • Code errors: there are errors in my code that are preventing successful login

I've tried many ways, but only get "Request failed with status code 403". Here is an example of one of many tries:

const axios = require("axios"); // Import axios for making HTTP requests
const { HttpProxyAgent } = require("http-proxy-agent"); // For using a proxy
const cheerio = require("cheerio"); // For parsing HTML to extract CSRF token

// Configure the proxy agent with proxy details
const httpAgent = new HttpProxyAgent(
  `link`, // Proxy link (e.g.,, original proxy has local adress
    rejectUnauthorized: false,

// Function to fetch the CSRF token from the login page
async function getCSRFToken(url) {
  try {
    const response = await axios.get(url, {
      httpAgent, // Use the configured proxy agent
      proxy: false, // Disable axios's built-in proxy handling
      headers: {
        // Set a user-agent header to mimic a browser
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36",

    // Parse the HTML response using cheerio
    const $ = cheerio.load(;
    const tokenInput = $("input[name='token']"); // Find the input field with name 'token'
    return tokenInput.attr("value"); // Extract the CSRF token value
  } catch (err) {
    console.error("Error fetching CSRF token:", err.message);
    return null; // Return null if there's an error

// Main function to perform the login
(async () => {
  try {
    const loginUrl = ""; // URL of the OpenWrt router's login page

    // Get the CSRF token
    const csrfToken = await getCSRFToken(loginUrl);

    // Check if the token was obtained successfully
    if (!csrfToken) {
      throw new Error("Failed to obtain CSRF token");

    // Create a FormData object to hold login credentials and CSRF token
    const bodyFormData = new FormData();
    bodyFormData.append("luci_username", "root"); // Replace with your username
    bodyFormData.append("luci_password", "admin"); // Replace with your password
    bodyFormData.append("token", csrfToken); // Add the CSRF token

    // Send the POST request to the login URL
    const response = await, bodyFormData, {
      httpAgent, // Use the proxy agent
      proxy: false,
      headers: {
        ...bodyFormData.getHeaders(), // Include headers from FormData
        // Set the user-agent header again
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36",

    console.log("Login successful:",; // Log the response if successful
  } catch (err) {
    console.error("Login failed:", err.message); // Log the error message if login fails

I am beginner to programming and working with proxies so I would appreciate any help!