Compare commits

..

No commits in common. "53e9dd02fc25426641c004cd0f286129385ab0e7" and "1715184876fb171401762aa2efbfb348a55172b9" have entirely different histories.

5 changed files with 999 additions and 633 deletions

1
.gitignore vendored
View file

@ -131,4 +131,3 @@ dist
.dev.vars .dev.vars
/.wrangler

1163
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,8 +0,0 @@
DROP TABLE IF EXISTS requests;
CREATE TABLE IF NOT EXISTS requests (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp DATETIME, domain TEXT, method TEXT, path TEXT, country TEXT);
INSERT INTO requests (timestamp, domain, method, path, country)
VALUES
('1734918464464', 'example.com', 'GET', '/home', 'US'),
('1734918464464', 'anotherdomain.com', 'POST', '/login', 'DE'),
('1734918464464', 'yetanotherdomain.com', 'GET', '/products', 'GB'),
('1734918464464', 'coolwebsite.com', 'PUT', '/update', 'FR');

View file

@ -1,24 +1,41 @@
export default { addEventListener('fetch', event => {
async fetch(request, env, context) { event.respondWith(handleRequest(event.request))
const url = new URL(request.url); })
const AUTH_KEY = AUTH_KEY_SECRET
const FILE_KEY = 'analytics/requests.json'
const ALLOWED_ORIGINS = [
'https://jonasjones.dev',
'https://www.jonasjones.dev',
'https://blog.jonasjones.dev',
'https://docs.jonasjones.dev',
'https://analytics.jonasjones.dev',
'https://wiki.jonasjones.dev',
'https://kcomebacks.jonasjones.dev',
'https://jonasjonesstudios.com',
'https://lastlovedsyncify.jonasjones.dev',
'https://syncify.jonasjones.dev'
]
async function handleRequest(request) {
const url = new URL(request.url)
if (url.pathname === '/requests/record' && request.method === 'POST') { if (url.pathname === '/requests/record' && request.method === 'POST') {
return this.handleRecordRequest(request, env); return handleRecordRequest(request)
} else if (url.pathname === '/requests/record/ipunknown' && request.method === 'POST') { } else if (url.pathname === '/requests/record/ipunknown' && request.method === 'POST') {
return this.handleRecordIpRequest(request, env); return handleRecordIpRequest(request)
} else if (url.pathname === '/requests/get/count') { } else if (url.pathname === '/requests/get/count') {
return this.handleGetCountRequest(request, env); return handleGetCountRequest(request)
} else if (url.pathname.startsWith('/requests/get')) { } else if (url.pathname.startsWith('/requests/get')) {
return this.handleGetRequest(url); return handleGetRequest(url)
} else if (request.method === 'OPTIONS') { } else if (request.method === 'OPTIONS') {
return this.handleOptions(request); return handleOptions(request);
} }
return new Response('Not Found', { status: 404 }); return new Response('Not Found', { status: 404 })
}, }
// CORS handling for OPTIONS request function handleOptions(request) {
async handleOptions(request) {
const origin = request.headers.get('Origin'); const origin = request.headers.get('Origin');
const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost'); const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost');
@ -29,68 +46,80 @@ export default {
'Access-Control-Max-Age': '86400', // Cache the preflight response for 1 day 'Access-Control-Max-Age': '86400', // Cache the preflight response for 1 day
}; };
return new Response(null, { headers: headers }); return new Response(null, {
}, headers: headers,
});
}
// Handle recording request with the provided data
async handleRecordRequest(request, env) {
const { headers } = request;
const authKey = headers.get('Authorization');
if (authKey !== env.AUTH_KEY_SECRET) { async function handleRecordRequest(request) {
return new Response('Unauthorized', { status: 401 }); const { headers } = request
const authKey = headers.get('Authorization')
if (authKey !== AUTH_KEY) {
return new Response('Unauthorized', { status: 401 })
} }
try { try {
const { timestamp, domain, method, path, country } = await request.json(); const { timestamp, domain, method, path, ipcountry } = await request.json()
if (!timestamp || !domain || !method || !path || !country) { if (!timestamp || !domain || !method || !path || !ipcountry) {
return new Response('Bad Request: Missing required fields', { status: 400 }); return new Response('Bad Request: Missing required fields', { status: 400 })
} }
const record = { timestamp, domain, method, path, country }; const record = { timestamp, domain, method, path, ipcountry }
// Retrieve existing records from R2
let records = await getRecordsFromR2()
if (!records) {
records = []
}
// Add new record to the list
records.push(record)
// Store the updated list back to R2
await uploadRecordsToR2(records)
return new Response('Recorded', { status: 200 })
try {
// Store the new record in the database
await this.storeRecordInDB(record, env);
} catch (error) { } catch (error) {
return new Response('Error storing record in DB', { status: 500 }); console.log('Error recording request:', error)
return new Response('Bad Request: Invalid JSON', { status: 400 })
}
} }
return new Response('Recorded', { status: 200 }); async function handleRecordIpRequest(request) {
} catch (error) { const { headers } = request
console.log('Error recording request:', error); const authKey = headers.get('Authorization')
return new Response('Bad Request: Invalid JSON', { status: 400 });
}
},
// Handle recording request with IP country detection if (authKey !== AUTH_KEY) {
async handleRecordIpRequest(request, env) { return new Response('Unauthorized', { status: 401 })
const { headers } = request;
const authKey = headers.get('Authorization');
if (authKey !== env.AUTH_KEY_SECRET) {
return new Response('Unauthorized', { status: 401 });
} }
try { try {
const { timestamp, domain, method, path } = await request.json(); const { timestamp, domain, method, path } = await request.json()
if (!timestamp || !domain || !method || !path) { if (!timestamp || !domain || !method || !path) {
return new Response('Bad Request: Missing required fields', { status: 400 }); return new Response('Bad Request: Missing required fields', { status: 400 })
} }
const country = request.cf.country; const ipcountry = request.cf.country
const record = { timestamp, domain, method, path, country }; const record = { timestamp, domain, method, path, ipcountry }
try { // Retrieve existing records from R2
// Store the new record in the database let records = await getRecordsFromR2()
await this.storeRecordInDB(record, env); if (!records) {
} catch (error) { records = []
return new Response('Error storing record in DB', { status: 500 });
} }
// Add new record to the list
records.push(record)
// Store the updated list back to R2
await uploadRecordsToR2(records)
const origin = request.headers.get('Origin'); const origin = request.headers.get('Origin');
const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost'); const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost');
@ -101,137 +130,115 @@ export default {
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
}, },
}); })
} catch (error) {
console.log('Error recording request:', error);
return new Response('Bad Request: Invalid JSON', { status: 400 });
}
},
// Handle request to get count of records } catch (error) {
async handleGetCountRequest(request, env) { console.log('Error recording request:', error)
return new Response('Bad Request: Invalid JSON', { status: 400 })
}
}
async function handleGetCountRequest(request) {
const origin = request.headers.get('Origin'); const origin = request.headers.get('Origin');
const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost'); const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost');
try { const records = await getRecordsFromR2() || []
// Get number of entries in DB const count = records.length
//const resp = await env.DB.prepare('SELECT COUNT(*) FROM requests').get();
//const count = resp['COUNT(*)'];
const result = await env.DB.prepare("SELECT COUNT(*) AS count FROM requests").all();
console.log(result);
const count = result.results[0].count;
return new Response(JSON.stringify({ count }), { return new Response(JSON.stringify({ count }), {
headers: { headers: { 'Content-Type': 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization', 'Access-Control-Allow-Headers': 'Content-Type, Authorization',},
}, })
});
} catch (error) {
console.error('Error fetching count from DB:', error);
return new Response('Error fetching count', { status: 500 });
} }
},
// Handle GET requests with filters (start, end, count, etc.) async function handleGetRequest(url) {
async handleGetRequest(url) { const params = new URLSearchParams(url.search)
const params = new URLSearchParams(url.search);
const start = parseInt(params.get('start')) || 0; const start = parseInt(params.get('start')) || 0
const end = parseInt(params.get('end')) || Number.MAX_SAFE_INTEGER; const end = parseInt(params.get('end')) || Number.MAX_SAFE_INTEGER
const count = Math.min(parseInt(params.get('count')) || 100, 100); const count = Math.min(parseInt(params.get('count')) || 100, 100)
const offset = parseInt(params.get('offset')) || 0; const offset = parseInt(params.get('offset')) || 0
const domain = params.get('domain'); const domain = params.get('domain')
const method = params.get('method'); const method = params.get('method')
const path = params.get('path'); const path = params.get('path')
const country = params.get('country'); const ipcountry = params.get('ipcountry')
// Get records from the database with filtering let records = await getRecordsFromR2() || []
const records = await this.getRecordsFromDB(env, start, end, domain, method, path, country, offset, count);
return new Response(JSON.stringify(records), { let filteredRecords = records
.filter(record => {
return (
record.timestamp >= start &&
record.timestamp <= end &&
(!domain || record.domain === domain) &&
(!method || record.method === method) &&
(!path || record.path === path) &&
(!ipcountry || record.ipcountry === ipcountry)
)
})
.slice(offset, offset + count)
return new Response(JSON.stringify(filteredRecords), {
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}); })
},
// Store the new record in the database
async storeRecordInDB(record, env) {
await env.DB.prepare('INSERT INTO requests (timestamp, domain, method, path, country) VALUES (?, ?, ?, ?, ?)')
.bind(record.timestamp, record.domain, record.method, record.path, record.country)
.run();
},
// Retrieve filtered records from the database
async getRecordsFromDB(env, start, end, domain, method, path, country, offset, count) {
try {
const query = `
SELECT * FROM requests
WHERE timestamp >= ? AND timestamp <= ?
${domain ? 'AND domain = ?' : ''}
${method ? 'AND method = ?' : ''}
${path ? 'AND path = ?' : ''}
${country ? 'AND country = ?' : ''}
ORDER BY timestamp DESC
LIMIT ? OFFSET ?
`;
const params = [
start,
end,
domain,
method,
path,
country,
count,
offset,
].filter(param => param !== undefined);
const records = await env.DB.prepare(query).bind(...params).all();
return records.map(record => ({
timestamp: record.timestamp,
domain: record.domain,
method: record.method,
path: record.path,
country: record.country,
}));
} catch (error) {
console.error('Error fetching records from DB:', error);
return [];
} }
},
// Utility functions (e.g., country lookup, hashing, etc.) async function getRecordsFromR2() {
async getCountryCode(ip) { try {
const object = await CDN_BUCKET.get(FILE_KEY)
if (object === null) {
return []
}
const text = await object.text()
return JSON.parse(text)
} catch (error) {
console.error('Error fetching records:', error)
return []
}
}
async function uploadRecordsToR2(records) {
try {
const json = JSON.stringify(records)
await CDN_BUCKET.put(FILE_KEY, json)
} catch (error) {
console.error('Error uploading records:', error)
}
}
async function getCountryCode(ip) {
const response = await fetch(`https://ipinfo.io/${ip}/country?token=${IPINFO_TOKEN}`); const response = await fetch(`https://ipinfo.io/${ip}/country?token=${IPINFO_TOKEN}`);
if (!response.ok) { if (!response.ok) {
return "unknown"; return "unknown";
} }
// Since the response is plain text, use response.text()
const countryCode = await response.text(); const countryCode = await response.text();
return countryCode.trim() || "unknown";
},
async hashString(input) { // Trim any whitespace (like newline characters) from the response
const trimmedCountryCode = countryCode.trim();
// Check if the response is a valid country code
if (!trimmedCountryCode) {
return "unknown";
}
return trimmedCountryCode;
}
async function hashString(input) {
// Encode the input string as a Uint8Array (UTF-8)
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const data = encoder.encode(input); const data = encoder.encode(input);
// Hash the data using SHA-256
const hashBuffer = await crypto.subtle.digest('SHA-256', data); const hashBuffer = await crypto.subtle.digest('SHA-256', data);
// Convert the hash to a hexadecimal string
const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
},
}; return hashHex;
const ALLOWED_ORIGINS = [ }
'https://jonasjones.dev',
'https://www.jonasjones.dev',
'https://blog.jonasjones.dev',
'https://docs.jonasjones.dev',
'https://analytics.jonasjones.dev',
'https://wiki.jonasjones.dev',
'https://kcomebacks.jonasjones.dev',
'https://jonasjonesstudios.com',
'https://lastlovedsyncify.jonasjones.dev',
'https://syncify.jonasjones.dev',
];

View file

@ -10,11 +10,6 @@ workers_dev = true
binding = "CDN_BUCKET" binding = "CDN_BUCKET"
bucket_name = "cdn" bucket_name = "cdn"
[[d1_databases]]
binding = "DB"
database_name = "REQUESTS"
database_id = "665c17d4-250a-4a6d-8317-9b4522c7842f"
# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables) # Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
# Note: Use secrets to store sensitive data. # Note: Use secrets to store sensitive data.
# Docs: https://developers.cloudflare.com/workers/platform/environment-variables # Docs: https://developers.cloudflare.com/workers/platform/environment-variables