Skip to content

Commit

Permalink
fix: cookie storage permission issues on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ifedapoolarewaju committed Jun 10, 2017
1 parent ce9b7cc commit 140fc69
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 44 deletions.
51 changes: 18 additions & 33 deletions igdm.js → instagram.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
const fs = require('fs');
const Client = require('instagram-private-api').V1;
let storage, device;
const utils = require('./utils');

exports.checkAuth = function () {
exports.checkAuth = function (session) {
return new Promise((resolve, reject) => {
let files = fs.readdirSync(`${__dirname}/cookies`);

if (files.length && files[0].endsWith('.json')) {
let filename = files[0]
device = new Client.Device(filename.slice(0, -5)); // remove .json extension
storage = new Client.CookieFileStorage(`${__dirname}/cookies/${filename}`);

const session = new Client.Session(device, storage);
// see if IG allows this session to make request
return session.getAccountId()
.then(() => resolve({ isLoggedIn: true, session }))
.catch(Client.Exceptions.CookieNotValidError, () => resolve({ isLoggedIn: false }))
} else {
resolve({ isLoggedIn: false })
if (!session) {
const device = utils.getDevice();
const storage = utils.getCookieStorage();
if (!device || !storage) {
return resolve({ isLoggedIn: false });
}
session = new Client.Session(device, storage);
}
})

session.getAccountId()
.then(() => resolve({ isLoggedIn: true, session }))
.catch(Client.Exceptions.CookieNotValidError, () => resolve({ isLoggedIn: false }))
});
}

exports.login = function (username, password) {
// delete all session storage
let files = fs.readdirSync(`${__dirname}/cookies`);
files.forEach((filename) => {
fs.unlinkSync(`${__dirname}/cookies/${filename}`);
})

utils.clearCookieFiles();
return new Promise((resolve, reject) => {
// create file
device = new Client.Device(username);
storage = new Client.CookieFileStorage(`${__dirname}/cookies/${username}.json`);

const device = utils.getDevice(username);
const storage = utils.getCookieStorage(`${__dirname}/cookies/${username}.json`);
Client.Session.create(device, storage, username, password).then(resolve).catch(reject)
})
}

exports.logout = function () {
// delete all session storage
let files = fs.readdirSync(`${__dirname}/cookies`);
files.forEach((filename) => {
fs.unlinkSync(`${__dirname}/cookies/${filename}`);
})
utils.clearCookieFiles();
}

exports.getChatList = function (session) {
Expand Down
22 changes: 11 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const menuTemplate = require('./menutemplate');
const BrowserWindow = electron.BrowserWindow;
const path = require('path');
const url = require('url');
const igdm = require('./igdm');
const instagram = require('./instagram');
const autoUpdater = require('./autoupdater');
const notifier = require('node-notifier');
let pollingInterval = 10000;
Expand Down Expand Up @@ -39,7 +39,7 @@ function createWindow () {
}
mainWindow.setTitle('Desktop IG:dm')

igdm.checkAuth().then((result) => {
instagram.checkAuth(session).then((result) => {
let view = result.isLoggedIn ? 'browser/index.html' : 'browser/login.html'
session = result.session || session

Expand All @@ -54,7 +54,7 @@ function createWindow () {
}

function getChatList () {
igdm.getChatList(session).then((chats) => {
instagram.getChatList(session).then((chats) => {
mainWindow.webContents.send('chatList', chats)

setTimeout(getChatList, pollingInterval);
Expand All @@ -63,7 +63,7 @@ function getChatList () {

let timeoutObj;
function getChat (evt, id) {
igdm.getChat(session, id).then((chat) => {
instagram.getChat(session, id).then((chat) => {
mainWindow.webContents.send('chat', chat);

if (timeoutObj) clearTimeout(timeoutObj)
Expand Down Expand Up @@ -103,14 +103,14 @@ app.on('browser-window-focus', () => {
})

electron.ipcMain.on('login', (evt, data) => {
igdm.login(data.username, data.password).then((session_) => {
instagram.login(data.username, data.password).then((session_) => {
session = session_
createWindow()
}).catch(() => createWindow())
})

electron.ipcMain.on('logout', (evt, data) => {
igdm.logout()
instagram.logout()
session = null
createWindow()
})
Expand All @@ -125,19 +125,19 @@ electron.ipcMain.on('getChatList', getChatList)
electron.ipcMain.on('getChat', getChat)

electron.ipcMain.on('message', (evt, data) => {
igdm.sendMessage(session, data.message, data.users).then((chat) => {
instagram.sendMessage(session, data.message, data.users).then((chat) => {
if (data.isNewChat) getChat(null, chat[0].id)
})
})

electron.ipcMain.on('searchUsers', (evt, search) => {
igdm.searchUsers(session, search).then((users) => {
instagram.searchUsers(session, search).then((users) => {
mainWindow.webContents.send('searchResult', users);
})
})

electron.ipcMain.on('markAsRead', (evt, thread) => {
igdm.seen(session, thread)
instagram.seen(session, thread)
})

electron.ipcMain.on('notify', (evt, message) => {
Expand All @@ -147,11 +147,11 @@ electron.ipcMain.on('notify', (evt, message) => {
})

electron.ipcMain.on('getUnfollowers', (evt) => {
igdm.getUnfollowers(session).then((users) => {
instagram.getUnfollowers(session).then((users) => {
mainWindow.webContents.send('unfollowers', users)
})
})

electron.ipcMain.on('unfollow', (evt, userId) => {
igdm.unfollow(session, userId)
instagram.unfollow(session, userId)
})
60 changes: 60 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require('fs');
const Client = require('instagram-private-api').V1;

const canUseFileStorage = () => {
try {
fs.accessSync(`${__dirname}/cookies/`, fs.W_OK);
return true
} catch (error) {
return false
}
}

const guessUsername = () => {
let username;
if (canUseFileStorage()) {
const files = fs.readdirSync(`${__dirname}/cookies`);
if (files.length && files[0].endsWith('.json')) {
username = files[0].slice(0, -5);
}
}
return username;
}

const getCookieStorage = (filePath) => {
let storage;
let username;

if (canUseFileStorage()) {
if (!filePath && (username = guessUsername())) {
filePath = `${__dirname}/cookies/${username}.json`
}

if (filePath) storage = new Client.CookieFileStorage(filePath);
} else {
storage = new Client.CookieMemoryStorage();
}

return storage;
}

const clearCookieFiles = () => {
// delete all session storage
if (canUseFileStorage()) {
fs.readdirSync(`${__dirname}/cookies`).forEach((filename) => {
fs.unlinkSync(`${__dirname}/cookies/${filename}`);
})
}
}

const getDevice = (username) => {
let device;
username = username || guessUsername();
if (username) device = new Client.Device(username);
return device;
}

module.exports = {
canUseFileStorage, guessUsername,
getCookieStorage, clearCookieFiles, getDevice
}

0 comments on commit 140fc69

Please sign in to comment.