From 8042dcad0c61110111949a856c935325ca65f8ef Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Sun, 7 Nov 2021 12:29:06 -0700 Subject: Refactor project to new name The name of the project is being refactored from Privacy Revolver to Farside. The reasoning behind this is: 1. A shorter name is easier to remember 2. It can stand for "FOSS alternative redirecting service" (which I know doesn't encapsulate all letters from "farside", but it's close enough). This commit also includes improvements to the update script for determining how far along the script is. --- README.md | 15 +++++++++-- config/config.exs | 2 +- lib/farside/application.ex | 17 +++++++++++++ lib/farside/router.ex | 51 +++++++++++++++++++++++++++++++++++++ lib/privacy_revolver/application.ex | 17 ------------- lib/privacy_revolver/router.ex | 51 ------------------------------------- mix.exs | 6 ++--- test/farside_test.exs | 18 +++++++++++++ test/privacy_revolver_test.exs | 18 ------------- update.exs | 10 +++++--- 10 files changed, 110 insertions(+), 95 deletions(-) create mode 100644 lib/farside/application.ex create mode 100644 lib/farside/router.ex delete mode 100644 lib/privacy_revolver/application.ex delete mode 100644 lib/privacy_revolver/router.ex create mode 100644 test/farside_test.exs delete mode 100644 test/privacy_revolver_test.exs diff --git a/README.md b/README.md index 88082d7..8d78543 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,16 @@ -# Privacy Revolver +# Farside +FOSS alternative redirecting service [![Elixir CI](https://github.com/benbusby/privacy-revolver/actions/workflows/elixir.yml/badge.svg)](https://github.com/benbusby/privacy-revolver/actions/workflows/elixir.yml) -A smart redirecting proxy for privacy-friendly frontends +A tool for evenly distributing traffic across various open source alternative frontends + +### Development + +- Install [redis](https://redis.io) +- Install [elixir](https://elixir-lang.org/install.html) +- Start redis: `redis-server /usr/local/etc/redis.conf` +- Install dependencies: `mix deps.get` +- Initialize redis contents: `mix run update.exs` +- Run Farside: `mix run --no-halt` + - Uses localhost:4001 diff --git a/config/config.exs b/config/config.exs index 7d74cd3..11fa98f 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,6 +1,6 @@ import Config -config :privacy_revolver, +config :farside, redis_conn: "redis://localhost:6379", fallback_str: "-fallback", update_file: ".update-results", diff --git a/lib/farside/application.ex b/lib/farside/application.ex new file mode 100644 index 0000000..708ff0d --- /dev/null +++ b/lib/farside/application.ex @@ -0,0 +1,17 @@ +defmodule Farside.Application do + @redis_conn Application.fetch_env!(:farside, :redis_conn) + @moduledoc false + + use Application + + @impl true + def start(_type, _args) do + children = [ + Plug.Cowboy.child_spec(scheme: :http, plug: Farside.Router, options: [port: 4001]), + {Redix, {@redis_conn, [name: :redix]}} + ] + + opts = [strategy: :one_for_one, name: Farside.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/lib/farside/router.ex b/lib/farside/router.ex new file mode 100644 index 0000000..95d9b41 --- /dev/null +++ b/lib/farside/router.ex @@ -0,0 +1,51 @@ +defmodule Farside.Router do + @fallback_str Application.fetch_env!(:farside, :fallback_str) + + use Plug.Router + + plug(:match) + plug(:dispatch) + + get "/" do + send_resp(conn, 200, "") + end + + get "/ping" do + # Useful for app healthcheck + {:ok, resp} = Redix.command(:redix, ["PING"]) + send_resp(conn, 200, resp) + end + + get "/:service/*glob" do + path = Enum.join(glob, "/") + + {:ok, instances} = + Redix.command( + :redix, + ["LRANGE", service, "0", "-1"] + ) + + # Either pick a random available instance, + # or fall back to the default one + instance = + if Enum.count(instances) > 0 do + Enum.random(instances) + else + {:ok, result} = + Redix.command( + :redix, + ["GET", "#{service}#{@fallback_str}"] + ) + + result + end + + # Redirect to the available instance + conn + |> Plug.Conn.resp(:found, "") + |> Plug.Conn.put_resp_header( + "location", + "#{instance}/#{path}" + ) + end +end diff --git a/lib/privacy_revolver/application.ex b/lib/privacy_revolver/application.ex deleted file mode 100644 index 04b7540..0000000 --- a/lib/privacy_revolver/application.ex +++ /dev/null @@ -1,17 +0,0 @@ -defmodule PrivacyRevolver.Application do - @redis_conn Application.fetch_env!(:privacy_revolver, :redis_conn) - @moduledoc false - - use Application - - @impl true - def start(_type, _args) do - children = [ - Plug.Cowboy.child_spec(scheme: :http, plug: PrivacyRevolver.Router, options: [port: 4001]), - {Redix, {@redis_conn, [name: :redix]}} - ] - - opts = [strategy: :one_for_one, name: PrivacyRevolver.Supervisor] - Supervisor.start_link(children, opts) - end -end diff --git a/lib/privacy_revolver/router.ex b/lib/privacy_revolver/router.ex deleted file mode 100644 index 2a51094..0000000 --- a/lib/privacy_revolver/router.ex +++ /dev/null @@ -1,51 +0,0 @@ -defmodule PrivacyRevolver.Router do - @fallback_str Application.fetch_env!(:privacy_revolver, :fallback_str) - - use Plug.Router - - plug(:match) - plug(:dispatch) - - get "/" do - send_resp(conn, 200, "") - end - - get "/ping" do - # Useful for app healthcheck - {:ok, resp} = Redix.command(:redix, ["PING"]) - send_resp(conn, 200, resp) - end - - get "/:service/*glob" do - path = Enum.join(glob, "/") - - {:ok, instances} = - Redix.command( - :redix, - ["LRANGE", service, "0", "-1"] - ) - - # Either pick a random available instance, - # or fall back to the default one - instance = - if Enum.count(instances) > 0 do - Enum.random(instances) - else - {:ok, result} = - Redix.command( - :redix, - ["GET", "#{service}#{@fallback_str}"] - ) - - result - end - - # Redirect to the available instance - conn - |> Plug.Conn.resp(:found, "") - |> Plug.Conn.put_resp_header( - "location", - "#{instance}/#{path}" - ) - end -end diff --git a/mix.exs b/mix.exs index 8adbae8..fbbbcc4 100644 --- a/mix.exs +++ b/mix.exs @@ -1,9 +1,9 @@ -defmodule PrivacyRevolver.MixProject do +defmodule Farside.MixProject do use Mix.Project def project do [ - app: :privacy_revolver, + app: :farside, version: "0.1.0", elixir: "~> 1.8", start_permanent: Mix.env() == :prod, @@ -15,7 +15,7 @@ defmodule PrivacyRevolver.MixProject do def application do [ extra_applications: [:logger], - mod: {PrivacyRevolver.Application, []} + mod: {Farside.Application, []} ] end diff --git a/test/farside_test.exs b/test/farside_test.exs new file mode 100644 index 0000000..0673fc4 --- /dev/null +++ b/test/farside_test.exs @@ -0,0 +1,18 @@ +defmodule FarsideTest do + use ExUnit.Case + use Plug.Test + + alias Farside.Router + + @opts Router.init([]) + + test "/" do + conn = + :get + |> conn("/", "") + |> Router.call(@opts) + + assert conn.state == :sent + assert conn.status == 200 + end +end diff --git a/test/privacy_revolver_test.exs b/test/privacy_revolver_test.exs deleted file mode 100644 index 8c85530..0000000 --- a/test/privacy_revolver_test.exs +++ /dev/null @@ -1,18 +0,0 @@ -defmodule PrivacyRevolverTest do - use ExUnit.Case - use Plug.Test - - alias PrivacyRevolver.Router - - @opts Router.init([]) - - test "/" do - conn = - :get - |> conn("/", "") - |> Router.call(@opts) - - assert conn.state == :sent - assert conn.status == 200 - end -end diff --git a/update.exs b/update.exs index 9ed320d..fe1445b 100644 --- a/update.exs +++ b/update.exs @@ -8,9 +8,9 @@ defmodule Service do end defmodule Instances do - @fallback_str Application.fetch_env!(:privacy_revolver, :fallback_str) - @update_file Application.fetch_env!(:privacy_revolver, :update_file) - @services_json Application.fetch_env!(:privacy_revolver, :services_json) + @fallback_str Application.fetch_env!(:farside, :fallback_str) + @update_file Application.fetch_env!(:farside, :update_file) + @services_json Application.fetch_env!(:farside, :services_json) def init() do File.rename(@update_file, "#{@update_file}-prev") @@ -33,7 +33,9 @@ defmodule Instances do # Loop through all instances and check each for availability for service <- json do + IO.puts "======== " <> service.type result = Enum.filter(service.instances, fn(instance_url) -> + IO.puts " " <> instance_url request(instance_url <> service.test_url) == :good end) @@ -43,6 +45,8 @@ defmodule Instances do end def add_to_redis(service, instances) do + IO.puts " --------" + IO.inspect "OK: " <> instances # Remove previous list of instances Redix.command(:redix, [ "DEL", -- cgit v1.2.3