|
|
@ -2,16 +2,17 @@
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
|
|
|
|
defmodule Pleroma.Gun.API.Mock do
|
|
|
|
defmodule Pleroma.GunMock do
|
|
|
|
@behaviour Pleroma.Gun.API
|
|
|
|
@behaviour Pleroma.Gun
|
|
|
|
|
|
|
|
|
|
|
|
alias Pleroma.Gun.API
|
|
|
|
alias Pleroma.Gun
|
|
|
|
|
|
|
|
alias Pleroma.GunMock
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open('some-domain.com', 443, _) do
|
|
|
|
def open('some-domain.com', 443, _) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
|
|
|
|
|
|
|
Registry.register(API.Mock, conn_pid, %{
|
|
|
|
Registry.register(GunMock, conn_pid, %{
|
|
|
|
origin_scheme: "https",
|
|
|
|
origin_scheme: "https",
|
|
|
|
origin_host: 'some-domain.com',
|
|
|
|
origin_host: 'some-domain.com',
|
|
|
|
origin_port: 443
|
|
|
|
origin_port: 443
|
|
|
@ -20,7 +21,7 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
{:ok, conn_pid}
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open(ip, port, _)
|
|
|
|
def open(ip, port, _)
|
|
|
|
when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and
|
|
|
|
when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and
|
|
|
|
port in [80, 443] do
|
|
|
|
port in [80, 443] do
|
|
|
@ -28,7 +29,7 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
|
|
|
|
|
|
|
|
scheme = if port == 443, do: "https", else: "http"
|
|
|
|
scheme = if port == 443, do: "https", else: "http"
|
|
|
|
|
|
|
|
|
|
|
|
Registry.register(API.Mock, conn_pid, %{
|
|
|
|
Registry.register(GunMock, conn_pid, %{
|
|
|
|
origin_scheme: scheme,
|
|
|
|
origin_scheme: scheme,
|
|
|
|
origin_host: ip,
|
|
|
|
origin_host: ip,
|
|
|
|
origin_port: port
|
|
|
|
origin_port: port
|
|
|
@ -37,7 +38,7 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
{:ok, conn_pid}
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open('localhost', 1234, %{
|
|
|
|
def open('localhost', 1234, %{
|
|
|
|
protocols: [:socks],
|
|
|
|
protocols: [:socks],
|
|
|
|
proxy: {:socks5, 'localhost', 1234},
|
|
|
|
proxy: {:socks5, 'localhost', 1234},
|
|
|
@ -45,7 +46,7 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
}) do
|
|
|
|
}) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
|
|
|
|
|
|
|
Registry.register(API.Mock, conn_pid, %{
|
|
|
|
Registry.register(GunMock, conn_pid, %{
|
|
|
|
origin_scheme: "http",
|
|
|
|
origin_scheme: "http",
|
|
|
|
origin_host: 'proxy-socks.com',
|
|
|
|
origin_host: 'proxy-socks.com',
|
|
|
|
origin_port: 80
|
|
|
|
origin_port: 80
|
|
|
@ -54,7 +55,7 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
{:ok, conn_pid}
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open('localhost', 1234, %{
|
|
|
|
def open('localhost', 1234, %{
|
|
|
|
protocols: [:socks],
|
|
|
|
protocols: [:socks],
|
|
|
|
proxy: {:socks4, 'localhost', 1234},
|
|
|
|
proxy: {:socks4, 'localhost', 1234},
|
|
|
@ -69,7 +70,7 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
}) do
|
|
|
|
}) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
|
|
|
|
|
|
|
Registry.register(API.Mock, conn_pid, %{
|
|
|
|
Registry.register(GunMock, conn_pid, %{
|
|
|
|
origin_scheme: "https",
|
|
|
|
origin_scheme: "https",
|
|
|
|
origin_host: 'proxy-socks.com',
|
|
|
|
origin_host: 'proxy-socks.com',
|
|
|
|
origin_port: 443
|
|
|
|
origin_port: 443
|
|
|
@ -78,14 +79,14 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
{:ok, conn_pid}
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
|
|
|
|
def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open('example.com', port, _) when port in [443, 115] do
|
|
|
|
def open('example.com', port, _) when port in [443, 115] do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
|
|
|
|
|
|
|
Registry.register(API.Mock, conn_pid, %{
|
|
|
|
Registry.register(GunMock, conn_pid, %{
|
|
|
|
origin_scheme: "https",
|
|
|
|
origin_scheme: "https",
|
|
|
|
origin_host: 'example.com',
|
|
|
|
origin_host: 'example.com',
|
|
|
|
origin_port: 443
|
|
|
|
origin_port: 443
|
|
|
@ -94,11 +95,11 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
{:ok, conn_pid}
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open(domain, 80, _) do
|
|
|
|
def open(domain, 80, _) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
|
|
|
|
|
|
|
Registry.register(API.Mock, conn_pid, %{
|
|
|
|
Registry.register(GunMock, conn_pid, %{
|
|
|
|
origin_scheme: "http",
|
|
|
|
origin_scheme: "http",
|
|
|
|
origin_host: domain,
|
|
|
|
origin_host: domain,
|
|
|
|
origin_port: 80
|
|
|
|
origin_port: 80
|
|
|
@ -107,48 +108,48 @@ defmodule Pleroma.Gun.API.Mock do
|
|
|
|
{:ok, conn_pid}
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open({127, 0, 0, 1}, 8123, _) do
|
|
|
|
def open({127, 0, 0, 1}, 8123, _) do
|
|
|
|
Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def open('localhost', 9050, _) do
|
|
|
|
def open('localhost', 9050, _) do
|
|
|
|
Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def await_up(_pid, _timeout), do: {:ok, :http}
|
|
|
|
def await_up(_pid, _timeout), do: {:ok, :http}
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def set_owner(_pid, _owner), do: :ok
|
|
|
|
def set_owner(_pid, _owner), do: :ok
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def connect(pid, %{host: _, port: 80}) do
|
|
|
|
def connect(pid, %{host: _, port: 80}) do
|
|
|
|
ref = make_ref()
|
|
|
|
ref = make_ref()
|
|
|
|
Registry.register(API.Mock, ref, pid)
|
|
|
|
Registry.register(GunMock, ref, pid)
|
|
|
|
ref
|
|
|
|
ref
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
|
|
|
|
def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
|
|
|
|
ref = make_ref()
|
|
|
|
ref = make_ref()
|
|
|
|
Registry.register(API.Mock, ref, pid)
|
|
|
|
Registry.register(GunMock, ref, pid)
|
|
|
|
ref
|
|
|
|
ref
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def await(pid, ref) do
|
|
|
|
def await(pid, ref) do
|
|
|
|
[{_, ^pid}] = Registry.lookup(API.Mock, ref)
|
|
|
|
[{_, ^pid}] = Registry.lookup(GunMock, ref)
|
|
|
|
{:response, :fin, 200, []}
|
|
|
|
{:response, :fin, 200, []}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def info(pid) do
|
|
|
|
def info(pid) do
|
|
|
|
[{_, info}] = Registry.lookup(API.Mock, pid)
|
|
|
|
[{_, info}] = Registry.lookup(GunMock, pid)
|
|
|
|
info
|
|
|
|
info
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@impl API
|
|
|
|
@impl Gun
|
|
|
|
def close(_pid), do: :ok
|
|
|
|
def close(_pid), do: :ok
|
|
|
|
end
|
|
|
|
end
|