]> Untitled Git - lemmy.git/commitdiff
Remove hardcoded usage of https (fixes #1126)
authorFelix Ableitner <me@nutomic.com>
Fri, 25 Sep 2020 15:33:00 +0000 (17:33 +0200)
committerFelix Ableitner <me@nutomic.com>
Fri, 25 Sep 2020 15:33:00 +0000 (17:33 +0200)
17 files changed:
Cargo.lock
config/defaults.hjson
docker/federation/docker-compose.yml
lemmy_api/src/user.rs
lemmy_apub/src/activities.rs
lemmy_apub/src/community.rs
lemmy_apub/src/fetcher.rs
lemmy_apub/src/lib.rs
lemmy_db/Cargo.toml
lemmy_db/src/user.rs
lemmy_structs/src/lib.rs
lemmy_utils/src/apub.rs
lemmy_utils/src/request.rs
lemmy_utils/src/settings.rs
src/code_migrations.rs
src/routes/feeds.rs
src/routes/nodeinfo.rs

index 7c709ffa6288af3891b64ed811f2cfaea0dabde9..65f718b3e29ce4a70f54c41239ce3f680e4f5f7e 100644 (file)
@@ -1896,6 +1896,7 @@ dependencies = [
  "chrono",
  "diesel",
  "lazy_static",
+ "lemmy_utils",
  "log",
  "regex",
  "serde 1.0.116",
index 1da2a16cf66541d5e59533645d747169020162b5..62fa98a6d9d41842acb180d23667062cf607af77 100644 (file)
@@ -31,6 +31,8 @@
   bind: "0.0.0.0"
   # port where lemmy should listen for incoming requests
   port: 8536
+  # whether tls is required for activitypub. only disable this for debugging, never for producion.
+  tls_enabled: true
   # json web token for authorization between server and client
   jwt_secret: "changeme"
   # address where pictrs is available
@@ -58,8 +60,6 @@
   federation: {
     # whether to enable activitypub federation. this feature is in alpha, do not enable in production.
     enabled: false
-    # whether tls is required for activitypub. only disable this for debugging, never for producion.
-    tls_enabled: true
     # comma separated list of instances with which federation is allowed
     allowed_instances: ""
     # comma separated list of instances which are blocked from federating
index b4181e5d2d3a1a0d4424444be081f535ac9a3d57..8f01eadf7f2e5e6f95e1073ee48247d11af38cb6 100644 (file)
@@ -43,7 +43,7 @@ services:
       - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_alpha:5432/lemmy
       - LEMMY_JWT_SECRET=changeme
       - LEMMY_FEDERATION__ENABLED=true
-      - LEMMY_FEDERATION__TLS_ENABLED=false
+      - LEMMY_TLS_ENABLED=false
       - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon
       - LEMMY_PORT=8541
       - LEMMY_SETUP__ADMIN_USERNAME=lemmy_alpha
@@ -82,7 +82,7 @@ services:
       - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_beta:5432/lemmy
       - LEMMY_JWT_SECRET=changeme
       - LEMMY_FEDERATION__ENABLED=true
-      - LEMMY_FEDERATION__TLS_ENABLED=false
+      - LEMMY_TLS_ENABLED=false
       - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon
       - LEMMY_PORT=8551
       - LEMMY_SETUP__ADMIN_USERNAME=lemmy_beta
@@ -121,7 +121,7 @@ services:
       - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_gamma:5432/lemmy
       - LEMMY_JWT_SECRET=changeme
       - LEMMY_FEDERATION__ENABLED=true
-      - LEMMY_FEDERATION__TLS_ENABLED=false
+      - LEMMY_TLS_ENABLED=false
       - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon
       - LEMMY_PORT=8561
       - LEMMY_SETUP__ADMIN_USERNAME=lemmy_gamma
@@ -161,7 +161,7 @@ services:
       - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_delta:5432/lemmy
       - LEMMY_JWT_SECRET=changeme
       - LEMMY_FEDERATION__ENABLED=true
-      - LEMMY_FEDERATION__TLS_ENABLED=false
+      - LEMMY_TLS_ENABLED=false
       - LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta
       - LEMMY_PORT=8571
       - LEMMY_SETUP__ADMIN_USERNAME=lemmy_delta
@@ -201,7 +201,7 @@ services:
       - LEMMY_DATABASE_URL=postgres://lemmy:password@postgres_epsilon:5432/lemmy
       - LEMMY_JWT_SECRET=changeme
       - LEMMY_FEDERATION__ENABLED=true
-      - LEMMY_FEDERATION__TLS_ENABLED=false
+      - LEMMY_TLS_ENABLED=false
       - LEMMY_FEDERATION__BLOCKED_INSTANCES=lemmy-alpha
       - LEMMY_PORT=8581
       - LEMMY_SETUP__ADMIN_USERNAME=lemmy_epsilon
index e2b73c53ed327ba9f8e48c72675dcfef1b26ea3e..4e6269268725a89f2a7de666309078992c281a5a 100644 (file)
@@ -913,7 +913,7 @@ impl Perform for PasswordReset {
     // TODO no i18n support here.
     let user_email = &user.email.expect("email");
     let subject = &format!("Password reset for {}", user.name);
-    let hostname = &format!("https://{}", Settings::get().hostname); //TODO add https for now.
+    let hostname = &Settings::get().get_protocol_and_hostname();
     let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", user.name, hostname, &token);
     match send_email(subject, user_email, &user.name, html) {
       Ok(_o) => _o,
@@ -977,8 +977,6 @@ impl Perform for CreatePrivateMessage {
     let data: &CreatePrivateMessage = &self;
     let user = get_user_from_jwt(&data.auth, context.pool()).await?;
 
-    let hostname = &format!("https://{}", Settings::get().hostname);
-
     let content_slurs_removed = remove_slurs(&data.content.to_owned());
 
     let private_message_form = PrivateMessageForm {
@@ -1034,7 +1032,9 @@ impl Perform for CreatePrivateMessage {
         );
         let html = &format!(
           "<h1>Private Message</h1><br><div>{} - {}</div><br><a href={}/inbox>inbox</a>",
-          user.name, &content_slurs_removed, hostname
+          user.name,
+          &content_slurs_removed,
+          Settings::get().get_protocol_and_hostname()
         );
         match send_email(subject, &email, &recipient_user.name, html) {
           Ok(_o) => _o,
index 3b1b12ab3592c6c36cb47b5ea72ea76727359085..18781ef42c3773dba30c284a9b864d8c6d53e8ed 100644 (file)
@@ -4,7 +4,7 @@ use activitystreams::{
   object::AsObject,
 };
 use lemmy_db::{community::Community, user::User_};
-use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
+use lemmy_utils::{settings::Settings, LemmyError};
 use lemmy_websocket::LemmyContext;
 use serde::{export::fmt::Debug, Serialize};
 use url::{ParseError, Url};
@@ -40,9 +40,8 @@ where
   T: ToString,
 {
   let id = format!(
-    "{}://{}/activities/{}/{}",
-    get_apub_protocol_string(),
-    Settings::get().hostname,
+    "{}/activities/{}/{}",
+    Settings::get().get_protocol_and_hostname(),
     kind.to_string().to_lowercase(),
     Uuid::new_v4()
   );
index 8a41e88665e9dc9caf73ff442f9cadfe7a6509d1..715b765b3756b88aa667f9dd70c36293f9d96dcf 100644 (file)
@@ -44,8 +44,8 @@ use lemmy_db::{
 };
 use lemmy_structs::blocking;
 use lemmy_utils::{
-  apub::get_apub_protocol_string,
   location_info,
+  settings::Settings,
   utils::{check_slurs, check_slurs_opt, convert_datetime},
   LemmyError,
 };
@@ -299,7 +299,7 @@ impl ActorType for Community {
         };
         Ok(Url::parse(&format!(
           "{}://{}{}/inbox",
-          get_apub_protocol_string(),
+          Settings::get().get_protocol_string(),
           domain,
           port,
         ))?)
index 4ce4082cdcd4cf6c5bba57b5b919be8e506b77d4..3f2109ecbb71d13629105bdf3316635f69299009 100644 (file)
@@ -27,9 +27,9 @@ use lemmy_db::{
 };
 use lemmy_structs::{blocking, site::SearchResponse};
 use lemmy_utils::{
-  apub::get_apub_protocol_string,
   location_info,
   request::{retry, RecvError},
+  settings::Settings,
   LemmyError,
 };
 use lemmy_websocket::LemmyContext;
@@ -117,7 +117,12 @@ pub async fn search_by_apub_id(
       return Err(anyhow!("Invalid search query: {}", query).into());
     };
 
-    let url = format!("{}://{}{}", get_apub_protocol_string(), instance, name);
+    let url = format!(
+      "{}://{}{}",
+      Settings::get().get_protocol_string(),
+      instance,
+      name
+    );
     Url::parse(&url)?
   } else {
     Url::parse(&query)?
index 22eb9fbe056548f9ec834ae330b4c50bf17f687e..3f37c5d3c036f423186fda03b7668f2b90a70d39 100644 (file)
@@ -32,7 +32,6 @@ use chrono::NaiveDateTime;
 use lemmy_db::{activity::do_insert_activity, user::User_, DbPool};
 use lemmy_structs::{blocking, WebFingerResponse};
 use lemmy_utils::{
-  apub::get_apub_protocol_string,
   location_info,
   request::{retry, RecvError},
   settings::Settings,
@@ -97,7 +96,7 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
     };
   }
 
-  if apub_id.scheme() != get_apub_protocol_string() {
+  if apub_id.scheme() != Settings::get().get_protocol_string() {
     return Err(anyhow!("invalid apub id scheme: {:?}", apub_id.scheme()).into());
   }
 
@@ -319,7 +318,7 @@ pub async fn fetch_webfinger_url(
 ) -> Result<Url, LemmyError> {
   let fetch_url = format!(
     "{}://{}/.well-known/webfinger?resource=acct:{}@{}",
-    get_apub_protocol_string(),
+    Settings::get().get_protocol_string(),
     mention.domain,
     mention.name,
     mention.domain
index 814c169fa182dc4eeed6d6c4856386628feee60b..904b16937ec2becd67de6e3d7a1cc68c12156fba 100644 (file)
@@ -8,6 +8,7 @@ name = "lemmy_db"
 path = "src/lib.rs"
 
 [dependencies]
+lemmy_utils = { path = "../lemmy_utils" }
 diesel = { version = "1.4", features = ["postgres","chrono","r2d2","64-column-tables","serde_json"] }
 chrono = { version = "0.4", features = ["serde"] }
 serde = { version = "1.0", features = ["derive"] }
index f2c20e9474455683a85543e5717c6770acfda325..15fb592c7b91c9741f8e9cdb504168d91dd83697 100644 (file)
@@ -6,6 +6,7 @@ use crate::{
 };
 use bcrypt::{hash, DEFAULT_COST};
 use diesel::{dsl::*, result::Error, *};
+use lemmy_utils::settings::Settings;
 use serde::Serialize;
 
 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
@@ -151,7 +152,12 @@ impl User_ {
   }
 
   pub fn get_profile_url(&self, hostname: &str) -> String {
-    format!("https://{}/u/{}", hostname, self.name)
+    format!(
+      "{}://{}/u/{}",
+      Settings::get().get_protocol_string(),
+      hostname,
+      self.name
+    )
   }
 
   pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
index 3efe0beada50682b09b3d1b6e48b81c8b86cafd0..9b67866a988b9d712f333b896aaabca2275a2a3b 100644 (file)
@@ -77,7 +77,7 @@ fn do_send_local_notifs(
   do_send_email: bool,
 ) -> Vec<i32> {
   let mut recipient_ids = Vec::new();
-  let hostname = &format!("https://{}", Settings::get().hostname);
+  let hostname = &Settings::get().get_protocol_and_hostname();
 
   // Send the local mentions
   for mention in mentions
index 08e7a449182e833c14dc10eb2b4fae1121ebb5c1..4f6ec22f009a6f60b38f6e01b8ab79daf381a2fb 100644 (file)
@@ -35,14 +35,6 @@ pub enum EndpointType {
   PrivateMessage,
 }
 
-pub fn get_apub_protocol_string() -> &'static str {
-  if Settings::get().federation.tls_enabled {
-    "https"
-  } else {
-    "http"
-  }
-}
-
 /// Generates the ActivityPub ID for a given object type and ID.
 pub fn make_apub_endpoint(endpoint_type: EndpointType, name: &str) -> Url {
   let point = match endpoint_type {
@@ -54,9 +46,8 @@ pub fn make_apub_endpoint(endpoint_type: EndpointType, name: &str) -> Url {
   };
 
   Url::parse(&format!(
-    "{}://{}/{}/{}",
-    get_apub_protocol_string(),
-    Settings::get().hostname,
+    "{}/{}/{}",
+    Settings::get().get_protocol_and_hostname(),
     point,
     name
   ))
index 4aa70c6fd42a4655b978754a41404479b2ee0f75..cbd778c6ea8065d0da103dd3e3e72af07d641e33 100644 (file)
@@ -1,4 +1,4 @@
-use crate::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
+use crate::{settings::Settings, LemmyError};
 use anyhow::anyhow;
 use log::error;
 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
@@ -149,9 +149,8 @@ pub async fn fetch_iframely_and_pictrs_data(
       // The full urls are necessary for federation
       let pictrs_thumbnail = if let Some(pictrs_hash) = pictrs_hash {
         Some(format!(
-          "{}://{}/pictrs/image/{}",
-          get_apub_protocol_string(),
-          Settings::get().hostname,
+          "{}/pictrs/image/{}",
+          Settings::get().get_protocol_and_hostname(),
           pictrs_hash
         ))
       } else {
index 82bdceb219ea1eed7967744c9103f50e02f00ae9..d3775c4b4d6ccf29032452ef3496a95fa452d49e 100644 (file)
@@ -12,6 +12,7 @@ pub struct Settings {
   pub hostname: String,
   pub bind: IpAddr,
   pub port: u16,
+  pub tls_enabled: bool,
   pub jwt_secret: String,
   pub pictrs_url: String,
   pub rate_limit: RateLimitConfig,
@@ -68,7 +69,6 @@ pub struct DatabaseConfig {
 #[derive(Debug, Deserialize, Clone)]
 pub struct FederationConfig {
   pub enabled: bool,
-  pub tls_enabled: bool,
   pub allowed_instances: String,
   pub blocked_instances: String,
 }
@@ -157,6 +157,21 @@ impl Settings {
     blocked_instances
   }
 
+  /// Returns either "http" or "https", depending on tls_enabled setting
+  pub fn get_protocol_string(&self) -> &'static str {
+    if self.tls_enabled {
+      "https"
+    } else {
+      "http"
+    }
+  }
+
+  /// Returns something like `http://localhost` or `https://dev.lemmy.ml`,
+  /// with the correct protocol and hostname.
+  pub fn get_protocol_and_hostname(&self) -> String {
+    format!("{}://{}", self.get_protocol_string(), self.hostname)
+  }
+
   pub fn save_config_file(data: &str) -> Result<String, Error> {
     fs::write(CONFIG_FILE, data)?;
 
index 6a69035daba9972ab09773432107a6429f07c312..6743fb29867b496f822c8f87346b53dfac874559 100644 (file)
@@ -13,7 +13,7 @@ use lemmy_db::{
   Crud,
 };
 use lemmy_utils::{
-  apub::{generate_actor_keypair, get_apub_protocol_string, make_apub_endpoint, EndpointType},
+  apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
   settings::Settings,
   LemmyError,
 };
@@ -206,9 +206,8 @@ fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), Lemm
   info!("Running post_thumbnail_url_updates_2020_07_27");
 
   let domain_prefix = format!(
-    "{}://{}/pictrs/image/",
-    get_apub_protocol_string(),
-    Settings::get().hostname
+    "{}/pictrs/image/",
+    Settings::get().get_protocol_and_hostname(),
   );
 
   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
index 2c36ac2335a4ad7c97cf1c348bfa004bf7401ca7..f9111169a661cb61bcca7f1d325cd0c559abde66 100644 (file)
@@ -71,7 +71,7 @@ fn get_feed_all_data(conn: &PgConnection, sort_type: &SortType) -> Result<String
   let mut channel_builder = ChannelBuilder::default();
   channel_builder
     .title(&format!("{} - All", site_view.name))
-    .link(format!("https://{}", Settings::get().hostname))
+    .link(Settings::get().get_protocol_and_hostname())
     .items(items);
 
   if let Some(site_desc) = site_view.description {
@@ -196,7 +196,7 @@ fn get_feed_front(
   let mut channel_builder = ChannelBuilder::default();
   channel_builder
     .title(&format!("{} - Subscribed", site_view.name))
-    .link(format!("https://{}", Settings::get().hostname))
+    .link(Settings::get().get_protocol_and_hostname())
     .items(items);
 
   if let Some(site_desc) = site_view.description {
@@ -225,7 +225,10 @@ fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result<ChannelBuilder, Le
   let mut channel_builder = ChannelBuilder::default();
   channel_builder
     .title(&format!("{} - Inbox", site_view.name))
-    .link(format!("https://{}/inbox", Settings::get().hostname))
+    .link(format!(
+      "{}/inbox",
+      Settings::get().get_protocol_and_hostname()
+    ))
     .items(items);
 
   if let Some(site_desc) = site_view.description {
@@ -243,8 +246,8 @@ fn create_reply_and_mention_items(
     .iter()
     .map(|r| {
       let reply_url = format!(
-        "https://{}/post/{}/comment/{}",
-        Settings::get().hostname,
+        "{}/post/{}/comment/{}",
+        Settings::get().get_protocol_and_hostname(),
         r.post_id,
         r.id
       );
@@ -256,8 +259,8 @@ fn create_reply_and_mention_items(
     .iter()
     .map(|m| {
       let mention_url = format!(
-        "https://{}/post/{}/comment/{}",
-        Settings::get().hostname,
+        "{}/post/{}/comment/{}",
+        Settings::get().get_protocol_and_hostname(),
         m.post_id,
         m.id
       );
@@ -277,7 +280,11 @@ fn build_item(
 ) -> Result<Item, LemmyError> {
   let mut i = ItemBuilder::default();
   i.title(format!("Reply from {}", creator_name));
-  let author_url = format!("https://{}/u/{}", Settings::get().hostname, creator_name);
+  let author_url = format!(
+    "{}/u/{}",
+    Settings::get().get_protocol_and_hostname(),
+    creator_name
+  );
   i.author(format!(
     "/u/{} <a href=\"{}\">(link)</a>",
     creator_name, author_url
@@ -306,7 +313,11 @@ fn create_post_items(posts: Vec<PostView>) -> Result<Vec<Item>, LemmyError> {
 
     i.title(p.name);
 
-    let author_url = format!("https://{}/u/{}", Settings::get().hostname, p.creator_name);
+    let author_url = format!(
+      "{}/u/{}",
+      Settings::get().get_protocol_and_hostname(),
+      p.creator_name
+    );
     i.author(format!(
       "/u/{} <a href=\"{}\">(link)</a>",
       p.creator_name, author_url
@@ -315,7 +326,11 @@ fn create_post_items(posts: Vec<PostView>) -> Result<Vec<Item>, LemmyError> {
     let dt = DateTime::<Utc>::from_utc(p.published, Utc);
     i.pub_date(dt.to_rfc2822());
 
-    let post_url = format!("https://{}/post/{}", Settings::get().hostname, p.id);
+    let post_url = format!(
+      "{}/post/{}",
+      Settings::get().get_protocol_and_hostname(),
+      p.id
+    );
     i.comments(post_url.to_owned());
     let guid = GuidBuilder::default()
       .permalink(true)
@@ -325,8 +340,8 @@ fn create_post_items(posts: Vec<PostView>) -> Result<Vec<Item>, LemmyError> {
     i.guid(guid);
 
     let community_url = format!(
-      "https://{}/c/{}",
-      Settings::get().hostname,
+      "{}/c/{}",
+      Settings::get().get_protocol_and_hostname(),
       p.community_name
     );
 
index 984151c429c333fe05eaeea65c9b236029da9d00..c41be0e94ab67d8270d878c6080b685eab7066ce 100644 (file)
@@ -3,7 +3,7 @@ use anyhow::anyhow;
 use lemmy_api::version;
 use lemmy_db::site_view::SiteView;
 use lemmy_structs::blocking;
-use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError};
+use lemmy_utils::{settings::Settings, LemmyError};
 use lemmy_websocket::LemmyContext;
 use serde::{Deserialize, Serialize};
 use url::Url;
@@ -19,9 +19,8 @@ async fn node_info_well_known() -> Result<HttpResponse<Body>, LemmyError> {
     links: NodeInfoWellKnownLinks {
       rel: Url::parse("http://nodeinfo.diaspora.software/ns/schema/2.0")?,
       href: Url::parse(&format!(
-        "{}://{}/nodeinfo/2.0.json",
-        get_apub_protocol_string(),
-        Settings::get().hostname
+        "{}/nodeinfo/2.0.json",
+        Settings::get().get_protocol_and_hostname()
       ))?,
     },
   };