From 6aa913ad62c5768eeb4bdef8dc88c78a6f1056fc Mon Sep 17 00:00:00 2001 From: jackyzy823 Date: Thu, 30 Sep 2021 17:03:07 +0100 Subject: [PATCH] Add http proxy config --- nitter.example.conf | 2 ++ src/config.nim | 2 ++ src/http_pool.nim | 9 ++++++++- src/nitter.nim | 1 + src/types.nim | 2 ++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/nitter.example.conf b/nitter.example.conf index 518114d..93526b4 100644 --- a/nitter.example.conf +++ b/nitter.example.conf @@ -23,6 +23,8 @@ redisMaxConnections = 30 hmacKey = "secretkey" # random key for cryptographic signing of video urls base64Media = false # use base64 encoding for proxied media urls enableRSS = true # set this to false to disable RSS feeds +proxy = "" # proxy type http/https +proxyAuth = "" tokenCount = 10 # minimum amount of usable tokens. tokens are used to authorize API requests, # but they expire after ~1 hour, and have a limit of 187 requests. diff --git a/src/config.nim b/src/config.nim index a07c224..46409b6 100644 --- a/src/config.nim +++ b/src/config.nim @@ -27,6 +27,8 @@ proc getConfig*(path: string): (Config, parseCfg.Config) = base64Media: cfg.get("Config", "base64Media", false), minTokens: cfg.get("Config", "tokenCount", 10), enableRss: cfg.get("Config", "enableRSS", true), + proxy: cfg.get("Config", "proxy", ""), + proxyAuth: cfg.get("Config", "proxyAuth", ""), listCacheTime: cfg.get("Cache", "listMinutes", 120), rssCacheTime: cfg.get("Cache", "rssMinutes", 10), diff --git a/src/http_pool.nim b/src/http_pool.nim index f6feaf4..45aeea2 100644 --- a/src/http_pool.nim +++ b/src/http_pool.nim @@ -6,10 +6,17 @@ type conns*: seq[AsyncHttpClient] var maxConns {.threadvar.}: int +var proxy {.threadvar.}: Proxy proc setMaxHttpConns*(n: int) = maxConns = n +proc setHttpProxy*(url: string; auth: string) = + if url.len > 0: + proxy = newProxy(url, auth) + else: + proxy = nil + proc release*(pool: HttpPool; client: AsyncHttpClient) = if pool.conns.len >= maxConns: client.close() @@ -20,7 +27,7 @@ template use*(pool: HttpPool; heads: HttpHeaders; body: untyped): untyped = var c {.inject.}: AsyncHttpClient if pool.conns.len == 0: - c = newAsyncHttpClient(headers=heads) + c = newAsyncHttpClient(headers=heads, proxy=proxy) else: c = pool.conns.pop() c.headers = heads diff --git a/src/nitter.nim b/src/nitter.nim index 2e69576..225d01b 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -32,6 +32,7 @@ setCacheTimes(cfg) setHmacKey(cfg.hmacKey) setProxyEncoding(cfg.base64Media) setMaxHttpConns(cfg.httpMaxConns) +setHttpProxy(cfg.proxy, cfg.proxyAuth) waitFor initRedisPool(cfg) stdout.write &"Connected to Redis at {cfg.redisHost}:{cfg.redisPort}\n" diff --git a/src/types.nim b/src/types.nim index 8ab3236..59f141b 100644 --- a/src/types.nim +++ b/src/types.nim @@ -217,6 +217,8 @@ type base64Media*: bool minTokens*: int enableRss*: bool + proxy*: string + proxyAuth*: string rssCacheTime*: int listCacheTime*: int