2020-09-12 17:11:13 +02:00
|
|
|
<template>
|
2020-12-16 12:16:43 +01:00
|
|
|
<view-base
|
|
|
|
:queries="queries" @queries-response="formatFirewallData"
|
|
|
|
ref="view" skeleton="card-form-skeleton"
|
|
|
|
>
|
2020-09-12 17:11:13 +02:00
|
|
|
<!-- PORTS -->
|
2020-12-16 12:16:43 +01:00
|
|
|
<card :title="$t('ports')" icon="shield">
|
2020-09-12 17:11:13 +02:00
|
|
|
<div v-for="(items, protocol) in protocols" :key="protocol">
|
|
|
|
<h5>{{ $t(protocol) }}</h5>
|
2020-12-16 12:16:43 +01:00
|
|
|
|
2020-09-12 17:11:13 +02:00
|
|
|
<b-table
|
|
|
|
:fields="fields" :items="items"
|
2020-12-16 12:16:43 +01:00
|
|
|
small striped responsive
|
2020-09-12 17:11:13 +02:00
|
|
|
>
|
|
|
|
<!-- PORT CELL -->
|
2020-12-16 12:16:43 +01:00
|
|
|
<template #cell(port)="data">
|
2020-09-12 17:11:13 +02:00
|
|
|
{{ data.value }}
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<!-- CONNECTIONS CELL -->
|
2020-12-16 12:16:43 +01:00
|
|
|
<template #cell()="data">
|
2020-09-12 17:11:13 +02:00
|
|
|
<b-checkbox
|
|
|
|
v-if="data.field.key !== 'uPnP'"
|
|
|
|
class="on-off-switch"
|
|
|
|
v-model="data.value"
|
|
|
|
switch
|
2020-12-16 12:16:43 +01:00
|
|
|
@change="onTablePortToggling(data.item.port, protocol, data.field.key, data.index, $event)"
|
2020-09-12 17:11:13 +02:00
|
|
|
>
|
|
|
|
<span :class="'btn btn-sm py-0 btn-' + (data.value ? 'danger' : 'success')">
|
|
|
|
{{ $t(data.value ? 'close' : 'open') }}
|
|
|
|
</span>
|
|
|
|
</b-checkbox>
|
|
|
|
|
|
|
|
<icon
|
|
|
|
v-else
|
|
|
|
:iname="data.value ? 'check' : 'times'"
|
|
|
|
:class="data.value ? 'text-success' : 'text-danger'"
|
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
</b-table>
|
|
|
|
</div>
|
2020-12-16 12:16:43 +01:00
|
|
|
</card>
|
2020-09-12 17:11:13 +02:00
|
|
|
|
|
|
|
<!-- OPERATIONS -->
|
2020-12-16 12:16:43 +01:00
|
|
|
<card-form
|
|
|
|
:title="$t('operations')" icon="cogs"
|
|
|
|
:validation="$v" :server-error="serverError"
|
|
|
|
@submit.prevent="onFormPortToggling"
|
|
|
|
inline form-classes="d-flex justify-content-between align-items-start"
|
|
|
|
>
|
|
|
|
<b-input-group :prepend="$t('action')">
|
|
|
|
<b-select v-model="form.action" :options="actionChoices" />
|
|
|
|
</b-input-group>
|
2020-09-12 17:11:13 +02:00
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
<form-field :validation="$v.form.port">
|
2020-09-12 17:11:13 +02:00
|
|
|
<b-input-group :prepend="$t('port')">
|
2020-12-16 12:16:43 +01:00
|
|
|
<input-item
|
|
|
|
id="input-port" placeholder="0" type="number"
|
|
|
|
v-model="form.port"
|
2020-09-12 17:11:13 +02:00
|
|
|
/>
|
|
|
|
</b-input-group>
|
2020-12-16 12:16:43 +01:00
|
|
|
</form-field>
|
2020-09-12 17:11:13 +02:00
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
<b-input-group :prepend="$t('connection')">
|
|
|
|
<b-select v-model="form.connection" :options="connectionChoices" id="input-connection" />
|
|
|
|
</b-input-group>
|
2020-09-12 17:11:13 +02:00
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
<b-input-group :prepend="$t('protocol')">
|
|
|
|
<b-select v-model="form.protocol" :options="protocolChoices" id="input-protocol" />
|
|
|
|
</b-input-group>
|
|
|
|
</card-form>
|
2020-09-12 17:11:13 +02:00
|
|
|
|
|
|
|
<!-- UPnP -->
|
2020-12-16 12:16:43 +01:00
|
|
|
<card :title="$t('upnp')" icon="exchange" :body-text-variant="upnpEnabled ? 'success' : 'danger'">
|
2020-09-12 17:11:13 +02:00
|
|
|
{{ $t(upnpEnabled ? 'upnp_enabled' : 'upnp_disabled' ) }}
|
|
|
|
|
|
|
|
<b-form-invalid-feedback :state="upnpError !== '' ? false : null">
|
|
|
|
{{ upnpError }}
|
|
|
|
</b-form-invalid-feedback>
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
<template #buttons>
|
|
|
|
<b-button @click="toggleUpnp" :variant="!upnpEnabled ? 'success' : 'danger'">
|
2020-11-30 14:49:13 +01:00
|
|
|
{{ $t(!upnpEnabled ? 'enable' : 'disable' ) }}
|
2020-09-12 17:11:13 +02:00
|
|
|
</b-button>
|
|
|
|
</template>
|
2020-12-16 12:16:43 +01:00
|
|
|
</card>
|
|
|
|
</view-base>
|
2020-09-12 17:11:13 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-12-16 12:16:43 +01:00
|
|
|
import { validationMixin } from 'vuelidate'
|
|
|
|
|
2020-10-12 17:36:47 +02:00
|
|
|
import api from '@/api'
|
2020-12-16 12:16:43 +01:00
|
|
|
import { required, integer, between } from '@/helpers/validators'
|
2020-09-12 17:11:13 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'ToolFirewall',
|
|
|
|
|
|
|
|
data () {
|
|
|
|
return {
|
2020-12-16 12:16:43 +01:00
|
|
|
queries: ['/firewall?raw'],
|
|
|
|
serverError: '',
|
|
|
|
|
|
|
|
// Ports tables data
|
2020-09-12 17:11:13 +02:00
|
|
|
fields: [
|
|
|
|
{ key: 'port', label: this.$i18n.t('port') },
|
|
|
|
{ key: 'ipv4', label: this.$i18n.t('ipv4') },
|
|
|
|
{ key: 'ipv6', label: this.$i18n.t('ipv6') },
|
|
|
|
{ key: 'uPnP', label: this.$i18n.t('upnp') }
|
|
|
|
],
|
|
|
|
protocols: undefined,
|
|
|
|
portToToggle: undefined,
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
// Ports form data
|
2020-09-12 17:11:13 +02:00
|
|
|
actionChoices: [
|
|
|
|
{ value: 'open', text: this.$i18n.t('open') },
|
|
|
|
{ value: 'close', text: this.$i18n.t('close') }
|
|
|
|
],
|
|
|
|
connectionChoices: [
|
|
|
|
{ value: 'ipv4', text: this.$i18n.t('ipv4') },
|
|
|
|
{ value: 'ipv6', text: this.$i18n.t('ipv6') }
|
|
|
|
],
|
|
|
|
protocolChoices: [
|
|
|
|
{ value: 'TCP', text: this.$i18n.t('tcp') },
|
|
|
|
{ value: 'UDP', text: this.$i18n.t('udp') },
|
|
|
|
{ value: 'Both', text: this.$i18n.t('both') }
|
|
|
|
],
|
|
|
|
form: {
|
|
|
|
action: 'open',
|
|
|
|
port: undefined,
|
|
|
|
connection: 'ipv4',
|
|
|
|
protocol: 'TCP'
|
|
|
|
},
|
|
|
|
|
|
|
|
// uPnP
|
|
|
|
upnpEnabled: undefined,
|
|
|
|
upnpError: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
validations: {
|
|
|
|
form: {
|
|
|
|
port: { number: required, integer, between: between(0, 65535) }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-09-12 17:11:13 +02:00
|
|
|
methods: {
|
2020-12-16 12:16:43 +01:00
|
|
|
formatFirewallData (data) {
|
|
|
|
const ports = Object.values(data).reduce((ports, protocols) => {
|
|
|
|
for (const type of ['TCP', 'UDP']) {
|
|
|
|
for (const port of protocols[type]) {
|
|
|
|
ports[type].add(port)
|
2020-09-12 17:11:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-16 12:16:43 +01:00
|
|
|
return ports
|
|
|
|
}, { TCP: new Set(), UDP: new Set() })
|
|
|
|
|
|
|
|
const tables = {
|
|
|
|
TCP: [],
|
|
|
|
UDP: []
|
|
|
|
}
|
|
|
|
for (const protocol of ['TCP', 'UDP']) {
|
|
|
|
for (const port of ports[protocol]) {
|
|
|
|
const row = { port }
|
|
|
|
for (const connection of ['ipv4', 'ipv6', 'uPnP']) {
|
|
|
|
row[connection] = data[connection][protocol].includes(port)
|
2020-09-12 17:11:13 +02:00
|
|
|
}
|
2020-12-16 12:16:43 +01:00
|
|
|
tables[protocol].push(row)
|
2020-09-12 17:11:13 +02:00
|
|
|
}
|
2020-12-16 12:16:43 +01:00
|
|
|
tables[protocol].sort((a, b) => a.port < b.port ? -1 : 1)
|
|
|
|
}
|
2020-09-12 17:11:13 +02:00
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
this.protocols = tables
|
|
|
|
this.upnpEnabled = data.uPnP.enabled
|
2020-09-12 17:11:13 +02:00
|
|
|
},
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
togglePort ({ action, port, protocol, connection }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.$askConfirmation(
|
|
|
|
this.$i18n.t('confirm_firewall_' + action, { port, protocol, connection })
|
|
|
|
).then(confirmed => {
|
|
|
|
if (confirmed) {
|
|
|
|
const method = action === 'open' ? 'post' : 'delete'
|
|
|
|
api[method](`/firewall/port?${connection}_only`, { port, protocol }).then(() => {
|
|
|
|
resolve(confirmed)
|
|
|
|
}).catch(error => {
|
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
resolve(confirmed)
|
|
|
|
}
|
|
|
|
})
|
2020-09-12 17:11:13 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
async toggleUpnp (value) {
|
|
|
|
const action = this.upnpEnabled ? 'disable' : 'enable'
|
|
|
|
const confirmed = await this.$askConfirmation(this.$i18n.t('confirm_upnp_' + action))
|
|
|
|
if (!confirmed) return
|
|
|
|
|
|
|
|
api.get('firewall/upnp?action=' + action).then(() => {
|
2020-09-12 17:11:13 +02:00
|
|
|
// FIXME Couldn't test when it works.
|
2020-12-16 12:16:43 +01:00
|
|
|
this.$refs.view.fetchQueries()
|
2020-09-12 17:11:13 +02:00
|
|
|
}).catch(err => {
|
|
|
|
this.upnpError = err.message
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
onTablePortToggling (port, protocol, connection, index, value) {
|
2020-09-12 17:11:13 +02:00
|
|
|
this.$set(this.protocols[protocol][index], connection, value)
|
2020-12-16 12:16:43 +01:00
|
|
|
const action = value ? 'open' : 'close'
|
|
|
|
this.togglePort({ action, port, protocol, connection }).then(toggled => {
|
|
|
|
// Revert change on cancel
|
|
|
|
if (!toggled) {
|
|
|
|
this.$set(this.protocols[protocol][index], connection, !value)
|
|
|
|
}
|
|
|
|
})
|
2020-09-12 17:11:13 +02:00
|
|
|
},
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
onFormPortToggling (e) {
|
|
|
|
this.togglePort(this.form).then(toggled => {
|
|
|
|
if (toggled) this.$refs.view.fetchQueries()
|
|
|
|
})
|
2020-09-12 17:11:13 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
mixins: [validationMixin]
|
2020-09-12 17:11:13 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
::v-deep .on-off-switch {
|
|
|
|
.custom-control-input {
|
|
|
|
&:checked ~ .custom-control-label::before {
|
|
|
|
border-color: $success;
|
|
|
|
background-color: $success;
|
|
|
|
}
|
|
|
|
&:not(:checked) ~ .custom-control-label {
|
|
|
|
&::before {
|
|
|
|
border-color: $danger;
|
|
|
|
background-color: $danger;
|
|
|
|
}
|
|
|
|
&::after {
|
|
|
|
background-color: $white;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input:focus ~ .custom-control-label, &:hover {
|
|
|
|
span {
|
|
|
|
visibility: visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
span {
|
|
|
|
visibility: hidden;
|
|
|
|
@include media-breakpoint-down(xs) {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
::v-deep form {
|
2020-09-12 17:11:13 +02:00
|
|
|
margin-bottom: -1rem;
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
& > * {
|
|
|
|
margin-bottom: 1rem;
|
2020-09-12 17:11:13 +02:00
|
|
|
}
|
|
|
|
|
2020-12-16 12:16:43 +01:00
|
|
|
@include media-breakpoint-down(xs) {
|
|
|
|
fieldset {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 17:11:13 +02:00
|
|
|
}
|
|
|
|
</style>
|