Add native captcha and enable it by default.
[akkoma] / lib / mix / tasks / pleroma / instance.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Instance do
6 use Mix.Task
7 import Mix.Pleroma
8
9 @shortdoc "Manages Pleroma instance"
10 @moduledoc """
11 Manages Pleroma instance.
12
13 ## Generate a new instance config.
14
15 mix pleroma.instance gen [OPTION...]
16
17 If any options are left unspecified, you will be prompted interactively
18
19 ## Options
20
21 - `-f`, `--force` - overwrite any output files
22 - `-o PATH`, `--output PATH` - the output file for the generated configuration
23 - `--output-psql PATH` - the output file for the generated PostgreSQL setup
24 - `--domain DOMAIN` - the domain of your instance
25 - `--instance-name INSTANCE_NAME` - the name of your instance
26 - `--admin-email ADMIN_EMAIL` - the email address of the instance admin
27 - `--notify-email NOTIFY_EMAIL` - email address for notifications
28 - `--dbhost HOSTNAME` - the hostname of the PostgreSQL database to use
29 - `--dbname DBNAME` - the name of the database to use
30 - `--dbuser DBUSER` - the user (aka role) to use for the database connection
31 - `--dbpass DBPASS` - the password to use for the database connection
32 - `--rum Y/N` - Whether to enable RUM indexes
33 - `--indexable Y/N` - Allow/disallow indexing site by search engines
34 - `--uploads-dir` - the directory uploads go in when using a local uploader
35 - `--static-dir` - the directory custom public files should be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)
36 - `--listen-ip` - the ip the app should listen to, defaults to 127.0.0.1
37 - `--listen-port` - the port the app should listen to, defaults to 4000
38 """
39
40 def run(["gen" | rest]) do
41 {options, [], []} =
42 OptionParser.parse(
43 rest,
44 strict: [
45 force: :boolean,
46 output: :string,
47 output_psql: :string,
48 domain: :string,
49 instance_name: :string,
50 admin_email: :string,
51 notify_email: :string,
52 dbhost: :string,
53 dbname: :string,
54 dbuser: :string,
55 dbpass: :string,
56 rum: :string,
57 indexable: :string,
58 uploads_dir: :string,
59 static_dir: :string,
60 listen_ip: :string,
61 listen_port: :string
62 ],
63 aliases: [
64 o: :output,
65 f: :force
66 ]
67 )
68
69 paths =
70 [config_path, psql_path] = [
71 Keyword.get(options, :output, "config/generated_config.exs"),
72 Keyword.get(options, :output_psql, "config/setup_db.psql")
73 ]
74
75 will_overwrite = Enum.filter(paths, &File.exists?/1)
76 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
77
78 if proceed? do
79 [domain, port | _] =
80 String.split(
81 get_option(
82 options,
83 :domain,
84 "What domain will your instance use? (e.g pleroma.soykaf.com)"
85 ),
86 ":"
87 ) ++ [443]
88
89 name =
90 get_option(
91 options,
92 :instance_name,
93 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
94 )
95
96 email = get_option(options, :admin_email, "What is your admin email address?")
97
98 notify_email =
99 get_option(
100 options,
101 :notify_email,
102 "What email address do you want to use for sending email notifications?",
103 email
104 )
105
106 indexable =
107 get_option(
108 options,
109 :indexable,
110 "Do you want search engines to index your site? (y/n)",
111 "y"
112 ) === "y"
113
114 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
115
116 dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma")
117
118 dbuser =
119 get_option(
120 options,
121 :dbuser,
122 "What is the user used to connect to your database?",
123 "pleroma"
124 )
125
126 dbpass =
127 get_option(
128 options,
129 :dbpass,
130 "What is the password used to connect to your database?",
131 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
132 "autogenerated"
133 )
134
135 rum_enabled =
136 get_option(
137 options,
138 :rum,
139 "Would you like to use RUM indices?",
140 "n"
141 ) === "y"
142
143 listen_port =
144 get_option(
145 options,
146 :listen_port,
147 "What port will the app listen to (leave it if you are using the default setup with nginx)?",
148 4000
149 )
150
151 listen_ip =
152 get_option(
153 options,
154 :listen_ip,
155 "What ip will the app listen to (leave it if you are using the default setup with nginx)?",
156 "127.0.0.1"
157 )
158
159 uploads_dir =
160 get_option(
161 options,
162 :uploads_dir,
163 "What directory should media uploads go in (when using the local uploader)?",
164 Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads])
165 )
166
167 static_dir =
168 get_option(
169 options,
170 :static_dir,
171 "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
172 Pleroma.Config.get([:instance, :static_dir])
173 )
174
175 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
176 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
177 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
178 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
179 template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
180
181 result_config =
182 EEx.eval_file(
183 template_dir <> "/sample_config.eex",
184 domain: domain,
185 port: port,
186 email: email,
187 notify_email: notify_email,
188 name: name,
189 dbhost: dbhost,
190 dbname: dbname,
191 dbuser: dbuser,
192 dbpass: dbpass,
193 secret: secret,
194 jwt_secret: jwt_secret,
195 signing_salt: signing_salt,
196 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
197 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
198 db_configurable?: false,
199 static_dir: static_dir,
200 uploads_dir: uploads_dir,
201 rum_enabled: rum_enabled,
202 listen_ip: listen_ip,
203 listen_port: listen_port
204 )
205
206 result_psql =
207 EEx.eval_file(
208 template_dir <> "/sample_psql.eex",
209 dbname: dbname,
210 dbuser: dbuser,
211 dbpass: dbpass,
212 rum_enabled: rum_enabled
213 )
214
215 shell_info("Writing config to #{config_path}.")
216
217 File.write(config_path, result_config)
218 shell_info("Writing the postgres script to #{psql_path}.")
219 File.write(psql_path, result_psql)
220
221 write_robots_txt(indexable, template_dir)
222
223 shell_info(
224 "\n All files successfully written! Refer to the installation instructions for your platform for next steps"
225 )
226 else
227 shell_error(
228 "The task would have overwritten the following files:\n" <>
229 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
230 "Rerun with `--force` to overwrite them."
231 )
232 end
233 end
234
235 defp write_robots_txt(indexable, template_dir) do
236 robots_txt =
237 EEx.eval_file(
238 template_dir <> "/robots_txt.eex",
239 indexable: indexable
240 )
241
242 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
243
244 unless File.exists?(static_dir) do
245 File.mkdir_p!(static_dir)
246 end
247
248 robots_txt_path = Path.join(static_dir, "robots.txt")
249
250 if File.exists?(robots_txt_path) do
251 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
252 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
253 end
254
255 File.write(robots_txt_path, robots_txt)
256 shell_info("Writing #{robots_txt_path}.")
257 end
258 end