yunohost-admin/app/src/views/tool/ToolFirewall.vue

281 lines
7.7 KiB
Vue
Raw Normal View History

2020-09-12 17:11:13 +02:00
<template>
<view-base
2021-02-19 18:52:54 +01:00
:queries="queries" @queries-response="onQueriesResponse"
ref="view" skeleton="card-form-skeleton"
>
2020-09-12 17:11:13 +02:00
<!-- PORTS -->
<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-09-12 17:11:13 +02:00
<b-table
:fields="fields" :items="items"
small striped responsive
2020-09-12 17:11:13 +02:00
>
<!-- PORT CELL -->
<template #cell(port)="data">
2020-09-12 17:11:13 +02:00
{{ data.value }}
</template>
<!-- CONNECTIONS CELL -->
<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
@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>
</card>
2020-09-12 17:11:13 +02:00
<!-- OPERATIONS -->
<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
<form-field :validation="$v.form.port">
2020-09-12 17:11:13 +02:00
<b-input-group :prepend="$t('port')">
<input-item
id="input-port" placeholder="0" type="number"
v-model="form.port"
2020-09-12 17:11:13 +02:00
/>
</b-input-group>
</form-field>
2020-09-12 17:11:13 +02: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
<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 -->
<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>
<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>
</card>
</view-base>
2020-09-12 17:11:13 +02:00
</template>
<script>
import { validationMixin } from 'vuelidate'
2020-10-12 17:36:47 +02:00
import api from '@/api'
import { required, integer, between } from '@/helpers/validators'
2020-09-12 17:11:13 +02:00
export default {
name: 'ToolFirewall',
data () {
return {
2021-02-19 18:52:54 +01:00
queries: [
['GET', '/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,
// Ports form data
2020-09-12 17:11:13 +02:00
actionChoices: [
2021-03-22 20:30:04 +01:00
{ value: 'allow', text: this.$i18n.t('open') },
{ value: 'disallow', text: this.$i18n.t('close') }
2020-09-12 17:11:13 +02:00
],
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: {
2021-03-22 20:30:04 +01:00
action: 'allow',
2020-09-12 17:11:13 +02:00
port: undefined,
connection: 'ipv4',
protocol: 'TCP'
},
// uPnP
upnpEnabled: undefined,
upnpError: ''
}
},
validations: {
form: {
port: { number: required, integer, between: between(0, 65535) }
}
},
2020-09-12 17:11:13 +02:00
methods: {
2021-02-19 18:52:54 +01:00
onQueriesResponse (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
}
}
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
}
tables[protocol].push(row)
2020-09-12 17:11:13 +02:00
}
tables[protocol].sort((a, b) => a.port < b.port ? -1 : 1)
}
2020-09-12 17:11:13 +02:00
this.protocols = tables
this.upnpEnabled = data.uPnP.enabled
2020-09-12 17:11:13 +02:00
},
2021-03-22 20:30:04 +01:00
async togglePort ({ action, port, protocol, connection }) {
const confirmed = await this.$askConfirmation(
this.$i18n.t('confirm_firewall_' + action, { port, protocol, connection })
)
if (!confirmed) {
return Promise.resolve(confirmed)
}
2021-04-09 21:47:19 +02:00
const actionTrad = this.$i18n.t({ allow: 'open', disallow: 'close' }[action])
2021-03-22 20:30:04 +01:00
return api.put(
`firewall/${protocol}/${action}/${port}?${connection}_only`,
2021-04-09 21:47:19 +02:00
{},
{ key: 'firewall.ports', protocol, action: actionTrad, port, connection },
2021-03-22 20:30:04 +01:00
{ wait: false }
).then(() => confirmed)
2020-09-12 17:11:13 +02:00
},
async toggleUpnp (value) {
const action = this.upnpEnabled ? 'disable' : 'enable'
const confirmed = await this.$askConfirmation(this.$i18n.t('confirm_upnp_' + action))
if (!confirmed) return
2021-04-09 21:47:19 +02:00
api.put(
'firewall/upnp/' + action,
{},
{ key: 'firewall.upnp', action: this.$i18n.t(action) }
).then(() => {
2020-09-12 17:11:13 +02:00
// FIXME Couldn't test when it works.
this.$refs.view.fetchQueries()
2020-09-12 17:11:13 +02:00
}).catch(err => {
2021-02-19 18:52:54 +01:00
if (err.name !== 'APIBadRequestError') throw err
2020-09-12 17:11:13 +02:00
this.upnpError = err.message
})
},
onTablePortToggling (port, protocol, connection, index, value) {
2020-09-12 17:11:13 +02:00
this.$set(this.protocols[protocol][index], connection, value)
2021-03-22 20:30:04 +01:00
const action = value ? 'allow' : 'disallow'
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
},
onFormPortToggling (e) {
this.togglePort(this.form).then(toggled => {
if (toggled) this.$refs.view.fetchQueries()
})
2020-09-12 17:11:13 +02: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;
}
}
}
::v-deep form {
2020-09-12 17:11:13 +02:00
margin-bottom: -1rem;
& > * {
margin-bottom: 1rem;
2020-09-12 17:11:13 +02:00
}
@include media-breakpoint-down(xs) {
fieldset {
width: 100%;
}
}
2020-09-12 17:11:13 +02:00
}
</style>