2020-06-19 20:43:08 +03:00
'use strict' ;
'require fs' ;
'require form' ;
'require tools.widgets as widgets' ;
2022-05-05 18:28:32 +03:00
'require uci' ;
'require ui' ;
2021-12-05 19:18:32 +03:00
'require view' ;
2020-06-19 20:43:08 +03:00
'require view.ruantiblock.tools as tools' ;
2021-12-05 19:18:32 +03:00
return view . extend ( {
2023-01-09 18:11:41 +03:00
parsers : { } ,
2021-11-04 18:57:08 +03:00
appStatusCode : null ,
depends : function ( elem , key , array , empty = true ) {
if ( empty && array . length === 0 ) {
elem . depends ( key , '_dummy' ) ;
} else {
array . forEach ( e => elem . depends ( key , e ) ) ;
} ;
} ,
validateIpPort : function ( section , value ) {
return ( /^$|^([0-9]{1,3}\.){3}[0-9]{1,3}(#[\d]{2,5})?$/ . test ( value ) ) ? true : _ ( 'Expecting:' )
+ ` ${ _ ( 'One of the following:' ) } \n - ${ _ ( 'valid IP address' ) } \n - ${ _ ( 'valid address#port' ) } \n ` ;
} ,
load : function ( ) {
return Promise . all ( [
L . resolveDefault ( fs . exec ( tools . execPath , [ 'raw-status' ] ) , 1 ) ,
2023-01-09 18:11:41 +03:00
L . resolveDefault ( fs . list ( tools . parsersDir ) , null ) ,
uci . load ( tools . appName ) ,
2021-11-04 18:57:08 +03:00
] ) . catch ( e => {
ui . addNotification ( null , E ( 'p' , _ ( 'Unable to read the contents' )
+ ': %s [ %s ]' . format (
e . message , tools . parsersDir
) ) ) ;
} ) ;
} ,
render : function ( data ) {
if ( ! data ) {
return ;
} ;
this . appStatusCode = data [ 0 ] . code ;
2023-01-09 18:11:41 +03:00
let p _dir _arr = data [ 1 ] ;
let curent _module = uci . get ( tools . appName , 'config' , 'bllist_module' ) ;
let curent _preset = uci . get ( tools . appName , 'config' , 'bllist_preset' ) ;
2021-11-04 18:57:08 +03:00
if ( p _dir _arr ) {
p _dir _arr . forEach ( e => {
let fname = e . name ;
if ( fname . startsWith ( 'ruab_parser' ) ) {
2023-01-09 18:11:41 +03:00
this . parsers [ fname ] = tools . parsersDir + '/' + fname ;
2021-11-04 18:57:08 +03:00
} ;
} ) ;
} ;
2023-01-09 18:11:41 +03:00
let availableParsers = Object . keys ( this . parsers ) . length > 0 ;
if ( ! availableParsers ) {
for ( let i of Object . keys ( tools . blacklistPresets ) ) {
if ( ! new RegExp ( '^($|' + tools . appName + ')' ) . test ( i ) && i !== curent _preset ) {
delete tools . blacklistPresets [ i ] ;
} ;
} ;
} ;
if ( curent _module ) {
this . parsers [ curent _module . match ( /([^/]*)$/ ) [ 0 ] ] = curent _module ;
} ;
2021-11-04 18:57:08 +03:00
let ip _filter _edit = new tools . fileEditDialog (
tools . ipFilterFile ,
_ ( 'IP filter' ) ,
_ ( 'Patterns can be strings or regular expressions. Each pattern in a separate line, the symbol <code>#</code> in the first position of the line - comments on the line.<br />Examples (dot is a special character):' ) +
'<br /><code>128[.]199[.]0[.]0/16<br />34[.]217[.]90[.]52<br />162[.]13[.]190[.]</code>'
) ;
let fqdn _filter _edit = new tools . fileEditDialog (
tools . fqdnFilterFile ,
_ ( 'FQDN filter' ) ,
_ ( 'Patterns can be strings or regular expressions. Each pattern in a separate line, the symbol <code>#</code> in the first position of the line - comments on the line.<br />Examples:' ) +
'<br /><code>poker<br />[ck]?a[sz]ino?<br />[vw]ulkan<br />slots?</code>'
) ;
let user _entries _edit = new tools . fileEditDialog (
tools . userEntriesFile ,
_ ( 'User entries' ) ,
_ ( 'One entry (IP, CIDR or FQDN) per line. In the FQDN records, you can specify the DNS server for resolving this domain (separated by a space). You can also comment on lines (<code>#</code> is the first character of a line).<br />Examples:' ) +
'<br /><code>#comment<br />domain.net<br />sub.domain.com 8.8.8.8<br />sub.domain.com 8.8.8.8#53<br />74.125.131.19<br />74.125.0.0/16</code>'
) ;
let torrc _edit = new tools . fileEditDialog (
tools . torrcFile ,
_ ( 'Tor configuration file' ) ,
null ,
function ( rc ) {
return tools . getInitStatus ( 'tor' ) . then ( res => {
if ( res ) {
return tools . handleServiceAction ( 'tor' , 'restart' ) ;
} ;
} ) ;
}
) ;
let m , s , o ;
m = new form . Map ( tools . appName , _ ( 'Ruantiblock' ) + ' - ' + _ ( 'Settings' ) ) ;
s = m . section ( form . NamedSection , 'config' ) ;
s . anonymous = true ;
s . addremove = false ;
/* Main settings tab */
s . tab ( 'main_settings' , _ ( 'Main settings' ) ) ;
// PROXY_MODE
if ( this . appStatusCode == 1 || this . appStatusCode == 2 ) {
o = s . taboption ( 'main_settings' , form . ListValue , 'proxy_mode' ,
_ ( 'Proxy mode' ) ) ;
o . value ( '1' , 'Tor' ) ;
o . value ( '2' , 'VPN' ) ;
2023-01-09 18:11:41 +03:00
o . value ( '3' , _ ( 'Transparent proxy' ) ) ;
2021-11-04 18:57:08 +03:00
} ;
// PROXY_LOCAL_CLIENTS
let proxy _local _clients = s . taboption ( 'main_settings' , form . Flag , 'proxy_local_clients' ,
_ ( "Apply proxy rules to router application traffic" ) ) ;
proxy _local _clients . rmempty = false ;
2022-05-05 18:28:32 +03:00
// ENABLE_LOGGING
o = s . taboption ( 'main_settings' , form . Flag , 'enable_logging' ,
2021-11-04 18:57:08 +03:00
_ ( 'Logging events' ) ) ;
o . rmempty = false ;
2022-05-10 01:22:25 +03:00
// update_at_startup
o = s . taboption ( 'main_settings' , form . Flag , 'update_at_startup' ,
_ ( 'Update at startup' ) ) ;
o . description = _ ( 'Update blacklist after system startup' ) ;
o . rmempty = false ;
2021-11-04 18:57:08 +03:00
// IPSET_CLEAR_SETS
o = s . taboption ( 'main_settings' , form . Flag , 'ipset_clear_sets' ,
_ ( 'Clean up ipsets before updating blacklist' ) ) ;
o . description = _ ( 'Reduces RAM consumption during update' ) ;
o . rmempty = false ;
2022-05-06 03:27:08 +03:00
// ALLOWED_HOSTS_MODE
o = s . taboption ( 'main_settings' , form . ListValue , 'allowed_hosts_mode' ,
2022-05-05 18:28:32 +03:00
_ ( 'Host filter' ) ) ;
o . value ( '0' , _ ( 'Disabled' ) ) ;
o . value ( '1' , _ ( 'Only listed hosts' ) ) ;
o . value ( '2' , _ ( 'All hosts except listed' ) ) ;
o . description = _ ( 'Restriction of hosts that are allowed to bypass blocking' ) ;
2022-05-28 20:17:52 +03:00
// ALLOWED_HOSTS_LIST
2022-05-06 03:27:08 +03:00
o = s . taboption ( 'main_settings' , form . DynamicList , 'allowed_hosts_list' ,
2022-05-05 18:28:32 +03:00
_ ( 'IP addresses of hosts' ) ) ;
o . datatype = "ip4addr" ;
2021-11-04 18:57:08 +03:00
if ( this . appStatusCode == 1 || this . appStatusCode == 2 ) {
/* Tor tab */
s . tab ( 'tor_settings' , _ ( 'Tor mode' ) ) ;
// TOR_TRANS_PORT
o = s . taboption ( 'tor_settings' , form . Value , 'tor_trans_port' ,
2023-01-09 18:11:41 +03:00
_ ( 'Transparent proxy port' ) ) ;
2021-11-04 18:57:08 +03:00
o . rmempty = false ;
o . datatype = "port" ;
2022-05-05 18:28:32 +03:00
//TOR_ALLOW_UDP
o = s . taboption ( 'tor_settings' , form . Flag , 'tor_allow_udp' ,
_ ( "Send UDP traffic to Tor" ) ) ;
o . rmempty = false ;
2021-11-04 18:57:08 +03:00
// ONION_DNS_ADDR
o = s . taboption ( 'tor_settings' , form . Value , 'onion_dns_addr' ,
_ ( "Optional DNS resolver for '.onion' zone" ) , '<code>ipaddress#port</code>' ) ;
o . rmempty = false ;
o . validate = this . validateIpPort ;
// Torrc edit dialog
o = s . taboption ( 'tor_settings' , form . Button , '_torrc_btn' ,
_ ( 'Tor configuration file' ) ) ;
o . onclick = ( ) => torrc _edit . show ( ) ;
o . inputtitle = _ ( 'Edit' ) ;
o . inputstyle = 'edit btn' ;
/* VPN tab */
s . tab ( 'vpn_settings' , _ ( 'VPN mode' ) ) ;
// IF_VPN
o = s . taboption ( 'vpn_settings' , widgets . DeviceSelect , 'if_vpn' ,
_ ( 'VPN interface' ) ) ;
o . multiple = false ;
o . noaliases = true ;
o . rmempty = false ;
2023-01-09 18:11:41 +03:00
o . default = 'tun0' ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
/* Proxy tab */
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
s . tab ( 'proxy_settings' , _ ( 'Transparent proxy mode' ) ) ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// T_PROXY_PORT_TCP
o = s . taboption ( 'proxy_settings' , form . Value , 't_proxy_port_tcp' ,
_ ( 'Transparent proxy TCP port' ) ) ;
o . rmempty = false ;
o . datatype = "port" ;
//T_PROXY_ALLOW_UDP
o = s . taboption ( 'proxy_settings' , form . Flag , 't_proxy_allow_udp' ,
_ ( "Send UDP traffic to transparent proxy" ) ) ;
o . rmempty = false ;
// T_PROXY_PORT_UDP
o = s . taboption ( 'proxy_settings' , form . Value , 't_proxy_port_udp' ,
_ ( 'Transparent proxy UDP port' ) ) ;
o . rmempty = false ;
o . datatype = "port" ;
} ;
/* Blacklist module tab */
s . tab ( 'blacklist_tab' , _ ( 'Blacklist settings' ) ) ;
2021-11-04 18:57:08 +03:00
2022-05-05 18:28:32 +03:00
// BLLIST_PRESET
2023-01-09 18:11:41 +03:00
let bllist _preset = s . taboption ( 'blacklist_tab' , form . ListValue ,
2022-05-05 18:28:32 +03:00
'bllist_preset' , _ ( 'Blacklist update mode' ) ) ;
bllist _preset . description = _ ( "Blacklist sources" ) + ':' ;
2023-01-09 18:11:41 +03:00
bllist _preset . value ( '' , _ ( 'user entries only' ) ) ;
2022-05-05 18:28:32 +03:00
Object . entries ( tools . blacklistPresets ) . forEach ( e => {
2023-01-09 18:11:41 +03:00
bllist _preset . value ( e [ 0 ] , ( ( e [ 1 ] [ 1 ] ) ? ` ${ e [ 1 ] [ 0 ] } - ${ e [ 1 ] [ 1 ] } ` : e [ 1 ] [ 0 ] ) ) ;
2022-05-05 18:28:32 +03:00
} ) ;
let bllist _sources = { } ;
2023-01-09 18:11:41 +03:00
Object . values ( tools . blacklistPresets ) . forEach ( v => { bllist _sources [ v [ 0 ] ] = v [ 2 ] } ) ;
2022-05-05 18:28:32 +03:00
Object . entries ( bllist _sources ) . forEach ( e => {
2023-01-09 18:11:41 +03:00
if ( e [ 1 ] ) {
bllist _preset . description += ` <br /> ${ e [ 0 ] } - <a href=" ${ e [ 1 ] } " target="_blank"> ${ e [ 1 ] } </a> ` ;
} ;
2022-05-05 18:28:32 +03:00
} ) ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_MODULE
let bllist _module = s . taboption ( 'blacklist_tab' , form . ListValue ,
'bllist_module' , _ ( 'Blacklist module' ) + '*' ) ;
bllist _module . value ( '' , _ ( 'disabled' ) ) ;
bllist _module . depends ( { bllist _preset : new RegExp ( '^($|' + tools . appName + ')' ) , '!reverse' : true } ) ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
Object . entries ( this . parsers ) . forEach (
e => bllist _module . value ( e [ 1 ] , e [ 0 ] ) ) ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
if ( availableParsers ) {
bllist _preset . description += '<br /> ( * - ' + _ ( 'requires installed blacklist module' ) + ' )' ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
/* Parser settings tab */
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
s . tab ( 'parser_settings_tab' , _ ( 'Module settings' ) ) ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_FQDN_FILTER
o = s . taboption ( 'parser_settings_tab' , form . Flag , 'bllist_fqdn_filter' ,
_ ( "Enable FQDN filter" ) ) ;
o . description = _ ( 'Exclude domains from blacklist by FQDN filter patterns' ) ;
o . rmempty = false ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_FQDN_FILTER_FILE edit dialog
o = s . taboption ( 'parser_settings_tab' , form . Button , '_fqdn_filter_btn' ,
_ ( "FQDN filter" ) ) ;
o . onclick = ( ) => fqdn _filter _edit . show ( ) ;
o . inputtitle = _ ( 'Edit' ) ;
o . inputstyle = 'edit btn' ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_SD_LIMIT
o = s . taboption ( 'parser_settings_tab' , form . Value , 'bllist_sd_limit' ,
_ ( "Subdomains limit" ) ) ;
o . description = _ ( 'The number of subdomains in the domain, upon reaching which the entire 2nd level domain is added to the list' ) ;
o . rmempty = false ;
o . datatype = 'uinteger' ;
// BLLIST_GR_EXCLUDED_SLD
o = s . taboption ( 'parser_settings_tab' , form . DynamicList , 'bllist_gr_excluded_sld' ,
_ ( '2nd level domains that are excluded from optimization' ) ) ;
o . description = _ ( 'e.g:' ) + ' <code>livejournal.com</code>' ;
o . placeholder = _ ( 'e.g:' ) + ' livejournal.com' ;
o . datatype = "hostname" ;
// BLLIST_ENABLE_IDN
o = s . taboption ( 'parser_settings_tab' , form . Flag , 'bllist_enable_idn' ,
_ ( "Convert cyrillic domains to punycode" ) ) ;
o . rmempty = false ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_ALT_NSLOOKUP
o = s . taboption ( 'parser_settings_tab' , form . Flag , 'bllist_alt_nslookup' ,
_ ( 'Use optional DNS resolver' ) ) ;
o . rmempty = false ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_ALT_DNS_ADDR
o = s . taboption ( 'parser_settings_tab' , form . Value , 'bllist_alt_dns_addr' ,
_ ( "Optional DNS resolver" ) , '<code>ipaddress[#port]</code>' ) ;
o . rmempty = false ;
o . validate = this . validateIpPort ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_IP_FILTER
o = s . taboption ( 'parser_settings_tab' , form . Flag , 'bllist_ip_filter' ,
_ ( "Enable IP filter" ) ) ;
o . description = _ ( 'Exclude IP addresses from blacklist by IP filter patterns' ) ;
o . rmempty = false ;
2021-11-04 18:57:08 +03:00
2023-01-09 18:11:41 +03:00
// BLLIST_IP_FILTER_FILE edit dialog
o = s . taboption ( 'parser_settings_tab' , form . Button , '_ip_filter_btn' ,
_ ( "IP filter" ) ) ;
o . onclick = ( ) => ip _filter _edit . show ( ) ;
o . inputtitle = _ ( 'Edit' ) ;
o . inputstyle = 'edit btn' ;
// BLLIST_IP_LIMIT
o = s . taboption ( 'parser_settings_tab' , form . Value , 'bllist_ip_limit' , _ ( "IP limit" ) ) ;
o . description = _ ( "The number of IP addresses in the subnet, upon reaching which the entire '/24' subnet is added to the list" ) ;
o . rmempty = false ;
o . datatype = 'uinteger' ;
// BLLIST_GR_EXCLUDED_NETS
o = s . taboption ( 'parser_settings_tab' , form . DynamicList , 'bllist_gr_excluded_nets' ) ;
o . title = _ ( 'IP subnet patterns (/24) that are excluded from optimization' ) ;
o . description = _ ( 'e.g:' ) + ' <code>192.168.1.</code>' ;
o . placeholder = _ ( 'e.g:' ) + ' 192.168.1.' ;
o . validate = ( section , value ) => {
return ( /^$|^([0-9]{1,3}[.]){3}$/ . test ( value ) ) ? true : _ ( 'Expecting:' )
+ ' ' + _ ( 'net pattern' ) + ' (' + _ ( 'e.g:' ) + ' 192.168.3.)\n' ;
} ;
// BLLIST_SUMMARIZE_IP
o = s . taboption ( 'parser_settings_tab' , form . Flag , 'bllist_summarize_ip' ,
_ ( "Summarize IP ranges" ) ) ;
o . rmempty = false ;
// BLLIST_SUMMARIZE_CIDR
o = s . taboption ( 'parser_settings_tab' , form . Flag , 'bllist_summarize_cidr' ,
_ ( "Summarize '/24' networks" ) ) ;
o . rmempty = false ;
} ;
2021-11-04 18:57:08 +03:00
/* User entries tab */
s . tab ( 'user_entries_tab' , _ ( 'User entries' ) ) ;
// ADD_USER_ENTRIES
o = s . taboption ( 'user_entries_tab' , form . Flag , 'add_user_entries' ,
_ ( 'Enable' ) , _ ( "Add user entries to the blacklist when updating" ) ) ;
o . rmempty = false ;
o . default = 0 ;
2023-01-09 18:11:41 +03:00
o . depends ( { bllist _preset : '' , '!reverse' : true } ) ;
2021-11-04 18:57:08 +03:00
// USER_ENTRIES_DNS
o = s . taboption ( 'user_entries_tab' , form . Value , 'user_entries_dns' ,
_ ( "DNS server that is used for FQDN entries" ) , '<code>ipaddress[#port]</code>' ) ;
o . validate = this . validateIpPort ;
// USER_ENTRIES edit dialog
o = s . taboption ( 'user_entries_tab' , form . Button , '_user_entries_btn' ,
_ ( 'User entries' ) ) ;
o . onclick = ( ) => user _entries _edit . show ( ) ;
o . inputtitle = _ ( 'Edit' ) ;
o . inputstyle = 'edit btn' ;
let map _promise = m . render ( ) ;
map _promise . then ( node => node . classList . add ( 'fade-in' ) ) ;
return map _promise ;
} ,
handleSaveApply : function ( ev , mode ) {
return this . handleSave ( ev ) . then ( ( ) => {
ui . changes . apply ( mode == '0' ) ;
if ( this . appStatusCode != 1 && this . appStatusCode != 2 ) {
2022-05-10 01:22:25 +03:00
window . setTimeout ( ( ) => fs . exec ( tools . execPath , [ 'restart' ] ) , 3000 ) ;
2021-11-04 18:57:08 +03:00
} ;
} ) ;
} ,
2020-06-19 20:43:08 +03:00
} ) ;