0, 'v4' => Softvolt_Ai_Bodyguard_Ranges::count( self::NAME . '-v4', 4 ), 'v6' => Softvolt_Ai_Bodyguard_Ranges::count( self::NAME . '-v6', 6 ), 'sources' => array(), 'error' => '', 'blocked' => 0, ), $s, array( 'v4' => Softvolt_Ai_Bodyguard_Ranges::count( self::NAME . '-v4', 4 ), 'v6' => Softvolt_Ai_Bodyguard_Ranges::count( self::NAME . '-v6', 6 ), ) ); } /** * Download every source and rebuild the list. * * @return array|WP_Error The new status. */ public static function update() { $ranges = array( 4 => array(), 6 => array() ); $sources = array(); $errors = array(); $urls = array_merge( self::SOURCES, Softvolt_Ai_Bodyguard_Settings::list_to_array( Softvolt_Ai_Bodyguard_Settings::get( 'firewall_feed_urls', '' ) ) ); foreach ( array_unique( $urls ) as $url ) { $url = esc_url_raw( trim( $url ), array( 'https', 'http' ) ); if ( '' === $url ) { continue; } $res = wp_safe_remote_get( $url, array( 'timeout' => 30, 'user-agent' => 'AI-GuardWall/' . SOFTVOLT_AI_BODYGUARD_VERSION, ) ); if ( is_wp_error( $res ) || 200 !== (int) wp_remote_retrieve_response_code( $res ) ) { $errors[] = sprintf( '%s: %s', wp_parse_url( $url, PHP_URL_HOST ), is_wp_error( $res ) ? $res->get_error_message() : 'HTTP ' . wp_remote_retrieve_response_code( $res ) ); continue; } $n = 0; foreach ( self::entries( (string) wp_remote_retrieve_body( $res ) ) as $entry ) { $r = Softvolt_Ai_Bodyguard_Ranges::parse( $entry ); if ( $r && ! self::too_wide( $r ) ) { $ranges[ $r[0] ][] = array( $r[1], $r[2], '' ); $n++; } } $sources[] = array( 'url' => $url, 'entries' => $n ); } $old = self::status(); if ( ! $sources ) { $old['error'] = implode( ' · ', $errors ); update_option( self::OPTION, $old, false ); return new WP_Error( 'svabg_feed', $old['error'] ? $old['error'] : __( 'No list could be downloaded.', 'ai-guardwall' ) ); } foreach ( array( 4, 6 ) as $family ) { $written = Softvolt_Ai_Bodyguard_Ranges::write( self::NAME, $family, $ranges[ $family ] ); if ( is_wp_error( $written ) ) { return $written; } } $status = array( 'updated' => time(), 'sources' => $sources, 'error' => implode( ' · ', $errors ), 'blocked' => (int) $old['blocked'], ); update_option( self::OPTION, $status, false ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'threat_feed', sprintf( /* translators: %d: count */ __( 'Threat feed updated: %d networks.', 'ai-guardwall' ), count( $ranges[4] ) + count( $ranges[6] ) ) ); return self::status(); } /** * Entries from a feed body: Spamhaus JSON lines, or plain text (one per line, ; and # comments). */ public static function entries( $body ) { $out = array(); foreach ( preg_split( '/\r\n|\r|\n/', $body ) as $line ) { $line = trim( $line ); if ( '' === $line || '#' === $line[0] || ';' === $line[0] ) { continue; } if ( '{' === $line[0] ) { $row = json_decode( $line, true ); if ( is_array( $row ) && ! empty( $row['cidr'] ) ) { $out[] = (string) $row['cidr']; } continue; } $line = trim( preg_split( '/[\s;#,]+/', $line )[0] ); if ( '' !== $line ) { $out[] = $line; } } return $out; } /** * Never accept a feed entry that would block a huge part of the internet * (a broken or hostile feed): IPv4 wider than /8, IPv6 wider than /19. */ private static function too_wide( $r ) { if ( 4 === $r[0] ) { $start = unpack( 'N', $r[1] )[1]; $end = unpack( 'N', $r[2] )[1]; return ( $end - $start ) > 16777216; } return strcmp( substr( $r[1], 0, 2 ), substr( $r[2], 0, 2 ) ) !== 0 && ( ord( $r[2][1] ) - ord( $r[1][1] ) ) > 31; } public static function count_block() { $s = get_option( self::OPTION, array() ); if ( is_array( $s ) ) { $s['blocked'] = ( isset( $s['blocked'] ) ? (int) $s['blocked'] : 0 ) + 1; update_option( self::OPTION, $s, false ); } } public static function remove() { wp_clear_scheduled_hook( self::HOOK ); Softvolt_Ai_Bodyguard_Ranges::delete( self::NAME ); delete_option( self::OPTION ); } }