Bladeren bron

Add http proxy config

jackyzy823 5 jaren geleden
bovenliggende
commit
6aa913ad62
5 gewijzigde bestanden met toevoegingen van 15 en 1 verwijderingen
  1. 2 0
      nitter.example.conf
  2. 2 0
      src/config.nim
  3. 8 1
      src/http_pool.nim
  4. 1 0
      src/nitter.nim
  5. 2 0
      src/types.nim

+ 2 - 0
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.

+ 2 - 0
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),

+ 8 - 1
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

+ 1 - 0
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"

+ 2 - 0
src/types.nim

@@ -217,6 +217,8 @@ type
     base64Media*: bool
     minTokens*: int
     enableRss*: bool
+    proxy*: string
+    proxyAuth*: string
 
     rssCacheTime*: int
     listCacheTime*: int