Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Jun 2, 2024
2 parents 214c16b + 5729cac commit f404e08
Show file tree
Hide file tree
Showing 31 changed files with 333 additions and 140 deletions.
10 changes: 10 additions & 0 deletions assets/static/opensearch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Derpibooru</ShortName>
<Description>Derpibooru image search</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16" type="image/x-icon">https://derpibooru.org/favicon.ico</Image>
<Image width="64" height="64" type="image/svg+xml">https://derpibooru.org/favicon.svg</Image>
<Url type="text/html" method="get" template="https://derpibooru.org/search">
<Param name="q" value="{searchTerms}"/>
</Url>
</OpenSearchDescription>
1 change: 0 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ config :logger, :console,

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
config :bamboo, :json_library, Jason

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
Expand Down
22 changes: 9 additions & 13 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Config
# by calling `mix release`.
#
# See `mix help release` for more information.
{:ok, _} = Application.ensure_all_started(:tls_certificate_check)

config :bcrypt_elixir,
log_rounds: String.to_integer(System.get_env("BCRYPT_ROUNDS", "12"))
Expand Down Expand Up @@ -118,17 +117,14 @@ end
if config_env() == :prod do
# Production mailer config
config :philomena, Philomena.Mailer,
adapter: Bamboo.SMTPAdapter,
server: System.fetch_env!("SMTP_RELAY"),
hostname: System.fetch_env!("SMTP_DOMAIN"),
port: System.get_env("SMTP_PORT") || 587,
username: System.fetch_env!("SMTP_USERNAME"),
password: System.fetch_env!("SMTP_PASSWORD"),
tls: :always,
auth: :always,
tls_options:
[middlebox_comp_mode: false] ++
:tls_certificate_check.options(System.fetch_env!("SMTP_RELAY"))
adapter: Swoosh.Adapters.Mua,
relay: System.fetch_env!("SMTP_RELAY"),
port: String.to_integer(System.get_env("SMTP_PORT", "587")),
auth: [
username: System.fetch_env!("SMTP_USERNAME"),
password: System.fetch_env!("SMTP_PASSWORD")
],
ssl: [middlebox_comp_mode: false]

# Production endpoint config
{:ok, ip} = :inet.parse_address(System.get_env("APP_IP", "127.0.0.1") |> String.to_charlist())
Expand All @@ -140,7 +136,7 @@ if config_env() == :prod do
server: not is_nil(System.get_env("START_ENDPOINT"))
else
# Don't send email in development
config :philomena, Philomena.Mailer, adapter: Bamboo.LocalAdapter
config :philomena, Philomena.Mailer, adapter: Swoosh.Adapters.Local

# Use this to debug slime templates
# config :slime, :keep_lines, true
Expand Down
8 changes: 0 additions & 8 deletions lib/camo/image.ex

This file was deleted.

3 changes: 3 additions & 0 deletions lib/philomena/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ defmodule Philomena.Application do
# Background queueing system
Philomena.ExqSupervisor,

# Mailer
{Task.Supervisor, name: Philomena.AsyncEmailSupervisor},

# Starts a worker by calling: Philomena.Worker.start_link(arg)
# {Philomena.Worker, arg},
{Redix, name: :redix, host: Application.get_env(:philomena, :redis_host)},
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena/artist_links/automatic_verifier.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Philomena.ArtistLinks.AutomaticVerifier do
def check_link(artist_link, recheck_time) do
artist_link.uri
|> Philomena.Http.get()
|> PhilomenaProxy.Http.get()
|> contains_verification_code?(artist_link.verification_code)
|> case do
true ->
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena/channels/picarto_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Philomena.Channels.PicartoChannel do
@spec live_channels(DateTime.t()) :: map()
def live_channels(now) do
@api_online
|> Philomena.Http.get()
|> PhilomenaProxy.Http.get()
|> case do
{:ok, %Tesla.Env{body: body, status: 200}} ->
body
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena/channels/piczel_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Philomena.Channels.PiczelChannel do
@spec live_channels(DateTime.t()) :: map()
def live_channels(now) do
@api_online
|> Philomena.Http.get()
|> PhilomenaProxy.Http.get()
|> case do
{:ok, %Tesla.Env{body: body, status: 200}} ->
body
Expand Down
46 changes: 0 additions & 46 deletions lib/philomena/http.ex

This file was deleted.

8 changes: 7 additions & 1 deletion lib/philomena/mailer.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
defmodule Philomena.Mailer do
use Bamboo.Mailer, otp_app: :philomena
use Swoosh.Mailer, otp_app: :philomena

@spec deliver_later(Swoosh.Email.t()) :: {:ok, Swoosh.Email.t()}
def deliver_later(mail) do
Task.Supervisor.start_child(Philomena.AsyncEmailSupervisor, fn -> deliver(mail) end)
{:ok, mail}
end
end
25 changes: 0 additions & 25 deletions lib/philomena/scrapers.ex

This file was deleted.

4 changes: 2 additions & 2 deletions lib/philomena/users/user_notifier.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Philomena.Users.UserNotifier do
alias Bamboo.Email
alias Swoosh.Email
alias Philomena.Mailer

defp deliver(to, subject, body) do
Email.new_email(
Email.new(
to: to,
from: mailer_address(),
subject: subject,
Expand Down
24 changes: 24 additions & 0 deletions lib/philomena_proxy/camo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule PhilomenaProxy.Camo do
@moduledoc """
Image proxying utilities.
"""

@doc """
Convert a potentially untrusted external image URL into a trusted one
loaded through a gocamo proxy (specified by the environment).
Configuration is read from environment variables at runtime by Philomena.
config :philomena,
camo_host: System.get_env("CAMO_HOST"),
camo_key: System.get_env("CAMO_KEY"),
## Example
iex> PhilomenaProxy.Camo.image_url("https://example.org/img/view/2024/1/1/1.png")
"https://example.net/L5MqSmYq1ZEqiBGGvsvSDpILyJI/aHR0cHM6Ly9leGFtcGxlLm9yZy9pbWcvdmlldy8yMDI0LzEvMS8xLnBuZwo"
"""
@spec image_url(String.t()) :: String.t()
def image_url(input), do: Philomena.Native.camo_image_url(input)
end
107 changes: 107 additions & 0 deletions lib/philomena_proxy/http.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
defmodule PhilomenaProxy.Http do
@moduledoc """
HTTP client implementation.
This applies the Philomena User-Agent header, and optionally proxies traffic through a SOCKS5
HTTP proxy to allow the application to connect when the local network is restricted.
If a proxy host is not specified in the configuration, then a proxy is not used and external
traffic is originated from the same network as application.
Proxy options are read from environment variables at runtime by Philomena.
config :philomena,
proxy_host: System.get_env("PROXY_HOST"),
"""

@type url :: String.t()
@type header_list :: [{String.t(), String.t()}]
@type body :: binary()

@type client_options :: keyword()

@doc ~S"""
Perform a HTTP GET request.
## Example
iex> PhilomenaProxy.Http.get("http://example.com", [{"authorization", "Bearer #{token}"}])
{:ok, %Tesla.Env{...}}
iex> PhilomenaProxy.Http.get("http://nonexistent.example.com")
{:error, %Mint.TransportError{reason: :nxdomain}}
"""
@spec get(url(), header_list(), client_options()) :: Tesla.Env.result()
def get(url, headers \\ [], options \\ []) do
Tesla.get(client(headers), url, opts: [adapter: adapter_opts(options)])
end

@doc ~S"""
Perform a HTTP HEAD request.
## Example
iex> PhilomenaProxy.Http.head("http://example.com", [{"authorization", "Bearer #{token}"}])
{:ok, %Tesla.Env{...}}
iex> PhilomenaProxy.Http.head("http://nonexistent.example.com")
{:error, %Mint.TransportError{reason: :nxdomain}}
"""
@spec head(url(), header_list(), client_options()) :: Tesla.Env.result()
def head(url, headers \\ [], options \\ []) do
Tesla.head(client(headers), url, opts: [adapter: adapter_opts(options)])
end

@doc ~S"""
Perform a HTTP POST request.
## Example
iex> PhilomenaProxy.Http.post("http://example.com", "", [{"authorization", "Bearer #{token}"}])
{:ok, %Tesla.Env{...}}
iex> PhilomenaProxy.Http.post("http://nonexistent.example.com", "")
{:error, %Mint.TransportError{reason: :nxdomain}}
"""
@spec post(url(), body(), header_list(), client_options()) :: Tesla.Env.result()
def post(url, body, headers \\ [], options \\ []) do
Tesla.post(client(headers), url, body, opts: [adapter: adapter_opts(options)])
end

defp adapter_opts(opts) do
opts = Keyword.merge(opts, max_body: 125_000_000, inet6: true)

case Application.get_env(:philomena, :proxy_host) do
nil ->
opts

url ->
Keyword.merge(opts, proxy: proxy_opts(URI.parse(url)))
end
end

defp proxy_opts(%{host: host, port: port, scheme: "https"}),
do: {:https, host, port, [transport_opts: [inet6: true]]}

defp proxy_opts(%{host: host, port: port, scheme: "http"}),
do: {:http, host, port, [transport_opts: [inet6: true]]}

defp client(headers) do
Tesla.client(
[
{Tesla.Middleware.FollowRedirects, max_redirects: 1},
{Tesla.Middleware.Headers,
[
{"User-Agent",
"Mozilla/5.0 (X11; Philomena; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0"}
| headers
]}
],
Tesla.Adapter.Mint
)
end
end
Loading

0 comments on commit f404e08

Please sign in to comment.