JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Revert authentication payload change

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨16b4bc2

⁨giterated-daemon/src/main.rs⁩ - ⁨4792⁩ bytes
Raw
1 use anyhow::Error;
2 use connection::{Connections, RawConnection};
3 use giterated_daemon::{
4 authentication::AuthenticationTokenGranter,
5 backend::{
6 git::GitBackend, settings::DatabaseSettings, user::UserAuth, RepositoryBackend, UserBackend,
7 },
8 connection::{self, wrapper::connection_wrapper},
9 federation::connections::InstanceConnections,
10 };
11 use giterated_models::model::instance::Instance;
12 use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgPool};
13 use std::{net::SocketAddr, str::FromStr, sync::Arc};
14 use tokio::{
15 fs::File,
16 io::{AsyncRead, AsyncReadExt, AsyncWrite},
17 net::{TcpListener, TcpStream},
18 sync::Mutex,
19 };
20 use tokio_tungstenite::{accept_async, WebSocketStream};
21 use toml::Table;
22
23 #[macro_use]
24 extern crate tracing;
25
26 #[tokio::main]
27 async fn main() -> Result<(), Error> {
28 tracing_subscriber::fmt::init();
29 let config: Table = {
30 let mut file = File::open("Giterated.toml").await?;
31 let mut text = String::new();
32 file.read_to_string(&mut text).await?;
33 text.parse()?
34 };
35 let mut listener = TcpListener::bind(config["giterated"]["bind"].as_str().unwrap()).await?;
36 let connections: Arc<Mutex<Connections>> = Arc::default();
37 let instance_connections: Arc<Mutex<InstanceConnections>> = Arc::default();
38 let db_conn_options = PgConnectOptions::new()
39 .host(config["postgres"]["host"].as_str().unwrap())
40 .port(config["postgres"]["port"].as_integer().unwrap() as u16)
41 .database(config["postgres"]["database"].as_str().unwrap())
42 .username(config["postgres"]["user"].as_str().unwrap())
43 .password(config["postgres"]["password"].as_str().unwrap())
44 .log_statements(log::LevelFilter::Off);
45 let db_pool = PgPool::connect_with(db_conn_options).await?;
46
47 debug!("Running database migrations...");
48 sqlx::migrate!().run(&db_pool).await?;
49 info!("Connected");
50
51 let settings = Arc::new(Mutex::new(DatabaseSettings {
52 pg_pool: db_pool.clone(),
53 }));
54
55 let repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>> =
56 Arc::new(Mutex::new(GitBackend {
57 pg_pool: db_pool.clone(),
58 repository_folder: String::from(
59 config["giterated"]["backend"]["git"]["root"]
60 .as_str()
61 .unwrap(),
62 ),
63 instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap())
64 .unwrap(),
65 }));
66
67 let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter {
68 config: config.clone(),
69 instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(),
70 }));
71
72 let user_backend: Arc<Mutex<dyn UserBackend + Send>> = Arc::new(Mutex::new(UserAuth::new(
73 db_pool.clone(),
74 &Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(),
75 token_granter.clone(),
76 settings.clone(),
77 )));
78
79 info!("Connected");
80
81 loop {
82 let stream = accept_stream(&mut listener).await;
83 info!("Connected");
84
85 let (stream, address) = match stream {
86 Ok(stream) => stream,
87 Err(err) => {
88 error!("Failed to accept connection. {:?}", err);
89 continue;
90 }
91 };
92
93 info!("Accepted connection from {}", address);
94
95 let connection = accept_websocket_connection(stream).await;
96
97 let connection = match connection {
98 Ok(connection) => connection,
99 Err(err) => {
100 error!(
101 "Failed to initiate Websocket connection from {}. {:?}",
102 address, err
103 );
104 continue;
105 }
106 };
107
108 info!("Websocket connection established with {}", address);
109
110 let connection = RawConnection {
111 task: tokio::spawn(connection_wrapper(
112 connection,
113 connections.clone(),
114 repository_backend.clone(),
115 user_backend.clone(),
116 token_granter.clone(),
117 settings.clone(),
118 address,
119 Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(),
120 instance_connections.clone(),
121 config.clone(),
122 )),
123 };
124
125 connections.lock().await.connections.push(connection);
126 }
127 }
128
129 async fn accept_stream(listener: &mut TcpListener) -> Result<(TcpStream, SocketAddr), Error> {
130 let stream = listener.accept().await?;
131
132 Ok(stream)
133 }
134
135 async fn accept_websocket_connection<S: AsyncRead + AsyncWrite + Unpin>(
136 stream: S,
137 ) -> Result<WebSocketStream<S>, Error> {
138 let connection = accept_async(stream).await?;
139
140 Ok(connection)
141 }
142