Skip to content

Commit

Permalink
Patch for the automatic removal of tickets every 6 months (customizab…
Browse files Browse the repository at this point in the history
…le for future updates)"

Add to .ENV file new Variable

PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS="6"
  • Loading branch information
rickyb82 committed Nov 12, 2024
1 parent 86d99e0 commit 7f05b4c
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 292 deletions.
Binary file added .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static function env_exist($key){
}

private static function env_auth($key){
return in_array($key,["PIRACY_SHIELD_VPN_PEER_IP","PIRACY_SHIELD_VPN_REMOTE_LAN_IP","PIRACY_SHIELD_VPN_LOCAL_LAN_IP","PIRACY_SHIELD_VPN_PSK","NET_IP","NET_MASK","NET_GATEWAY","BGP_ROUTER_IP","BGP_ASN","BGP_LOCAL_IP","BGP_LOCAL_MASK","BGP_LOCAL_GATEWAY","DNS_SERVER_PRIMARY_IP","DNS_SERVER_PRIMARY_PORT","DNS_SERVER_PRIMARY_USER","DNS_SERVER_PRIMARY_PSW","DNS_SERVER_PRIMARY_PATH","DNS_SERVER_PRIMARY_RELOAD","DNS_SERVER_SECONDARY_IP","DNS_SERVER_SECONDARY_PORT","DNS_SERVER_SECONDARY_USER","DNS_SERVER_SECONDARY_PSW","DNS_SERVER_SECONDARY_PATH","DNS_SERVER_SECONDARY_RELOAD","PIRACY_SHIELD_MAIL","PIRACY_SHIELD_PSW","PIRACY_SHIELD_API_URL","EXTERNAL_DNS_SERVERS","CNCPO_DOWNLOAD_URL","CNCPO_PFX_PATH","CNCPO_PFX_PASS","ADM_BETTING_URL","ADM_SMOKING_URL","CNCPO_DNS_REDIRECT_IP","ADM_DNS_REDIRECT_IP","PIRACY_SHIELD_DNS_REDIRECT_IP","CNCPO_ENABLED","ADM_ENABLED","PIRACY_SHIELD_ENABLED","MANUAL_ENABLED","MANUAL_DNS_REDIRECT_IP","MAIL_HOST","MAIL_PORT","MAIL_USERNAME","MAIL_PASSWORD","MAIL_ENCRYPTION","MAIL_FROM_ADDRESS","MAIL_FROM_NAME","MAIL_TO_ADDRESSES"]);
return in_array($key,["PIRACY_SHIELD_VPN_PEER_IP","PIRACY_SHIELD_VPN_REMOTE_LAN_IP","PIRACY_SHIELD_VPN_LOCAL_LAN_IP","PIRACY_SHIELD_VPN_PSK","PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS","NET_IP","NET_MASK","NET_GATEWAY","BGP_ROUTER_IP","BGP_ASN","BGP_LOCAL_IP","BGP_LOCAL_MASK","BGP_LOCAL_GATEWAY","DNS_SERVER_PRIMARY_IP","DNS_SERVER_PRIMARY_PORT","DNS_SERVER_PRIMARY_USER","DNS_SERVER_PRIMARY_PSW","DNS_SERVER_PRIMARY_PATH","DNS_SERVER_PRIMARY_RELOAD","DNS_SERVER_SECONDARY_IP","DNS_SERVER_SECONDARY_PORT","DNS_SERVER_SECONDARY_USER","DNS_SERVER_SECONDARY_PSW","DNS_SERVER_SECONDARY_PATH","DNS_SERVER_SECONDARY_RELOAD","PIRACY_SHIELD_MAIL","PIRACY_SHIELD_PSW","PIRACY_SHIELD_API_URL","EXTERNAL_DNS_SERVERS","CNCPO_DOWNLOAD_URL","CNCPO_PFX_PATH","CNCPO_PFX_PASS","ADM_BETTING_URL","ADM_SMOKING_URL","CNCPO_DNS_REDIRECT_IP","ADM_DNS_REDIRECT_IP","PIRACY_SHIELD_DNS_REDIRECT_IP","CNCPO_ENABLED","ADM_ENABLED","PIRACY_SHIELD_ENABLED","MANUAL_ENABLED","MANUAL_DNS_REDIRECT_IP","MAIL_HOST","MAIL_PORT","MAIL_USERNAME","MAIL_PASSWORD","MAIL_ENCRYPTION","MAIL_FROM_ADDRESS","MAIL_FROM_NAME","MAIL_TO_ADDRESSES"]);
}

public function update_dns(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class BGPController extends Controller
{
Expand All @@ -16,7 +17,8 @@ public function __construct()
public function make_ipv4_list_file(){
\App\Http\Controllers\Admin\ActionLogController::log(0,"bgp_system","trying to make ipv4 bgp file in '".base_path('storage/download/').'ipv4.txt'."'");
$content = '';
$ipv4s_piracy = \App\Piracy\IPv4s::select('ipv4')->distinct()->pluck('ipv4')->toArray();
$months = env('PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS');
$ipv4s_piracy = collect(DB::connection('piracy_shield')->select("select feedbacks.item from (select distinct item from ticket_items_log where item_type = 'ipv4' and `timestamp` > DATE_SUB(now(), INTERVAL ? MONTH)) as feedbacks INNER JOIN (select ipv4 as item from ipv4s where `timestamp` > DATE_SUB(now(), INTERVAL ? MONTH)) as lastitems on feedbacks.item = lastitems.item order by item",[$months,$months]))->pluck('item');
$ipv4s_manual = \App\Manual\IPv4s::select('ipv4')->distinct()->pluck('ipv4')->toArray();
$done = [];
foreach ($ipv4s_piracy as $ipv4) {
Expand Down Expand Up @@ -44,7 +46,8 @@ public function make_ipv4_list_file(){
public function make_ipv6_list_file(){
\App\Http\Controllers\Admin\ActionLogController::log(0,"bgp_system","trying to make ipv6 bgp file in '".base_path('storage/download/').'ipv6.txt'."'");
$content = '';
$ipv6s_piracy = \App\Piracy\IPv6s::select('ipv6')->distinct()->pluck('ipv6')->toArray();
$months = env('PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS');
$ipv6s_piracy = collect(DB::connection('piracy_shield')->select("select feedbacks.item from (select distinct item from ticket_items_log where item_type = 'ipv6' and `timestamp` > DATE_SUB(now(), INTERVAL ? MONTH)) as feedbacks INNER JOIN (select ipv6 as item from ipv6s where `timestamp` > DATE_SUB(now(), INTERVAL ? MONTH)) as lastitems on feedbacks.item = lastitems.item order by item",[$months,$months]))->pluck('item');
$ipv6s_manual = \App\Manual\IPv6s::select('ipv6')->distinct()->pluck('ipv6')->toArray();
$done = [];
foreach ($ipv6s_piracy as $ipv6) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use phpseclib\Net\SSH2;
use DB;

class DNSController extends Controller
{
Expand Down Expand Up @@ -207,7 +208,8 @@ private function install_zone(){
$admbettingblacklist = \App\ADM\BettingBlacklist::select('fqdn')->distinct()->pluck('fqdn')->toArray();
$admsmokingblacklist = \App\ADM\SmokingBlacklist::select('fqdn')->distinct()->pluck('fqdn')->toArray();
$cncpoblacklist = \App\CNCPO\Blacklist::select('fqdn')->distinct()->pluck('fqdn')->toArray();
$piracyshield = \App\Piracy\FQDNs::select('fqdn')->distinct()->pluck('fqdn')->toArray();
$months = env('PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS');
$piracyshield = collect(DB::connection('piracy_shield')->select("select feedbacks.item from (select distinct item from ticket_items_log where item_type = 'fqdn' and `timestamp` > DATE_SUB(now(), INTERVAL ? MONTH)) as feedbacks INNER JOIN (select fqdn as item from fqdns where `timestamp` > DATE_SUB(now(), INTERVAL ? MONTH)) as lastitems on feedbacks.item = lastitems.item order by item",[$months,$months]))->pluck('item');
$manual = \App\Manual\FQDNs::select('fqdn')->distinct()->pluck('fqdn')->toArray();
$done = [];
foreach ($admbettingblacklist as $fqdn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,17 @@ private static function check_env(){
if(!env('PIRACY_SHIELD_VPN_PSK')){
$errors[] = "VPN pre-shared key not filled";
}
if(!env('PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS')){
$errors[] = "Items validity not filled";
}else{
if(!is_numeric(env('PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS'))){
$errors[] = "Items validity is not a number";
}else{
if(env('PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS') <= 0){
$errors[] = "Items validity must be at least 1";
}
}
}
return $errors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label for="PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS">Items validity (in months)</label>
<div class="input-group">
<input class="form-control" id="PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS" name="PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS" value="{{env("PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS")}}" placeholder="6" type="number" min="1" step="1">
<div class="input-group-append">
<div class="input-group-text">
<i class="fas fa-history text-dark mr-1"></i>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function __construct()
public function datatable_actions_log(Request $request){
if($request->ajax()){
if($request->input('hide_system_cron') == "true"){
$data = \App\ActionLog::where("user_id","<>","0");
$data = \App\ActionLog::where('timestamp', '>=', Carbon::now()->subDay())->where("user_id","<>","0")->orderBy("id","desc")->get();
}else{
$data = \App\ActionLog::query();
$data = \App\ActionLog::where('timestamp', '>=', Carbon::now()->subDay())->orderBy("id","desc")->get();
}
return Datatables::of($data)->addColumn('action',function($row){
return htmlspecialchars($row->action);
Expand All @@ -29,17 +29,17 @@ public function datatable_actions_log(Request $request){
}

public function datatable_ps_api_log(Request $request){
$data = \App\Piracy\APILog::query();
$data = \App\Piracy\APILog::where('timestamp', '>=', Carbon::now()->subDay())->orderBy("id","desc")->get();
return Datatables::of($data)->make(true);
}

public function datatable_ps_access_tokens(Request $request){
$data = \App\Piracy\APIAccessTokens::query();
$data = \App\Piracy\APIAccessTokens::orderBy("id","desc")->get();
return Datatables::of($data)->make(true);
}

public function datatable_ps_refresh_tokens(Request $request){
$data = \App\Piracy\APIRefreshTokens::query();
$data = \App\Piracy\APIRefreshTokens::orderBy("id","desc")->get();
return Datatables::of($data)->make(true);
}

Expand Down Expand Up @@ -84,10 +84,6 @@ private static function update_env($key,$value){
}
}
break;
case 'PIRACY_SHIELD_VPN_PSK':
file_put_contents($path, str_replace("$key=\"".env($key)."\"","$key=\"".base64_encode($value)."\"", file_get_contents($path)));
$_ENV[$key] = $value;
break;
default:
file_put_contents($path, str_replace("$key=\"".env($key)."\"","$key=\"$value\"", file_get_contents($path)));
$_ENV[$key] = $value;
Expand All @@ -110,31 +106,16 @@ private static function env_exist($key){
}

private static function env_auth($key){
return in_array($key,["PIRACY_SHIELD_VPN_PEER_IP","PIRACY_SHIELD_VPN_REMOTE_LAN_IP","PIRACY_SHIELD_VPN_LOCAL_LAN_IP","PIRACY_SHIELD_VPN_PSK",
"PIRACY_SHIELD_ENABLED","PIRACY_SHIELD_MAIL","PIRACY_SHIELD_PSW","PIRACY_SHIELD_API_URL","PIRACY_SHIELD_DNS_REDIRECT_IP",
"NET_IP","NET_MASK","NET_GATEWAY","EXTERNAL_DNS_SERVERS",
"BGP_ROUTER_IP","BGP_ASN","BGP_LOCAL_IP","BGP_LOCAL_MASK","BGP_LOCAL_GATEWAY",
"DNS_SERVER_PRIMARY_IP","DNS_SERVER_PRIMARY_PORT","DNS_SERVER_PRIMARY_USER","DNS_SERVER_PRIMARY_PSW","DNS_SERVER_PRIMARY_PRIVKEY","DNS_SERVER_PRIMARY_PATH","DNS_SERVER_PRIMARY_RELOAD","DNS_SERVER_PRIMARY_EXPORT_PLAIN",
"DNS_SERVER_SECONDARY_IP","DNS_SERVER_SECONDARY_PORT","DNS_SERVER_SECONDARY_USER","DNS_SERVER_SECONDARY_PSW","DNS_SERVER_SECONDARY_PRIVKEY","DNS_SERVER_SECONDARY_PATH","DNS_SERVER_SECONDARY_RELOAD","DNS_SERVER_SECONDARY_EXPORT_PLAIN",
"CNCPO_ENABLED","CNCPO_DOWNLOAD_URL","CNCPO_PFX_PATH","CNCPO_PFX_PASS","CNCPO_DNS_REDIRECT_IP",
"ADM_ENABLED","ADM_BETTING_URL","ADM_SMOKING_URL","ADM_DNS_REDIRECT_IP",
"MANUAL_ENABLED","MANUAL_DNS_REDIRECT_IP",
"MAIL_HOST","MAIL_PORT","MAIL_USERNAME","MAIL_PASSWORD","MAIL_ENCRYPTION","MAIL_FROM_ADDRESS","MAIL_FROM_NAME","MAIL_TO_ADDRESSES",
"LOGS_DAYS_ACTION","LOGS_DAYS_AUTHENTICATION","LOGS_DAYS_PS_API","LOGS_DAYS_PS_API_ACCESS_TOKENS","LOGS_DAYS_PS_API_REFRESH_TOKENS"
]);
return in_array($key,["PIRACY_SHIELD_VPN_PEER_IP","PIRACY_SHIELD_VPN_REMOTE_LAN_IP","PIRACY_SHIELD_VPN_LOCAL_LAN_IP","PIRACY_SHIELD_VPN_PSK","PIRACY_SHIELD_ITEMS_VALIDITY_MONTHS","NET_IP","NET_MASK","NET_GATEWAY","BGP_ROUTER_IP","BGP_ASN","BGP_LOCAL_IP","BGP_LOCAL_MASK","BGP_LOCAL_GATEWAY","DNS_SERVER_PRIMARY_IP","DNS_SERVER_PRIMARY_PORT","DNS_SERVER_PRIMARY_USER","DNS_SERVER_PRIMARY_PSW","DNS_SERVER_PRIMARY_PATH","DNS_SERVER_PRIMARY_RELOAD","DNS_SERVER_SECONDARY_IP","DNS_SERVER_SECONDARY_PORT","DNS_SERVER_SECONDARY_USER","DNS_SERVER_SECONDARY_PSW","DNS_SERVER_SECONDARY_PATH","DNS_SERVER_SECONDARY_RELOAD","PIRACY_SHIELD_MAIL","PIRACY_SHIELD_PSW","PIRACY_SHIELD_API_URL","EXTERNAL_DNS_SERVERS","CNCPO_DOWNLOAD_URL","CNCPO_PFX_PATH","CNCPO_PFX_PASS","ADM_BETTING_URL","ADM_SMOKING_URL","CNCPO_DNS_REDIRECT_IP","ADM_DNS_REDIRECT_IP","PIRACY_SHIELD_DNS_REDIRECT_IP","CNCPO_ENABLED","ADM_ENABLED","PIRACY_SHIELD_ENABLED","MANUAL_ENABLED","MANUAL_DNS_REDIRECT_IP","MAIL_HOST","MAIL_PORT","MAIL_USERNAME","MAIL_PASSWORD","MAIL_ENCRYPTION","MAIL_FROM_ADDRESS","MAIL_FROM_NAME","MAIL_TO_ADDRESSES"]);
}

public function update_dns(){
$check_env = self::check_env_dns();
if(count($check_env) == 0){
$dns1 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_PRIMARY_IP'),env('DNS_SERVER_PRIMARY_PORT'),
env('DNS_SERVER_PRIMARY_USER'),env('DNS_SERVER_PRIMARY_PSW'),env('DNS_SERVER_PRIMARY_PRIVKEY'),
env('DNS_SERVER_PRIMARY_PATH'),env('DNS_SERVER_PRIMARY_RELOAD'),env('DNS_SERVER_PRIMARY_EXPORT_PLAIN'));
$dns1 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_PRIMARY_IP'),env('DNS_SERVER_PRIMARY_PORT'),env('DNS_SERVER_PRIMARY_USER'),env('DNS_SERVER_PRIMARY_PSW'),env('DNS_SERVER_PRIMARY_PATH'),env('DNS_SERVER_PRIMARY_RELOAD'));
$dns1->update();
if(env('DNS_SERVER_SECONDARY_IP')){
$dns2 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_SECONDARY_IP'),env('DNS_SERVER_SECONDARY_PORT'),
env('DNS_SERVER_SECONDARY_USER'),env('DNS_SERVER_SECONDARY_PSW'),env('DNS_SERVER_SECONDARY_PRIVKEY'),
env('DNS_SERVER_SECONDARY_PATH'),env('DNS_SERVER_SECONDARY_RELOAD'),env('DNS_SERVER_SECONDARY_EXPORT_PLAIN'));
$dns2 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_SECONDARY_IP'),env('DNS_SERVER_SECONDARY_PORT'),env('DNS_SERVER_SECONDARY_USER'),env('DNS_SERVER_SECONDARY_PSW'),env('DNS_SERVER_SECONDARY_PATH'),env('DNS_SERVER_SECONDARY_RELOAD'));
$dns2->update();
}else{
\App\Http\Controllers\Admin\ActionLogController::log(0,"dns_cron","secondary DNS server IP not set, skipping run");
Expand All @@ -157,18 +138,6 @@ public function update_bgp(){
}
}

public function log_retention(){
$check_env = \App\Http\Controllers\Admin\ActionLogController::check_env();
if(count($check_env) == 0){
\App\Http\Controllers\Admin\ActionLogController::log(0,"log_retention_cron","starting run");
$c = new \App\Http\Controllers\Admin\ActionLogController();
$c->log_retention();
\App\Http\Controllers\Admin\ActionLogController::log(0,"log_retention_cron","run ended");
}else{
\App\Http\Controllers\Admin\ActionLogController::log(0,"log_retention_cron","run not started because of: ".implode(", ",$check_env));
}
}

private static function check_env_dns(){
$errors = [];
if(!env('DNS_SERVER_PRIMARY_IP')){
Expand All @@ -187,18 +156,15 @@ private static function check_env_dns(){
if(!env('DNS_SERVER_PRIMARY_USER')){
$errors[] = "Primary DNS server SSH username not filled";
}
if(!env('DNS_SERVER_PRIMARY_PSW') && !env('DNS_SERVER_PRIMARY_PRIVKEY')){
$errors[] = "Either primary DNS server SSH password or private key must be filled";
if(!env('DNS_SERVER_PRIMARY_PSW')){
$errors[] = "Primary DNS server SSH password not filled";
}
if(!env('DNS_SERVER_PRIMARY_PATH')){
$errors[] = "Primary DNS server zone path not filled";
}
if(!env('DNS_SERVER_PRIMARY_RELOAD')){
$errors[] = "Primary DNS server reload command not filled";
}
if(!env('DNS_SERVER_PRIMARY_EXPORT_PLAIN')){
$errors[] = "Primary DNS server export plain flag not filled";
}
if(env('DNS_SERVER_SECONDARY_IP')){
if(!filter_var(env('DNS_SERVER_SECONDARY_IP'), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){
$errors[] = "Secondary DNS server IP not valid";
Expand All @@ -213,18 +179,15 @@ private static function check_env_dns(){
if(!env('DNS_SERVER_SECONDARY_USER')){
$errors[] = "Secondary DNS server SSH username not filled";
}
if(!env('DNS_SERVER_SECONDARY_PSW') && !env('DNS_SERVER_SECONDARY_PRIVKEY')){
$errors[] = "Either secondary DNS server SSH password or private key must be filled";
if(!env('DNS_SERVER_SECONDARY_PSW')){
$errors[] = "Secondary DNS server SSH password not filled";
}
if(!env('DNS_SERVER_SECONDARY_PATH')){
$errors[] = "Secondary DNS server zone path not filled";
}
if(!env('DNS_SERVER_SECONDARY_RELOAD')){
$errors[] = "Secondary DNS server reload command not filled";
}
if(!env('DNS_SERVER_SECONDARY_EXPORT_PLAIN')){
$errors[] = "Secondary DNS server export plain flag not filled";
}
}
}
}
Expand Down Expand Up @@ -295,14 +258,10 @@ public function test_dns(){
$obj->settings->passed = (count($env_test) == 0);
$obj->settings->messages = (count($env_test) == 0) ? ["Settings formally correct"] : $env_test;
if($obj->settings->passed){
$dns1 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_PRIMARY_IP'),env('DNS_SERVER_PRIMARY_PORT'),
env('DNS_SERVER_PRIMARY_USER'),env('DNS_SERVER_PRIMARY_PSW'),env('DNS_SERVER_PRIMARY_PRIVKEY'),
env('DNS_SERVER_PRIMARY_PATH'),env('DNS_SERVER_PRIMARY_RELOAD'),env('DNS_SERVER_PRIMARY_EXPORT_PLAIN'));
$dns1 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_PRIMARY_IP'),env('DNS_SERVER_PRIMARY_PORT'),env('DNS_SERVER_PRIMARY_USER'),env('DNS_SERVER_PRIMARY_PSW'),env('DNS_SERVER_PRIMARY_PATH'),env('DNS_SERVER_PRIMARY_RELOAD'));
$obj->primary = $dns1->test();
if(env('DNS_SERVER_SECONDARY_IP')){
$dns2 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_SECONDARY_IP'),env('DNS_SERVER_SECONDARY_PORT'),
env('DNS_SERVER_SECONDARY_USER'),env('DNS_SERVER_SECONDARY_PSW'),env('DNS_SERVER_SECONDARY_PRIVKEY'),
env('DNS_SERVER_SECONDARY_PATH'),env('DNS_SERVER_SECONDARY_RELOAD'),env('DNS_SERVER_SECONDARY_EXPORT_PLAIN'));
$dns2 = new \App\Http\Controllers\Admin\DNSController(env('DNS_SERVER_SECONDARY_IP'),env('DNS_SERVER_SECONDARY_PORT'),env('DNS_SERVER_SECONDARY_USER'),env('DNS_SERVER_SECONDARY_PSW'),env('DNS_SERVER_SECONDARY_PATH'),env('DNS_SERVER_SECONDARY_RELOAD'));
$obj->secondary = $dns2->test();
}
}
Expand Down
Loading

0 comments on commit 7f05b4c

Please sign in to comment.