<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming &#8211; TristanPoulsen.com</title>
	<atom:link href="https://tristanpoulsen.com/category/tech/programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://tristanpoulsen.com</link>
	<description>Welcome to TristanPoulsen.com</description>
	<lastBuildDate>Wed, 07 May 2025 17:38:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://tristanpoulsen.com/wp-content/uploads/2024/07/cropped-Tristan-Forryst-Poulsen-initials-logo-32x32.png</url>
	<title>Programming &#8211; TristanPoulsen.com</title>
	<link>https://tristanpoulsen.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How I Stopped Bots from Crashing My cPanel Server</title>
		<link>https://tristanpoulsen.com/how-i-stopped-bots-from-crashing-my-cpanel-server/</link>
		
		<dc:creator><![CDATA[tristanpoulsen]]></dc:creator>
		<pubDate>Wed, 07 May 2025 17:36:09 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Random Tech Posts]]></category>
		<category><![CDATA[Strategy And Tips]]></category>
		<category><![CDATA[Tech]]></category>
		<guid isPermaLink="false">https://tristanpoulsen.com/?p=856</guid>

					<description><![CDATA[If you&#8217;re managing a cPanel + WHM server (mine runs on AlmaLinux) and notice CPU usage spiking to 100%, there&#8217;s a good chance that aggressive bots or inefficient PHP processes are hammering your server. That’s exactly what happened to me — and here’s how I fixed it. The Problem My server load was pinned at ... <a title="How I Stopped Bots from Crashing My cPanel Server" class="read-more" href="https://tristanpoulsen.com/how-i-stopped-bots-from-crashing-my-cpanel-server/" aria-label="Read more about How I Stopped Bots from Crashing My cPanel Server">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>If you&#8217;re managing a cPanel + WHM server (mine runs on AlmaLinux) and notice CPU usage spiking to 100%, there&#8217;s a good chance that <strong>aggressive bots</strong> or <strong>inefficient PHP processes</strong> are hammering your server. That’s exactly what happened to me — and here’s how I fixed it.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">The Problem</h2>



<p>My server load was pinned at 100%, with <code>php-fpm</code> processes consuming the bulk of the CPU. Using tools like <code>htop</code> and <code>ps</code>, I traced the activity back to a few specific WordPress sites hosted on the server.</p>



<p>The common culprit? <strong>Bot traffic</strong> hammering uncached pages and XML-RPC endpoints.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">The Fix (3-Part Solution)</h2>



<h3 class="wp-block-heading">1. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9f1.png" alt="🧱" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Block Bots with <code>.htaccess</code></h3>



<p>I edited the <code>.htaccess</code> file for each affected site and added rules to block known abusive bots by user agent:</p>



<pre class="wp-block-preformatted"><code># Block bad bots by user-agent<br>&lt;IfModule mod_rewrite.c><br>RewriteEngine On<br>RewriteCond %{HTTP_USER_AGENT} ^$ [OR]<br>RewriteCond %{HTTP_USER_AGENT} (semrush|ahrefs|mj12bot|dotbot|python-requests|curl|wget|masscan|sqlmap|fimap|nmap|nikto) [NC]<br>RewriteRule .* - [F,L]<br>&lt;/IfModule><br></code></pre>



<p>This simple change started deflecting unnecessary load <strong>before</strong> it could hit WordPress or PHP.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f6e1.png" alt="🛡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Install and Configure Fail2Ban</h3>



<p>I installed <strong>Fail2Ban</strong> to automatically block IPs that triggered suspicious patterns in my Apache logs (like repeated bot-like requests):</p>



<pre class="wp-block-preformatted"><code>sudo yum install epel-release -y<br>sudo yum install fail2ban -y<br>sudo systemctl enable fail2ban<br>sudo systemctl start fail2ban<br></code></pre>



<p>Then, I created a jail to detect and ban bad bots:</p>



<p><strong><code>/etc/fail2ban/jail.d/apache-badbots.conf</code></strong></p>



<pre class="wp-block-preformatted"><code>[apache-badbots]<br>enabled = true<br>port    = http,https<br>filter  = apache-badbots<br>logpath = /usr/local/apache/logs/access_log<br>maxretry = 10<br>findtime = 300<br>bantime = 3600<br></code></pre>



<p><strong><code>/etc/fail2ban/filter.d/apache-badbots.conf</code></strong></p>



<pre class="wp-block-preformatted"><code>[Definition]<br>failregex = ^&lt;HOST> -.*"(GET|POST).*(crawler|bot|spider|python|curl|wget).*HTTP.*"<br></code></pre>



<p>With this in place, the server now proactively bans repeat offenders and malicious bots.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f504.png" alt="🔄" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Restart PHP-FPM and Apache to Flush Load</h3>



<p>Once the defenses were in place, I restarted the two main services to clear out any bloated or stuck processes:</p>



<pre class="wp-block-preformatted"><code>systemctl restart ea-php81-php-fpm<br>systemctl restart httpd<br></code></pre>



<p>This dropped my CPU usage almost instantly and gave the server a clean slate to work with.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Summary</h2>



<p>After these changes:</p>



<ul class="wp-block-list">
<li>CPU usage dropped from 100% to &lt;10%</li>



<li>PHP-FPM processes normalized</li>



<li>The server has stayed stable under load, even with continued traffic</li>
</ul>



<p>If you&#8217;re running cPanel/WHM and notice unexplained high load:</p>



<ul class="wp-block-list">
<li><strong>Check your access logs</strong></li>



<li><strong>Filter bots at the .htaccess level</strong></li>



<li><strong>Set up Fail2Ban to auto-ban bad actors</strong></li>



<li>And always <strong>restart PHP-FPM/Apache</strong> after config changes</li>
</ul>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Auto Email Filter Script Python</title>
		<link>https://tristanpoulsen.com/auto-email-filter-script-python/</link>
		
		<dc:creator><![CDATA[tristanpoulsen]]></dc:creator>
		<pubDate>Wed, 21 Aug 2024 01:19:41 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Random Tech Posts]]></category>
		<category><![CDATA[Tech]]></category>
		<guid isPermaLink="false">https://tristanpoulsen.com/?p=715</guid>

					<description><![CDATA[Today I built a python script that takes a csv file with a list of emails and using the Gmail API, moves the emails to the corresponding folder I&#8217;d like to move them to. Then it archives them. Now obviously you can do this in Gmail with an automated filter for incoming emails, so the ... <a title="Auto Email Filter Script Python" class="read-more" href="https://tristanpoulsen.com/auto-email-filter-script-python/" aria-label="Read more about Auto Email Filter Script Python">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size">Today I built a python script that takes a csv file with a list of emails and using the Gmail API, moves the emails to the corresponding folder I&#8217;d like to move them to. Then it archives them.<br><br>Now obviously you can do this in Gmail with an automated filter for incoming emails, so the reason I&#8217;ve done this is so occasionally, probably once every other week, I&#8217;ll check all the emails in my inbox and I move emails (99% of the time to the same folders). <br>I wanted to automate this cleaning process without using <a href="https://www.sanebox.com/">SaneBox</a> and I did try to use <a href="http://make.com">Make.com</a> to do the same thing, but this was honestly simpler and I can run it without the additional operations cost.</p>



<p class="has-medium-font-size">Here&#8217;s the csv format for the headers and for the Folders &#8211; I couldn&#8217;t get it to link via the name so I have it cross-reference the Label ID to match the folders:<br>email, label<br>example_email@yahoo.com, Label_55<br><br>This is what the output looks like:</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="630" height="320" src="http://3.239.168.193/wp-content/uploads/2024/08/email_filter_python_script_image-e1724203635699.png" alt="" class="wp-image-719" srcset="https://tristanpoulsen.com/wp-content/uploads/2024/08/email_filter_python_script_image-e1724203635699.png 630w, https://tristanpoulsen.com/wp-content/uploads/2024/08/email_filter_python_script_image-e1724203635699-300x152.png 300w" sizes="(max-width: 630px) 100vw, 630px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading"><strong>Here&#8217;s my code if anyone wants to make their own python script:</strong></h3>



<pre class="wp-block-code"><code>import os
import pickle
import pandas as pd
import base64
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Step 1: Authentication and Service Initialization
def authenticate_gmail_api():
    SCOPES = &#91;'https://www.googleapis.com/auth/gmail.modify']
    creds = None

    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('gmail', 'v1', credentials=creds)
    return service

# Step 2: Read Filters from CSV File
def read_filters_from_csv(csv_file):
    filters_df = pd.read_csv(csv_file)
    return filters_df

# Step 3: Search for Matching Emails
# Step 3: Search for Matching Emails
def search_emails(service, query):
    try:
        # Modify the query to explicitly search only within the INBOX
        query = f'label:inbox {query}'
        results = service.users().messages().list(userId='me', q=query).execute()
        messages = results.get('messages', &#91;])
        return messages
    except HttpError as error:
        print(f'An error occurred: {error}')
        return &#91;]


# Step 4: Apply Filters and Move Emails
def apply_filters(service, filters_df):
    for index, row in filters_df.iterrows():
        email_filter = row&#91;'email']
        label = row&#91;'label']

        query = f'from:{email_filter}'
        messages = search_emails(service, query)

        if not messages:
            print(f'No emails found for {email_filter}')
            continue

        for message in messages:
            msg_id = message&#91;'id']
            # Move email to the specified folder (label)
            service.users().messages().modify(
                userId='me',
                id=msg_id,
                body={'addLabelIds': &#91;label], 'removeLabelIds': &#91;'INBOX']}
            ).execute()
            print(f'Email from {email_filter} moved to {label} and archived.')

# Step 5: Retrieve and List Label IDs
def list_labels(service):
    results = service.users().labels().list(userId='me').execute()
    labels = results.get('labels', &#91;])

    if not labels:
        print('No labels found.')
    else:
        print('Labels:')
        for label in labels:
            print(f"{label&#91;'name']} - {label&#91;'id']}")

# Step 6: Main Function
def main():
    # Authenticate and create the Gmail API service
    service = authenticate_gmail_api()

    # Uncomment the line below to list all labels and their IDs
    list_labels(service)

    # Load filter rules from CSV
    csv_file = 'filters.csv'  # Replace with your CSV file
    filters_df = read_filters_from_csv(csv_file)

    # Apply filters to emails
    apply_filters(service, filters_df)

if __name__ == '__main__':
    main()
</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
