Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add firewall command #163

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 57 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ Access your Arris TG3442DE or Technicolor CGA4322DE, CGA6444VF (aka Vodafone Sta
[![npm](https://img.shields.io/npm/v/vodafone-station-cli)](https://www.npmjs.com/package/vodafone-station-cli)

<!-- toc -->
* [Features](#features)
* [Demo](#demo)
* [Supported hardware](#supported-hardware)
* [Software requirements](#software-requirements)
* [Notes](#notes)
* [Useful related projects:](#useful-related-projects)
* [Running from source](#running-from-source)
* [Usage](#usage)
* [Commands](#commands)
- [vodafone-station-cli](#vodafone-station-cli)
- [Features](#features)
- [Demo](#demo)
- [Supported hardware](#supported-hardware)
- [Software requirements](#software-requirements)
- [Notes](#notes)
- [Useful related projects:](#useful-related-projects)
- [Running from source](#running-from-source)
- [Usage](#usage)
- [Commands](#commands)
- [`vodafone-station-cli diagnose`](#vodafone-station-cli-diagnose)
- [`vodafone-station-cli discover`](#vodafone-station-cli-discover)
- [`vodafone-station-cli docsis`](#vodafone-station-cli-docsis)
- [`vodafone-station-cli firewall`](#vodafone-station-cli-firewall)
- [`vodafone-station-cli help [COMMAND]`](#vodafone-station-cli-help-command)
- [`vodafone-station-cli restart`](#vodafone-station-cli-restart)
<!-- tocstop -->

# Features
Expand Down Expand Up @@ -551,11 +558,22 @@ USAGE
<!-- usagestop -->
# Commands
<!-- commands -->
* [`vodafone-station-cli diagnose`](#vodafone-station-cli-diagnose)
* [`vodafone-station-cli discover`](#vodafone-station-cli-discover)
* [`vodafone-station-cli docsis`](#vodafone-station-cli-docsis)
* [`vodafone-station-cli help [COMMAND]`](#vodafone-station-cli-help-command)
* [`vodafone-station-cli restart`](#vodafone-station-cli-restart)
- [vodafone-station-cli](#vodafone-station-cli)
- [Features](#features)
- [Demo](#demo)
- [Supported hardware](#supported-hardware)
- [Software requirements](#software-requirements)
- [Notes](#notes)
- [Useful related projects:](#useful-related-projects)
- [Running from source](#running-from-source)
- [Usage](#usage)
- [Commands](#commands)
- [`vodafone-station-cli diagnose`](#vodafone-station-cli-diagnose)
- [`vodafone-station-cli discover`](#vodafone-station-cli-discover)
- [`vodafone-station-cli docsis`](#vodafone-station-cli-docsis)
- [`vodafone-station-cli firewall`](#vodafone-station-cli-firewall)
- [`vodafone-station-cli help [COMMAND]`](#vodafone-station-cli-help-command)
- [`vodafone-station-cli restart`](#vodafone-station-cli-restart)

## `vodafone-station-cli diagnose`

Expand All @@ -576,7 +594,7 @@ EXAMPLES
$ vodafone-station-cli diagnose
```

_See code: [src/commands/diagnose.ts](https://github.com/totev/vodafone-station-cli/blob/v1.2.8/src/commands/diagnose.ts)_
_See code: [src/commands/diagnose.ts](./src/commands/diagnose.ts)_

## `vodafone-station-cli discover`

Expand All @@ -593,7 +611,7 @@ EXAMPLES
$ vodafone-station-cli discover
```

_See code: [src/commands/discover.ts](https://github.com/totev/vodafone-station-cli/blob/v1.2.8/src/commands/discover.ts)_
_See code: [src/commands/discover.ts](./src/commands/discover.ts)_

## `vodafone-station-cli docsis`

Expand All @@ -616,7 +634,28 @@ EXAMPLES
{JSON data}
```

_See code: [src/commands/docsis.ts](https://github.com/totev/vodafone-station-cli/blob/v1.2.8/src/commands/docsis.ts)_
_See code: [src/commands/docsis.ts](./src/commands/docsis.ts)_

## `vodafone-station-cli firewall`

turn on/off the firewall

```
USAGE
$ vodafone-station-cli firewall [-a on|off] [-p <value>]

FLAGS
-a, --action=(on|off) firewall action
-p, --password=<value> router/modem password

DESCRIPTION
turn on/off the firewall

EXAMPLES
$ vodafone-station-cli firewall -a off -p PASSWORD
```

_See code: [src/commands/firewall.ts](./src/commands/firewall.ts)_

## `vodafone-station-cli help [COMMAND]`

Expand Down Expand Up @@ -656,5 +695,5 @@ EXAMPLES
$ vodafone-station-cli restart -p PASSWORD
```

_See code: [src/commands/restart.ts](https://github.com/totev/vodafone-station-cli/blob/v1.2.8/src/commands/restart.ts)_
_See code: [src/commands/restart.ts](./src/commands/restart.ts)_
<!-- commandsstop -->
57 changes: 57 additions & 0 deletions src/commands/firewall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Action } from './../../node_modules/rxjs/src/internal/scheduler/Action';
import {Flags} from '@oclif/core'
import Command from '../base-command'
import {discoverModemIp, ModemDiscovery} from '../modem/discovery'
import {modemFactory} from '../modem/factory'

export default class Restart extends Command {
static description =
'turn on/off the firewall';

static examples = [
'$ vodafone-station-cli firewall -a off -p PASSWORD',
];


static flags = {
action: Flags.enum({
char: 'a',
options: ['on', 'off'],
description: 'firewall action',
}),
password: Flags.string({
char: 'p',
description: 'router/modem password',
}),
};

async firewallAction(password: string, action:boolean): Promise<void> {
const modemIp = await discoverModemIp()
const discoveredModem = await new ModemDiscovery(modemIp, this.logger).discover()
const modem = modemFactory(discoveredModem, this.logger)
try {
await modem.login(password)
await modem.firewall(action);
} catch (error) {
this.log('Something went wrong.', error)
} finally {
action ? this.log('Firewall is now enabled') : this.log('Firewall is now disabled')
await modem.logout()
}
}

async run(): Promise<void> {
const {flags} = await this.parse(Restart)

const password: string = flags.password ?? process.env.VODAFONE_ROUTER_PASSWORD
const action: boolean = flags.action === "off" ? false : true
if (!password || password === '') {
this.log(
'You must provide a password either using -p or by setting the environment variable VODAFONE_ROUTER_PASSWORD'
)
this.exit()
}
await this.firewallAction(password!, action)
this.exit()
}
}
25 changes: 25 additions & 0 deletions src/modem/arris-modem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,31 @@ export class Arris extends Modem {
}
}

async firewall(status:boolean = true): Promise<void> {
const action = status ? 'on' : 'off'
try {
const {data} = await this.httpClient.post(
'api/v1/firewall',
{
FirewallLevel: action,
FirewallLevelV6: action
},
{
headers: {
csrfNonce: this.csrfNonce,
Referer: `http://${this.modemIp}/?status_docsis&mid=StatusDocsis`,
Connection: 'keep-alive',
},
}
)
this.logger.log('Firewall is enabled')
return data
} catch (error) {
this.logger.error('Could not enable firewall.', error)
throw error
}
}

_convertGetExposedHostSettings(settings: ArrisGetExposedHostSettings): ExposedHostSettings {
return {
serviceName: settings.ServiceName,
Expand Down
5 changes: 5 additions & 0 deletions src/modem/modem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface HostExposureSettings {
export interface GenericModem {
logout(): Promise<void>;
login(password: string): Promise<void>;
firewall(status?: boolean): Promise<void>;
docsis(): Promise<DocsisStatus>;
restart(): Promise<unknown>;
getHostExposure(): Promise<HostExposureSettings>;
Expand All @@ -96,6 +97,10 @@ export abstract class Modem implements GenericModem {
throw new Error('Method not implemented.')
}

firewall(_status: boolean = true): Promise<void> {
throw new Error('Method not implemented.')
}

logout(): Promise<void> {
throw new Error('Method not implemented.')
}
Expand Down
25 changes: 24 additions & 1 deletion src/modem/technicolor-modem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class Technicolor extends Modem {
'Referer': `http://${this.modemIp}`,
}
})

this.logger.debug('Token response: ', tokenResponse)
const {data: restartResponse} = await this.httpClient.post<TechnicolorBaseResponse>('api/v1/sta_restart',
'restart=Router%2CWifi%2CVoIP%2CDect%2CMoCA&ui_access=reboot_device', {
Expand All @@ -219,5 +219,28 @@ export class Technicolor extends Modem {
}
return restartResponse
}

async firewall(status: boolean = true): Promise<void> {
const action = status ? 'on' : 'off'
const { data: tokenResponse } = await this.httpClient.get<TechnicolorTokenResponse>('api/v1/session/init_page', {
headers: {
'Referer': `http://${this.modemIp}`,
}
})
this.logger.debug('Token response: ', tokenResponse)
const {data: restartResponse} = await this.httpClient.post<TechnicolorBaseResponse>('api/v1/firewall',
`FirewallLevel=${action}&FirewallLevelV6=${action}`, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-CSRF-TOKEN': tokenResponse.token,
'Referer': `http://${this.modemIp}`,
},
})

if (restartResponse?.error === 'error') {
this.logger.debug(restartResponse)
throw new Error(`Could not restart router: ${restartResponse.message}`)
}
}
}