logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

gun_mock.ex (3557B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.GunMock do
      6   @behaviour Pleroma.Gun
      7 
      8   alias Pleroma.Gun
      9   alias Pleroma.GunMock
     10 
     11   @impl Gun
     12   def open('some-domain.com', 443, _) do
     13     {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
     14 
     15     Registry.register(GunMock, conn_pid, %{
     16       origin_scheme: "https",
     17       origin_host: 'some-domain.com',
     18       origin_port: 443
     19     })
     20 
     21     {:ok, conn_pid}
     22   end
     23 
     24   @impl Gun
     25   def open(ip, port, _)
     26       when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and
     27              port in [80, 443] do
     28     {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
     29 
     30     scheme = if port == 443, do: "https", else: "http"
     31 
     32     Registry.register(GunMock, conn_pid, %{
     33       origin_scheme: scheme,
     34       origin_host: ip,
     35       origin_port: port
     36     })
     37 
     38     {:ok, conn_pid}
     39   end
     40 
     41   @impl Gun
     42   def open('localhost', 1234, %{
     43         protocols: [:socks],
     44         proxy: {:socks5, 'localhost', 1234},
     45         socks_opts: %{host: 'proxy-socks.com', port: 80, version: 5}
     46       }) do
     47     {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
     48 
     49     Registry.register(GunMock, conn_pid, %{
     50       origin_scheme: "http",
     51       origin_host: 'proxy-socks.com',
     52       origin_port: 80
     53     })
     54 
     55     {:ok, conn_pid}
     56   end
     57 
     58   @impl Gun
     59   def open('localhost', 1234, %{
     60         protocols: [:socks],
     61         proxy: {:socks4, 'localhost', 1234},
     62         socks_opts: %{
     63           host: 'proxy-socks.com',
     64           port: 443,
     65           protocols: [:http2],
     66           tls_opts: [],
     67           transport: :tls,
     68           version: 4
     69         }
     70       }) do
     71     {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
     72 
     73     Registry.register(GunMock, conn_pid, %{
     74       origin_scheme: "https",
     75       origin_host: 'proxy-socks.com',
     76       origin_port: 443
     77     })
     78 
     79     {:ok, conn_pid}
     80   end
     81 
     82   @impl Gun
     83   def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
     84 
     85   @impl Gun
     86   def open('example.com', port, _) when port in [443, 115] do
     87     {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
     88 
     89     Registry.register(GunMock, conn_pid, %{
     90       origin_scheme: "https",
     91       origin_host: 'example.com',
     92       origin_port: 443
     93     })
     94 
     95     {:ok, conn_pid}
     96   end
     97 
     98   @impl Gun
     99   def open(domain, 80, _) do
    100     {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
    101 
    102     Registry.register(GunMock, conn_pid, %{
    103       origin_scheme: "http",
    104       origin_host: domain,
    105       origin_port: 80
    106     })
    107 
    108     {:ok, conn_pid}
    109   end
    110 
    111   @impl Gun
    112   def open({127, 0, 0, 1}, 8123, _) do
    113     Task.start_link(fn -> Process.sleep(1_000) end)
    114   end
    115 
    116   @impl Gun
    117   def open('localhost', 9050, _) do
    118     Task.start_link(fn -> Process.sleep(1_000) end)
    119   end
    120 
    121   @impl Gun
    122   def await_up(_pid, _timeout), do: {:ok, :http}
    123 
    124   @impl Gun
    125   def set_owner(_pid, _owner), do: :ok
    126 
    127   @impl Gun
    128   def connect(pid, %{host: _, port: 80}) do
    129     ref = make_ref()
    130     Registry.register(GunMock, ref, pid)
    131     ref
    132   end
    133 
    134   @impl Gun
    135   def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
    136     ref = make_ref()
    137     Registry.register(GunMock, ref, pid)
    138     ref
    139   end
    140 
    141   @impl Gun
    142   def await(pid, ref) do
    143     [{_, ^pid}] = Registry.lookup(GunMock, ref)
    144     {:response, :fin, 200, []}
    145   end
    146 
    147   @impl Gun
    148   def info(pid) do
    149     [{_, info}] = Registry.lookup(GunMock, pid)
    150     info
    151   end
    152 
    153   @impl Gun
    154   def close(_pid), do: :ok
    155 end