2021-12-27 01:37:38 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2022-01-05 18:08:08 +00:00
|
|
|
import asyncdispatch, strformat, logging
|
2019-07-31 00:15:43 +00:00
|
|
|
from net import Port
|
2021-01-07 21:31:29 +00:00
|
|
|
from htmlgen import a
|
2021-06-23 21:17:16 +00:00
|
|
|
from os import getEnv
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-09-06 00:42:35 +00:00
|
|
|
import jester
|
2019-06-24 21:25:21 +00:00
|
|
|
|
2020-11-07 20:31:03 +00:00
|
|
|
import types, config, prefs, formatters, redis_cache, http_pool, tokens
|
2019-09-09 02:41:20 +00:00
|
|
|
import views/[general, about]
|
2019-09-23 14:12:20 +00:00
|
|
|
import routes/[
|
2022-01-05 21:49:16 +00:00
|
|
|
preferences, timeline, status, media, search, rss, list, debug,
|
2020-06-09 14:45:21 +00:00
|
|
|
unsupported, embed, resolver, router_utils]
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2021-01-07 21:31:29 +00:00
|
|
|
const instancesUrl = "https://github.com/zedeus/nitter/wiki/Instances"
|
2021-12-28 04:02:28 +00:00
|
|
|
const issuesUrl = "https://github.com/zedeus/nitter/issues"
|
2021-01-07 21:31:29 +00:00
|
|
|
|
2021-06-23 21:17:16 +00:00
|
|
|
let configPath = getEnv("NITTER_CONF_FILE", "./nitter.conf")
|
2020-05-08 00:48:47 +00:00
|
|
|
let (cfg, fullCfg) = getConfig(configPath)
|
|
|
|
|
2022-01-05 18:08:08 +00:00
|
|
|
if not cfg.enableDebug:
|
2020-06-09 18:29:05 +00:00
|
|
|
# Silence Jester's query warning
|
|
|
|
addHandler(newConsoleLogger())
|
|
|
|
setLogFilter(lvlError)
|
2020-06-07 07:18:40 +00:00
|
|
|
|
2021-01-08 01:25:43 +00:00
|
|
|
stdout.write &"Starting Nitter at {getUrlPrefix(cfg)}\n"
|
2020-06-07 07:18:40 +00:00
|
|
|
stdout.flushFile
|
|
|
|
|
2020-05-08 00:48:47 +00:00
|
|
|
updateDefaultPrefs(fullCfg)
|
2020-06-01 00:25:39 +00:00
|
|
|
setCacheTimes(cfg)
|
2019-10-23 22:17:38 +00:00
|
|
|
setHmacKey(cfg.hmacKey)
|
2020-06-09 13:04:38 +00:00
|
|
|
setProxyEncoding(cfg.base64Media)
|
2021-06-23 21:17:16 +00:00
|
|
|
setMaxHttpConns(cfg.httpMaxConns)
|
2021-09-30 16:03:07 +00:00
|
|
|
setHttpProxy(cfg.proxy, cfg.proxyAuth)
|
2022-01-09 22:48:25 +00:00
|
|
|
initAboutPage(cfg.staticDir)
|
2020-06-01 11:36:10 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
waitFor initRedisPool(cfg)
|
2020-06-07 07:18:40 +00:00
|
|
|
stdout.write &"Connected to Redis at {cfg.redisHost}:{cfg.redisPort}\n"
|
|
|
|
stdout.flushFile
|
|
|
|
|
2020-06-01 00:25:39 +00:00
|
|
|
asyncCheck initTokenPool(cfg)
|
2019-10-23 22:17:38 +00:00
|
|
|
|
2019-09-23 14:12:20 +00:00
|
|
|
createUnsupportedRouter(cfg)
|
2019-12-30 10:41:09 +00:00
|
|
|
createResolverRouter(cfg)
|
2019-09-06 00:42:35 +00:00
|
|
|
createPrefRouter(cfg)
|
|
|
|
createTimelineRouter(cfg)
|
2019-09-20 23:08:30 +00:00
|
|
|
createListRouter(cfg)
|
2019-09-20 12:10:10 +00:00
|
|
|
createStatusRouter(cfg)
|
2019-09-13 20:24:58 +00:00
|
|
|
createSearchRouter(cfg)
|
2019-09-06 00:42:35 +00:00
|
|
|
createMediaRouter(cfg)
|
2019-12-06 14:15:56 +00:00
|
|
|
createEmbedRouter(cfg)
|
2021-12-28 05:21:22 +00:00
|
|
|
createRssRouter(cfg)
|
2022-01-05 21:49:16 +00:00
|
|
|
createDebugRouter(cfg)
|
2019-07-31 00:15:43 +00:00
|
|
|
|
|
|
|
settings:
|
|
|
|
port = Port(cfg.port)
|
|
|
|
staticDir = cfg.staticDir
|
|
|
|
bindAddr = cfg.address
|
2023-01-08 23:11:33 +00:00
|
|
|
reusePort = true
|
2019-07-31 00:15:43 +00:00
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
routes:
|
|
|
|
get "/":
|
2020-06-09 14:45:21 +00:00
|
|
|
resp renderMain(renderSearch(), request, cfg, themePrefs())
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-09-09 02:41:20 +00:00
|
|
|
get "/about":
|
2020-06-09 14:45:21 +00:00
|
|
|
resp renderMain(renderAbout(), request, cfg, themePrefs())
|
2019-09-09 02:41:20 +00:00
|
|
|
|
2019-09-23 14:12:20 +00:00
|
|
|
get "/explore":
|
|
|
|
redirect("/about")
|
2019-09-22 23:55:02 +00:00
|
|
|
|
2019-09-23 14:12:20 +00:00
|
|
|
get "/help":
|
|
|
|
redirect("/about")
|
|
|
|
|
2019-10-17 22:45:54 +00:00
|
|
|
get "/i/redirect":
|
|
|
|
let url = decodeUrl(@"url")
|
2019-10-21 05:59:22 +00:00
|
|
|
if url.len == 0: resp Http404
|
2021-12-27 01:27:49 +00:00
|
|
|
redirect(replaceUrls(url, cookiePrefs()))
|
2019-10-17 22:45:54 +00:00
|
|
|
|
2019-10-07 14:47:53 +00:00
|
|
|
error Http404:
|
2019-10-21 05:59:22 +00:00
|
|
|
resp Http404, showError("Page not found", cfg)
|
2019-10-07 14:47:53 +00:00
|
|
|
|
2021-12-28 04:02:28 +00:00
|
|
|
error InternalError:
|
2021-12-28 04:41:41 +00:00
|
|
|
echo error.exc.name, ": ", error.exc.msg
|
2021-12-28 04:02:28 +00:00
|
|
|
const link = a("open a GitHub issue", href = issuesUrl)
|
2021-12-28 04:41:41 +00:00
|
|
|
resp Http500, showError(
|
|
|
|
&"An error occurred, please {link} with the URL you tried to visit.", cfg)
|
2021-12-28 04:02:28 +00:00
|
|
|
|
2023-04-21 12:41:30 +00:00
|
|
|
error BadClientError:
|
|
|
|
echo error.exc.name, ": ", error.exc.msg
|
|
|
|
resp Http500, showError("Network error occured, please try again.", cfg)
|
|
|
|
|
2021-01-07 21:31:29 +00:00
|
|
|
error RateLimitError:
|
2021-12-28 04:02:28 +00:00
|
|
|
const link = a("another instance", href = instancesUrl)
|
2021-12-28 04:41:41 +00:00
|
|
|
resp Http429, showError(
|
|
|
|
&"Instance has been rate limited.<br>Use {link} or try again later.", cfg)
|
2021-01-07 21:31:29 +00:00
|
|
|
|
2019-09-15 10:10:43 +00:00
|
|
|
extend rss, ""
|
2023-04-21 12:41:30 +00:00
|
|
|
extend status, ""
|
2019-09-13 20:24:58 +00:00
|
|
|
extend search, ""
|
2019-09-06 00:42:35 +00:00
|
|
|
extend timeline, ""
|
|
|
|
extend media, ""
|
2023-04-21 12:41:30 +00:00
|
|
|
extend list, ""
|
|
|
|
extend preferences, ""
|
|
|
|
extend resolver, ""
|
2019-12-06 14:15:56 +00:00
|
|
|
extend embed, ""
|
2022-01-05 21:49:16 +00:00
|
|
|
extend debug, ""
|
2023-04-21 12:41:30 +00:00
|
|
|
extend unsupported, ""
|