mirror of
https://github.com/zedeus/nitter.git
synced 2025-12-06 03:55:36 -05:00
@@ -60,11 +60,11 @@ proc getAndValidateSession*(api: Api): Future[Session] {.async.} =
|
|||||||
case result.kind
|
case result.kind
|
||||||
of SessionKind.oauth:
|
of SessionKind.oauth:
|
||||||
if result.oauthToken.len == 0:
|
if result.oauthToken.len == 0:
|
||||||
echo "[sessions] Empty oauth token, session: ", result.id
|
echo "[sessions] Empty oauth token, session: ", result.pretty
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
of SessionKind.cookie:
|
of SessionKind.cookie:
|
||||||
if result.authToken.len == 0 or result.ct0.len == 0:
|
if result.authToken.len == 0 or result.ct0.len == 0:
|
||||||
echo "[sessions] Empty cookie credentials, session: ", result.id
|
echo "[sessions] Empty cookie credentials, session: ", result.pretty
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
|
|
||||||
template fetchImpl(result, fetchBody) {.dirty.} =
|
template fetchImpl(result, fetchBody) {.dirty.} =
|
||||||
@@ -107,7 +107,7 @@ template fetchImpl(result, fetchBody) {.dirty.} =
|
|||||||
setLimited(session, api)
|
setLimited(session, api)
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
elif result.startsWith("429 Too Many Requests"):
|
elif result.startsWith("429 Too Many Requests"):
|
||||||
echo "[sessions] 429 error, API: ", api, ", session: ", session.id
|
echo "[sessions] 429 error, API: ", api, ", session: ", session.pretty
|
||||||
session.apis[api].remaining = 0
|
session.apis[api].remaining = 0
|
||||||
# rate limit hit, resets after the 15 minute window
|
# rate limit hit, resets after the 15 minute window
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
@@ -124,8 +124,8 @@ template fetchImpl(result, fetchBody) {.dirty.} =
|
|||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise e
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
let id = if session.isNil: "null" else: $session.id
|
let s = session.pretty
|
||||||
echo "error: ", e.name, ", msg: ", e.msg, ", sessionId: ", id, ", url: ", url
|
echo "error: ", e.name, ", msg: ", e.msg, ", session: ", s, ", url: ", url
|
||||||
raise rateLimitError()
|
raise rateLimitError()
|
||||||
finally:
|
finally:
|
||||||
release(session)
|
release(session)
|
||||||
|
|||||||
20
src/auth.nim
20
src/auth.nim
@@ -29,6 +29,20 @@ var
|
|||||||
template log(str: varargs[string, `$`]) =
|
template log(str: varargs[string, `$`]) =
|
||||||
echo "[sessions] ", str.join("")
|
echo "[sessions] ", str.join("")
|
||||||
|
|
||||||
|
proc pretty*(session: Session): string =
|
||||||
|
if session.isNil:
|
||||||
|
return "<null>"
|
||||||
|
|
||||||
|
if session.id > 0 and session.username.len > 0:
|
||||||
|
result = $session.id & " (" & session.username & ")"
|
||||||
|
elif session.username.len > 0:
|
||||||
|
result = session.username
|
||||||
|
elif session.id > 0:
|
||||||
|
result = $session.id
|
||||||
|
else:
|
||||||
|
result = "<unknown>"
|
||||||
|
result = $session.kind & " " & result
|
||||||
|
|
||||||
proc snowflakeToEpoch(flake: int64): int64 =
|
proc snowflakeToEpoch(flake: int64): int64 =
|
||||||
int64(((flake shr 22) + 1288834974657) div 1000)
|
int64(((flake shr 22) + 1288834974657) div 1000)
|
||||||
|
|
||||||
@@ -130,7 +144,7 @@ proc isLimited(session: Session; api: Api): bool =
|
|||||||
if session.limited and api != Api.userTweets:
|
if session.limited and api != Api.userTweets:
|
||||||
if (epochTime().int - session.limitedAt) > hourInSeconds:
|
if (epochTime().int - session.limitedAt) > hourInSeconds:
|
||||||
session.limited = false
|
session.limited = false
|
||||||
log "resetting limit: ", session.id
|
log "resetting limit: ", session.pretty
|
||||||
return false
|
return false
|
||||||
else:
|
else:
|
||||||
return true
|
return true
|
||||||
@@ -146,7 +160,7 @@ proc isReady(session: Session; api: Api): bool =
|
|||||||
|
|
||||||
proc invalidate*(session: var Session) =
|
proc invalidate*(session: var Session) =
|
||||||
if session.isNil: return
|
if session.isNil: return
|
||||||
log "invalidating: ", session.id
|
log "invalidating: ", session.pretty
|
||||||
|
|
||||||
# TODO: This isn't sufficient, but it works for now
|
# TODO: This isn't sufficient, but it works for now
|
||||||
let idx = sessionPool.find(session)
|
let idx = sessionPool.find(session)
|
||||||
@@ -171,7 +185,7 @@ proc getSession*(api: Api): Future[Session] {.async.} =
|
|||||||
proc setLimited*(session: Session; api: Api) =
|
proc setLimited*(session: Session; api: Api) =
|
||||||
session.limited = true
|
session.limited = true
|
||||||
session.limitedAt = epochTime().int
|
session.limitedAt = epochTime().int
|
||||||
log "rate limited by api: ", api, ", reqs left: ", session.apis[api].remaining, ", id: ", session.id
|
log "rate limited by api: ", api, ", reqs left: ", session.apis[api].remaining, ", ", session.pretty
|
||||||
|
|
||||||
proc setRateLimit*(session: Session; api: Api; remaining, reset, limit: int) =
|
proc setRateLimit*(session: Session; api: Api; remaining, reset, limit: int) =
|
||||||
# avoid undefined behavior in race conditions
|
# avoid undefined behavior in race conditions
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ proc parseSession*(raw: string): Session =
|
|||||||
result = Session(
|
result = Session(
|
||||||
kind: SessionKind.oauth,
|
kind: SessionKind.oauth,
|
||||||
id: parseBiggestInt(id),
|
id: parseBiggestInt(id),
|
||||||
|
username: session.username,
|
||||||
oauthToken: session.oauthToken,
|
oauthToken: session.oauthToken,
|
||||||
oauthSecret: session.oauthTokenSecret
|
oauthSecret: session.oauthTokenSecret
|
||||||
)
|
)
|
||||||
@@ -21,6 +22,7 @@ proc parseSession*(raw: string): Session =
|
|||||||
result = Session(
|
result = Session(
|
||||||
kind: SessionKind.cookie,
|
kind: SessionKind.cookie,
|
||||||
id: id,
|
id: id,
|
||||||
|
username: session.username,
|
||||||
authToken: session.authToken,
|
authToken: session.authToken,
|
||||||
ct0: session.ct0
|
ct0: session.ct0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
type
|
type
|
||||||
RawSession* = object
|
RawSession* = object
|
||||||
kind*: string
|
kind*: string
|
||||||
username*: string
|
|
||||||
id*: string
|
id*: string
|
||||||
|
username*: string
|
||||||
oauthToken*: string
|
oauthToken*: string
|
||||||
oauthTokenSecret*: string
|
oauthTokenSecret*: string
|
||||||
authToken*: string
|
authToken*: string
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ type
|
|||||||
|
|
||||||
Session* = ref object
|
Session* = ref object
|
||||||
id*: int64
|
id*: int64
|
||||||
|
username*: string
|
||||||
pending*: int
|
pending*: int
|
||||||
limited*: bool
|
limited*: bool
|
||||||
limitedAt*: int
|
limitedAt*: int
|
||||||
|
|||||||
Reference in New Issue
Block a user