Skip to content

Commit

Permalink
chore: removed console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
totev committed Mar 19, 2021
1 parent 4a106cc commit 0ce9656
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 25 deletions.
11 changes: 6 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": ["oclif", "oclif-typescript"],
"rules": {
"no-console": "off"
}
}
"extends": [
"oclif",
"oclif-typescript"
],
"rules": {}
}
5 changes: 3 additions & 2 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {CryptoVars} from './html-parser'
import {CliClient} from './client'
import {CryptoVars} from './html-parser'
import {ConsoleLogger} from './logger'

describe('client', () => {
test('should encrypt', () => {
Expand All @@ -15,7 +16,7 @@ describe('client', () => {
sessionId: '01a91cedd129fd8c6f18e3a1b58d096f',
nonce: 'WslSZgE7NuQr+1BMqiYEOBMzQlo=',
}
const cliClient = new CliClient('0.0.0.0')
const cliClient = new CliClient('0.0.0.0', new ConsoleLogger())
expect(cliClient.encryptPassword('test', given)).toEqual(expected)
})
})
30 changes: 15 additions & 15 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
extractCryptoVars,
extractDocsisStatus,
} from './html-parser'
import {Log} from './logger'

// axios cookie support
axiosCookieJarSupport(axios)
Expand All @@ -32,7 +33,7 @@ export class CliClient {

private readonly httpClient: AxiosInstance

constructor(private readonly modemIp: string) {
constructor(private readonly modemIp: string, private readonly logger: Log) {
this.cookieJar = new CookieJar()
this.httpClient = this.initAxios()
}
Expand All @@ -48,16 +49,16 @@ export class CliClient {
async login(password: string) {
const cryptoVars = await this.getCurrentCryptoVars()
const encPw = this.encryptPassword(password, cryptoVars)
console.debug('Encrypted password: ', encPw)
this.logger.debug('Encrypted password: ', encPw)
const serverSetPassword = await this.createServerRecord(encPw)
console.debug('ServerSetPassword: ', serverSetPassword)
this.logger.debug('ServerSetPassword: ', serverSetPassword)

const csrfNonce = this.loginPasswordCheck(
serverSetPassword.encryptData,
cryptoVars,
deriveKey(password, cryptoVars.salt)
)
console.debug('Csrf nonce: ', csrfNonce)
this.logger.debug('Csrf nonce: ', csrfNonce)

await this.addCredentialToCookie()
return csrfNonce
Expand All @@ -69,10 +70,10 @@ export class CliClient {
headers: {Accept: 'text/html,application/xhtml+xml,application/xml'},
})
const cryptoVars = extractCryptoVars(data)
console.debug('Parsed crypto vars: ', cryptoVars)
this.logger.debug('Parsed crypto vars: ', cryptoVars)
return cryptoVars
} catch (error) {
console.error('Could not get the index page from the router', error)
this.logger.error('Could not get the index page from the router', error)
throw error
}
}
Expand Down Expand Up @@ -115,14 +116,14 @@ export class CliClient {
// { p_status: 'Lockout', p_waitTime: 1 }
return data
} catch (error) {
console.error('Could not set password on remote router.', error)
this.logger.error('Could not set password on remote router.', error)
throw error
}
}

async addCredentialToCookie() {
const credential = await this.fetchCredential()
console.debug('Credential: ', credential)
this.logger.debug('Credential: ', credential)
// set obligatory static cookie
this.cookieJar.setCookie(`credential= ${credential}`, `http://${this.modemIp}`)
}
Expand All @@ -132,7 +133,7 @@ export class CliClient {
const {data} = await this.httpClient.get('/base_95x.js')
return extractCredentialString(data)
} catch (error) {
console.error('Could not fetch credential.', error)
this.logger.error('Could not fetch credential.', error)
throw error
}
}
Expand All @@ -151,8 +152,7 @@ export class CliClient {
})
return extractDocsisStatus(data)
} catch (error) {
console.error('Could not fetch remote docsis status'
)
this.logger.error('Could not fetch remote docsis status', error)
throw error
}
}
Expand All @@ -173,21 +173,21 @@ export class CliClient {
},
}
)
console.info('Router is restarting')
this.logger.log('Router is restarting')
return data
} catch (error) {
console.error('Could not restart router.', error)
this.logger.error('Could not restart router.', error)
throw error
}
}

async logout(): Promise<boolean> {
try {
console.info('Logging out...')
this.logger.log('Logging out...')
await this.httpClient.post('/php/logout.php')
return true
} catch (error) {
console.error('Could not do a full session logout', error)
this.logger.error('Could not do a full session logout', error)
throw error
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/docsis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {promises as fsp} from 'fs'
import Command from '../base-command'
import {CliClient} from '../client'
import {discoverModemIp} from '../discovery'
import {OclifLogger} from '../logger'

export default class Docsis extends Command {
static description =
Expand All @@ -26,7 +27,7 @@ JSON data
};

async getDocsisStatus(password: string) {
const cliClient = new CliClient(await discoverModemIp())
const cliClient = new CliClient(await discoverModemIp(), new OclifLogger(this.log, this.warn, this.debug, this.error))
try {
const csrfNonce = await cliClient.login(password)
return cliClient.fetchDocsisStatus(csrfNonce)
Expand Down
3 changes: 2 additions & 1 deletion src/commands/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {flags} from '@oclif/command'
import Command from '../base-command'
import {CliClient} from '../client'
import {discoverModemIp} from '../discovery'
import {OclifLogger} from '../logger'

export default class Restart extends Command {
static description =
Expand All @@ -19,7 +20,7 @@ export default class Restart extends Command {
};

async restartRouter(password: string) {
const cliClient = new CliClient(await discoverModemIp())
const cliClient = new CliClient(await discoverModemIp(), new OclifLogger(this.log, this.warn, this.debug, this.error))
try {
const csrfNonce = await cliClient.login(password)
await cliClient.restart(csrfNonce)
Expand Down
2 changes: 1 addition & 1 deletion src/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export async function discoverModemIp(): Promise<string> {
try {
const result = await Promise.any([axios.head(`http://${BRIDGED_MODEM_IP}`), axios.head(`http://${ROUTER_IP}`)])
const hostIp = result.request?.host
console.debug(`Found potential router/modem under ${hostIp}`)
return hostIp
} catch (error) {
// eslint-disable-next-line no-console
console.error('Could not find a router/modem under the known addresses', error)
throw error
}
Expand Down
51 changes: 51 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable no-console */

export interface Log{
log(message?: string, ...args: any[]): void;
warn(input: string | Error): void;
debug(...args: any[]): void;
error(input: string | Error, options: {
code?: string;
exit: false;
}): void;
}

export class OclifLogger implements Log {
// eslint-disable-next-line no-useless-constructor
constructor(private delegateLog: Function, private delegateWarn: Function, private delegateDebug: Function, private delegateError: Function) {
}

error(input: string | Error, options: { code?: string | undefined; exit: false }): void {
this.delegateError(input, options)
}

log(message?: string, ...args: any[]): void {
this.delegateLog(message, args)
}

warn(input: string | Error): void {
this.delegateWarn(input)
}

debug(...args: any[]): void {
this.delegateDebug(args)
}
}

export class ConsoleLogger implements Log {
error(input: string | Error, options: { code?: string | undefined; exit: false }): void {
console.log(input, options)
}

debug(...args: any[]): void {
console.debug(args)
}

warn(input: string | Error): void {
console.warn(input)
}

log(message?: string, ...args: any[]): void{
console.log(message, args)
}
}

0 comments on commit 0ce9656

Please sign in to comment.