ahmadawais / ptcl-cli

📟 Control PTCL router via command line. [Reboot, Get Stats, Screenshot xDSL for complaints].
https://twitter.com/MrAhmadAwais/
MIT License
11 stars 2 forks source link

Invalid credentials error on config #3

Open hayderameen opened 4 years ago

hayderameen commented 4 years ago

I just installed it, as it looks super useful. But I'm getting ERR_INVALID_AUTH_CREDENTIALS error in config. The credentials are working when I manually open router page in browser.

× UNHANDLED ERROR × ERROR → Error i REASON → net::ERR_INVALID_AUTH_CREDENTIALS at http://192.168.10.1/ i ERROR STACK ↓ Error: net::ERR_INVALID_AUTH_CREDENTIALS at http://192.168.10.1/ at navigate (C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\node_modules\puppeteer\lib\FrameManager.js:120:37) at processTicksAndRejections (internal/process/task_queues.js:97:5) at async FrameManager.navigateFrame (C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\node_modules\puppeteer\lib\FrameManager.js:94:17) at async Frame.goto (C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\node_modules\puppeteer\lib\FrameManager.js:406:12) at async Page.goto (C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\node_modules\puppeteer\lib\Page.js:672:12) at async C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\index.js:90:2 -- ASYNC -- at Frame. (C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\node_modules\puppeteer\lib\helper.js:111:15) at Page.goto (C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\node_modules\puppeteer\lib\Page.js:672:49) at Page. (C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\node_modules\puppeteer\lib\helper.js:112:23) at C:\Users\Hayder Ameen\AppData\Roaming\npm\node_modules\ptcl-cli\index.js:90:13 at processTicksAndRejections (internal/process/task_queues.js:97:5)

ahmadawais commented 4 years ago

Do you access your router with /192.168.10.1/ http://192.168.10.1/ if not then it won't work for you. I'll add the ability to change the IP route but I don't have access to every dashboard so can't be sure if it'll work there.

hayderameen commented 4 years ago

Screenshot_20200331-222628 Yes, it is 192.168.10.1. See screenshot.

ahmadawais commented 4 years ago

Well, run the config command. Save the password and username, then run without the headless mode and see how it works and where it breaks. It should open a Chrome browser, you should not click anything there, it will work automatically. That's how you can debug it.

🙌

AHMAD AWAIS https://twitter.com/MrAhmadAwais/ ❯ Award-winning Open Source Dev Advocate ❯ Node.js Foundation Community Committee Member ❯ Google Developers Expert for Web Tech ❯ WordPress Core Developer ❯ EE-CS Engineer ❯ Self-confessed tech comedian ;)

→ Edutaining devs at AhmadAwais.com https://ahmadawais.com/

Twitter https://twitter.com/MrAhmadAwais/ | GitHub https://github.com/AhmadAwais | Facebook https://www.facebook.com/ahmadawais | LinkedIn https://www.linkedin.com/in/MrAhmadAwais/ → I've released a course on VSCode.pro https://vscode.pro/?utm_source=EmailFooter

On Tue, Mar 31, 2020 at 10:29 PM Hayder Ameen notifications@github.com wrote:

[image: Screenshot_20200331-222628] https://user-images.githubusercontent.com/17459984/78056673-f32c1180-739e-11ea-8557-0ade93d3d65e.jpg Yes, it is 192.168.10.1. See screenshot.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ahmadawais/ptcl-cli/issues/3#issuecomment-606766795, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHKNBIC3EEH6U5HQWAMU4LRKIR6HANCNFSM4LWYGYFQ .

hayderameen commented 4 years ago

Hey! I have run it both headless and without it. And when the chrome window loads, it does not load anything and automatically closes after like 2 seconds. Attached screenshot. (It does show 192.168.10.1 in address bar for solid 10ms before closing but I'm not fast enough to capture that :P).

Maybe I could be missing something else on my system? I really want this to work since it's a really cool idea. ptclcli

hayderameen commented 4 years ago

Disclaimer: I'm not as experienced as you so it is possible that my question might be entirely wrong. However, I am curious and I think you can give me a good answer. :)

Here in the code you are using identifiers for username and password if (!isLoggedIn) { spinner.start(${yellow(LOGIN)} attempt…); await page.type('#Frm_Username', user); await page.type('#Frm_Password', pass); await page.click('#LoginId'); spinner.succeed(${green(LOGIN)} successful); }

Now I thought maybe these are different for my DSL page with different software version.

Now how do you get these IDs from this popup? Since i cannot inspect element these or see in source code. I tried searching for it entire HTML page after it is loaded, but no luck either. Screenshot_2

ahmadawais commented 4 years ago

All it looks like is that you are entering the wrong password and username.

Make sure your Node is above v12.

hayderameen commented 4 years ago

Node version is 13.4.0. And the username/password are correct. 100% verified.

ahmadawais commented 4 years ago

Yeah, you see, you have a different router than I do. Mine has an auth form like this

Also, a good opportunity for you to contribute. Figure out how the puppeteer can fill that auth pop up and add a -p --popup option :)

ahmadawais commented 4 years ago

Helpers:

https://github.com/puppeteer/puppeteer/issues/418#issuecomment-328680346

https://github.com/puppeteer/puppeteer/issues/1608#issuecomment-352185043

You'll have to change the code and test it since without your router I can't.

ahmadawais commented 4 years ago

The image from my comment above.

hayderameen commented 4 years ago

Thanks! Looking into it.

hayderameen commented 4 years ago

I fixed the authorization by adding HTTP Header. Had to check frame name to see if page is loaded and logged in. waituntil changed to "networkidle2" because "domcontentloaded" did not work for frames.

`(async () => { // CLI. const [input] = cli.input; const reboot = cli.flags.reboot; const headless = cli.flags.headless; const screenshot = cli.flags.screenshot; const data = cli.flags.data;

init();
input === 'help' && (await cli.showHelp(0));
const configure = input === 'config';

// Should I run?
if (!configure && !reboot && !headless && !screenshot & !data) {
    spinner.fail(`${red(`Nops`)} You didn't define any options to run.`);
    spinner.info(`${green(`HELP`)} section below:`);
    await cli.showHelp(0);
    console.log();
    process.exit(0);
}

// Config.
const config = new Configstore(pkgJSON.name, {});
let user = config.get('user');
let pass = config.get('pass');

// Header for HTTP Auth box
let auth;

if ((!user && !pass) || configure) {
    const [errUsername, username] = await to(
        prompt({
            type: `input`,
            name: `username`,
            initial: `admin`,
            message: `Enter the username for 192.168.10.1?`
        })
    );
    handleError(`USERNAME`, errUsername);
    await shouldCancel(username);
    config.set('user', username.username);

    const [errPassword, password] = await to(
        prompt({
            type: `input`,
            name: `password`,
            initial: `admin`,
            message: `Enter the password for 192.168.10.1?`
        })
    );
    handleError(`PASSWORD`, errPassword);
    await shouldCancel(password);
    config.set('pass', password.password);

    // Get again.
    user = config.get('user');
    pass = config.get('pass');

    // Setting HTTP Header for Auth Popup - First Implementation
    auth = new Buffer(`${user}:${pass}`).toString('base64');
}

spinner.start(`${yellow(`BROWSER`)} starting…`);
const browser = await puppeteer.launch({
    headless: headless,
    userDataDir: 'data'
});
spinner.succeed(`${green(`BROWSER`)} started`);

spinner.start(`${yellow(`PAGE`)} opening…`);
const page = (await browser.pages())[0];
await page.setExtraHTTPHeaders({
    Authorization: `Basic ${auth}`
}); // Setting authorization http header

page.setViewport({width: 1000, height: 1000, deviceScaleFactor: 2});

await page.goto('http://192.168.10.1/', {
    timeout: 15000,
    waitUntil: 'networkidle2' // For some reasons I could not check frames loading with other method
    // waitUntil: 'domcontentloaded'
});

const [errIsLoggedIn, isLoggedIn] = await to(
    page.waitFor('div[id="EasyInstall"]', {
        timeout: 3000
    })
);
// Testing for this class in my DSL router page -> frame(logofrm)
const frame = page.frames().find(f => f.name() === 'logofrm');

spinner.succeed(`${green(`PAGE`)} open`);

if (!frame && !isLoggedIn) {
    spinner.start(`${yellow(`LOGIN`)} attempt…`);
    await page.type('#Frm_Username', user);
    await page.type('#Frm_Password', pass);
    await page.click('#LoginId');
    spinner.succeed(`${green(`LOGIN`)} successful`);
}

....

`

The config commands now work, logs in, loads the page and no errors. Now I am getting errors in commands like "--reboot

This is because the entire structure of router page is different compared to what you have coded.

I might add all of it later on. But Great Learning experience :)

P.S. This was my first time coding in such an app so forgot that you asked me to define a flag. I did it later offline but still getting hang of it :)

ahmadawais commented 4 years ago

I'm glad you're enjoying the experience. Since your router page is different I believe we'll have to create a complete separate file and a flag that runs that file and ignores the rest.

hayderameen commented 4 years ago

Oh, so that is doable. Loading different file with the flag. Thanks. I'll certainly look into it.

On Wed, Apr 1, 2020, 10:29 PM Ahmad Awais ⚡️ notifications@github.com wrote:

I'm glad you're enjoying the experience. Since your router page is different I believe we'll have to create a complete separate file and a flag that runs that file and ignores the rest.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ahmadawais/ptcl-cli/issues/3#issuecomment-607384320, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEFGWEEP7XSR54ZR45VH4NLRKN2JFANCNFSM4LWYGYFQ .