-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkol.sh
205 lines (168 loc) · 7.21 KB
/
kol.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
# ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to show menu and handle user input
function show_menu() {
# Display channel names in colors
echo -e "${RED}YOUTUBE: KOLANDONE${NC}"
echo -e "${BLUE}TELEGRAM: KOLANDJS${NC}"
echo "Choose an option or type 'exit' to quit:"
echo "1) List all Workers"
echo "2) Create a Worker"
echo "3) Delete a Worker"
read -r USER_OPTION
case $USER_OPTION in
1)
list_all_workers
;;
2)
create_worker
;;
3)
delete_worker
;;
"exit")
echo "Exiting script."
exit 0
;;
*)
echo "Invalid option selected."
;;
esac
}
# Function to list all Workers and allow user to select one to get the visit link
function list_all_workers() {
# Retrieve the list of Workers and their details
WORKERS_DETAILS=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/scripts" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN")
# Parse the list of Workers
echo "List of Workers:"
echo "$WORKERS_DETAILS" | jq -r '.result[] | .id' | nl -w1 -s') '
# Ask the user to select a Worker to get the visit link
echo "Enter the number of the Worker to get the visit link or type 'back' to return to the main menu:"
read -r WORKER_SELECTION
if [[ "$WORKER_SELECTION" =~ ^[0-9]+$ ]]; then
# Get the Worker name based on user selection
SELECTED_WORKER_NAME=$(echo "$WORKERS_DETAILS" | jq -r --argjson WORKER_SELECTION "$WORKER_SELECTION" '.result[$WORKER_SELECTION - 1] | .id')
# Call the function to get the workers.dev subdomain for the selected Worker
get_workers_dev_subdomain "$SELECTED_WORKER_NAME"
elif [ "$WORKER_SELECTION" == "back" ]; then
return
else
echo "Invalid selection."
fi
}
# Function to get the workers.dev subdomain for a Worker
function get_workers_dev_subdomain() {
local WORKER_NAME=$1
# Retrieve the workers.dev subdomain for the given Worker name
WORKER_SUBDOMAIN=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/subdomain" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result.subdomain')
# Check if the workers.dev subdomain was retrieved successfully
if [ -n "$WORKER_SUBDOMAIN" ]; then
echo -e "The visit link for ${GREEN}$WORKER_NAME${NC} is: ${GREEN}https://${WORKER_NAME}.${WORKER_SUBDOMAIN}.workers.dev${NC}"
else
echo "Failed to retrieve the workers.dev subdomain for $WORKER_NAME."
fi
}
# Function to create a Worker
function create_worker() {
# Prompt for the Worker name
echo "Please enter a name for your Cloudflare Worker:"
read -r WORKER_NAME
# Ask if the user needs a KV namespace
echo "Do you need a KV namespace? (yes/no)"
read -r NEED_KV_NAMESPACE
if [ "$NEED_KV_NAMESPACE" == "yes" ]; then
# Prompt for the KV namespace name
echo "Please enter a name for your KV namespace:"
read -r KV_NAMESPACE_NAME
# Prompt for the binding variable name
echo "Please enter the name for the binding variable:"
read -r BINDING_VARIABLE_NAME
# Create the KV namespace using the Cloudflare API
CREATE_KV_RESPONSE=$(curl -s -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/storage/kv/namespaces" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
--data "{\"title\":\"$KV_NAMESPACE_NAME\"}")
# Extract the KV namespace ID from the response
KV_NAMESPACE_ID=$(echo $CREATE_KV_RESPONSE | jq -r '.result.id')
# Check if the KV namespace was created successfully
if [ -n "$KV_NAMESPACE_ID" ]; then
echo "KV namespace created successfully with ID: $KV_NAMESPACE_ID"
# Create a new Cloudflare worker using wrangler and automate responses
wrangler generate "$WORKER_NAME"
# Change to the worker directory
cd "$WORKER_NAME" || { echo "Failed to change directory to $WORKER_NAME"; exit 1; }
# Update the wrangler.toml file to include the KV namespace binding
echo "kv_namespaces = [ { binding = \"$BINDING_VARIABLE_NAME\", id = \"$KV_NAMESPACE_ID\" } ]" >> wrangler.toml
else
echo "Failed to create KV namespace."
echo "Response: $CREATE_KV_RESPONSE"
return
fi
else
# Create a new Cloudflare worker using wrangler and automate responses
wrangler generate "$WORKER_NAME"
# Change to the worker directory
cd "$WORKER_NAME" || { echo "Failed to change directory to $WORKER_NAME"; exit 1; }
fi
# Prompt for the URL of the new script
echo "Please enter the URL of the script you want to use to update index.js:"
read -r SCRIPT_URL
# Fetch the new script content from the URL and save it as index.js in the src directory
curl -s "$SCRIPT_URL" -o src/index.js
# Check if the download was successful
if [ $? -eq 0 ]; then
echo "New script content downloaded successfully to src/index.js."
# Ask if the user wants to change the UUID
echo "Do you want to change the UUID in the script? (yes/no)"
read -r CHANGE_UUID
if [ "$CHANGE_UUID" == "yes" ]; then
# Generate a random UUID
NEW_UUID=$(uuidgen)
# Replace the UUID in the script (assuming the UUID is defined as `let userID = "some-uuid";`)
sed -i "s/\([0-9a-fA-F]\{8\}-[0-9a-fA-F]\{4\}-[0-9a-fA-F]\{4\}-[0-9a-fA-F]\{4\}-[0-9a-fA-F]\{12\}\)/$NEW_UUID/g" src/index.js
echo "UUID has been changed to: $NEW_UUID"
fi
# Deploy the worker with the updated index.js
DEPLOY_RESPONSE=$(wrangler deploy)
# Show the visit link of the deployed Worker to the user
get_workers_dev_subdomain "$WORKER_NAME"
# If UUID was changed, display it below the worker link
if [ "$CHANGE_UUID" == "yes" ]; then
echo "New UUID: $NEW_UUID"
fi
else
echo "Failed to download the new script content."
fi
}
# Function to delete a Worker
function delete_worker() {
# Prompt for the Worker name to delete
echo "Enter the name of the Worker you want to delete:"
read -r DELETE_WORKER_NAME
# Delete the selected Worker
DELETE_RESPONSE=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/scripts/$DELETE_WORKER_NAME" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN")
# Check if the deletion was successful
if echo "$DELETE_RESPONSE" | grep -q '"success":true'; then
echo "Worker $DELETE_WORKER_NAME deleted successfully."
else
echo "success $DELETE_WORKER_NAME."
echo "Response: $DELETE_RESPONSE"
fi
}
# Main script logic
echo "Please enter your Cloudflare API token:"
read -r CLOUDFLARE_API_TOKEN
export CLOUDFLARE_API_TOKEN="$CLOUDFLARE_API_TOKEN"
echo "Please enter your Cloudflare Account ID:"
read -r ACCOUNT_ID
# Loop to show the menu repeatedly
while true; do
show_menu
done