2022-10-09 10:23:45 +00:00
$ ( function ( ) {
$ ( ".redirectUpstream" ) . on ( "click" , function ( ) {
resetProxySettings ( ) ;
$ ( "#tabs" ) . tabs ( "option" , "active" , 1 ) ;
$ ( "#serv" ) . selectmenu ( "open" ) ;
} ) ;
$ ( "#serv" ) . on ( 'selectmenuchange' , function ( ) {
$ ( '#name' ) . focus ( ) ;
} ) ;
var add _server _var = '<br /><input name="servers" title="Backend IP" size=14 placeholder="xxx.xxx.xxx.xxx" class="form-control second-server" style="margin: 2px 0 4px 0;">: ' +
'<input name="server_port" required title="Backend port" size=3 placeholder="yyy" class="form-control second-server add_server_number" type="number"> ' +
2023-05-17 17:38:13 +00:00
'max_fails: <input name="max_fails" required title="By default, the number of unsuccessful attempts is set to 1" size=5 value="1" class="form-control add_server_number" type="number">' +
2022-10-09 10:23:45 +00:00
' fail_timeout: <input name="fail_timeout" required title="By default, the number of unsuccessful attempts is set to 1" size=5 value="1" class="form-control add_server_number" type="number">s'
$ ( '[name=add-server-input]' ) . click ( function ( ) {
$ ( "[name=add_servers]" ) . append ( add _server _var ) ;
changePortCheckFromServerPort ( ) ;
} ) ;
$ ( '.advance-show-button' ) . click ( function ( ) {
$ ( '.advance' ) . fadeIn ( ) ;
$ ( '.advance-show-button' ) . css ( 'display' , 'none' ) ;
$ ( '.advance-hide-button' ) . css ( 'display' , 'block' ) ;
return false ;
} ) ;
$ ( '.advance-hide-button' ) . click ( function ( ) {
$ ( '.advance' ) . fadeOut ( ) ;
$ ( '.advance-show-button' ) . css ( 'display' , 'block' ) ;
$ ( '.advance-hide-button' ) . css ( 'display' , 'none' ) ;
return false ;
} ) ;
} ) ;
function resetProxySettings ( ) {
2023-09-17 09:42:39 +00:00
$ ( '[name=upstream]' ) . val ( '' ) ;
$ ( 'input:checkbox' ) . prop ( "checked" , false ) ;
$ ( '[name=check-servers]' ) . prop ( "checked" , true ) ;
$ ( 'input:checkbox' ) . checkboxradio ( "refresh" ) ;
$ ( '.advance-show' ) . fadeIn ( ) ;
$ ( '.advance' ) . fadeOut ( ) ;
$ ( '[name=mode' ) . val ( 'http' ) ;
$ ( 'select' ) . selectmenu ( 'refresh' ) ;
$ ( "#path-cert-listen" ) . attr ( 'required' , false ) ;
$ ( "#path-cert-frontend" ) . attr ( 'required' , false ) ;
2022-10-09 10:23:45 +00:00
}
function checkIsServerFiled ( select _id , message = 'Select a server first' ) {
if ( $ ( select _id ) . val ( ) == null || $ ( select _id ) . val ( ) == '' ) {
toastr . warning ( message ) ;
return false ;
}
return true ;
}
function generateConfig ( form _name ) {
2023-09-17 09:42:39 +00:00
var frm = $ ( '#' + form _name ) ;
2022-10-09 10:23:45 +00:00
if ( form _name == 'add-upstream' ) {
serv = '#serv'
name _id = '#name'
}
2023-09-17 09:42:39 +00:00
if ( ! checkIsServerFiled ( serv ) ) return false ;
if ( ! checkIsServerFiled ( name _id , 'The name cannot be empty' ) ) return false ;
2022-10-09 10:23:45 +00:00
var input = $ ( "<input>" )
. attr ( "name" , "generateconfig" ) . val ( "1" ) . attr ( "type" , "hidden" ) . attr ( "id" , "generateconfig" ) ;
2023-09-17 09:42:39 +00:00
$ ( '#' + form _name + ' input[name=acl_then_value]' ) . each ( function ( ) {
if ( ! $ ( this ) . val ( ) ) {
2022-10-09 10:23:45 +00:00
$ ( this ) . val ( 'IsEmptY' )
}
} ) ;
2023-09-17 09:42:39 +00:00
$ ( '#' + form _name + ' input[name=ip]' ) . each ( function ( ) {
if ( ! $ ( this ) . val ( ) ) {
2022-10-09 10:23:45 +00:00
$ ( this ) . val ( 'IsEmptY' )
}
} ) ;
2023-09-17 09:42:39 +00:00
$ ( '#' + form _name + ' input[name=port]' ) . each ( function ( ) {
if ( ! $ ( this ) . val ( ) ) {
2022-10-09 10:23:45 +00:00
$ ( this ) . val ( 'IsEmptY' )
}
} ) ;
frm . append ( input ) ;
2023-09-17 09:42:39 +00:00
var generated _title = $ ( '#translate' ) . attr ( 'data-generated_config' ) ;
2022-10-09 10:23:45 +00:00
$ . ajax ( {
url : frm . attr ( 'action' ) ,
data : frm . serialize ( ) ,
type : frm . attr ( 'method' ) ,
success : function ( data ) {
if ( data . indexOf ( 'error: ' ) != '-1' || data . indexOf ( 'Fatal' ) != '-1' ) {
toastr . clear ( ) ;
toastr . error ( data ) ;
} else {
$ ( '#dialog-confirm-body' ) . text ( data ) ;
$ ( "#dialog-confirm-cert" ) . dialog ( {
resizable : false ,
height : "auto" ,
width : 650 ,
modal : true ,
2023-09-17 09:42:39 +00:00
title : generated _title ,
2022-10-09 10:23:45 +00:00
buttons : {
Ok : function ( ) {
$ ( this ) . dialog ( "close" ) ;
}
}
} ) ;
}
}
} ) ;
$ ( "#generateconfig" ) . remove ( ) ;
2023-09-17 09:42:39 +00:00
$ ( '#' + form _name + ' input[name=ip]' ) . each ( function ( ) {
if ( $ ( this ) . val ( ) == 'IsEmptY' ) {
2022-10-09 10:23:45 +00:00
$ ( this ) . val ( '' )
}
} ) ;
2023-09-17 09:42:39 +00:00
$ ( '#' + form _name + ' input[name=port]' ) . each ( function ( ) {
if ( $ ( this ) . val ( ) == 'IsEmptY' ) {
2022-10-09 10:23:45 +00:00
$ ( this ) . val ( '' )
}
} ) ;
}
function addProxy ( form _name ) {
var frm = $ ( '#' + form _name ) ;
if ( form _name == 'add-upstream' ) {
serv = '#serv'
name _id = '#name'
}
if ( ! checkIsServerFiled ( serv ) ) return false ;
if ( ! checkIsServerFiled ( name _id , 'The name cannot be empty' ) ) return false ;
$ ( '#' + form _name + ' input[name=ip]' ) . each ( function ( ) {
if ( $ ( this ) . val ( ) . length === 0 ) {
$ ( this ) . val ( 'IsEmptY' )
}
} ) ;
$ ( '#' + form _name + ' input[name=port]' ) . each ( function ( ) {
if ( $ ( this ) . val ( ) . length === 0 ) {
$ ( this ) . val ( 'IsEmptY' )
}
} ) ;
$ . ajax ( {
url : frm . attr ( 'action' ) ,
data : frm . serialize ( ) ,
type : frm . attr ( 'method' ) ,
success : function ( data ) {
data = data . replace ( /\n/g , "<br>" ) ;
if ( data . indexOf ( 'error: ' ) != '-1' || data . indexOf ( 'Fatal' ) != '-1' ) {
returnNiceCheckingConfig ( data ) ;
} else if ( data == '' ) {
toastr . clear ( ) ;
toastr . error ( 'error: Proxy cannot be empty' ) ;
} else {
toastr . clear ( ) ;
returnNiceCheckingConfig ( data ) ;
toastr . info ( 'Section has been added. Do not forget to restart the server' ) ;
resetProxySettings ( ) ;
}
}
} ) ;
$ ( '#' + form _name + ' input[name=ip]' ) . each ( function ( ) {
if ( $ ( this ) . val ( ) == 'IsEmptY' ) {
$ ( this ) . val ( '' )
}
} ) ;
$ ( '#' + form _name + ' input[name=port]' ) . each ( function ( ) {
if ( $ ( this ) . val ( ) == 'IsEmptY' ) {
$ ( this ) . val ( '' )
}
} ) ;
}