ovpn-admin/frontend/src/main.js

287 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-05-14 19:13:33 -04:00
import Vue from 'vue';
import axios from 'axios';
import VueCookies from 'vue-cookies'
2020-05-14 19:13:33 -04:00
import VueClipboard from 'vue-clipboard2'
2020-10-29 06:50:19 -04:00
import VueGoodTablePlugin from 'vue-good-table'
import 'vue-good-table/dist/vue-good-table.css'
2020-05-14 19:13:33 -04:00
Vue.use(VueClipboard)
2020-10-29 06:50:19 -04:00
Vue.use(VueGoodTablePlugin)
Vue.use(VueCookies)
2020-05-14 19:13:33 -04:00
var axios_cfg = function(url, data='', type='form') {
if (data == '') {
return {
method: 'get',
url: url
};
} else if (type == 'form') {
return {
method: 'post',
url: url,
data: data,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
} else if (type == 'file') {
return {
method: 'post',
url: url,
data: data,
headers: { 'Content-Type': 'multipart/form-data' }
};
2020-10-29 06:50:19 -04:00
} else if (type == 'json') {
return {
method: 'post',
url: url,
data: data,
headers: { 'Content-Type': 'application/json' }
};
2020-05-14 19:13:33 -04:00
}
};
new Vue({
el: '#app',
data: {
2020-10-29 06:50:19 -04:00
columns: [
{
label: 'Name',
field: 'Identity',
// filterable: true,
},
{
label: 'Account Status',
field: 'AccountStatus',
filterable: true,
},
{
label: 'Expiration Date',
field: 'ExpirationDate',
type: 'date',
dateInputFormat: 'yyyy-MM-dd HH:mm:ss',
dateOutputFormat: 'yyyy-MM-dd HH:mm:ss',
formatFn: function (value) {
return value != "" ? value : ""
}
},
{
label: 'Revocation Date',
field: 'RevocationDate',
type: 'date',
dateInputFormat: 'yyyy-MM-dd HH:mm:ss',
dateOutputFormat: 'yyyy-MM-dd HH:mm:ss',
formatFn: function (value) {
return value != "" ? value : ""
}
},
{
label: 'Actions',
field: 'actions',
sortable: false,
tdClass: 'text-right',
globalSearchDisabled: true,
},
],
rows: [],
actions: [
{
name: 'u-revoke',
label: 'Revoke',
showWhenStatus: 'Active'
},
{
name: 'u-unrevoke',
label: 'Unrevoke',
showWhenStatus: 'Revoked'
},
{
name: 'u-show-config',
label: 'Show config',
showWhenStatus: 'Active'
},
{
name: 'u-download-config',
label: 'Download config',
showWhenStatus: 'Active'
},
{
name: 'u-edit-ccd',
label: 'Edit routes',
showWhenStatus: 'Active'
}
],
filters: {
hideRevoked: true,
},
2020-05-14 19:13:33 -04:00
u: {
newUserName: '',
2020-10-29 06:50:19 -04:00
// newUserPassword: 'nopass',
newUserCreateError: '',
2020-05-14 19:13:33 -04:00
modalNewUserVisible: false,
modalShowConfigVisible: false,
2020-10-29 06:50:19 -04:00
modalShowCcdVisible: false,
openvpnConfig: '',
ccd: {
Name: '',
ClientAddress: '',
CustomRoutes: []
},
newRoute: {},
ccdApplyStatus: "",
ccdApplyStatusMessage: "",
2020-05-14 19:13:33 -04:00
}
},
watch: {
},
mounted: function () {
this.u_get_data();
this.filters.hideRevoked = this.$cookies.isKey('hideRevoked') ? (this.$cookies.get('hideRevoked') == "true") : false
2020-05-14 19:13:33 -04:00
},
created() {
var _this = this;
// if (!_this.$cookies.isKey('hideRevoked')) {
// _this.$cookies.set('hideRevoked', true, -1);
// }
2020-10-29 06:50:19 -04:00
_this.$root.$on('u-revoke', function (msg) {
2020-05-14 19:13:33 -04:00
var data = new URLSearchParams();
2020-10-29 06:50:19 -04:00
data.append('username', _this.username);
2020-05-14 19:13:33 -04:00
axios.request(axios_cfg('api/user/revoke', data, 'form'))
.then(function(response) {
_this.u_get_data();
});
})
2020-10-29 06:50:19 -04:00
_this.$root.$on('u-unrevoke', function () {
2020-05-14 19:13:33 -04:00
var data = new URLSearchParams();
2020-10-29 06:50:19 -04:00
data.append('username', _this.username);
2020-05-14 19:13:33 -04:00
axios.request(axios_cfg('api/user/unrevoke', data, 'form'))
.then(function(response) {
_this.u_get_data();
});
})
2020-10-29 06:50:19 -04:00
_this.$root.$on('u-show-config', function () {
_this.u.modalShowConfigVisible = true;
2020-05-14 19:13:33 -04:00
var data = new URLSearchParams();
2020-10-29 06:50:19 -04:00
data.append('username', _this.username);
axios.request(axios_cfg('api/user/config/show', data, 'form'))
2020-05-14 19:13:33 -04:00
.then(function(response) {
_this.u.openvpnConfig = response.data;
});
})
2020-10-29 06:50:19 -04:00
_this.$root.$on('u-download-config', function () {
2020-10-15 12:12:31 -04:00
var data = new URLSearchParams();
2020-10-29 06:50:19 -04:00
data.append('username', _this.username);
axios.request(axios_cfg('api/user/config/show', data, 'form'))
2020-10-15 12:12:31 -04:00
.then(function(response) {
2020-10-29 06:50:19 -04:00
const blob = new Blob([response.data], { type: 'text/plain' })
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = _this.username + ".ovpn"
link.click()
URL.revokeObjectURL(link.href)
}).catch(console.error);
})
_this.$root.$on('u-edit-ccd', function () {
_this.u.modalShowCcdVisible = true;
var data = new URLSearchParams();
data.append('username', _this.username);
axios.request(axios_cfg('api/user/ccd', data, 'form'))
.then(function(response) {
_this.u.ccd = response.data;
});
})
_this.$root.$on('u-disconnect-use', function () {
_this.u.modalShowCcdVisible = true;
var data = new URLSearchParams();
data.append('username', _this.username);
axios.request(axios_cfg('api/user/disconnect', data, 'form'))
.then(function(response) {
_this.u.ccd = response.data;
2020-10-15 12:12:31 -04:00
});
})
2020-05-14 19:13:33 -04:00
},
computed: {
2020-10-29 06:50:19 -04:00
customAddressEnabled: function () {
return this.u.ccd.ClientAddress == "dynamic"
2020-05-14 19:13:33 -04:00
},
2020-10-29 06:50:19 -04:00
ccdApplyStatusCssClass: function () {
return this.u.ccdApplyStatus == 200 ? "alert-success" : "alert-danger"
2020-05-14 19:13:33 -04:00
},
2020-10-29 06:50:19 -04:00
modalNewUserDisplay: function () {
return this.u.modalNewUserVisible ? {display: 'flex'} : {}
2020-05-14 19:13:33 -04:00
},
2020-10-29 06:50:19 -04:00
modalShowConfigDisplay: function () {
return this.u.modalShowConfigVisible ? {display: 'flex'} : {}
},
modalShowCcdDisplay: function () {
return this.u.modalShowCcdVisible ? {display: 'flex'} : {}
},
revokeFilterText: function() {
return this.filters.hideRevoked ? "Show revoked" : "Hide revoked"
},
filteredRows: function() {
if (this.filters.hideRevoked) {
return this.rows.filter(function(account) {
return account.AccountStatus === "Active";
});
} else {
return this.rows;
}
}
2020-10-29 06:50:19 -04:00
},
methods: {
rowStyleClassFn: function(row) {
return row.ConnectionStatus == 'Connected' ? 'connected-user' : '' ;
2020-10-29 06:50:19 -04:00
},
rowActionFn: function(e) {
this.username = e.target.dataset.username;
this.$root.$emit(e.target.dataset.name);
2020-05-14 19:13:33 -04:00
},
u_get_data: function() {
var _this = this;
axios.request(axios_cfg('api/users/list'))
.then(function(response) {
2020-10-29 06:50:19 -04:00
_this.rows = response.data;
2020-10-15 12:12:31 -04:00
});
},
2020-05-14 19:13:33 -04:00
create_user: function() {
var _this = this;
2020-10-29 06:50:19 -04:00
_this.u.newUserCreateError = "";
2020-05-14 19:13:33 -04:00
var data = new URLSearchParams();
2020-10-29 06:50:19 -04:00
data.append('username', _this.u.newUserName);
// data.append('password', this.u.newUserPassword);
2020-05-14 19:13:33 -04:00
axios.request(axios_cfg('api/user/create', data, 'form'))
.then(function(response) {
_this.u_get_data();
2020-10-29 06:50:19 -04:00
_this.u.modalNewUserVisible = false;
2020-05-14 19:13:33 -04:00
_this.u.newUserName = '';
2020-10-29 06:50:19 -04:00
// _this.u.newUserPassword = 'nopass';
})
.catch(function(error) {
_this.u.newUserCreateError = error.response.data;
2020-05-14 19:13:33 -04:00
});
2020-10-15 12:12:31 -04:00
},
ccd_apply: function() {
var _this = this;
2020-10-29 06:50:19 -04:00
_this.u.ccdApplyStatus = "";
_this.u.ccdApplyStatusMessage = "";
axios.request(axios_cfg('api/user/ccd/apply', JSON.stringify(_this.u.ccd), 'json'))
2020-10-15 12:12:31 -04:00
.then(function(response) {
2020-10-29 06:50:19 -04:00
_this.u.ccdApplyStatus = 200;
_this.u.ccdApplyStatusMessage = response.data;
})
.catch(function(error) {
_this.u.ccdApplyStatus = error.response.status;
_this.u.ccdApplyStatusMessage = error.response.data;
2020-10-15 12:12:31 -04:00
});
2020-05-14 19:13:33 -04:00
}
}
})