From 99193718fb7a4152b5264e75bf56551daf9c4e6f Mon Sep 17 00:00:00 2001 From: syeopite Date: Thu, 27 May 2021 16:09:45 -0700 Subject: [PATCH 1/4] Allow inst stats to be fetched from onion insts. --- config.yml | 9 +++++++++ src/helpers/helpers.cr | 7 +++++++ src/instances.cr | 23 ++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 config.yml create mode 100644 src/helpers/helpers.cr diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..08f2a5e --- /dev/null +++ b/config.yml @@ -0,0 +1,9 @@ +# Uses the system's CURL binary to do so. +fetch_onion_instance_stats: true + +# TOR's Sock proxy address that CURL uses to connect to hidden services +tor_sock_proxy_address: "127.0.0.1" +tor_sock_proxy_port: 9050 + +# Minutes before refreshing the instance stats +minutes_between_refresh: 30 \ No newline at end of file diff --git a/src/helpers/helpers.cr b/src/helpers/helpers.cr new file mode 100644 index 0000000..8308c13 --- /dev/null +++ b/src/helpers/helpers.cr @@ -0,0 +1,7 @@ +require "yaml" + +def load_config() + config = YAML.parse(File.read("config.yml")) + return config +end + diff --git a/src/instances.cr b/src/instances.cr index 71c4551..67a36a1 100644 --- a/src/instances.cr +++ b/src/instances.cr @@ -18,6 +18,10 @@ require "http/client" require "kemal" require "uri" +require "./helpers/*" + +CONFIG = load_config() + Kemal::CLI.new ARGV macro rendered(filename) @@ -68,6 +72,23 @@ spawn do case type = host.split(".")[-1] when "onion" + type = "onion" + + if CONFIG["fetch_onion_instance_stats"]? + begin + args = Process.parse_arguments("--socks5-hostname '#{CONFIG["tor_sock_proxy_address"]}:#{CONFIG["tor_sock_proxy_port"]}' 'http://#{uri.host}/api/v1/stats'") + response = nil + Process.run("curl", args: args) do |result| + data = result.output.read_line + response = JSON.parse(data) + end + + stats = response + + rescue ex + stats = nil + end + end when "i2p" else type = uri.scheme.not_nil! @@ -88,7 +109,7 @@ spawn do INSTANCES.clear INSTANCES.merge! instances - sleep 5.minutes + sleep CONFIG["minutes_between_refresh"].as_i.minutes end end From e58e7024e47f47183ebfdddc3f27eaccfa8dcbb3 Mon Sep 17 00:00:00 2001 From: syeopite Date: Thu, 27 May 2021 16:39:17 -0700 Subject: [PATCH 2/4] Fix lint --- src/helpers/helpers.cr | 3 +-- src/instances.cr | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/helpers/helpers.cr b/src/helpers/helpers.cr index 8308c13..f599087 100644 --- a/src/helpers/helpers.cr +++ b/src/helpers/helpers.cr @@ -1,7 +1,6 @@ require "yaml" -def load_config() +def load_config config = YAML.parse(File.read("config.yml")) return config end - diff --git a/src/instances.cr b/src/instances.cr index 67a36a1..4862f3d 100644 --- a/src/instances.cr +++ b/src/instances.cr @@ -84,7 +84,6 @@ spawn do end stats = response - rescue ex stats = nil end From c4da7f353fc7e4d52bf4dd20ce6dcb96ae35e8a7 Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 30 May 2021 06:44:09 -0700 Subject: [PATCH 3/4] Update dockerfile with Tor, curl and yaml support --- Dockerfile | 5 +++-- docker-compose.yml | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2b3b239..42044a1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,14 +3,15 @@ WORKDIR /app COPY ./shard.yml ./shard.yml RUN shards install COPY ./src/ ./src/ -RUN crystal build ./src/instances.cr --release +RUN crystal build ./src/instances.cr -s -p -t FROM alpine:latest -RUN apk add --no-cache gc pcre libgcc +RUN apk add --no-cache gc pcre libgcc yaml WORKDIR /app RUN addgroup -g 1000 -S invidious && \ adduser -u 1000 -S invidious -G invidious COPY ./assets/ ./assets/ +COPY ./config.yml ./config.yml COPY --from=builder /app/instances . EXPOSE 3000 diff --git a/docker-compose.yml b/docker-compose.yml index 83e664a..57fc7a1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,13 @@ version: '3' services: + tor-socks-proxy: + container_name: tor-socks-proxy + image: peterdavehello/tor-socks-proxy:latest + ports: + - "127.0.0.1:8853:53/udp" + - "127.0.0.1:9150:9150/tcp" + restart: unless-stopped + instances: build: . restart: unless-stopped From 67a99e2c3b1d5102006bb6127d4709e5e692032e Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 30 May 2021 11:50:52 -0700 Subject: [PATCH 4/4] Re add --release option in Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 42044a1..0f59097 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ WORKDIR /app COPY ./shard.yml ./shard.yml RUN shards install COPY ./src/ ./src/ -RUN crystal build ./src/instances.cr -s -p -t +RUN crystal build ./src/instances.cr --release FROM alpine:latest RUN apk add --no-cache gc pcre libgcc yaml