feat: wip visitors stuff
This commit is contained in:
parent
630a1c043c
commit
1374dbece4
@ -7,17 +7,20 @@ import { get, writable } from "svelte/store";
|
|||||||
const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount`
|
const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount`
|
||||||
const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
|
const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
|
||||||
|
|
||||||
|
type Visitor = { since: number }
|
||||||
|
const lastVisitors = writable<Visitor[]>([]);
|
||||||
|
const MAX_VISITORS = 10
|
||||||
|
const VISITOR_EXPIRY_SECONDS = 60 * 30 // half an hour is reasonable
|
||||||
|
|
||||||
export const incrementVisitCount = (request: Request, cookies: Cookies) => {
|
export const incrementVisitCount = (request: Request, cookies: Cookies) => {
|
||||||
let currentVisitCount = get(visitCount)
|
let currentVisitCount = get(visitCount)
|
||||||
// check whether the request is from a bot or not (this doesnt need to be accurate we just want to filter out honest bots)
|
// check whether the request is from a bot or not (this doesnt need to be accurate we just want to filter out honest bots)
|
||||||
const ua = request.headers.get('user-agent')
|
if (isBot(request)) { return currentVisitCount }
|
||||||
const isBot = ua ? ua.toLowerCase().match(/(bot|crawl|spider|walk)/) !== null : true
|
|
||||||
if (!isBot) {
|
|
||||||
const scopedCookies = scopeCookies(cookies, '/')
|
const scopedCookies = scopeCookies(cookies, '/')
|
||||||
// parse the last visit timestamp from cookies if it exists
|
// parse the last visit timestamp from cookies if it exists
|
||||||
const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0")
|
const visitedTimestamp = parseInt(scopedCookies.get('visitedTimestamp') || "0")
|
||||||
// get unix timestamp
|
// get unix timestamp
|
||||||
const currentTime = new Date().getTime()
|
const currentTime = Date.now()
|
||||||
const timeSinceVisit = currentTime - visitedTimestamp
|
const timeSinceVisit = currentTime - visitedTimestamp
|
||||||
// check if this is the first time a client is visiting or if an hour has passed since they last visited
|
// check if this is the first time a client is visiting or if an hour has passed since they last visited
|
||||||
if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60 * 24) {
|
if (visitedTimestamp === 0 || timeSinceVisit > 1000 * 60 * 60 * 24) {
|
||||||
@ -28,10 +31,37 @@ export const incrementVisitCount = (request: Request, cookies: Cookies) => {
|
|||||||
// write the visit count to a file so we can load it later again
|
// write the visit count to a file so we can load it later again
|
||||||
writeFileSync(visitCountFile, currentVisitCount.toString())
|
writeFileSync(visitCountFile, currentVisitCount.toString())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return currentVisitCount
|
return currentVisitCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const addLastVisitor = (request: Request, cookies: Cookies) => {
|
||||||
|
const currentTime = Date.now()
|
||||||
|
let visitors = get(lastVisitors).filter(
|
||||||
|
(value) => { return currentTime - value.since > 1000 * VISITOR_EXPIRY_SECONDS }
|
||||||
|
)
|
||||||
|
// check whether the request is from a bot or not (this doesnt need to be accurate we just want to filter out honest bots)
|
||||||
|
if (isBot(request)) { return visitors }
|
||||||
|
const scopedCookies = scopeCookies(cookies, '/')
|
||||||
|
// parse the last visit timestamp from cookies if it exists
|
||||||
|
const visitorTimestamp = parseInt(scopedCookies.get('visitorTimestamp') || "0")
|
||||||
|
// get unix timestamp
|
||||||
|
const timeSinceVisit = currentTime - visitorTimestamp
|
||||||
|
// check if this is the first time a client is visiting or if an hour has passed since they last visited
|
||||||
|
if (visitorTimestamp === 0 || timeSinceVisit > 1000 * VISITOR_EXPIRY_SECONDS) {
|
||||||
|
visitors.push({ since: currentTime })
|
||||||
|
if (visitors.length > MAX_VISITORS) { visitors.shift() }
|
||||||
|
// update the cookie with the current timestamp
|
||||||
|
scopedCookies.set('visitorTimestamp', currentTime.toString())
|
||||||
|
}
|
||||||
|
return visitors
|
||||||
|
}
|
||||||
|
|
||||||
|
const isBot = (request: Request) => {
|
||||||
|
const ua = request.headers.get('user-agent')
|
||||||
|
return ua ? ua.toLowerCase().match(/(bot|crawl|spider|walk|fetch|scrap|proxy|image)/) !== null : true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
export const notifyDarkVisitors = (url: URL, request: Request) => {
|
export const notifyDarkVisitors = (url: URL, request: Request) => {
|
||||||
fetch('https://api.darkvisitors.com/visits', {
|
fetch('https://api.darkvisitors.com/visits', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
Loading…
Reference in New Issue
Block a user