Added --singleuser flag to server

replaces the old --serverwide-arl flag of deemix-pyweb
it's always off on the gui
This commit is contained in:
RemixDev 2022-03-09 12:34:18 +01:00
parent 79d602a490
commit c8f02b310d
10 changed files with 86 additions and 6 deletions

View File

@ -16,7 +16,7 @@ const PORT = process.env.DEEMIX_SERVER_PORT || argv.port
process.env.DEEMIX_SERVER_PORT = PORT process.env.DEEMIX_SERVER_PORT = PORT
process.env.DEEMIX_HOST = argv.host process.env.DEEMIX_HOST = argv.host
const server = new DeemixServer(argv.host, PORT) const server = new DeemixServer(argv.host, PORT, false)
server.init() server.init()
let win let win

View File

@ -0,0 +1,45 @@
import fs from 'fs'
// @ts-expect-error
import deemix from 'deemix'
import { LoginFile } from '../types'
const configFolder = deemix.utils.localpaths.getConfigFolder()
const DEFAULTS: LoginFile = {
accessToken: null,
arl: null
}
let loginData: LoginFile = {
accessToken: null,
arl: null
}
export function loadLoginCredentials() {
if (!fs.existsSync(configFolder)) fs.mkdirSync(configFolder)
if (!fs.existsSync(configFolder + 'login.json')) resetLoginCredentials()
try {
loginData = JSON.parse(fs.readFileSync(configFolder + 'login.json').toString())
} catch (e) {
if (e.name === 'SyntaxError') resetLoginCredentials()
}
}
export function getLoginCredentials(): LoginFile {
if (!loginData.arl) loadLoginCredentials()
return loginData
}
export function saveLoginCredentials(newLogin: LoginFile) {
if (newLogin.arl) loginData.arl = newLogin.arl
if (newLogin.accessToken) loginData.accessToken = newLogin.accessToken
if (!fs.existsSync(configFolder)) fs.mkdirSync(configFolder)
fs.writeFileSync(configFolder + 'login.json', JSON.stringify(loginData, null, 2))
}
export function resetLoginCredentials() {
if (!fs.existsSync(configFolder)) fs.mkdirSync(configFolder)
fs.writeFileSync(configFolder + 'login.json', JSON.stringify(DEFAULTS, null, 2))
loginData = JSON.parse(JSON.stringify(DEFAULTS))
}

View File

@ -10,13 +10,15 @@ if (!isModule) {
// TODO: Remove type assertion while keeping correct types // TODO: Remove type assertion while keeping correct types
const argv = yargs(hideBin(process.argv)).options({ const argv = yargs(hideBin(process.argv)).options({
port: { type: 'string', default: '6595' }, port: { type: 'string', default: '6595' },
host: { type: 'string', default: '127.0.0.1' } host: { type: 'string', default: '127.0.0.1' },
singleuser: { type: 'boolean', default: false }
}).argv as Arguments }).argv as Arguments
const DEEMIX_SERVER_PORT = process.env.DEEMIX_SERVER_PORT ?? argv.port const DEEMIX_SERVER_PORT = process.env.DEEMIX_SERVER_PORT ?? argv.port
const DEEMIX_HOST = process.env.DEEMIX_HOST ?? argv.host const DEEMIX_HOST = process.env.DEEMIX_HOST ?? argv.host
const IS_SINGLE_USER = !!argv.singleuser
const server = new DeemixServer(DEEMIX_HOST, DEEMIX_SERVER_PORT) const server = new DeemixServer(DEEMIX_HOST, DEEMIX_SERVER_PORT, IS_SINGLE_USER)
server.init() server.init()
} }

View File

@ -4,6 +4,7 @@ import { Deezer } from 'deezer-js'
import { sessionDZ } from '../../../app' import { sessionDZ } from '../../../app'
import { ApiHandler } from '../../../types' import { ApiHandler } from '../../../types'
import { logger } from '../../../helpers/logger' import { logger } from '../../../helpers/logger'
import { saveLoginCredentials, resetLoginCredentials } from '../../../helpers/loginStorage'
export interface RawLoginArlBody { export interface RawLoginArlBody {
arl: string arl: string
@ -24,6 +25,7 @@ const handler: RequestHandler<{}, {}, RawLoginArlBody, {}> = async (req, res, _)
if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer()
const deemix = req.app.get('deemix') const deemix = req.app.get('deemix')
const dz = sessionDZ[req.session.id] const dz = sessionDZ[req.session.id]
const isSingleUser = req.app.get('isSingleUser')
if (!req.body) { if (!req.body) {
return res.status(400).send() return res.status(400).send()
@ -69,7 +71,14 @@ const handler: RequestHandler<{}, {}, RawLoginArlBody, {}> = async (req, res, _)
currentChild: dz.selected_account currentChild: dz.selected_account
} }
if (response !== LoginStatus.NOT_AVAILABLE && response !== LoginStatus.FAILED) deemix.startQueue(dz) if (response !== LoginStatus.NOT_AVAILABLE && response !== LoginStatus.FAILED) {
deemix.startQueue(dz)
if (isSingleUser)
saveLoginCredentials({
accessToken: null,
arl: returnValue.arl
})
} else if (isSingleUser) resetLoginCredentials()
return res.status(200).send(returnValue) return res.status(200).send(returnValue)
} }

View File

@ -1,9 +1,11 @@
import { ApiHandler } from '../../../types' import { ApiHandler } from '../../../types'
import { getAccessToken, getArlFromAccessToken } from '../../../app' import { getAccessToken, getArlFromAccessToken } from '../../../app'
import { saveLoginCredentials } from '../../../helpers/loginStorage'
const path = '/loginEmail' const path = '/loginEmail'
const handler: ApiHandler['handler'] = async (req, res) => { const handler: ApiHandler['handler'] = async (req, res) => {
const isSingleUser = req.app.get('isSingleUser')
const { email, password } = req.body const { email, password } = req.body
let accessToken = req.body.accessToken let accessToken = req.body.accessToken
@ -14,6 +16,12 @@ const handler: ApiHandler['handler'] = async (req, res) => {
let arl let arl
if (accessToken) arl = await getArlFromAccessToken(accessToken) if (accessToken) arl = await getArlFromAccessToken(accessToken)
if (isSingleUser && accessToken)
saveLoginCredentials({
accessToken,
arl: arl || null
})
res.send({ accessToken, arl }) res.send({ accessToken, arl })
} }

View File

@ -2,12 +2,14 @@
import { Deezer } from 'deezer-js' import { Deezer } from 'deezer-js'
import { ApiHandler } from '../../../types' import { ApiHandler } from '../../../types'
import { sessionDZ } from '../../../app' import { sessionDZ } from '../../../app'
import { resetLoginCredentials } from '../../../helpers/loginStorage'
const path: ApiHandler['path'] = '/logout' const path: ApiHandler['path'] = '/logout'
const handler: ApiHandler['handler'] = (req, res) => { const handler: ApiHandler['handler'] = (req, res) => {
sessionDZ[req.session.id] = new Deezer() sessionDZ[req.session.id] = new Deezer()
res.send({ logged_out: true }) res.send({ logged_out: true })
if (req.app.get('isSingleUser')) resetLoginCredentials()
} }
const apiHandler: ApiHandler = { path, handler } const apiHandler: ApiHandler = { path, handler }

View File

@ -2,6 +2,7 @@ import express from 'express'
// @ts-expect-error // @ts-expect-error
import { Deezer } from 'deezer-js' import { Deezer } from 'deezer-js'
import { logger } from '../helpers/logger' import { logger } from '../helpers/logger'
import { getLoginCredentials } from '../helpers/loginStorage'
import { sessionDZ, deemixVersion, currentVersion } from '../app' import { sessionDZ, deemixVersion, currentVersion } from '../app'
const router = express.Router() const router = express.Router()
@ -11,6 +12,7 @@ router.get('/connect', async (req, res) => {
if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer()
const dz = sessionDZ[req.session.id] const dz = sessionDZ[req.session.id]
const deemix = req.app.get('deemix') const deemix = req.app.get('deemix')
const isSingleUser = req.app.get('isSingleUser')
if (!update) { if (!update) {
logger.info(`Currently running deemix-gui version ${currentVersion}`) logger.info(`Currently running deemix-gui version ${currentVersion}`)
@ -30,6 +32,8 @@ router.get('/connect', async (req, res) => {
settingsData: deemix.getSettings() settingsData: deemix.getSettings()
} }
if (isSingleUser && result.autologin) result.singleUser = getLoginCredentials()
if (result.settingsData.settings.autoCheckForUpdates) result.checkForUpdates = true if (result.settingsData.settings.autoCheckForUpdates) result.checkForUpdates = true
const queue = deemix.getQueue() const queue = deemix.getQueue()

View File

@ -10,6 +10,7 @@ import { getErrorCb, getListeningCb } from './helpers/server-callbacks'
import { registerApis } from './routes/api/register' import { registerApis } from './routes/api/register'
import { registerWebsocket } from './websocket' import { registerWebsocket } from './websocket'
import { logger, removeOldLogs } from './helpers/logger' import { logger, removeOldLogs } from './helpers/logger'
import { loadLoginCredentials } from './helpers/loginStorage'
import { Port, Listener } from './types' import { Port, Listener } from './types'
import { DeemixApp } from './app' import { DeemixApp } from './app'
import { normalizePort } from './helpers/port' import { normalizePort } from './helpers/port'
@ -17,15 +18,17 @@ import { normalizePort } from './helpers/port'
export class DeemixServer { export class DeemixServer {
host: string host: string
port: Port port: Port
isSingleUser: boolean
wss: WsServer wss: WsServer
app: Application app: Application
server: Server server: Server
deemixApp: DeemixApp deemixApp: DeemixApp
constructor(host: string, port: string) { constructor(host: string, port: string, singleuser: boolean = false) {
this.host = host this.host = host
this.port = normalizePort(port) this.port = normalizePort(port)
this.isSingleUser = singleuser
this.wss = new WsServer({ noServer: true }) this.wss = new WsServer({ noServer: true })
this.app = express() this.app = express()
@ -45,11 +48,13 @@ export class DeemixServer {
} }
this.deemixApp = new DeemixApp(listener) this.deemixApp = new DeemixApp(listener)
if (this.isSingleUser) loadLoginCredentials()
} }
init() { init() {
const debug = initDebug('deemix-gui:server') const debug = initDebug('deemix-gui:server')
this.app.set('deemix', this.deemixApp) this.app.set('deemix', this.deemixApp)
this.app.set('isSingleUser', this.isSingleUser)
/* === Middlewares === */ /* === Middlewares === */
registerMiddlewares(this.app) registerMiddlewares(this.app)

View File

@ -243,3 +243,8 @@ export interface Arguments {
export interface Listener { export interface Listener {
send: (key: string, data?: any) => void send: (key: string, data?: any) => void
} }
export interface LoginFile {
arl: string | null
accessToken: string | null
}

2
webui

@ -1 +1 @@
Subproject commit 283bb5fa0df7042c78919f64c1c6638996ca6ac8 Subproject commit f4f4debf7f905ab1c64a0af5f393e554ddfb427e