1
0
mirror of https://github.com/zedeus/nitter.git synced 2025-12-05 19:45:36 -05:00

Include username in session logs if available

Fixes #1310
This commit is contained in:
Zed
2025-11-17 05:42:35 +01:00
parent 778eb35ee3
commit a666c4867c
5 changed files with 26 additions and 9 deletions

View File

@@ -60,11 +60,11 @@ proc getAndValidateSession*(api: Api): Future[Session] {.async.} =
case result.kind
of SessionKind.oauth:
if result.oauthToken.len == 0:
echo "[sessions] Empty oauth token, session: ", result.id
echo "[sessions] Empty oauth token, session: ", result.pretty
raise rateLimitError()
of SessionKind.cookie:
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()
template fetchImpl(result, fetchBody) {.dirty.} =
@@ -107,7 +107,7 @@ template fetchImpl(result, fetchBody) {.dirty.} =
setLimited(session, api)
raise rateLimitError()
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
# rate limit hit, resets after the 15 minute window
raise rateLimitError()
@@ -124,8 +124,8 @@ template fetchImpl(result, fetchBody) {.dirty.} =
except OSError as e:
raise e
except Exception as e:
let id = if session.isNil: "null" else: $session.id
echo "error: ", e.name, ", msg: ", e.msg, ", sessionId: ", id, ", url: ", url
let s = session.pretty
echo "error: ", e.name, ", msg: ", e.msg, ", session: ", s, ", url: ", url
raise rateLimitError()
finally:
release(session)

View File

@@ -29,6 +29,20 @@ var
template log(str: varargs[string, `$`]) =
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 =
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 (epochTime().int - session.limitedAt) > hourInSeconds:
session.limited = false
log "resetting limit: ", session.id
log "resetting limit: ", session.pretty
return false
else:
return true
@@ -146,7 +160,7 @@ proc isReady(session: Session; api: Api): bool =
proc invalidate*(session: var Session) =
if session.isNil: return
log "invalidating: ", session.id
log "invalidating: ", session.pretty
# TODO: This isn't sufficient, but it works for now
let idx = sessionPool.find(session)
@@ -171,7 +185,7 @@ proc getSession*(api: Api): Future[Session] {.async.} =
proc setLimited*(session: Session; api: Api) =
session.limited = true
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) =
# avoid undefined behavior in race conditions

View File

@@ -13,6 +13,7 @@ proc parseSession*(raw: string): Session =
result = Session(
kind: SessionKind.oauth,
id: parseBiggestInt(id),
username: session.username,
oauthToken: session.oauthToken,
oauthSecret: session.oauthTokenSecret
)
@@ -21,6 +22,7 @@ proc parseSession*(raw: string): Session =
result = Session(
kind: SessionKind.cookie,
id: id,
username: session.username,
authToken: session.authToken,
ct0: session.ct0
)

View File

@@ -1,8 +1,8 @@
type
RawSession* = object
kind*: string
username*: string
id*: string
username*: string
oauthToken*: string
oauthTokenSecret*: string
authToken*: string

View File

@@ -38,6 +38,7 @@ type
Session* = ref object
id*: int64
username*: string
pending*: int
limited*: bool
limitedAt*: int