Skip to content

Commit

Permalink
Define some types
Browse files Browse the repository at this point in the history
Fix undefined var cookies in getCookies
Update Chrome UserAgent

Signed-off-by: Kazakov Stepan <[email protected]>
  • Loading branch information
ksrt12 committed Apr 16, 2022
1 parent 07a069d commit 7c53056
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ npm install *full path to cloned repo folder*

## Changelog

Version 2.0.3 17/04/22:

- Define some types
- Fix undefined var cookies in getCookies
- Update Chrome UserAgent

Version 2.0.2 14/04/22:

- Move some code out of async function
Expand Down
26 changes: 23 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,35 @@ interface device {
item_type: string;
groups: string[];
}
interface group {
id: string;
name: string;
type: string;
icon_url: string;
state: string;
capabilities: {
reportable: boolean;
retrievable: boolean;
type: string;
parameters: { split: boolean; };
state: {
instance: string;
value: boolean;
};
last_updated: number;
}[];
devices_count: number;
}

interface ansDevices extends ansBase {
rooms: {
id: string;
name: string;
devices: device[];
}[];
groups: any[];
groups: group[];
unconfigured_devices: any[];
speakers: any[];
speakers: device[];
}

interface capability {
Expand Down Expand Up @@ -189,4 +208,5 @@ interface myFetchParams extends fetchPutPostParams {
color: NodeStatusFill;
method: "GET" | "PUT" | "POST";
}
type myFetch = arrowPromise<myFetchParams, ansGet | ansBase>;
type ansFetch = ansGet | ansBase;
type myFetch = arrowPromise<myFetchParams, ansFetch>;
11 changes: 7 additions & 4 deletions nodes/alice-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function (/** @type {RED} */ RED) {
const defFunc = { SetStatus, SetError, Debug_Log };

const getCreds = () => creds.getCredentialsRED({ RED, id: node.login });
const updateCreds = newCreds => {
const updateCreds = (/** @type {aliceCredsAdd} */ newCreds) => {
creds.updateCredentialsRED({ RED, id: node.login, newCreds });
Object.entries(newCreds).forEach(([key, val]) => {
Debug_Log(`The value of ${key} has been set to ${val}. Update alice-login manual.`);
Expand Down Expand Up @@ -132,7 +132,6 @@ module.exports = function (/** @type {RED} */ RED) {

if (!is_cookies_set) {
cookies = await getCookies({ RED, id: node.login, ...defFunc });
Debug_Log("Куки получены!");
}

if (is(cookies)) {
Expand All @@ -144,8 +143,12 @@ module.exports = function (/** @type {RED} */ RED) {
/** @type {defFetchGet} */
const defFetchGet = { headers: { "Cookie": cookies }, ...defFunc };
/** @type {defFetchPost} */
const defFetchPost = defFetchGet;
defFetchPost.headers['x-csrf-token'] = csrf_token;
const defFetchPost = {
headers: {
"Cookie": cookies,
'x-csrf-token': csrf_token
}, ...defFunc
};

if (!is_speaker_set) {

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"type": "git",
"url": "git+https://github.com/AntonTumilovich/https-flows.nodered.org-node-node-red-contrib-yandex-alice-command.git"
},
"version": "2.0.2",
"version": "2.0.3",
"description": "yandex-alice-command system",
"main": "yandex-alice-command.js",
"scripts": {
Expand Down
16 changes: 8 additions & 8 deletions utils/getCookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const https = require("https");
const { is, checkStatus, creds } = require("./functions");

/** @type {getCookies} */
module.exports = async ({ forceCreds, RED, id, SetStatus, SetError }) => {
module.exports = async ({ forceCreds, RED, id, SetStatus, SetError, Debug_Log }) => {

let topic = "Get cookies";

const getCredentials = () => creds.getCredentialsRED({ RED, id });
const updateCredentials = (newCreds) => creds.updateCredentialsRED({ RED, id, newCreds });
const updateCredentials = (/** @type {aliceCredsAdd} */ newCreds) => creds.updateCredentialsRED({ RED, id, newCreds });

/** @type {aliceCreds} */
let currCreds = forceCreds || getCredentials();
let { username, password, cookies } = currCreds;
let { username, password } = forceCreds || getCredentials();
let cookies = "";

const agent = new https.Agent({ keepAlive: true });
const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36";
const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36";

SetStatus("blue", "ring", topic, "init");
let { csrf_token, process_uuid } = await fetch("https://passport.yandex.ru/auth?repath=https://yandex.ru",
Expand All @@ -32,7 +32,7 @@ module.exports = async ({ forceCreds, RED, id, SetStatus, SetError }) => {
let yaheaders = res.headers.raw();
yaheaders['set-cookie'].forEach(tmp => {
let tmp_cookie = tmp.substring(0, tmp.indexOf('; ')) + ";";
if (tmp_cookie.length > 8) {
if (is(tmp_cookie, 4)) {
cookies += tmp_cookie;
}
});
Expand Down Expand Up @@ -117,7 +117,7 @@ module.exports = async ({ forceCreds, RED, id, SetStatus, SetError }) => {
let headers = password_res.headers.raw();
headers['set-cookie'].forEach(tmp => {
let tmp_cookie = tmp.substring(0, tmp.indexOf('; ')) + ";";
if (tmp_cookie.length > 4) {
if (is(tmp_cookie, 4)) {
cookies += tmp_cookie;
}
});
Expand All @@ -127,9 +127,9 @@ module.exports = async ({ forceCreds, RED, id, SetStatus, SetError }) => {
}
})
.catch(err => SetError(topic, err));
/////////////// GET COOKIES End //////////////

if (is(final_cookies, 200)) {
Debug_Log("Куки получены!");
SetStatus("blue", "dot", topic, "ok");
updateCredentials({ cookies: final_cookies });
return final_cookies;
Expand Down
2 changes: 1 addition & 1 deletion utils/myFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const general = async ({ color, topic, url, method, headers, body, SetStatus, Se
return await fetch(url, opt)
.then(checkStatus)
.then((/** @type {Response} */ res) => res.json())
.then((/** @type {ansGet | ansBase}*/ ans) => {
.then((/** @type {ansFetch}*/ ans) => {
SetStatus(color, "dot", topic, ans.status);
if (ans.status === "ok") {
return ans;
Expand Down

0 comments on commit 7c53056

Please sign in to comment.