Skip to content

Commit 284638f

Browse files
author
SMKRV
committed
feat: Integrate GitHub Gist functionality and environment variables support
BREAKING CHANGES: - Removed external gist update scripts (update_gist.sh and update_gist_special.sh) - Added direct GitHub Gist API integration Features: - Added built-in GitHub Gist update functionality - Added support for environment variables via .env file - Added new configuration options for GitHub Gist integration: * EXPORT_GISTS - enables/disables Gist updates (default: false) * GITHUB_TOKEN - GitHub Personal Access Token * GIST_ID_MAIN - Gist ID for main domain list * GIST_ID_SPECIAL - Gist ID for special domain list Improvements: - Reduced dependencies by removing external scripts - Added proper error handling for Gist updates - Added detailed logging for Gist operations - Added support for both hardcoded and environment variables - Improved security by supporting .env file for sensitive data
1 parent 9f1658d commit 284638f

File tree

7 files changed

+136
-148
lines changed

7 files changed

+136
-148
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# GitHub settings
2+
EXPORT_GISTS=true
3+
GITHUB_TOKEN="your-token-here"
4+
GIST_ID_MAIN="your-main-gist-id"
5+
GIST_ID_SPECIAL="your-special-gist-id"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ wheels/
1919
*.egg-info/
2020
.installed.cfg
2121
*.egg
22+
.env
2223

2324
# Home Assistant
2425
.storage

README.md

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ Lastly, the script can also aid in generating DNS FWD records, making it a compr
2222

2323
> **Prerequisites**
2424
> - Unix-like system
25-
> - Install dependencies: `sudo apt-get install curl jq gawk grep parallel`
25+
> - Install dependencies: `sudo apt-get install curl jq awk grep parallel`
2626
>
2727
> **Setup Steps**
2828
> 1. Create a working directory
29-
> 2. Copy scripts:
29+
> 2. Copy script:
3030
> - `mikrotik-domain-filter-bash.sh`
31-
> - `update_gist.sh`
32-
> - `update_gist_special.sh`
3331
>
3432
> 3. Configure scripts:
3533
> - Set working directory path in `mikrotik-domain-filter-bash.sh`
@@ -38,15 +36,23 @@ Lastly, the script can also aid in generating DNS FWD records, making it a compr
3836
> * `sources_special.txt`: Special domain list URLs
3937
> * `sources_whitelist.txt`: URLs of domain lists to exclude
4038
>
41-
> 4. Configure Gist updates (optional):
42-
> - Set GitHub token and Gist variables in `update_gist.sh` and `update_gist_special.sh`
43-
> - Or comment out Gist update functions in main script
39+
> 4. Configure Gist updates (optional):
40+
> - Create a `.env` file in the working directory with the following variables:
41+
> ```env
42+
> EXPORT_GISTS=true
43+
> GITHUB_TOKEN="your_github_personal_access_token"
44+
> GIST_ID_MAIN="your_main_gist_id"
45+
> GIST_ID_SPECIAL="your_special_gist_id"
46+
> ```
47+
> - Set `EXPORT_GISTS=true` to enable Gist updates
48+
> - Set `GITHUB_TOKEN` with your GitHub Personal Access Token
49+
> - Set `GIST_ID_MAIN` and `GIST_ID_SPECIAL` with respective Gist IDs
4450
>
4551
> 5. Add download URLs to source files
4652
>
4753
> 6. Set execution permissions:
4854
> ```bash
49-
> chmod +x mikrotik-domain-filter-bash.sh update_gist.sh update_gist_special.sh
55+
> chmod +x mikrotik-domain-filter-bash.sh
5056
> ```
5157
>
5258
> 7. Run the main script:
@@ -81,7 +87,7 @@ Lastly, the script can also aid in generating DNS FWD records, making it a compr
8187
8. [Pipeline Summary](#pipeline-summary)
8288
9. [File Descriptions](#file-descriptions)
8389
10. [Detailed Description of Domain Processing in Downloaded Lists](#detailed-description-of-domain-processing-in-downloaded-lists)
84-
11. [GitHub Gist Update Scripts](#github-gist-update-scripts)
90+
11. [GitHub Gist Exports](#github-gist-exports)
8591
12. [Project Structure](#project-structure)
8692
13. [Installation and Setup](#installation-and-setup)
8793
14. [Running the Script](#running-the-script)
@@ -100,7 +106,7 @@ Lastly, the script can also aid in generating DNS FWD records, making it a compr
100106
- **Logging**: A logging mechanism is set up to record events and errors in a log file.
101107
- **Lock Mechanism**: A file lock is used to ensure that only one instance of the script runs at a time, preventing conflicts.
102108
- **Directory Initialization**: Required directories are checked and created if they don’t exist.
103-
- **Dependency Check**: The script verifies the presence of required system tools like `curl`, `grep`, `awk`, `sort`, and `parallel`.
109+
- **Dependency Check**: The script verifies the presence of required system tools like `curl`, `jq`, `grep`, `awk`, `sort`, and `parallel`.
104110
105111
### File Checks and Cleanup
106112
@@ -442,45 +448,37 @@ tiktok.com
442448
youtube.co.uk
443449
```
444450
445-
### GitHub Gist Update Scripts
451+
### GitHub Gist Exports
446452
447-
This repository contains two identical shell scripts ([update_gist_special.sh](/update_gist_special.sh) and [update_gist.sh](/update_gist.sh)) that update different GitHub Gists with local file content. The scripts share the same functionality but use different variables and target different Gists.
453+
Configuration is currently done through environment variables, which can be set in a [.env](/.env.example) file:
448454
449-
#### Scripts Overview
455+
```env
456+
# Enable or disable Gist updates
457+
EXPORT_GISTS=true
450458
451-
Both scripts perform the same operations but are configured for different Gists:
452-
- `update_gist_special.sh` - configured for one specific Gist
453-
- `update_gist.sh` - configured for another Gist
459+
# GitHub Personal Access Token
460+
GITHUB_TOKEN="your_github_token"
454461
455-
The only difference between these scripts is in their configuration variables (GitHub token, Gist ID, file names, and paths).
456-
457-
#### Technical Details
458-
459-
The scripts require:
460-
- `curl` for making API requests
461-
- `jq` for JSON processing
462-
463-
Each script performs the following operations:
464-
1. Validates the presence of required utilities
465-
2. Reads the content from a specified local file
466-
3. Updates the target Gist via GitHub API
467-
4. Verifies the update was successful
468-
469-
#### Usage
470-
471-
To use either script, you need to configure the following variables:
472-
```bash
473-
GITHUB_TOKEN="your-github-token"
474-
GIST_ID="your-gist-id"
475-
FILENAME="filename-in-gist"
476-
LOCAL_FILE_PATH="path/to/local/file"
462+
# Gist IDs for main and special lists
463+
GIST_ID_MAIN="your_main_gist_id"
464+
GIST_ID_SPECIAL="your_special_gist_id"
477465
```
478466

479-
After configuring the variables, you can run either script to update its corresponding Gist with the content from the specified local file.
467+
#### Key Features
468+
- Direct GitHub API integration
469+
- Support for environment variables
470+
- Configurable Gist updates
471+
- Improved error handling
472+
- Detailed logging
480473

481-
#### Note
474+
#### Requirements
475+
- `curl` for API requests
476+
- `jq` for JSON processing
482477

483-
While the scripts are identical in functionality, they are maintained as separate files to avoid the need for changing variables when updating different Gists. This approach allows for easier automation and maintenance of multiple Gist updates.
478+
#### Notes
479+
- The `.env` file provides a secure way to manage sensitive tokens
480+
- Environment variables can also be set directly in the shell
481+
- Set `EXPORT_GISTS=false` to disable Gist updates
484482

485483
---
486484

@@ -490,8 +488,7 @@ While the scripts are identical in functionality, they are maintained as separat
490488
...
491489
├── dns-static-updater.rsc # MikroTik RouterOS script for DNS Static records import
492490
├── mikrotik-domain-filter-bash.sh # Main domain filtering script
493-
├── update_gist.sh # Script for updating main list Gist
494-
└── update_gist_special.sh # Script for updating special list Gist
491+
└── .env # Optional environment configuration file
495492
```
496493

497494
### Installation and Setup
@@ -514,21 +511,19 @@ While the scripts are identical in functionality, they are maintained as separat
514511
```bash
515512
# You might need sudo rights to change file permissions
516513
sudo chmod +x mikrotik-domain-filter-bash.sh
517-
sudo chmod +x update_gist.sh # If using Gist updates
518-
sudo chmod +x update_gist_special.sh # If using special Gist updates
519514

520515
# Or if you own the files:
521516
chmod +x mikrotik-domain-filter-bash.sh
522-
chmod +x update_gist.sh # If using Gist updates
523-
chmod +x update_gist_special.sh # If using special Gist updates
524517
```
525518

526519
3. **Configure Log Rotation**
527520
- Set up proper log rotation to manage script logs
528521
- Ensure sufficient disk space for logs
529522

530-
4. **Gist Updates (Optional)**
531-
- If you don't plan to use Gist updates, comment out the calls to `update_gist.sh` and `update_gist_special.sh` in `mikrotik-domain-filter-bash.sh`
523+
4. **Configure Gist Updates (Optional)**
524+
- Create a `.env` file in the working directory
525+
- Add GitHub Gist configuration variables
526+
- Ensure `.env` file is not tracked by version control
532527

533528
#### Running the Script
534529

@@ -559,7 +554,7 @@ The script requires several system utilities and proper permissions to function
559554
For Ubuntu/Debian systems:
560555
```bash
561556
sudo apt-get update
562-
sudo apt-get install curl jq gawk grep parallel
557+
sudo apt-get install curl jq awk grep parallel
563558
```
564559

565560
---

STRUCTURE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
│   └── [email protected]
1111
├── dns-static-updater.rsc
1212
├── mikrotik-domain-filter-bash.sh
13-
├── update_gist.sh
14-
└── update_gist_special.sh
13+
└── .env.example
1514
```

mikrotik-domain-filter-bash.sh

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ readonly CACHE_DIR="${WORK_DIR}/cache"
4343
readonly OUTPUT_FILE="${WORK_DIR}/filtered_domains_mikrotik.txt"
4444
readonly OUTPUT_FILESPECIAL="${WORK_DIR}/filtered_domains_special_mikrotik.txt"
4545

46+
# Load environment variables from .env file if it exists
47+
if [[ -f "${WORK_DIR}/.env" ]]; then
48+
# shellcheck disable=SC1090
49+
while IFS='=' read -r key value; do
50+
# Skip comments and empty lines
51+
[[ $key =~ ^#.*$ ]] && continue
52+
[[ -z "$key" ]] && continue
53+
# Remove quotes and export variable
54+
value="${value%\"}"
55+
value="${value#\"}"
56+
export "$key=$value"
57+
done < "${WORK_DIR}/.env"
58+
fi
59+
60+
# GitHub Gist settings !Use env var if available
61+
readonly EXPORT_GISTS=${EXPORT_GISTS:-false} # Default to false if not set
62+
readonly GITHUB_TOKEN=${GITHUB_TOKEN:-""} # GitHub access token
63+
readonly GIST_ID_MAIN=${GIST_ID_MAIN:-""} # Gist ID for main list
64+
readonly GIST_ID_SPECIAL=${GIST_ID_SPECIAL:-""} # Gist ID for special list
65+
4666
# Performance settings
4767
readonly MAX_PARALLEL_JOBS=5
4868
readonly DNS_RATE_LIMIT=5
@@ -181,7 +201,7 @@ cleanup() {
181201

182202
# Function to check dependencies
183203
check_dependencies() {
184-
local deps=(curl grep awk sort parallel)
204+
local deps=(curl jq awk grep parallel)
185205
for dep in "${deps[@]}"; do
186206
if ! command -v "$dep" >/dev/null 2>&1; then
187207
error "Required dependency: $dep"
@@ -759,31 +779,71 @@ save_results() {
759779

760780
# Function to update gists
761781
update_gists() {
762-
log "Starting gist update..."
763-
764-
# Check and run main gist
765-
if [[ -f "${WORK_DIR}/update_gist.sh" ]]; then
766-
log "Running update_gist.sh..."
767-
if bash "${WORK_DIR}/update_gist.sh"; then
768-
log "update_gist.sh executed successfully"
769-
else
770-
log "ERROR: Failed to execute update_gist.sh"
771-
return 1
772-
fi
773-
fi
782+
[[ "$EXPORT_GISTS" != "true" ]] && return 0
783+
784+
log "Starting gist update..."
785+
786+
# Check requirements
787+
command -v curl >/dev/null 2>&1 || { log "ERROR: curl is required"; return 1; }
788+
command -v jq >/dev/null 2>&1 || { log "ERROR: jq is required"; return 1; }
789+
790+
[[ -z "$GITHUB_TOKEN" ]] && { log "ERROR: GITHUB_TOKEN is not set"; return 1; }
791+
792+
# Update main list
793+
if [[ -n "$GIST_ID_MAIN" && -f "$OUTPUT_FILE" ]]; then
794+
log "Updating main list gist..."
795+
local content
796+
content=$(cat "$OUTPUT_FILE")
797+
798+
local response
799+
response=$(curl -s -X PATCH \
800+
-H "Authorization: token $GITHUB_TOKEN" \
801+
-H "Accept: application/vnd.github.v3+json" \
802+
-d "{
803+
\"files\": {
804+
\"$(basename "$OUTPUT_FILE")\": {
805+
\"content\": $(echo "$content" | jq -R -s .)
806+
}
807+
}
808+
}" \
809+
"https://api.github.com/gists/$GIST_ID_MAIN")
810+
811+
if echo "$response" | jq -e '.id' > /dev/null; then
812+
log "✅ Main list gist updated successfully"
813+
else
814+
log "❌ Error updating main list gist"
815+
return 1
816+
fi
817+
fi
774818

775-
# Check and run special gist
776-
if [[ -f "${WORK_DIR}/update_gist_special.sh" ]]; then
777-
log "Running update_gist_special.sh..."
778-
if bash "${WORK_DIR}/update_gist_special.sh"; then
779-
log "update_gist_special.sh executed successfully"
780-
else
781-
log "ERROR: Failed to execute update_gist_special.sh"
782-
return 1
783-
fi
784-
fi
819+
# Update special list
820+
if [[ -n "$GIST_ID_SPECIAL" && -f "$OUTPUT_FILESPECIAL" ]]; then
821+
log "Updating special list gist..."
822+
local content
823+
content=$(cat "$OUTPUT_FILESPECIAL")
824+
825+
local response
826+
response=$(curl -s -X PATCH \
827+
-H "Authorization: token $GITHUB_TOKEN" \
828+
-H "Accept: application/vnd.github.v3+json" \
829+
-d "{
830+
\"files\": {
831+
\"$(basename "$OUTPUT_FILESPECIAL")\": {
832+
\"content\": $(echo "$content" | jq -R -s .)
833+
}
834+
}
835+
}" \
836+
"https://api.github.com/gists/$GIST_ID_SPECIAL")
837+
838+
if echo "$response" | jq -e '.id' > /dev/null; then
839+
log "✅ Special list gist updated successfully"
840+
else
841+
log "❌ Error updating special list gist"
842+
return 1
843+
fi
844+
fi
785845

786-
return 0
846+
return 0
787847
}
788848

789849
# Function to check if update is needed

update_gist.sh

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)