2022-01-12 19:53:36 -05:00
/ * *
* configuration . js - Copyright ( C ) 2021 Donald Zou [ https : //github.com/donaldzou]
* Under Apache - 2.0 License
* /
2022-03-22 16:13:57 -04:00
let peers = [ ] ;
2022-03-21 22:33:19 -04:00
( function ( ) {
2022-01-16 20:35:24 -05:00
/ * *
* Definitions
2022-03-22 16:13:57 -04:00
* /
2022-03-04 22:09:01 -05:00
let configuration _name ;
2022-01-16 20:35:24 -05:00
let configuration _interval ;
2022-03-07 09:29:47 -05:00
let configuration _timeout = window . localStorage . getItem ( "configurationTimeout" ) ;
2022-03-21 22:33:19 -04:00
if ( configuration _timeout === null || ! [ "5000" , "10000" , "30000" , "60000" ] . includes ( configuration _timeout ) ) {
2022-03-07 09:29:47 -05:00
window . localStorage . setItem ( "configurationTimeout" , "10000" ) ;
configuration _timeout = window . localStorage . getItem ( "configurationTimeout" ) ;
}
document . querySelector ( ` button[data-refresh-interval=" ${ configuration _timeout } "] ` ) . classList . add ( "active" ) ;
let display _mode = window . localStorage . getItem ( "displayMode" ) ;
2022-03-21 22:33:19 -04:00
if ( display _mode === null || ! [ "grid" , "list" ] . includes ( display _mode ) ) {
2022-03-07 09:29:47 -05:00
window . localStorage . setItem ( "displayMode" , "grid" ) ;
display _mode = "grid" ;
}
document . querySelectorAll ( ".display-btn-group button" ) . forEach ( ele => ele . classList . remove ( "active" ) ) ;
document . querySelector ( ` button[data-display-mode=" ${ display _mode } "] ` ) . classList . add ( "active" ) ;
2022-01-16 20:35:24 -05:00
let $progress _bar = $ ( ".progress-bar" ) ;
let bootstrapModalConfig = {
keyboard : false ,
backdrop : 'static'
} ;
let addModal = new bootstrap . Modal ( document . getElementById ( 'add_modal' ) , bootstrapModalConfig ) ;
let deleteBulkModal = new bootstrap . Modal ( document . getElementById ( 'delete_bulk_modal' ) , bootstrapModalConfig ) ;
let ipModal = new bootstrap . Modal ( document . getElementById ( 'available_ip_modal' ) , bootstrapModalConfig ) ;
let qrcodeModal = new bootstrap . Modal ( document . getElementById ( 'qrcode_modal' ) , bootstrapModalConfig ) ;
let settingModal = new bootstrap . Modal ( document . getElementById ( 'setting_modal' ) , bootstrapModalConfig ) ;
let deleteModal = new bootstrap . Modal ( document . getElementById ( 'delete_modal' ) , bootstrapModalConfig ) ;
2022-03-24 20:43:56 -04:00
let configurationDeleteModal = new bootstrap . Modal ( document . getElementById ( 'configuration_delete_modal' ) , bootstrapModalConfig ) ;
2022-01-16 20:35:24 -05:00
$ ( "[data-toggle='tooltip']" ) . tooltip ( ) ;
$ ( "[data-toggle='popover']" ) . popover ( ) ;
2022-03-04 22:09:01 -05:00
/ * *
* Chart ! ! ! ! ! !
* @ type { any }
* /
2022-03-07 09:29:47 -05:00
let chartUnit = window . localStorage . chartUnit ;
let chartUnitAvailable = [ "GB" , "MB" , "KB" ] ;
2022-03-21 22:33:19 -04:00
if ( chartUnit === null || ! chartUnitAvailable . includes ( chartUnit ) ) {
2022-03-07 09:29:47 -05:00
window . localStorage . setItem ( "chartUnit" , "GB" ) ;
$ ( '.switchUnit[data-unit="GB"]' ) . addClass ( "active" ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-03-07 09:29:47 -05:00
$ ( ` .switchUnit[data-unit=" ${ chartUnit } "] ` ) . addClass ( "active" ) ;
}
chartUnit = window . localStorage . getItem ( "chartUnit" ) ;
2022-03-04 22:09:01 -05:00
const totalDataUsageChart = document . getElementById ( 'totalDataUsageChartObj' ) . getContext ( '2d' ) ;
const totalDataUsageChartObj = new Chart ( totalDataUsageChart , {
type : 'line' ,
data : {
labels : [ ] ,
2022-03-21 22:33:19 -04:00
datasets : [ {
2022-03-04 22:09:01 -05:00
label : 'Data Sent' ,
data : [ ] ,
stroke : '#FFFFFF' ,
borderColor : '#28a745' ,
tension : 0.1 ,
borderWidth : 2
} ,
{
label : 'Data Received' ,
data : [ ] ,
stroke : '#FFFFFF' ,
borderColor : '#007bff' ,
tension : 0.1 ,
borderWidth : 2
}
]
} ,
options : {
maintainAspectRatio : false ,
showScale : false ,
2022-03-21 22:33:19 -04:00
responsive : false ,
2022-03-04 22:09:01 -05:00
scales : {
y : {
min : 0 ,
ticks : {
min : 0 ,
callback : function ( value , index , ticks ) {
return ` ${ value } ${ chartUnit } ` ;
}
}
}
} ,
plugins : {
tooltip : {
callbacks : {
label : function ( context ) {
return ` ${ context . dataset . label } : ${ context . parsed . y } ${ chartUnit } ` ;
}
}
}
}
}
} ) ;
2022-03-21 22:33:19 -04:00
2022-03-04 22:09:01 -05:00
let $totalDataUsageChartObj = $ ( "#totalDataUsageChartObj" ) ;
$totalDataUsageChartObj . css ( "width" , "100%" ) ;
totalDataUsageChartObj . width = $totalDataUsageChartObj . parent ( ) . width ( ) ;
totalDataUsageChartObj . resize ( ) ;
$ ( window ) . on ( "resize" , function ( ) {
2022-03-21 22:33:19 -04:00
totalDataUsageChartObj . resize ( ) ;
2022-03-04 22:09:01 -05:00
} ) ;
2022-03-21 22:33:19 -04:00
$ ( ".fullScreen" ) . on ( "click" , function ( ) {
2022-03-04 22:09:01 -05:00
let $chartContainer = $ ( ".chartContainer" ) ;
2022-03-21 22:33:19 -04:00
if ( $chartContainer . hasClass ( "fullScreen" ) ) {
2022-03-04 22:09:01 -05:00
$ ( this ) . children ( ) . removeClass ( "bi-fullscreen-exit" ) . addClass ( "bi-fullscreen" ) ;
$chartContainer . removeClass ( "fullScreen" ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-03-04 22:09:01 -05:00
$ ( this ) . children ( ) . removeClass ( "bi-fullscreen" ) . addClass ( "bi-fullscreen-exit" ) ;
$chartContainer . addClass ( "fullScreen" ) ;
}
totalDataUsageChartObj . resize ( ) ;
} ) ;
let mul = 1 ;
2022-03-21 22:33:19 -04:00
$ ( ".switchUnit" ) . on ( "click" , function ( ) {
2022-03-04 22:09:01 -05:00
$ ( ".switchUnit" ) . removeClass ( "active" ) ;
$ ( this ) . addClass ( "active" ) ;
2022-03-21 22:33:19 -04:00
if ( $ ( this ) . data ( 'unit' ) !== chartUnit ) {
2022-03-04 22:09:01 -05:00
switch ( $ ( this ) . data ( 'unit' ) ) {
case "GB" :
2022-03-21 22:33:19 -04:00
if ( chartUnit === "MB" ) {
mul = 1 / 1024 ;
2022-03-04 22:09:01 -05:00
}
2022-03-21 22:33:19 -04:00
if ( chartUnit === "KB" ) {
mul = 1 / 1048576 ;
2022-03-04 22:09:01 -05:00
}
break ;
case "MB" :
2022-03-21 22:33:19 -04:00
if ( chartUnit === "GB" ) {
2022-03-07 09:29:47 -05:00
mul = 1024 ;
2022-03-04 22:09:01 -05:00
}
2022-03-21 22:33:19 -04:00
if ( chartUnit === "KB" ) {
mul = 1 / 1024 ;
2022-03-04 22:09:01 -05:00
}
break ;
case "KB" :
2022-03-21 22:33:19 -04:00
if ( chartUnit === "GB" ) {
2022-03-07 09:29:47 -05:00
mul = 1048576 ;
2022-03-04 22:09:01 -05:00
}
2022-03-21 22:33:19 -04:00
if ( chartUnit === "MB" ) {
2022-03-07 09:29:47 -05:00
mul = 1024 ;
2022-03-04 22:09:01 -05:00
}
break ;
default :
break ;
}
2022-03-07 09:29:47 -05:00
window . localStorage . setItem ( "chartUnit" , $ ( this ) . data ( 'unit' ) ) ;
2022-03-04 22:09:01 -05:00
chartUnit = $ ( this ) . data ( 'unit' ) ;
totalDataUsageChartObj . data . datasets [ 0 ] . data = totalDataUsageChartObj . data . datasets [ 0 ] . data . map ( x => x * mul ) ;
totalDataUsageChartObj . data . datasets [ 1 ] . data = totalDataUsageChartObj . data . datasets [ 1 ] . data . map ( x => x * mul ) ;
totalDataUsageChartObj . update ( ) ;
}
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* To show alert on the configuration page
* @ param response
* /
function configurationAlert ( response ) {
2022-03-21 22:33:19 -04:00
if ( response . listen _port === "" && response . status === "stopped" ) {
2022-01-16 20:35:24 -05:00
let configAlert = document . createElement ( "div" ) ;
configAlert . classList . add ( "alert" ) ;
configAlert . classList . add ( "alert-warning" ) ;
configAlert . setAttribute ( "role" , "alert" ) ;
configAlert . innerHTML = 'Peer QR Code and configuration file download required a specified <strong>Listen Port</strong>.' ;
document . querySelector ( "#config_info_alert" ) . appendChild ( configAlert ) ;
}
2022-03-21 22:33:19 -04:00
if ( response . conf _address === "N/A" ) {
2022-01-16 20:35:24 -05:00
let configAlert = document . createElement ( "div" ) ;
configAlert . classList . add ( "alert" ) ;
configAlert . classList . add ( "alert-warning" ) ;
configAlert . setAttribute ( "role" , "alert" ) ;
configAlert . innerHTML = 'Configuration <strong>Address</strong> need to be specified to have peers connect to it.' ;
document . querySelector ( "#config_info_alert" ) . appendChild ( configAlert ) ;
}
}
2022-03-21 22:33:19 -04:00
function setActiveConfigurationName ( ) {
2022-03-04 22:09:01 -05:00
$ ( ".nav-conf-link" ) . removeClass ( "active" ) ;
$ ( ` .sb- ${ configuration _name } -url ` ) . addClass ( "active" ) ;
}
2022-03-03 08:46:23 -05:00
let firstLoading = true ;
2022-03-21 22:33:19 -04:00
$ ( ".nav-conf-link" ) . on ( "click" , function ( e ) {
2022-03-04 22:09:01 -05:00
e . preventDefault ( ) ;
2022-03-21 22:33:19 -04:00
if ( configuration _name !== $ ( this ) . data ( "conf-id" ) ) {
2022-03-03 08:46:23 -05:00
firstLoading = true ;
$ ( "#config_body" ) . addClass ( "firstLoading" ) ;
2022-03-04 22:09:01 -05:00
configuration _name = $ ( this ) . data ( "conf-id" ) ;
2022-03-21 22:33:19 -04:00
if ( loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) ) ) {
2022-03-04 22:09:01 -05:00
setActiveConfigurationName ( ) ;
2022-03-21 22:33:19 -04:00
window . history . pushState ( null , null , ` /configuration/ ${ configuration _name } ` ) ;
2022-03-04 22:09:01 -05:00
$ ( "title" ) . text ( ` ${ configuration _name } | WGDashboard ` ) ;
2022-03-24 20:43:56 -04:00
$ ( ".index-alert" ) . addClass ( "d-none" ) . text ( ` ` ) ;
2022-03-04 22:09:01 -05:00
totalDataUsageChartObj . data . labels = [ ] ;
totalDataUsageChartObj . data . datasets [ 0 ] . data = [ ] ;
totalDataUsageChartObj . data . datasets [ 1 ] . data = [ ] ;
totalDataUsageChartObj . update ( ) ;
}
2022-03-03 08:46:23 -05:00
}
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* Parse all responded information onto the configuration header
* @ param response
* /
function configurationHeader ( response ) {
2022-03-21 22:33:19 -04:00
let $conf _status _btn = $ ( ".toggle--switch" ) ;
if ( response . checked === "checked" ) {
$conf _status _btn . prop ( "checked" , true )
2022-01-16 20:35:24 -05:00
} else {
2022-03-21 22:33:19 -04:00
$conf _status _btn . prop ( "checked" , false )
2022-01-16 20:35:24 -05:00
}
2022-03-21 22:33:19 -04:00
$conf _status _btn . data ( "conf-id" , configuration _name )
2022-03-03 08:46:23 -05:00
2022-03-21 22:33:19 -04:00
if ( response . running _peer > 0 ) {
2022-03-03 08:46:23 -05:00
let d = new Date ( ) ;
2022-03-21 22:33:19 -04:00
let time = d . toLocaleString ( "en-us" , { hour : '2-digit' , minute : '2-digit' , second : "2-digit" , hourCycle : 'h23' } ) ;
2022-03-03 08:46:23 -05:00
totalDataUsageChartObj . data . labels . push ( ` ${ time } ` ) ;
2022-03-21 22:33:19 -04:00
if ( totalDataUsageChartObj . data . datasets [ 0 ] . data . length === 0 ) {
2022-03-03 08:46:23 -05:00
totalDataUsageChartObj . data . datasets [ 1 ] . lastData = response . total _data _usage [ 2 ] ;
totalDataUsageChartObj . data . datasets [ 0 ] . lastData = response . total _data _usage [ 1 ] ;
totalDataUsageChartObj . data . datasets [ 0 ] . data . push ( 0 ) ;
totalDataUsageChartObj . data . datasets [ 1 ] . data . push ( 0 ) ;
2022-03-21 22:33:19 -04:00
} else {
if ( totalDataUsageChartObj . data . datasets [ 0 ] . data . length === 50 && totalDataUsageChartObj . data . datasets [ 1 ] . data . length === 50 ) {
2022-03-03 08:46:23 -05:00
totalDataUsageChartObj . data . labels . shift ( ) ;
totalDataUsageChartObj . data . datasets [ 0 ] . data . shift ( ) ;
totalDataUsageChartObj . data . datasets [ 1 ] . data . shift ( ) ;
}
let newTotalReceive = response . total _data _usage [ 2 ] - totalDataUsageChartObj . data . datasets [ 1 ] . lastData ;
let newTotalSent = response . total _data _usage [ 1 ] - totalDataUsageChartObj . data . datasets [ 0 ] . lastData ;
let k = 0 ;
2022-03-21 22:33:19 -04:00
if ( chartUnit === "MB" ) {
2022-03-03 08:46:23 -05:00
k = 1024 ;
2022-03-21 22:33:19 -04:00
} else if ( chartUnit === "KB" ) {
2022-03-03 08:46:23 -05:00
k = 1048576 ;
2022-03-21 22:33:19 -04:00
} else {
2022-03-03 08:46:23 -05:00
k = 1 ;
}
2022-03-21 22:33:19 -04:00
totalDataUsageChartObj . data . datasets [ 1 ] . data . push ( newTotalReceive * k ) ;
totalDataUsageChartObj . data . datasets [ 0 ] . data . push ( newTotalSent * k ) ;
2022-03-03 08:46:23 -05:00
totalDataUsageChartObj . data . datasets [ 0 ] . lastData = response . total _data _usage [ 1 ] ;
totalDataUsageChartObj . data . datasets [ 1 ] . lastData = response . total _data _usage [ 2 ] ;
}
totalDataUsageChartObj . update ( ) ;
}
2022-03-04 22:09:01 -05:00
document . querySelector ( "#conf_name" ) . textContent = configuration _name ;
2022-03-21 22:33:19 -04:00
$ ( "#switch" ) . removeClass ( "info_loading" ) ;
2022-01-16 20:35:24 -05:00
document . querySelectorAll ( "#sort_by_dropdown option" ) . forEach ( ele => ele . removeAttribute ( "selected" ) ) ;
document . querySelector ( ` #sort_by_dropdown option[value=" ${ response . sort _tag } "] ` ) . setAttribute ( "selected" , "selected" ) ;
document . querySelector ( "#conf_status" ) . innerHTML = ` ${ response . status } <span class="dot dot- ${ response . status } "></span> ` ;
document . querySelector ( "#conf_connected_peers" ) . innerHTML = response . running _peer ;
document . querySelector ( "#conf_total_data_usage" ) . innerHTML = ` ${ response . total _data _usage [ 0 ] } GB ` ;
document . querySelector ( "#conf_total_data_received" ) . innerHTML = ` ${ response . total _data _usage [ 2 ] } GB ` ;
document . querySelector ( "#conf_total_data_sent" ) . innerHTML = ` ${ response . total _data _usage [ 1 ] } GB ` ;
document . querySelector ( "#conf_public_key" ) . innerHTML = response . public _key ;
document . querySelector ( "#conf_listen_port" ) . innerHTML = response . listen _port === "" ? "N/A" : response . listen _port ;
document . querySelector ( "#conf_address" ) . innerHTML = response . conf _address ;
2022-03-21 22:33:19 -04:00
let delay = 0 ;
let h6 = $ ( ".info h6" ) ;
for ( let i = 0 ; i < h6 . length ; i ++ ) {
setTimeout ( function ( ) {
$ ( h6 [ i ] ) . removeClass ( "info_loading" ) ;
} , delay )
delay += 40
}
2022-01-16 20:35:24 -05:00
}
/ * *
* Parse all responded information onto the peers list
* @ param response
* /
function configurationPeers ( response ) {
let result = "" ;
2022-03-21 22:33:19 -04:00
if ( response . peer _data . length === 0 ) {
2022-01-16 20:35:24 -05:00
document . querySelector ( ".peer_list" ) . innerHTML = ` <div class="col-12" style="text-align: center; margin-top: 1.5rem"><h3 class="text-muted">Oops! No peers found ‘︿’</h3></div> ` ;
2022-03-21 22:33:19 -04:00
} else {
2022-03-07 09:29:47 -05:00
let mode = display _mode === "list" ? "col-12" : "col-sm-6 col-lg-4" ;
2022-03-21 22:33:19 -04:00
response . peer _data . forEach ( function ( peer ) {
2022-01-16 20:35:24 -05:00
let total _r = 0 ;
let total _s = 0 ;
total _r += peer . cumu _receive ;
total _s += peer . cumu _sent ;
2022-03-30 00:54:11 -04:00
2022-01-16 20:35:24 -05:00
let spliter = '<div class="w-100"></div>' ;
let peer _name =
2022-02-28 13:34:46 -05:00
` <div class="col-sm peerNameCol">
< h5 class = "peerName" > $ { peer . name === "" ? "Untitled" : peer . name } < / h 5 >
2022-03-30 00:54:11 -04:00
< h6 class = "peerLightContainer" >
< span class = "dot dot-${peer.status}" style = "margin-left: auto !important;" data - toggle = "tooltip" data - placement = "left" > < / s p a n >
< / h 6 >
2022-02-28 13:34:46 -05:00
< / d i v > ` ;
let peer _transfer =
` <div class="col-12 peer_data_group" style="">
< p class = "text-primary" style = "" >
< small > < i class = "bi bi-arrow-down-right" > < / i > $ { r o u n d N ( p e e r . t o t a l _ r e c e i v e + t o t a l _ r , 4 ) } G B < / s m a l l >
< / p >
< p class = "text-success" >
< small > < i class = "bi bi-arrow-up-right" > < / i > $ { r o u n d N ( p e e r . t o t a l _ s e n t + t o t a l _ s , 4 ) } G B < / s m a l l >
< / p >
< / d i v > ` ;
2022-03-30 00:54:11 -04:00
let peer _key =
` <div class="col-sm">
< small class = "text-muted" style = "display: flex" >
< strong > PEER < / s t r o n g >
< strong style = "margin-left: auto!important; opacity: 0; transition: 0.2s ease-in-out" class = "text-primary" > CLICK TO COPY < / s t r o n g >
< / s m a l l >
< h6 > < samp class = "ml-auto key" > $ { peer . id } < / s a m p > < / h 6 >
< / d i v > ` ;
let peer _allowed _ip = `
< div class = "col-sm" >
< small class = "text-muted" >
< strong > ALLOWED IP < / s t r o n g >
< / s m a l l >
< h6 style = "text-transform: uppercase;" > $ { peer . allowed _ip } < / h 6 >
< / d i v > ` ;
let peer _latest _handshake =
` <div class="col-sm">
< small class = "text-muted" > < strong > LATEST HANDSHAKE < / s t r o n g > < / s m a l l >
< h6 style = "text-transform: uppercase;" > $ { peer . latest _handshake } < / h 6 >
< / d i v > ` ;
let peer _endpoint =
` <div class="col-sm">
< small class = "text-muted" > < strong > END POINT < / s t r o n g > < / s m a l l >
< h6 style = "text-transform: uppercase;" > $ { peer . endpoint } < / h 6 >
< / d i v > ` ;
2022-03-04 22:09:01 -05:00
let peer _control = `
< div class = "col-sm" >
< hr >
< div class = "button-group" style = "display:flex" >
2022-03-21 22:33:19 -04:00
< button type = "button" class = "btn btn-outline-primary btn-setting-peer btn-control" data - peer - id = "${peer.id}" data - toggle = "modal" >
2022-03-04 22:09:01 -05:00
< i class = "bi bi-gear-fill" data - toggle = "tooltip" data - placement = "bottom" title = "Peer Settings" > < / i >
< / b u t t o n >
2022-03-21 22:33:19 -04:00
< button type = "button" class = "btn btn-outline-danger btn-delete-peer btn-control" data - peer - id = "${peer.id}" data - toggle = "modal" >
2022-03-04 22:09:01 -05:00
< i class = "bi bi-x-circle-fill" data - toggle = "tooltip" data - placement = "bottom" title = "Delete Peer" > < / i >
< / b u t t o n >
2022-03-21 22:33:19 -04:00
< button type = "button" class = "btn btn-outline-success btn-lock-peer btn-control" data - peer - id = "${peer.id}" data - toggle = "modal" >
< i class = "bi bi-ethernet" data - toggle = "tooltip" data - placement = "bottom" data - original - title = 'Peer enabled. Click to disable peer.' data - peer - name = "${peer.name}" > < / i >
2022-03-04 22:09:01 -05:00
< / b u t t o n > ` ;
2022-03-21 22:33:19 -04:00
if ( peer . private _key !== "" ) {
2022-03-30 00:54:11 -04:00
peer _control +=
` <div class="share_peer_btn_group" style="margin-left: auto !important; display: inline">
< button type = "button" class = "btn btn-outline-success btn-qrcode-peer btn-control" data - imgsrc = "/qrcode/${response.name}?id=${encodeURIComponent(peer.id)}" >
< svg xmlns = "http://www.w3.org/2000/svg" viewBox = "0 0 24 24" style = "width: 19px;" fill = "#28a745" > < path d = "M3 11h8V3H3v8zm2-6h4v4H5V5zM3 21h8v-8H3v8zm2-6h4v4H5v-4zM13 3v8h8V3h-8zm6 6h-4V5h4v4zM13 13h2v2h-2zM15 15h2v2h-2zM13 17h2v2h-2zM17 17h2v2h-2zM19 19h2v2h-2zM15 19h2v2h-2zM17 13h2v2h-2zM19 15h2v2h-2z" / > < / s v g >
< / b u t t o n >
< a href = "/download/${response.name}?id=${encodeURIComponent(peer.id)}" class = "btn btn-outline-info btn-download-peer btn-control" > < i class = "bi bi-download" > < / i > < / a >
< / d i v > ` ;
2022-01-16 20:35:24 -05:00
}
2022-03-30 00:54:11 -04:00
peer _control += '</div></div>' ;
let html =
` <div class=" ${ mode } " data-id=" ${ peer . id } ">
< div class = "card mb-3 card-${peer.status}" >
< div class = "card-body" >
< div class = "row" > ` +
peer _name +
spliter +
peer _transfer +
peer _key +
peer _allowed _ip +
peer _latest _handshake +
spliter +
peer _endpoint +
spliter +
peer _control +
` </div></div></div></div> ` ;
2022-03-21 22:33:19 -04:00
result += html ;
} ) ;
response . lock _access _peers . forEach ( function ( peer ) {
let total _r = 0 ;
let total _s = 0 ;
total _r += peer . cumu _receive ;
total _s += peer . cumu _sent ;
let spliter = '<div class="w-100"></div>' ;
let peer _name =
` <div class="col-sm peerNameCol">
< h5 class = "peerName" > $ { peer . name === "" ? "Untitled" : peer . name } < / h 5 >
< h6 class = "peerLightContainer" > < span class = "dot dot-${peer.status}" style = "margin-left: auto !important;" data - toggle = "tooltip" data - placement = "left" > < / s p a n > < / h 6 >
< / d i v > ` ;
let peer _transfer =
` <div class="col-12 peer_data_group" style="">
< p class = "text-primary" style = "" >
< small > < i class = "bi bi-arrow-down-right" > < / i > $ { r o u n d N ( p e e r . t o t a l _ r e c e i v e + t o t a l _ r , 4 ) } G B < / s m a l l >
< / p >
< p class = "text-success" >
< small > < i class = "bi bi-arrow-up-right" > < / i > $ { r o u n d N ( p e e r . t o t a l _ s e n t + t o t a l _ s , 4 ) } G B < / s m a l l >
< / p >
< / d i v > ` ;
let peer _key = '<div class="col-sm"><small class="text-muted" style="display: flex"><strong>PEER</strong><strong style="margin-left: auto!important; opacity: 0; transition: 0.2s ease-in-out" class="text-primary">CLICK TO COPY</strong></small> <h6><samp class="ml-auto key">' + peer . id + '</samp></h6></div>' ;
let peer _allowed _ip = '<div class="col-sm"><small class="text-muted"><strong>ALLOWED IP</strong></small><h6 style="text-transform: uppercase;">' + peer . allowed _ip + '</h6></div>' ;
let peer _latest _handshake = '<div class="col-sm"> <small class="text-muted"><strong>LATEST HANDSHAKE</strong></small> <h6 style="text-transform: uppercase;">' + peer . latest _handshake + '</h6> </div>' ;
let peer _endpoint = '<div class="col-sm"><small class="text-muted"><strong>END POINT</strong></small><h6 style="text-transform: uppercase;">' + peer . endpoint + '</h6></div>' ;
let peer _control = `
< div class = "col-sm" >
< hr >
< div class = "button-group" style = "display:flex; align-items: center;" >
< button type = "button" class = "btn btn-outline-success btn-lock-peer btn-control lock" data - peer - id = "${peer.id}" data - toggle = "modal" >
< i class = "bi bi-ethernet" data - toggle = "tooltip" data - placement = "bottom" data - original - title = 'Peer disabled. Click to enable peer.' data - peer - name = "${peer.name}" > < / i >
< / b u t t o n >
< small class = "text-muted" style = "margin-left: auto" > Peer Disabled < / s m a l l >
< / d i v > ` ;
let html = '<div class="' + mode + '" data-id="' + peer . id + '">' +
'<div class="card mb-3 card-' + peer . status + '">' +
'<div class="card-body">' +
'<div class="row">' +
peer _name +
spliter +
peer _transfer +
peer _key +
peer _allowed _ip +
peer _latest _handshake +
spliter +
peer _endpoint +
spliter +
peer _control +
'</div>' +
'</div>' +
'</div>' +
'</div></div>' ;
2022-01-16 20:35:24 -05:00
result += html ;
} ) ;
document . querySelector ( ".peer_list" ) . innerHTML = result ;
2022-03-21 22:33:19 -04:00
if ( configuration _interval === undefined ) {
2022-01-16 20:35:24 -05:00
setConfigurationInterval ( ) ;
2022-01-12 19:53:36 -05:00
}
2022-01-16 20:35:24 -05:00
}
}
/ * *
* Handle when adding peers by bulk
* /
function addPeersByBulk ( ) {
let $new _add _amount = $ ( "#new_add_amount" ) ;
2022-03-21 22:33:19 -04:00
$add _peer . setAttribute ( "disabled" , "disabled" ) ;
2022-01-16 20:35:24 -05:00
$add _peer . innerHTML = ` Adding ${ $new _add _amount . val ( ) } peers... ` ;
let $new _add _DNS = $ ( "#new_add_DNS" ) ;
$new _add _DNS . val ( window . configurations . cleanIp ( $new _add _DNS . val ( ) ) ) ;
let $new _add _endpoint _allowed _ip = $ ( "#new_add_endpoint_allowed_ip" ) ;
2022-03-21 22:33:19 -04:00
$new _add _endpoint _allowed _ip . val ( window . configurations . cleanIp ( $new _add _endpoint _allowed _ip . val ( ) ) ) ;
2022-01-16 20:35:24 -05:00
let $new _add _MTU = $ ( "#new_add_MTU" ) ;
let $new _add _keep _alive = $ ( "#new_add_keep_alive" ) ;
let $enable _preshare _key = $ ( "#enable_preshare_key" ) ;
2022-03-21 22:33:19 -04:00
let data _list = [ $new _add _DNS , $new _add _endpoint _allowed _ip , $new _add _MTU , $new _add _keep _alive ] ;
if ( $new _add _amount . val ( ) > 0 && ! $new _add _amount . hasClass ( "is-invalid" ) ) {
if ( $new _add _DNS . val ( ) !== "" && $new _add _endpoint _allowed _ip . val ( ) !== "" ) {
2022-03-04 22:09:01 -05:00
let conf = configuration _name ;
2022-01-16 20:35:24 -05:00
let keys = [ ] ;
for ( let i = 0 ; i < $new _add _amount . val ( ) ; i ++ ) {
keys . push ( window . wireguard . generateKeypair ( ) ) ;
}
$ . ajax ( {
method : "POST" ,
2022-03-21 22:33:19 -04:00
url : "/add_peer_bulk/" + conf ,
headers : {
2022-01-16 20:35:24 -05:00
"Content-Type" : "application/json"
} ,
data : JSON . stringify ( {
"DNS" : $new _add _DNS . val ( ) ,
"endpoint_allowed_ip" : $new _add _endpoint _allowed _ip . val ( ) ,
"MTU" : $new _add _MTU . val ( ) ,
"keep_alive" : $new _add _keep _alive . val ( ) ,
"enable_preshared_key" : $enable _preshare _key . prop ( "checked" ) ,
"keys" : keys ,
"amount" : $new _add _amount . val ( )
} ) ,
2022-03-21 22:33:19 -04:00
success : function ( response ) {
if ( response !== "true" ) {
2022-01-16 20:35:24 -05:00
$ ( "#add_peer_alert" ) . html ( response ) . removeClass ( "d-none" ) ;
data _list . forEach ( ( ele ) => ele . removeAttr ( "disabled" ) ) ;
$add _peer . removeAttribute ( "disabled" ) ;
$add _peer . innerHTML = "Save" ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
window . configurations . loadPeers ( "" ) ;
data _list . forEach ( ( ele ) => ele . removeAttr ( "disabled" ) ) ;
$ ( "#add_peer_form" ) . trigger ( "reset" ) ;
$add _peer . removeAttribute ( "disabled" ) ;
$add _peer . innerHTML = "Save" ;
2022-03-21 22:33:19 -04:00
window . configurations . showToast ( $new _add _amount . val ( ) + " peers added successful!" ) ;
2022-01-16 20:35:24 -05:00
window . configurations . addModal ( ) . toggle ( ) ;
}
}
} ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
$ ( "#add_peer_alert" ) . html ( "Please fill in all required box." ) . removeClass ( "d-none" ) ;
$add _peer . removeAttribute ( "disabled" ) ;
$add _peer . innerHTML = "Add" ;
2022-01-12 19:53:36 -05:00
}
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
$add _peer . removeAttribute ( "disabled" ) ;
$add _peer . innerHTML = "Add" ;
}
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Delete one peer or by bulk
* @ param config
* @ param peer _ids
* /
2022-03-21 22:33:19 -04:00
function deletePeers ( config , peer _ids ) {
2022-01-16 20:35:24 -05:00
$ . ajax ( {
method : "POST" ,
2022-03-21 22:33:19 -04:00
url : "/remove_peer/" + config ,
headers : {
2022-01-16 20:35:24 -05:00
"Content-Type" : "application/json"
} ,
2022-03-21 22:33:19 -04:00
data : JSON . stringify ( { "action" : "delete" , "peer_ids" : peer _ids } ) ,
success : function ( response ) {
if ( response !== "true" ) {
2022-01-16 20:35:24 -05:00
if ( window . configurations . deleteModal ( ) . _isShown ) {
2022-03-21 22:33:19 -04:00
$ ( "#remove_peer_alert" ) . html ( response + $ ( "#add_peer_alert" ) . html ( ) )
. removeClass ( "d-none" ) ;
2022-01-16 20:35:24 -05:00
$ ( "#delete_peer" ) . removeAttr ( "disabled" ) . html ( "Delete" ) ;
}
2022-03-21 22:33:19 -04:00
if ( window . configurations . deleteBulkModal ( ) . _isShown ) {
2022-01-16 20:35:24 -05:00
let $bulk _remove _peer _alert = $ ( "#bulk_remove_peer_alert" ) ;
2022-03-21 22:33:19 -04:00
$bulk _remove _peer _alert . html ( response + $bulk _remove _peer _alert . html ( ) )
. removeClass ( "d-none" ) ;
2022-01-16 20:35:24 -05:00
$ ( "#confirm_delete_bulk_peers" ) . removeAttr ( "disabled" ) . html ( "Delete" ) ;
}
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
if ( window . configurations . deleteModal ( ) . _isShown ) {
window . configurations . deleteModal ( ) . toggle ( ) ;
}
2022-03-21 22:33:19 -04:00
if ( window . configurations . deleteBulkModal ( ) . _isShown ) {
2022-01-16 20:35:24 -05:00
$ ( "#confirm_delete_bulk_peers" ) . removeAttr ( "disabled" ) . html ( "Delete" ) ;
$ ( "#selected_peer_list" ) . html ( '' ) ;
$ ( ".delete-bulk-peer-item.active" ) . removeClass ( 'active' ) ;
window . configurations . deleteBulkModal ( ) . toggle ( ) ;
}
window . configurations . loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) ) ;
2022-03-21 22:33:19 -04:00
window . configurations . showToast ( ` Deleted ${ peer _ids . length } peers ` )
2022-01-16 20:35:24 -05:00
$ ( "#delete_peer" ) . removeAttr ( "disabled" ) . html ( "Delete" ) ;
}
}
} ) ;
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when the server is not responding
* /
2022-03-21 22:33:19 -04:00
function noResponding ( message = "Opps! <br> I can't connect to the server." ) {
2022-01-16 20:35:24 -05:00
document . querySelectorAll ( ".no-response" ) . forEach ( ele => ele . classList . add ( "active" ) ) ;
2022-03-21 22:33:19 -04:00
setTimeout ( function ( ) {
2022-01-16 20:35:24 -05:00
document . querySelectorAll ( ".no-response" ) . forEach ( ele => ele . classList . add ( "show" ) ) ;
document . querySelector ( "#right_body" ) . classList . add ( "no-responding" ) ;
document . querySelector ( ".navbar" ) . classList . add ( "no-responding" ) ;
2022-03-03 08:46:23 -05:00
document . querySelector ( ".no-response .container h4" ) . innerHTML = message ;
2022-03-21 22:33:19 -04:00
} , 10 ) ;
2022-01-16 20:35:24 -05:00
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Remove no responding
* /
2022-03-21 22:33:19 -04:00
function removeNoResponding ( ) {
2022-01-16 20:35:24 -05:00
document . querySelectorAll ( ".no-response" ) . forEach ( ele => ele . classList . remove ( "show" ) ) ;
document . querySelector ( "#right_body" ) . classList . remove ( "no-responding" ) ;
document . querySelector ( ".navbar" ) . classList . remove ( "no-responding" ) ;
2022-03-21 22:33:19 -04:00
setTimeout ( function ( ) {
2022-01-16 20:35:24 -05:00
document . querySelectorAll ( ".no-response" ) . forEach ( ele => ele . classList . remove ( "active" ) ) ;
2022-03-21 22:33:19 -04:00
} , 1010 ) ;
2022-01-16 20:35:24 -05:00
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Set configuration refresh Interval
* /
2022-03-21 22:33:19 -04:00
function setConfigurationInterval ( ) {
configuration _interval = setInterval ( function ( ) {
2022-01-16 20:35:24 -05:00
loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) ) ;
2022-03-21 22:33:19 -04:00
} , configuration _timeout ) ;
2022-01-16 20:35:24 -05:00
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Remove configuration refresh interval
* /
2022-03-21 22:33:19 -04:00
function removeConfigurationInterval ( ) {
2022-01-16 20:35:24 -05:00
clearInterval ( configuration _interval ) ;
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Start Progress Bar
* /
2022-03-21 22:33:19 -04:00
function startProgressBar ( ) {
$progress _bar . css ( "width" , "0%" )
2022-01-16 20:35:24 -05:00
. css ( "opacity" , "100" )
. css ( "background" , "rgb(255,69,69)" )
. css ( "background" ,
"linear-gradient(145deg, rgba(255,69,69,1) 0%, rgba(0,115,186,1) 100%)" )
2022-03-21 22:33:19 -04:00
. css ( "width" , "25%" ) ;
setTimeout ( function ( ) {
2022-01-16 20:35:24 -05:00
stillLoadingProgressBar ( ) ;
2022-03-21 22:33:19 -04:00
} , 300 ) ;
2022-01-16 20:35:24 -05:00
}
/ * *
* Still Loading Progress Bar
* /
2022-03-21 22:33:19 -04:00
function stillLoadingProgressBar ( ) {
2022-01-16 20:35:24 -05:00
$progress _bar . css ( "transition" , "3s ease-in-out" ) . css ( "width" , "75%" ) ;
}
/ * *
* End Progress Bar
* /
2022-03-21 22:33:19 -04:00
function endProgressBar ( ) {
$progress _bar . css ( "transition" , "0.3s ease-in-out" ) . css ( "width" , "100%" ) ;
setTimeout ( function ( ) {
2022-01-16 20:35:24 -05:00
$progress _bar . css ( "opacity" , "0" ) ;
2022-03-21 22:33:19 -04:00
} , 250 ) ;
2022-01-16 20:35:24 -05:00
}
/ * *
* Round Transfer number into 4 digits
* @ param value
* @ param digits
* @ returns { number }
* /
function roundN ( value , digits ) {
2022-03-21 22:33:19 -04:00
let tenToN = 10 * * digits ;
return ( Math . round ( value * tenToN ) ) / tenToN ;
2022-01-16 20:35:24 -05:00
}
/ * *
* Load Peers from server to configuration page
* @ param searchString
* /
2022-01-19 10:27:17 -05:00
let time = 0 ;
let count = 0 ;
let d1 = new Date ( ) ;
2022-03-21 22:33:19 -04:00
function loadPeers ( searchString ) {
2022-01-19 10:27:17 -05:00
d1 = new Date ( ) ;
2022-03-04 22:09:01 -05:00
let good = true ;
$ . ajax ( {
method : "GET" ,
url : ` /get_config/ ${ configuration _name } ?search= ${ encodeURIComponent ( searchString ) } ` ,
2022-03-21 22:33:19 -04:00
headers : { "Content-Type" : "application/json" }
} ) . done ( function ( response ) {
2022-03-04 22:09:01 -05:00
console . log ( response ) ;
parsePeers ( response ) ;
2022-03-21 22:33:19 -04:00
} ) . fail ( function ( ) {
2022-03-04 22:09:01 -05:00
noResponding ( ) ;
2022-03-07 09:29:47 -05:00
good = false ;
2022-03-04 22:09:01 -05:00
} ) ;
return good ;
2022-01-19 10:27:17 -05:00
}
2022-03-21 22:33:19 -04:00
function parsePeers ( response ) {
if ( response . status ) {
removeAllTooltips ( ) ;
2022-03-03 08:46:23 -05:00
let d2 = new Date ( ) ;
let seconds = ( d2 - d1 ) ;
time += seconds ;
count += 1 ;
window . console . log ( ` Average time: ${ time / count } ms ` ) ;
$ ( "#peer_loading_time" ) . html ( ` Peer Loading Time: ${ seconds } ms ` ) ;
removeNoResponding ( ) ;
peers = response . data . peer _data ;
configurationAlert ( response . data ) ;
configurationHeader ( response . data ) ;
configurationPeers ( response . data ) ;
2022-03-21 22:33:19 -04:00
$ ( ".dot.dot-running" ) . attr ( "title" , "Peer Connected" ) . tooltip ( ) ;
$ ( ".dot.dot-stopped" ) . attr ( "title" , "Peer Disconnected" ) . tooltip ( ) ;
2022-03-03 08:46:23 -05:00
$ ( "i[data-toggle='tooltip']" ) . tooltip ( ) ;
2022-03-04 22:09:01 -05:00
$ ( "#configuration_name" ) . text ( configuration _name ) ;
2022-03-21 22:33:19 -04:00
if ( firstLoading ) {
2022-03-03 08:46:23 -05:00
firstLoading = false ;
$ ( "#config_body" ) . removeClass ( "firstLoading" ) ;
}
2022-03-21 22:33:19 -04:00
} else {
2022-03-03 08:46:23 -05:00
noResponding ( response . message ) ;
removeConfigurationInterval ( ) ;
}
2022-01-16 20:35:24 -05:00
}
2022-03-21 22:33:19 -04:00
function removeAllTooltips ( ) {
2022-03-24 20:43:56 -04:00
$ ( ".tooltip" ) . remove ( ) ;
2022-03-21 22:33:19 -04:00
}
function toggleAccess ( peerID ) {
$ . ajax ( {
url : "/api/togglePeerAccess" ,
method : "POST" ,
headers : { "Content-Type" : "application/json" } ,
data : JSON . stringify ( { "peerID" : peerID , "config" : configuration _name } )
} ) . done ( function ( res ) {
if ( res . status ) {
loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) ) ;
} else {
showToast ( res . reason ) ;
}
} ) ;
}
2022-01-16 20:35:24 -05:00
/ * *
* Generate Private and Public key for a new peer
* /
2022-03-21 22:33:19 -04:00
function generate _key ( ) {
2022-01-16 20:35:24 -05:00
let keys = window . wireguard . generateKeypair ( ) ;
document . querySelector ( "#private_key" ) . value = keys . privateKey ;
document . querySelector ( "#public_key" ) . value = keys . publicKey ;
document . querySelector ( "#add_peer_alert" ) . classList . add ( "d-none" ) ;
document . querySelector ( "#re_generate_key i" ) . classList . remove ( "rotating" ) ;
document . querySelector ( "#enable_preshare_key" ) . value = keys . presharedKey ;
}
/ * *
* Show toast
* @ param msg
* /
2022-03-21 22:33:19 -04:00
let numberToast = 0 ;
2022-01-16 20:35:24 -05:00
function showToast ( msg ) {
2022-03-21 22:33:19 -04:00
$ ( ".toastContainer" ) . append (
` <div id=" ${ numberToast } -toast" class="toast hide" role="alert" data-delay="5000">
< div class = "toast-header" >
< strong class = "mr-auto" > WGDashboard < / s t r o n g >
< button type = "button" class = "ml-2 mb-1 close" data - dismiss = "toast" aria - label = "Close" >
< span aria - hidden = "true" > & times ; < / s p a n >
< / b u t t o n >
< / d i v >
< div class = "toast-body" > $ { msg } < / d i v >
< div class = "toast-progressbar" > < / d i v >
< / d i v > ` )
$ ( ` # ${ numberToast } -toast ` ) . toast ( 'show' ) ;
$ ( ` # ${ numberToast } -toast .toast-body ` ) . html ( msg ) ;
$ ( ` # ${ numberToast } -toast .toast-progressbar ` ) . css ( "transition" , ` width ${ $ ( ` # ${ numberToast } -toast .toast-progressbar ` ) . parent ( ) . data ( 'delay' ) } ms cubic-bezier(0, 0, 0, 0) ` ) ;
$ ( ` # ${ numberToast } -toast .toast-progressbar ` ) . css ( "width" , "0px" ) ;
numberToast ++ ;
2022-01-16 20:35:24 -05:00
}
/ * *
* Update peer ' s refresh interval
* @ param res
* @ param interval
* /
2022-03-07 09:29:47 -05:00
function updateRefreshInterval ( interval ) {
configuration _timeout = interval ;
window . localStorage . setItem ( "configurationTimeout" , configuration _timeout . toString ( ) ) ;
removeConfigurationInterval ( ) ;
setConfigurationInterval ( ) ;
2022-03-21 22:33:19 -04:00
showToast ( "Refresh Interval set to " + Math . round ( interval / 1000 ) + " seconds" ) ;
2022-01-16 20:35:24 -05:00
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Clean IP
* @ param val
* @ returns { string }
* /
2022-03-21 22:33:19 -04:00
function cleanIp ( val ) {
2022-01-16 20:35:24 -05:00
let clean _ip = val . split ( ',' ) ;
for ( let i = 0 ; i < clean _ip . length ; i ++ ) {
clean _ip [ i ] = clean _ip [ i ] . trim ( ' ' ) ;
}
return clean _ip . filter ( Boolean ) . join ( "," ) ;
}
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Trigger IP badge and item
* @ param ip
* /
2022-03-21 22:33:19 -04:00
function trigger _ip ( ip ) {
2022-01-16 20:35:24 -05:00
let $ip _ele = document . querySelector ( ` .available-ip-item[data-ip=' ${ ip } '] ` ) ;
2022-03-21 22:33:19 -04:00
if ( $ip _ele ) {
if ( $ip _ele . classList . contains ( "active" ) ) {
2022-01-16 20:35:24 -05:00
$ip _ele . classList . remove ( "active" ) ;
document . querySelector ( ` #selected_ip_list .badge[data-ip=' ${ ip } '] ` ) . remove ( ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
$ip _ele . classList . add ( "active" ) ;
document . querySelector ( "#selected_ip_list" ) . innerHTML += ` <span class="badge badge-primary available-ip-badge" style="cursor: pointer" data-ip=" ${ ip } "> ${ ip } </span> ` ;
}
}
}
2022-01-04 16:32:23 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Download single configuration file
* @ param conf
* /
2022-03-21 22:33:19 -04:00
function download _one _config ( conf ) {
2022-01-16 20:35:24 -05:00
let link = document . createElement ( 'a' ) ;
link . download = conf . filename ;
2022-03-21 22:33:19 -04:00
let blob = new Blob ( [ conf . content ] , { type : 'text/conf' } ) ;
2022-01-16 20:35:24 -05:00
link . href = window . URL . createObjectURL ( blob ) ;
link . click ( ) ;
}
2021-12-29 12:17:44 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Toggle delete by bulk IP
* @ param element
* /
2022-03-21 22:33:19 -04:00
function toggleBulkIP ( element ) {
2022-01-16 20:35:24 -05:00
let $selected _peer _list = $ ( "#selected_peer_list" ) ;
let id = element . data ( "id" ) ;
let name = element . data ( "name" ) === "" ? "Untitled Peer" : element . data ( "name" ) ;
2022-03-21 22:33:19 -04:00
if ( element . hasClass ( "active" ) ) {
2022-01-16 20:35:24 -05:00
element . removeClass ( "active" ) ;
2022-03-21 22:33:19 -04:00
$ ( "#selected_peer_list .badge[data-id='" + id + "']" ) . remove ( ) ;
} else {
2022-01-16 20:35:24 -05:00
element . addClass ( "active" ) ;
2022-03-21 22:33:19 -04:00
$selected _peer _list . append ( '<span class="badge badge-danger delete-peer-bulk-badge" style="cursor: pointer; text-overflow: ellipsis; max-width: 100%; overflow-x: hidden" data-id="' + id + '">' + name + ' - ' + id + '</span>' ) ;
2022-01-16 20:35:24 -05:00
}
}
2021-12-30 15:21:25 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Copy public keys to clipboard
* @ param element
* /
function copyToClipboard ( element ) {
let $temp = $ ( "<input>" ) ;
$body . append ( $temp ) ;
2022-03-21 22:33:19 -04:00
$temp . val ( $ ( element ) . text ( ) ) . trigger ( "select" ) ;
2022-01-16 20:35:24 -05:00
document . execCommand ( "copy" ) ;
$temp . remove ( ) ;
}
2021-12-30 15:21:25 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Get all available IP for this configuration
* /
2022-03-21 22:33:19 -04:00
function getAvailableIps ( ) {
2022-01-16 20:35:24 -05:00
$ . ajax ( {
2022-03-04 22:09:01 -05:00
"url" : ` /available_ips/ ${ configuration _name } ` ,
2022-01-16 20:35:24 -05:00
"method" : "GET" ,
2022-03-21 22:33:19 -04:00
} ) . done ( function ( res ) {
if ( res . status === true ) {
2022-03-03 08:46:23 -05:00
available _ips = res . data ;
let $list _group = document . querySelector ( "#available_ip_modal .modal-body .list-group" ) ;
$list _group . innerHTML = "" ;
document . querySelector ( "#allowed_ips" ) . value = available _ips [ 0 ] ;
available _ips . forEach ( ( ip ) =>
$list _group . innerHTML +=
2022-03-21 22:33:19 -04:00
` <a class="list-group-item list-group-item-action available-ip-item" style="cursor: pointer" data-ip=" ${ ip } "> ${ ip } </a> ` ) ;
} else {
2022-03-03 08:46:23 -05:00
document . querySelector ( "#allowed_ips" ) . value = res . message ;
document . querySelector ( "#search_available_ip" ) . setAttribute ( "disabled" , "disabled" ) ;
}
2022-01-16 20:35:24 -05:00
} ) ;
}
2021-12-30 15:21:25 -05:00
2022-03-24 20:43:56 -04:00
function deleteConfiguration ( ) {
}
2022-01-16 20:35:24 -05:00
window . configurations = {
addModal : ( ) => { return addModal ; } ,
deleteBulkModal : ( ) => { return deleteBulkModal ; } ,
deleteModal : ( ) => { return deleteModal ; } ,
2022-03-24 20:43:56 -04:00
configurationDeleteModal : ( ) => { return configurationDeleteModal ; } ,
2022-01-16 20:35:24 -05:00
ipModal : ( ) => { return ipModal ; } ,
qrcodeModal : ( ) => { return qrcodeModal ; } ,
settingModal : ( ) => { return settingModal ; } ,
2022-03-21 22:33:19 -04:00
configurationTimeout : ( ) => { return configuration _timeout ; } ,
2022-03-24 20:43:56 -04:00
updateDisplayMode : ( ) => { display _mode = window . localStorage . getItem ( "displayMode" ) ; } ,
removeConfigurationInterval : ( ) => { removeConfigurationInterval ( ) ; } ,
2022-01-16 20:35:24 -05:00
loadPeers : ( searchString ) => { loadPeers ( searchString ) ; } ,
addPeersByBulk : ( ) => { addPeersByBulk ( ) ; } ,
deletePeers : ( config , peers _ids ) => { deletePeers ( config , peers _ids ) ; } ,
2022-03-24 20:43:56 -04:00
deleteConfiguration : ( ) => { deleteConfiguration ( ) } ,
2022-01-19 10:27:17 -05:00
parsePeers : ( response ) => { parsePeers ( response ) ; } ,
2022-03-24 20:43:56 -04:00
toggleAccess : ( peerID ) => { toggleAccess ( peerID ) ; } ,
2022-03-21 22:33:19 -04:00
2022-01-16 20:35:24 -05:00
2022-03-21 22:33:19 -04:00
setConfigurationName : ( confName ) => { configuration _name = confName ; } ,
2022-03-04 22:09:01 -05:00
getConfigurationName : ( ) => { return configuration _name ; } ,
setActiveConfigurationName : ( ) => { setActiveConfigurationName ( ) ; } ,
2022-01-16 20:35:24 -05:00
getAvailableIps : ( ) => { getAvailableIps ( ) ; } ,
generateKeyPair : ( ) => { generate _key ( ) ; } ,
showToast : ( message ) => { showToast ( message ) ; } ,
2022-03-07 09:29:47 -05:00
updateRefreshInterval : ( interval ) => { updateRefreshInterval ( interval ) ; } ,
2022-01-16 20:35:24 -05:00
copyToClipboard : ( element ) => { copyToClipboard ( element ) ; } ,
toggleDeleteByBulkIP : ( element ) => { toggleBulkIP ( element ) ; } ,
downloadOneConfig : ( conf ) => { download _one _config ( conf ) ; } ,
triggerIp : ( ip ) => { trigger _ip ( ip ) ; } ,
cleanIp : ( val ) => { return cleanIp ( val ) ; } ,
startProgressBar : ( ) => { startProgressBar ( ) ; } ,
stillLoadingProgressBar : ( ) => { stillLoadingProgressBar ( ) ; } ,
endProgressBar : ( ) => { endProgressBar ( ) ; }
} ;
} ) ( ) ;
2021-12-29 12:17:44 -05:00
2022-01-16 20:35:24 -05:00
let $body = $ ( "body" ) ;
let available _ips = [ ] ;
let $add _peer = document . getElementById ( "save_peer" ) ;
2021-12-29 12:17:44 -05:00
2022-03-24 20:43:56 -04:00
$ ( "#configuration_delete" ) . on ( "click" , function ( ) {
window . configurations . configurationDeleteModal ( ) . toggle ( ) ;
} ) ;
function ajaxPostJSON ( url , data , doneFunc ) {
$ . ajax ( {
url : url ,
method : "POST" ,
data : JSON . stringify ( data ) ,
headers : { "Content-Type" : "application/json" }
} ) . done ( function ( res ) {
doneFunc ( res ) ;
} ) ;
}
$ ( "#sure_delete_configuration" ) . on ( "click" , function ( ) {
window . configurations . removeConfigurationInterval ( ) ;
let ele = $ ( this )
ele . attr ( "disabled" , "disabled" ) ;
function done ( res ) {
if ( res . status ) {
$ ( '#configuration_delete_modal button[data-dismiss="modal"]' ) . remove ( ) ;
ele . text ( "Delete Successful! Redirecting in 5 seconds." ) ;
setTimeout ( function ( ) {
window . location . replace ( '/' ) ;
} , 5000 )
} else {
$ ( "#remove_configuration_alert" ) . removeClass ( "d-none" ) . text ( res . reason ) ;
}
}
ajaxPostJSON ( "/api/deleteConfiguration" , { "name" : window . configurations . getConfigurationName ( ) } , done ) ;
} ) ;
2022-01-04 16:32:23 -05:00
/ * *
2022-01-16 20:35:24 -05:00
* === === === =
* Add peers
* === === === =
2022-01-04 16:32:23 -05:00
* /
2021-12-29 12:17:44 -05:00
2022-01-04 16:32:23 -05:00
/ * *
2022-01-16 20:35:24 -05:00
* Toggle add peers modal when add button clicked
2022-01-04 16:32:23 -05:00
* /
2022-01-16 20:35:24 -05:00
document . querySelector ( ".add_btn" ) . addEventListener ( "click" , ( ) => {
window . configurations . addModal ( ) . toggle ( ) ;
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
2022-01-04 16:32:23 -05:00
/ * *
2022-01-16 20:35:24 -05:00
* When configuration switch got click
2022-01-04 16:32:23 -05:00
* /
2022-03-24 20:43:56 -04:00
$ ( ".toggle--switch" ) . on ( "change" , function ( ) {
console . log ( 'lol' )
2022-03-21 22:33:19 -04:00
$ ( this ) . addClass ( "waiting" ) . attr ( "disabled" , "disabled" ) ;
let id = window . configurations . getConfigurationName ( ) ;
let status = $ ( this ) . prop ( "checked" ) ;
let ele = $ ( this ) ;
$ . ajax ( {
url : ` /switch/ ${ id } `
} ) . done ( function ( res ) {
2022-03-24 20:43:56 -04:00
if ( res . status ) {
2022-03-21 22:33:19 -04:00
if ( status ) {
window . configurations . showToast ( ` ${ id } is running. ` )
} else {
window . configurations . showToast ( ` ${ id } is stopped. ` )
}
} else {
if ( status ) {
2022-03-24 20:43:56 -04:00
ele . prop ( "checked" , false )
2022-03-21 22:33:19 -04:00
} else {
2022-03-24 20:43:56 -04:00
ele . prop ( "checked" , true )
2022-03-21 22:33:19 -04:00
}
2022-03-24 20:43:56 -04:00
window . configurations . showToast ( res . reason ) ;
$ ( ".index-alert" ) . removeClass ( "d-none" ) . text ( ` Configuration toggle failed. Please check the following error message: \n ${ res . message } ` ) ;
2022-03-21 22:33:19 -04:00
}
2022-03-24 20:43:56 -04:00
ele . removeClass ( "waiting" ) ;
ele . removeAttr ( "disabled" ) ;
2022-03-21 22:33:19 -04:00
window . configurations . loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) )
} ) ;
2022-01-16 20:35:24 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
2022-01-04 16:32:23 -05:00
/ * *
* Generate Public key when private got change
* /
2022-01-16 20:35:24 -05:00
document . querySelector ( "#private_key" ) . addEventListener ( "change" , ( event ) => {
let publicKey = document . querySelector ( "#public_key" ) ;
2022-03-21 22:33:19 -04:00
if ( event . target . value . length === 44 ) {
2022-01-16 20:35:24 -05:00
publicKey . value = window . wireguard . generatePublicKey ( event . target . value ) ;
publicKey . setAttribute ( "disabled" , "disabled" ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
publicKey . attributes . removeNamedItem ( "disabled" ) ;
publicKey . value = "" ;
2021-08-05 23:15:50 -04:00
}
2021-12-29 23:26:15 -05:00
} ) ;
2022-01-04 16:32:23 -05:00
/ * *
2022-01-16 20:35:24 -05:00
* Handle when add modal is show and hide
2022-01-04 16:32:23 -05:00
* /
2022-03-21 22:33:19 -04:00
$ ( '#add_modal' ) . on ( 'show.bs.modal' , function ( ) {
2022-01-16 20:35:24 -05:00
window . configurations . generateKeyPair ( ) ;
window . configurations . getAvailableIps ( ) ;
2022-03-21 22:33:19 -04:00
} ) . on ( 'hide.bs.modal' , function ( ) {
2022-01-16 20:35:24 -05:00
$ ( "#allowed_ips_indicator" ) . html ( '' ) ;
} ) ;
2022-01-04 16:32:23 -05:00
/ * *
2022-01-16 20:35:24 -05:00
* Handle when user clicked the regenerate button
2022-01-04 16:32:23 -05:00
* /
2022-03-21 22:33:19 -04:00
$ ( "#re_generate_key" ) . on ( "click" , function ( ) {
$ ( "#public_key" ) . attr ( "disabled" , "disabled" ) ;
2022-01-16 20:35:24 -05:00
$ ( "#re_generate_key i" ) . addClass ( "rotating" ) ;
window . configurations . generateKeyPair ( ) ;
} ) ;
2022-01-04 16:32:23 -05:00
/ * *
2022-01-16 20:35:24 -05:00
* Handle when user is editing in allowed ips textbox
2022-01-04 16:32:23 -05:00
* /
2022-03-21 22:33:19 -04:00
$ ( "#allowed_ips" ) . on ( "keyup" , function ( ) {
2022-01-16 20:35:24 -05:00
let s = window . configurations . cleanIp ( $ ( this ) . val ( ) ) ;
2022-01-04 16:32:23 -05:00
s = s . split ( "," ) ;
2022-03-21 22:33:19 -04:00
if ( available _ips . includes ( s [ s . length - 1 ] ) ) {
2022-01-04 16:32:23 -05:00
$ ( "#allowed_ips_indicator" ) . removeClass ( ) . addClass ( "text-success" )
. html ( '<i class="bi bi-check-circle-fill"></i>' ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-04 16:32:23 -05:00
$ ( "#allowed_ips_indicator" ) . removeClass ( ) . addClass ( "text-warning" )
. html ( '<i class="bi bi-exclamation-circle-fill"></i>' ) ;
}
2021-12-29 23:26:15 -05:00
} ) ;
2021-12-29 12:17:44 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Change peer name when user typing in peer name textbox
* /
2022-03-21 22:33:19 -04:00
$ ( "#peer_name_textbox" ) . on ( "keyup" , function ( ) {
2022-01-16 20:35:24 -05:00
$ ( ".peer_name" ) . html ( $ ( this ) . val ( ) ) ;
2022-01-12 19:53:36 -05:00
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* When Add Peer button got clicked
* /
2022-03-21 22:33:19 -04:00
$add _peer . addEventListener ( "click" , function ( ) {
2022-01-06 15:17:43 -05:00
let $bulk _add = $ ( "#bulk_add" ) ;
2022-03-21 22:33:19 -04:00
if ( $bulk _add . prop ( "checked" ) ) {
if ( ! $ ( "#new_add_amount" ) . hasClass ( "is-invalid" ) ) {
window . configurations . addPeersByBulk ( ) ;
2022-01-12 19:53:36 -05:00
}
2022-01-16 20:35:24 -05:00
} else {
2022-01-06 15:17:43 -05:00
let $public _key = $ ( "#public_key" ) ;
let $private _key = $ ( "#private_key" ) ;
let $allowed _ips = $ ( "#allowed_ips" ) ;
2022-01-16 20:35:24 -05:00
$allowed _ips . val ( window . configurations . cleanIp ( $allowed _ips . val ( ) ) ) ;
2022-01-06 15:17:43 -05:00
let $new _add _DNS = $ ( "#new_add_DNS" ) ;
2022-01-16 20:35:24 -05:00
$new _add _DNS . val ( window . configurations . cleanIp ( $new _add _DNS . val ( ) ) ) ;
2022-01-06 15:17:43 -05:00
let $new _add _endpoint _allowed _ip = $ ( "#new_add_endpoint_allowed_ip" ) ;
2022-01-16 20:35:24 -05:00
$new _add _endpoint _allowed _ip . val ( window . configurations . cleanIp ( $new _add _endpoint _allowed _ip . val ( ) ) ) ;
2022-01-06 15:17:43 -05:00
let $new _add _name = $ ( "#new_add_name" ) ;
let $new _add _MTU = $ ( "#new_add_MTU" ) ;
let $new _add _keep _alive = $ ( "#new_add_keep_alive" ) ;
let $enable _preshare _key = $ ( "#enable_preshare_key" ) ;
2022-03-21 22:33:19 -04:00
$add _peer . setAttribute ( "disabled" , "disabled" ) ;
2022-01-16 20:35:24 -05:00
$add _peer . innerHTML = "Adding..." ;
2022-03-21 22:33:19 -04:00
if ( $allowed _ips . val ( ) !== "" && $public _key . val ( ) !== "" && $new _add _DNS . val ( ) !== "" && $new _add _endpoint _allowed _ip . val ( ) !== "" ) {
2022-03-04 22:09:01 -05:00
let conf = window . configurations . getConfigurationName ( ) ;
2022-03-21 22:33:19 -04:00
let data _list = [ $private _key , $allowed _ips , $new _add _name , $new _add _DNS , $new _add _endpoint _allowed _ip , $new _add _MTU , $new _add _keep _alive ] ;
2022-01-06 15:17:43 -05:00
data _list . forEach ( ( ele ) => ele . attr ( "disabled" , "disabled" ) ) ;
$ . ajax ( {
method : "POST" ,
2022-03-21 22:33:19 -04:00
url : "/add_peer/" + conf ,
headers : {
2022-01-06 15:17:43 -05:00
"Content-Type" : "application/json"
} ,
data : JSON . stringify ( {
2022-03-21 22:33:19 -04:00
"private_key" : $private _key . val ( ) ,
"public_key" : $public _key . val ( ) ,
2022-01-06 15:17:43 -05:00
"allowed_ips" : $allowed _ips . val ( ) ,
2022-03-21 22:33:19 -04:00
"name" : $new _add _name . val ( ) ,
2022-01-06 15:17:43 -05:00
"DNS" : $new _add _DNS . val ( ) ,
"endpoint_allowed_ip" : $new _add _endpoint _allowed _ip . val ( ) ,
"MTU" : $new _add _MTU . val ( ) ,
"keep_alive" : $new _add _keep _alive . val ( ) ,
"enable_preshared_key" : $enable _preshare _key . prop ( "checked" ) ,
"preshared_key" : $enable _preshare _key . val ( )
} ) ,
2022-03-21 22:33:19 -04:00
success : function ( response ) {
if ( response !== "true" ) {
2022-01-06 15:17:43 -05:00
$ ( "#add_peer_alert" ) . html ( response ) . removeClass ( "d-none" ) ;
data _list . forEach ( ( ele ) => ele . removeAttr ( "disabled" ) ) ;
2022-01-16 20:35:24 -05:00
$add _peer . removeAttribute ( "disabled" ) ;
$add _peer . innerHTML = "Save" ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
window . configurations . loadPeers ( "" ) ;
2022-01-06 15:17:43 -05:00
data _list . forEach ( ( ele ) => ele . removeAttr ( "disabled" ) ) ;
$ ( "#add_peer_form" ) . trigger ( "reset" ) ;
2022-01-16 20:35:24 -05:00
$add _peer . removeAttribute ( "disabled" ) ;
$add _peer . innerHTML = "Save" ;
window . configurations . showToast ( "Add peer successful!" ) ;
window . configurations . addModal ( ) . toggle ( ) ;
2022-01-06 15:17:43 -05:00
}
}
} ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-06 15:17:43 -05:00
$ ( "#add_peer_alert" ) . html ( "Please fill in all required box." ) . removeClass ( "d-none" ) ;
2022-01-16 20:35:24 -05:00
$add _peer . removeAttribute ( "disabled" ) ;
$add _peer . innerHTML = "Add" ;
2022-01-06 15:17:43 -05:00
}
2021-08-05 23:15:50 -04:00
}
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when user is typing the amount of peers they want to add , and will check if the amount is less than 1 or
* is larger than the amount of available ips
* /
2022-03-21 22:33:19 -04:00
$ ( "#new_add_amount" ) . on ( "keyup" , function ( ) {
2022-01-16 20:35:24 -05:00
let $bulk _amount _validation = $ ( "#bulk_amount_validation" ) ;
// $(this).removeClass("is-valid").addClass("is-invalid");
2022-03-21 22:33:19 -04:00
if ( $ ( this ) . val ( ) . length > 0 ) {
if ( isNaN ( $ ( this ) . val ( ) ) ) {
2022-01-16 20:35:24 -05:00
$ ( this ) . removeClass ( "is-valid" ) . addClass ( "is-invalid" ) ;
$bulk _amount _validation . html ( "Please enter a valid integer" ) ;
2022-03-21 22:33:19 -04:00
} else if ( $ ( this ) . val ( ) > available _ips . length ) {
2022-01-16 20:35:24 -05:00
$ ( this ) . removeClass ( "is-valid" ) . addClass ( "is-invalid" ) ;
$bulk _amount _validation . html ( ` Cannot create more than ${ available _ips . length } peers. ` ) ;
2022-03-21 22:33:19 -04:00
} else if ( $ ( this ) . val ( ) < 1 ) {
2022-01-16 20:35:24 -05:00
$ ( this ) . removeClass ( "is-valid" ) . addClass ( "is-invalid" ) ;
$bulk _amount _validation . html ( "Please enter at least 1 or more." ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
$ ( this ) . removeClass ( "is-invalid" ) . addClass ( "is-valid" ) ;
}
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
$ ( this ) . removeClass ( "is-invalid" ) . removeClass ( "is-valid" ) ;
}
} ) ;
/ * *
* Handle when user toggled add peers by bulk
* /
2022-03-21 22:33:19 -04:00
$ ( "#bulk_add" ) . on ( "change" , function ( ) {
2022-03-03 08:46:23 -05:00
let hide = $ ( ".non-bulk" ) ;
2022-01-16 20:35:24 -05:00
let amount = $ ( "#new_add_amount" ) ;
2022-03-21 22:33:19 -04:00
if ( $ ( this ) . prop ( "checked" ) === true ) {
for ( let i = 0 ; i < hide . length ; i ++ ) {
2022-01-16 20:35:24 -05:00
$ ( hide [ i ] ) . attr ( "disabled" , "disabled" ) ;
}
amount . removeAttr ( "disabled" ) ;
2022-03-21 22:33:19 -04:00
} else {
for ( let i = 0 ; i < hide . length ; i ++ ) {
if ( $ ( hide [ i ] ) . attr ( 'id' ) !== "public_key" ) {
2022-01-16 20:35:24 -05:00
$ ( hide [ i ] ) . removeAttr ( "disabled" ) ;
}
}
amount . attr ( "disabled" , "disabled" ) ;
}
2021-12-29 23:26:15 -05:00
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* === === === === === === === ==
* Available IP Related
* === === === === === === === ==
* /
/ * *
* Handle when available ip modal show and hide
* /
$ ( "#available_ip_modal" ) . on ( "show.bs.modal" , ( ) => {
document . querySelector ( '#add_modal' ) . classList . add ( "ip_modal_open" ) ;
} ) . on ( "hidden.bs.modal" , ( ) => {
document . querySelector ( '#add_modal' ) . classList . remove ( "ip_modal_open" ) ;
let ips = [ ] ;
let $selected _ip _list = document . querySelector ( "#selected_ip_list" ) ;
2022-03-21 22:33:19 -04:00
for ( let i = 0 ; i < $selected _ip _list . childElementCount ; i ++ ) {
2022-01-16 20:35:24 -05:00
ips . push ( $selected _ip _list . children [ i ] . dataset . ip ) ;
}
ips . forEach ( ( ele ) => window . configurations . triggerIp ( ele ) ) ;
} ) ;
/ * *
* When IP Badge got click
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".available-ip-badge" , function ( ) {
$ ( ".available-ip-item[data-ip='" + $ ( this ) . data ( "ip" ) + "']" ) . removeClass ( "active" ) ;
2022-01-16 20:35:24 -05:00
$ ( this ) . remove ( ) ;
} ) ;
/ * *
* When available ip item got click
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".available-ip-item" , function ( ) {
2022-01-16 20:35:24 -05:00
window . configurations . triggerIp ( $ ( this ) . data ( "ip" ) ) ;
} ) ;
/ * *
* When search IP button got clicked
* /
2022-03-21 22:33:19 -04:00
$ ( "#search_available_ip" ) . on ( "click" , function ( ) {
2022-01-16 20:35:24 -05:00
window . configurations . ipModal ( ) . toggle ( ) ;
let $allowed _ips = document . querySelector ( "#allowed_ips" ) ;
2022-03-21 22:33:19 -04:00
if ( $allowed _ips . value . length > 0 ) {
2022-01-16 20:35:24 -05:00
let s = $allowed _ips . value . split ( "," ) ;
2022-03-21 22:33:19 -04:00
for ( let i = 0 ; i < s . length ; i ++ ) {
2022-01-16 20:35:24 -05:00
s [ i ] = s [ i ] . trim ( ) ;
window . configurations . triggerIp ( s [ i ] ) ;
}
}
} ) . tooltip ( ) ;
/ * *
* When confirm IP is clicked
* /
$ ( "#confirm_ip" ) . on ( "click" , ( ) => {
window . configurations . ipModal ( ) . toggle ( ) ;
let ips = [ ] ;
let $selected _ip _list = $ ( "#selected_ip_list" ) ;
2022-03-21 22:33:19 -04:00
$selected _ip _list . children ( ) . each ( function ( ) {
2022-01-16 20:35:24 -05:00
ips . push ( $ ( this ) . data ( "ip" ) ) ;
} ) ;
$ ( "#allowed_ips" ) . val ( ips . join ( ", " ) ) ;
ips . forEach ( ( ele ) => window . configurations . triggerIp ( ele ) ) ;
} ) ;
/ * *
* === === =
* QR Code
* === === =
* /
/ * *
* When the QR - code button got clicked on each peer
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".btn-qrcode-peer" , function ( ) {
2022-01-16 20:35:24 -05:00
let src = $ ( this ) . data ( 'imgsrc' ) ;
2021-12-23 21:26:24 -05:00
$ . ajax ( {
"url" : src ,
"method" : "GET"
2022-03-21 22:33:19 -04:00
} ) . done ( function ( res ) {
2021-12-29 23:26:15 -05:00
$ ( "#qrcode_img" ) . attr ( 'src' , res ) ;
2022-01-16 20:35:24 -05:00
window . configurations . qrcodeModal ( ) . toggle ( ) ;
2021-12-29 23:26:15 -05:00
} ) ;
} ) ;
2021-08-05 23:15:50 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* === === === ==
* Delete Peer
* === === === ==
* /
2021-08-05 23:15:50 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* When the delete button got clicked on each peer
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".btn-delete-peer" , function ( ) {
let peer _id = $ ( this ) . data ( 'peer-id' )
2022-03-03 08:46:23 -05:00
$ ( "#delete_peer" ) . data ( "peer-id" , peer _id ) ;
2022-01-16 20:35:24 -05:00
window . configurations . deleteModal ( ) . toggle ( ) ;
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".btn-lock-peer" , function ( ) {
window . configurations . toggleAccess ( $ ( this ) . data ( 'peer-id' ) , window . configurations . getConfigurationName ( ) ) ;
if ( $ ( this ) . hasClass ( "lock" ) ) {
console . log ( $ ( this ) . data ( "peer-name" ) )
window . configurations . showToast ( ` Enabled ${ $ ( this ) . children ( ) . data ( "peer-name" ) } ` )
$ ( this ) . removeClass ( "lock" )
$ ( this ) . children ( ) . tooltip ( 'hide' ) . attr ( 'data-original-title' , 'Peer enabled. Click to disable peer.' ) . tooltip ( 'show' ) ;
} else {
// Currently unlocked
window . configurations . showToast ( ` Disabled ${ $ ( this ) . children ( ) . data ( "peer-name" ) } ` )
$ ( this ) . addClass ( "lock" ) ;
$ ( this ) . children ( ) . tooltip ( 'hide' ) . attr ( 'data-original-title' , 'Peer disabled. Click to enable peer.' ) . tooltip ( 'show' ) ;
2022-03-04 22:09:01 -05:00
}
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* When the confirm delete button clicked
* /
2022-03-21 22:33:19 -04:00
$ ( "#delete_peer" ) . on ( "click" , function ( ) {
$ ( this ) . attr ( "disabled" , "disabled" ) ;
2021-12-29 23:26:15 -05:00
$ ( this ) . html ( "Deleting..." ) ;
2022-03-04 22:09:01 -05:00
let config = window . configurations . getConfigurationName ( ) ;
2022-03-03 08:46:23 -05:00
let peer _ids = [ $ ( this ) . data ( "peer-id" ) ] ;
2022-01-16 20:35:24 -05:00
window . configurations . deletePeers ( config , peer _ids ) ;
2022-01-08 15:26:17 -05:00
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* === === === === =
* Peer Settings
* === === === === =
* /
2021-08-05 23:15:50 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when setting button got clicked for each peer
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".btn-setting-peer" , function ( ) {
2022-03-03 08:46:23 -05:00
// window.configurations.startProgressBar();
2022-03-21 22:33:19 -04:00
let peer _id = $ ( this ) . data ( "peer-id" ) ;
2021-08-05 23:15:50 -04:00
$ ( "#save_peer_setting" ) . attr ( "peer_id" , peer _id ) ;
$ . ajax ( {
method : "POST" ,
2022-03-21 22:33:19 -04:00
url : "/get_peer_data/" + window . configurations . getConfigurationName ( ) ,
headers : {
2021-08-05 23:15:50 -04:00
"Content-Type" : "application/json"
} ,
2022-03-21 22:33:19 -04:00
data : JSON . stringify ( { "id" : peer _id } ) ,
success : function ( response ) {
2022-01-16 20:35:24 -05:00
let peer _name = ( ( response . name === "" ) ? "Untitled" : response . name ) ;
2021-08-05 23:15:50 -04:00
$ ( "#setting_modal .peer_name" ) . html ( peer _name ) ;
2021-12-29 23:26:15 -05:00
$ ( "#setting_modal #peer_name_textbox" ) . val ( response . name ) ;
$ ( "#setting_modal #peer_private_key_textbox" ) . val ( response . private _key ) ;
$ ( "#setting_modal #peer_DNS_textbox" ) . val ( response . DNS ) ;
$ ( "#setting_modal #peer_allowed_ip_textbox" ) . val ( response . allowed _ip ) ;
$ ( "#setting_modal #peer_endpoint_allowed_ips" ) . val ( response . endpoint _allowed _ip ) ;
$ ( "#setting_modal #peer_mtu" ) . val ( response . mtu ) ;
$ ( "#setting_modal #peer_keep_alive" ) . val ( response . keep _alive ) ;
$ ( "#setting_modal #peer_preshared_key_textbox" ) . val ( response . preshared _key ) ;
2022-01-16 20:35:24 -05:00
window . configurations . settingModal ( ) . toggle ( ) ;
window . configurations . endProgressBar ( ) ;
2021-08-05 23:15:50 -04:00
}
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* Handle when setting modal is closing
* /
2022-03-21 22:33:19 -04:00
$ ( '#setting_modal' ) . on ( 'hidden.bs.modal' , function ( ) {
$ ( "#setting_peer_alert" ) . addClass ( "d-none" ) ;
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when private key text box in setting modal got changed
* /
2022-03-21 22:33:19 -04:00
$ ( "#peer_private_key_textbox" ) . on ( "change" , function ( ) {
2021-12-29 23:26:15 -05:00
let $save _peer _setting = $ ( "#save_peer_setting" ) ;
2022-03-21 22:33:19 -04:00
if ( $ ( this ) . val ( ) . length > 0 ) {
2021-08-05 23:15:50 -04:00
$ . ajax ( {
2022-03-21 22:33:19 -04:00
"url" : "/check_key_match/" + window . configurations . getConfigurationName ( ) ,
2021-08-05 23:15:50 -04:00
"method" : "POST" ,
2022-03-21 22:33:19 -04:00
"headers" : { "Content-Type" : "application/json" } ,
2021-08-05 23:15:50 -04:00
"data" : JSON . stringify ( {
"private_key" : $ ( "#peer_private_key_textbox" ) . val ( ) ,
2021-12-29 23:26:15 -05:00
"public_key" : $save _peer _setting . attr ( "peer_id" )
2021-08-05 23:15:50 -04:00
} )
2022-03-21 22:33:19 -04:00
} ) . done ( function ( res ) {
if ( res . status === "failed" ) {
2021-12-29 23:26:15 -05:00
$ ( "#setting_peer_alert" ) . html ( res . status ) . removeClass ( "d-none" ) ;
2022-03-21 22:33:19 -04:00
} else {
2021-08-05 23:15:50 -04:00
$ ( "#setting_peer_alert" ) . addClass ( "d-none" ) ;
}
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
}
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* When save peer setting button got clicked
* /
2022-03-21 22:33:19 -04:00
$ ( "#save_peer_setting" ) . on ( "click" , function ( ) {
$ ( this ) . attr ( "disabled" , "disabled" ) ;
2021-12-29 23:26:15 -05:00
$ ( this ) . html ( "Saving..." ) ;
let $peer _DNS _textbox = $ ( "#peer_DNS_textbox" ) ;
let $peer _allowed _ip _textbox = $ ( "#peer_allowed_ip_textbox" ) ;
let $peer _endpoint _allowed _ips = $ ( "#peer_endpoint_allowed_ips" ) ;
let $peer _name _textbox = $ ( "#peer_name_textbox" ) ;
let $peer _private _key _textbox = $ ( "#peer_private_key_textbox" ) ;
let $peer _preshared _key _textbox = $ ( "#peer_preshared_key_textbox" ) ;
let $peer _mtu = $ ( "#peer_mtu" ) ;
let $peer _keep _alive = $ ( "#peer_keep_alive" ) ;
if ( $peer _DNS _textbox . val ( ) !== "" &&
2022-03-21 22:33:19 -04:00
$peer _allowed _ip _textbox . val ( ) !== "" && $peer _endpoint _allowed _ips . val ( ) !== "" ) {
2021-12-29 23:26:15 -05:00
let peer _id = $ ( this ) . attr ( "peer_id" ) ;
let conf _id = $ ( this ) . attr ( "conf_id" ) ;
let data _list = [ $peer _name _textbox , $peer _DNS _textbox , $peer _private _key _textbox , $peer _preshared _key _textbox , $peer _allowed _ip _textbox , $peer _endpoint _allowed _ips , $peer _mtu , $peer _keep _alive ] ;
2022-03-21 22:33:19 -04:00
data _list . forEach ( ( ele ) => ele . attr ( "disabled" , "disabled" ) ) ;
2021-08-05 23:15:50 -04:00
$ . ajax ( {
method : "POST" ,
2022-03-21 22:33:19 -04:00
url : "/save_peer_setting/" + conf _id ,
headers : {
2021-08-05 23:15:50 -04:00
"Content-Type" : "application/json"
} ,
data : JSON . stringify ( {
id : peer _id ,
2021-12-29 23:26:15 -05:00
name : $peer _name _textbox . val ( ) ,
DNS : $peer _DNS _textbox . val ( ) ,
private _key : $peer _private _key _textbox . val ( ) ,
allowed _ip : $peer _allowed _ip _textbox . val ( ) ,
endpoint _allowed _ip : $peer _endpoint _allowed _ips . val ( ) ,
MTU : $peer _mtu . val ( ) ,
keep _alive : $peer _keep _alive . val ( ) ,
preshared _key : $peer _preshared _key _textbox . val ( )
2021-08-05 23:15:50 -04:00
} ) ,
2022-03-21 22:33:19 -04:00
success : function ( response ) {
if ( response . status === "failed" ) {
2021-12-29 23:26:15 -05:00
$ ( "#setting_peer_alert" ) . html ( response . msg ) . removeClass ( "d-none" ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-16 20:35:24 -05:00
window . configurations . settingModal ( ) . toggle ( ) ;
window . configurations . loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) ) ;
2021-08-05 23:15:50 -04:00
$ ( '#alertToast' ) . toast ( 'show' ) ;
$ ( '#alertToast .toast-body' ) . html ( "Peer Saved!" ) ;
}
2021-12-29 23:26:15 -05:00
$ ( "#save_peer_setting" ) . removeAttr ( "disabled" ) . html ( "Save" ) ;
data _list . forEach ( ( ele ) => ele . removeAttr ( "disabled" ) ) ;
2021-08-05 23:15:50 -04:00
}
2021-12-29 23:26:15 -05:00
} ) ;
2022-03-21 22:33:19 -04:00
} else {
2021-12-29 23:26:15 -05:00
$ ( "#setting_peer_alert" ) . html ( "Please fill in all required box." ) . removeClass ( "d-none" ) ;
$ ( "#save_peer_setting" ) . removeAttr ( "disabled" ) . html ( "Save" ) ;
2021-08-05 23:15:50 -04:00
}
2021-12-29 23:26:15 -05:00
} ) ;
2021-08-05 23:15:50 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* Toggle show or hide for the private key textbox in the setting modal
* /
2022-03-21 22:33:19 -04:00
$ ( ".peer_private_key_textbox_switch" ) . on ( "click" , function ( ) {
2021-12-29 23:26:15 -05:00
let $peer _private _key _textbox = $ ( "#peer_private_key_textbox" ) ;
2022-03-21 22:33:19 -04:00
let mode = ( ( $peer _private _key _textbox . attr ( 'type' ) === 'password' ) ? "text" : "password" ) ;
let icon = ( ( $peer _private _key _textbox . attr ( 'type' ) === 'password' ) ? "bi bi-eye-slash-fill" : "bi bi-eye-fill" ) ;
$peer _private _key _textbox . attr ( 'type' , mode ) ;
2021-12-29 23:26:15 -05:00
$ ( ".peer_private_key_textbox_switch i" ) . removeClass ( ) . addClass ( icon ) ;
} ) ;
2021-08-14 17:13:16 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* === === === ==
* Search Peer
* === === === ==
* /
2022-03-21 22:33:19 -04:00
let typingTimer ; // Timeout object
2022-01-16 20:35:24 -05:00
let doneTypingInterval = 200 ; // Timeout interval
2022-01-04 16:32:23 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when the user keyup and keydown on the search textbox
* /
2022-03-21 22:33:19 -04:00
$ ( '#search_peer_textbox' ) . on ( 'keyup' , function ( ) {
2022-01-16 20:35:24 -05:00
clearTimeout ( typingTimer ) ;
typingTimer = setTimeout ( ( ) => {
window . configurations . loadPeers ( $ ( this ) . val ( ) ) ;
} , doneTypingInterval ) ;
2022-03-21 22:33:19 -04:00
} ) . on ( 'keydown' , function ( ) {
2021-08-14 17:13:16 -04:00
clearTimeout ( typingTimer ) ;
} ) ;
2022-01-04 16:32:23 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Manage Peers
* /
2021-08-14 17:13:16 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when sort peers changed
* /
2022-03-21 22:33:19 -04:00
$body . on ( "change" , "#sort_by_dropdown" , function ( ) {
2021-08-14 17:13:16 -04:00
$ . ajax ( {
2022-03-21 22:33:19 -04:00
method : "POST" ,
data : JSON . stringify ( { 'sort' : $ ( "#sort_by_dropdown option:selected" ) . val ( ) } ) ,
headers : { "Content-Type" : "application/json" } ,
2021-08-14 17:13:16 -04:00
url : "/update_dashboard_sort" ,
2022-03-21 22:33:19 -04:00
success : function ( ) {
2022-01-16 20:35:24 -05:00
window . configurations . loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) ) ;
2021-08-14 17:13:16 -04:00
}
2021-12-29 23:26:15 -05:00
} ) ;
} ) ;
2021-08-14 17:13:16 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle copy public key
* /
2022-03-21 22:33:19 -04:00
$body . on ( "mouseenter" , ".key" , function ( ) {
2021-12-29 23:26:15 -05:00
let label = $ ( this ) . parent ( ) . siblings ( ) . children ( ) [ 1 ] ;
label . style . opacity = "100" ;
2022-03-21 22:33:19 -04:00
} ) . on ( "mouseout" , ".key" , function ( ) {
2021-12-29 23:26:15 -05:00
let label = $ ( this ) . parent ( ) . siblings ( ) . children ( ) [ 1 ] ;
label . style . opacity = "0" ;
2022-03-21 22:33:19 -04:00
setTimeout ( function ( ) {
2021-12-29 23:26:15 -05:00
label . innerHTML = "CLICK TO COPY" ;
2022-03-21 22:33:19 -04:00
} , 200 ) ;
} ) . on ( "click" , ".key" , function ( ) {
2022-01-16 20:35:24 -05:00
let label = $ ( this ) . parent ( ) . siblings ( ) . children ( ) [ 1 ] ;
window . configurations . copyToClipboard ( $ ( this ) ) ;
2021-12-29 23:26:15 -05:00
label . innerHTML = "COPIED!" ;
} ) ;
/ * *
2022-01-16 20:35:24 -05:00
* Handle when interval button got clicked
2021-12-29 23:26:15 -05:00
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".update_interval" , function ( ) {
2021-12-27 21:01:02 -05:00
$ ( ".interval-btn-group button" ) . removeClass ( "active" ) ;
2021-12-29 23:26:15 -05:00
let _new = $ ( this ) ;
2021-12-29 12:17:44 -05:00
_new . addClass ( "active" ) ;
let interval = $ ( this ) . data ( "refresh-interval" ) ;
2022-03-21 22:33:19 -04:00
if ( [ 5000 , 10000 , 30000 , 60000 ] . includes ( interval ) ) {
2022-03-07 09:29:47 -05:00
window . configurations . updateRefreshInterval ( interval ) ;
}
// $.ajax({
// method:"POST",
// data: "interval="+$(this).data("refresh-interval"),
// url: "/update_dashboard_refresh_interval",
// success: function (res){
// window.configurations.updateRefreshInterval(res, interval);
// }
// });
2021-12-27 21:01:02 -05:00
} ) ;
2021-12-29 23:26:15 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when refresh button got clicked
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".refresh" , function ( ) {
2022-01-16 20:35:24 -05:00
window . configurations . loadPeers ( $ ( '#search_peer_textbox' ) . val ( ) ) ;
2021-08-14 23:30:05 -04:00
} ) ;
2021-09-03 17:32:51 -04:00
2022-01-16 20:35:24 -05:00
/ * *
* Handle when display mode button got clicked
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".display_mode" , function ( ) {
2021-12-27 21:01:02 -05:00
$ ( ".display-btn-group button" ) . removeClass ( "active" ) ;
$ ( this ) . addClass ( "active" ) ;
2022-03-07 09:29:47 -05:00
window . localStorage . setItem ( "displayMode" , $ ( this ) . data ( "display-mode" ) ) ;
window . configurations . updateDisplayMode ( ) ;
2022-03-21 22:33:19 -04:00
if ( $ ( this ) . data ( "display-mode" ) === "list" ) {
Array ( $ ( ".peer_list" ) . children ( ) ) . forEach ( function ( child ) {
2022-03-07 09:29:47 -05:00
$ ( child ) . removeClass ( ) . addClass ( "col-12" ) ;
} ) ;
window . configurations . showToast ( "Displaying as List" ) ;
2022-03-21 22:33:19 -04:00
} else {
Array ( $ ( ".peer_list" ) . children ( ) ) . forEach ( function ( child ) {
2022-03-07 09:29:47 -05:00
$ ( child ) . removeClass ( ) . addClass ( "col-sm-6 col-lg-4" ) ;
2022-03-21 22:33:19 -04:00
} ) ;
window . configurations . showToast ( "Displaying as Grids" ) ;
2022-03-07 09:29:47 -05:00
}
2022-01-06 15:17:43 -05:00
} ) ;
2022-01-08 15:26:17 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* === === === === === ==
* Configuration Menu
* === === === === === ==
* /
2022-01-08 15:26:17 -05:00
let $setting _btn _menu = $ ( ".setting_btn_menu" ) ;
2022-03-21 22:33:19 -04:00
$setting _btn _menu . css ( "top" , ( $setting _btn _menu . height ( ) + 54 ) * ( - 1 ) ) ;
2022-01-08 15:26:17 -05:00
let $setting _btn = $ ( ".setting_btn" ) ;
2022-01-16 20:35:24 -05:00
/ * *
* When the menu button got clicked
* /
2022-03-21 22:33:19 -04:00
$setting _btn . on ( "click" , function ( ) {
if ( $setting _btn _menu . hasClass ( "show" ) ) {
2022-01-08 15:26:17 -05:00
$setting _btn _menu . removeClass ( "showing" ) ;
2022-03-21 22:33:19 -04:00
setTimeout ( function ( ) {
2022-01-08 15:26:17 -05:00
$setting _btn _menu . removeClass ( "show" ) ;
2022-01-12 19:53:36 -05:00
} , 201 ) ;
2022-03-21 22:33:19 -04:00
} else {
$setting _btn _menu . addClass ( "show" ) ;
setTimeout ( function ( ) {
$setting _btn _menu . addClass ( "showing" ) ;
} , 10 ) ;
2022-01-08 15:26:17 -05:00
}
2022-01-16 20:35:24 -05:00
} ) ;
/ * *
* Whenever the user clicked , if it is outside the menu and the menu is opened , hide the menu
* /
2022-03-21 22:33:19 -04:00
$ ( "html" ) . on ( "click" , function ( r ) {
if ( document . querySelector ( ".setting_btn" ) !== r . target ) {
if ( ! document . querySelector ( ".setting_btn" ) . contains ( r . target ) ) {
if ( ! document . querySelector ( ".setting_btn_menu" ) . contains ( r . target ) ) {
2022-01-08 15:26:17 -05:00
$setting _btn _menu . removeClass ( "showing" ) ;
2022-03-21 22:33:19 -04:00
setTimeout ( function ( ) {
2022-01-08 15:26:17 -05:00
$setting _btn _menu . removeClass ( "show" ) ;
2022-01-12 19:53:36 -05:00
} , 310 ) ;
2022-01-08 15:26:17 -05:00
}
}
}
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* === === === === === === ==
* Delete Peers by Bulk
* === === === === === === ==
* /
/ * *
* When delete peers by bulk clicked
* /
2022-01-08 15:26:17 -05:00
$ ( "#delete_peers_by_bulk_btn" ) . on ( "click" , ( ) => {
let $delete _bulk _modal _list = $ ( "#delete_bulk_modal .list-group" ) ;
$delete _bulk _modal _list . html ( '' ) ;
peers . forEach ( ( peer ) => {
2022-01-16 20:35:24 -05:00
let name ;
2022-03-21 22:33:19 -04:00
if ( peer . name === "" ) { name = "Untitled Peer" ; } else { name = peer . name ; }
2022-01-16 20:35:24 -05:00
$delete _bulk _modal _list . append ( '<a class="list-group-item list-group-item-action delete-bulk-peer-item" style="cursor: pointer" data-id="' +
2022-03-21 22:33:19 -04:00
peer . id + '" data-name="' + name + '">' + name + '<br><code>' + peer . id + '</code></a>' ) ;
2022-01-08 15:26:17 -05:00
} ) ;
2022-01-16 20:35:24 -05:00
window . configurations . deleteBulkModal ( ) . toggle ( ) ;
2022-01-08 15:26:17 -05:00
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* When the item or tag of delete peers by bulk got clicked
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".delete-bulk-peer-item" , function ( ) {
2022-01-16 20:35:24 -05:00
window . configurations . toggleDeleteByBulkIP ( $ ( this ) ) ;
2022-03-21 22:33:19 -04:00
} ) . on ( "click" , ".delete-peer-bulk-badge" , function ( ) {
2022-01-16 20:35:24 -05:00
window . configurations . toggleDeleteByBulkIP ( $ ( ".delete-bulk-peer-item[data-id='" + $ ( this ) . data ( "id" ) + "']" ) ) ;
2022-01-08 15:26:17 -05:00
} ) ;
let $selected _peer _list = document . getElementById ( "selected_peer_list" ) ;
2022-01-16 20:35:24 -05:00
/ * *
* The change observer to observe when user choose 1 or more peers to delete
* @ type { MutationObserver }
* /
2022-03-21 22:33:19 -04:00
let changeObserver = new MutationObserver ( function ( ) {
if ( $selected _peer _list . hasChildNodes ( ) ) {
2022-01-08 15:26:17 -05:00
$ ( "#confirm_delete_bulk_peers" ) . removeAttr ( "disabled" ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-08 15:26:17 -05:00
$ ( "#confirm_delete_bulk_peers" ) . attr ( "disabled" , "disabled" ) ;
}
} ) ;
changeObserver . observe ( $selected _peer _list , {
attributes : true ,
childList : true ,
characterData : true
2022-01-16 20:35:24 -05:00
} ) ;
2022-01-08 15:26:17 -05:00
2022-01-16 20:35:24 -05:00
let confirm _delete _bulk _peers _interval ;
2022-01-08 15:26:17 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* When the user clicked the delete button in the delete peers by bulk
* /
2022-03-21 22:33:19 -04:00
$ ( "#confirm_delete_bulk_peers" ) . on ( "click" , function ( ) {
2022-01-08 15:26:17 -05:00
let btn = $ ( this ) ;
2022-03-21 22:33:19 -04:00
if ( confirm _delete _bulk _peers _interval !== undefined ) {
2022-01-08 15:26:17 -05:00
clearInterval ( confirm _delete _bulk _peers _interval ) ;
confirm _delete _bulk _peers _interval = undefined ;
btn . html ( "Delete" ) ;
2022-03-21 22:33:19 -04:00
} else {
2022-01-08 15:26:17 -05:00
let timer = 5 ;
btn . html ( ` Deleting in ${ timer } secs... Click to cancel ` ) ;
2022-03-21 22:33:19 -04:00
confirm _delete _bulk _peers _interval = setInterval ( function ( ) {
2022-01-08 15:26:17 -05:00
timer -= 1 ;
btn . html ( ` Deleting in ${ timer } secs... Click to cancel ` ) ;
2022-03-21 22:33:19 -04:00
if ( timer === 0 ) {
2022-01-08 15:26:17 -05:00
btn . html ( ` Deleting... ` ) ;
btn . attr ( "disabled" , "disabled" ) ;
let ips = [ ] ;
$selected _peer _list . childNodes . forEach ( ( ele ) => ips . push ( ele . dataset . id ) ) ;
2022-03-04 22:09:01 -05:00
window . configurations . deletePeers ( window . configurations . getConfigurationName ( ) , ips ) ;
2022-01-08 15:26:17 -05:00
clearInterval ( confirm _delete _bulk _peers _interval ) ;
confirm _delete _bulk _peers _interval = undefined ;
}
2022-01-16 20:35:24 -05:00
} , 1000 ) ;
2022-01-08 15:26:17 -05:00
}
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* Select all peers to delete
* /
2022-03-21 22:33:19 -04:00
$ ( "#select_all_delete_bulk_peers" ) . on ( "click" , function ( ) {
$ ( ".delete-bulk-peer-item" ) . each ( function ( ) {
if ( ! $ ( this ) . hasClass ( "active" ) ) {
window . configurations . toggleDeleteByBulkIP ( $ ( this ) ) ;
}
} ) ;
2022-01-08 15:26:17 -05:00
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* When delete peers by bulk window is hidden
* /
2022-03-21 22:33:19 -04:00
$ ( window . configurations . deleteBulkModal ( ) . _element ) . on ( "hidden.bs.modal" , function ( ) {
$ ( ".delete-bulk-peer-item" ) . each ( function ( ) {
if ( $ ( this ) . hasClass ( "active" ) ) {
window . configurations . toggleDeleteByBulkIP ( $ ( this ) ) ;
}
} ) ;
2022-01-12 19:53:36 -05:00
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* === === === === ==
* Download Peers
* === === === === ==
* /
2022-01-12 19:53:36 -05:00
2022-01-16 20:35:24 -05:00
/ * *
* When the download peers button got clicked
* /
2022-03-21 22:33:19 -04:00
$body . on ( "click" , ".btn-download-peer" , function ( e ) {
2022-01-12 19:53:36 -05:00
e . preventDefault ( ) ;
let link = $ ( this ) . attr ( "href" ) ;
$ . ajax ( {
"url" : link ,
"method" : "GET" ,
2022-03-21 22:33:19 -04:00
success : function ( res ) {
2022-01-16 20:35:24 -05:00
window . configurations . downloadOneConfig ( res ) ;
2022-01-12 19:53:36 -05:00
}
} ) ;
} ) ;
2022-01-16 20:35:24 -05:00
/ * *
* When the download all peers got clicked
* /
2022-03-21 22:33:19 -04:00
$ ( "#download_all_peers" ) . on ( "click" , function ( ) {
2022-01-12 19:53:36 -05:00
$ . ajax ( {
2022-03-04 22:09:01 -05:00
"url" : ` /download_all/ ${ window . configurations . getConfigurationName ( ) } ` ,
2022-01-12 19:53:36 -05:00
"method" : "GET" ,
2022-03-21 22:33:19 -04:00
success : function ( res ) {
if ( res . peers . length > 0 ) {
window . wireguard . generateZipFiles ( res ) ;
window . configurations . showToast ( "Peers' zip file download successful!" ) ;
} else {
2022-01-16 20:35:24 -05:00
window . configurations . showToast ( "Oops! There are no peer can be download." ) ;
2022-01-12 19:53:36 -05:00
}
}
} ) ;
2022-01-16 20:35:24 -05:00
} ) ;