2020-02-11 02:12:57 -05:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 18:16:24 -05:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2020-02-11 02:12:57 -05:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
defmodule Pleroma.GunMock do
|
|
|
|
@behaviour Pleroma.Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
alias Pleroma.Gun
|
|
|
|
alias Pleroma.GunMock
|
2020-02-11 02:12:57 -05:00
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open('some-domain.com', 443, _) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, conn_pid, %{
|
2020-02-11 02:12:57 -05:00
|
|
|
origin_scheme: "https",
|
|
|
|
origin_host: 'some-domain.com',
|
|
|
|
origin_port: 443
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
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
|
|
|
|
port in [80, 443] do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
|
|
|
scheme = if port == 443, do: "https", else: "http"
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, conn_pid, %{
|
2020-02-11 02:12:57 -05:00
|
|
|
origin_scheme: scheme,
|
|
|
|
origin_host: ip,
|
|
|
|
origin_port: port
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open('localhost', 1234, %{
|
|
|
|
protocols: [:socks],
|
|
|
|
proxy: {:socks5, 'localhost', 1234},
|
|
|
|
socks_opts: %{host: 'proxy-socks.com', port: 80, version: 5}
|
|
|
|
}) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, conn_pid, %{
|
2020-02-11 02:12:57 -05:00
|
|
|
origin_scheme: "http",
|
|
|
|
origin_host: 'proxy-socks.com',
|
|
|
|
origin_port: 80
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open('localhost', 1234, %{
|
|
|
|
protocols: [:socks],
|
|
|
|
proxy: {:socks4, 'localhost', 1234},
|
|
|
|
socks_opts: %{
|
|
|
|
host: 'proxy-socks.com',
|
|
|
|
port: 443,
|
|
|
|
protocols: [:http2],
|
|
|
|
tls_opts: [],
|
|
|
|
transport: :tls,
|
|
|
|
version: 4
|
|
|
|
}
|
|
|
|
}) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, conn_pid, %{
|
2020-02-11 02:12:57 -05:00
|
|
|
origin_scheme: "https",
|
|
|
|
origin_host: 'proxy-socks.com',
|
|
|
|
origin_port: 443
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open('example.com', port, _) when port in [443, 115] do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, conn_pid, %{
|
2020-02-11 02:12:57 -05:00
|
|
|
origin_scheme: "https",
|
|
|
|
origin_host: 'example.com',
|
|
|
|
origin_port: 443
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open(domain, 80, _) do
|
|
|
|
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, conn_pid, %{
|
2020-02-11 02:12:57 -05:00
|
|
|
origin_scheme: "http",
|
|
|
|
origin_host: domain,
|
|
|
|
origin_port: 80
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, conn_pid}
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open({127, 0, 0, 1}, 8123, _) do
|
|
|
|
Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def open('localhost', 9050, _) do
|
|
|
|
Task.start_link(fn -> Process.sleep(1_000) end)
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-24 11:56:27 -05:00
|
|
|
def await_up(_pid, _timeout), do: {:ok, :http}
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-24 11:56:27 -05:00
|
|
|
def set_owner(_pid, _owner), do: :ok
|
2020-02-11 02:12:57 -05:00
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def connect(pid, %{host: _, port: 80}) do
|
|
|
|
ref = make_ref()
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, ref, pid)
|
2020-02-11 02:12:57 -05:00
|
|
|
ref
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
|
|
|
|
ref = make_ref()
|
2020-03-03 11:24:14 -05:00
|
|
|
Registry.register(GunMock, ref, pid)
|
2020-02-11 02:12:57 -05:00
|
|
|
ref
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def await(pid, ref) do
|
2020-03-03 11:24:14 -05:00
|
|
|
[{_, ^pid}] = Registry.lookup(GunMock, ref)
|
2020-02-11 02:12:57 -05:00
|
|
|
{:response, :fin, 200, []}
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def info(pid) do
|
2020-03-03 11:24:14 -05:00
|
|
|
[{_, info}] = Registry.lookup(GunMock, pid)
|
2020-02-11 02:12:57 -05:00
|
|
|
info
|
|
|
|
end
|
|
|
|
|
2020-03-03 11:24:14 -05:00
|
|
|
@impl Gun
|
2020-02-11 02:12:57 -05:00
|
|
|
def close(_pid), do: :ok
|
|
|
|
end
|