1use anyhow::{anyhow, Result};
16use bytes::{Buf, BytesMut};
17use log::{debug, info, trace, warn};
18use std::collections::HashMap;
19use tokio::io::{AsyncReadExt, AsyncWriteExt};
20use tokio::net::TcpStream;
21
22use super::protocol::{
23 parse_backend_message, AuthenticationMessage, BackendMessage, FrontendMessage, StartupMessage,
24 TransactionStatus,
25};
26use super::scram::ScramClient;
27use super::types::{ReplicationSlotInfo, StandbyStatusUpdate};
28
29pub struct ReplicationConnection {
30 stream: TcpStream,
31 read_buffer: BytesMut,
32 write_buffer: BytesMut,
33 parameters: HashMap<String, String>,
34 process_id: Option<i32>,
35 secret_key: Option<i32>,
36 transaction_status: TransactionStatus,
37 in_copy_mode: bool,
38}
39
40impl ReplicationConnection {
41 pub async fn connect(
42 host: &str,
43 port: u16,
44 database: &str,
45 user: &str,
46 password: &str,
47 ) -> Result<Self> {
48 info!("Connecting to PostgreSQL at {host}:{port}");
49
50 let stream = TcpStream::connect((host, port)).await?;
51 stream.set_nodelay(true)?;
52
53 let mut conn = Self {
54 stream,
55 read_buffer: BytesMut::with_capacity(8192),
56 write_buffer: BytesMut::with_capacity(8192),
57 parameters: HashMap::new(),
58 process_id: None,
59 secret_key: None,
60 transaction_status: TransactionStatus::Idle,
61 in_copy_mode: false,
62 };
63
64 conn.startup_replication(database, user, password).await?;
65
66 Ok(conn)
67 }
68
69 async fn startup_replication(
70 &mut self,
71 database: &str,
72 user: &str,
73 password: &str,
74 ) -> Result<()> {
75 debug!("Starting replication protocol handshake");
76
77 let startup = StartupMessage::new_replication(database, user);
79 self.send_message(FrontendMessage::StartupMessage(startup))
80 .await?;
81
82 loop {
84 let msg = self.read_message().await?;
85 match msg {
86 BackendMessage::Authentication(auth) => {
87 match auth {
88 AuthenticationMessage::Ok => {
89 debug!("Authentication successful");
90 break;
91 }
92 AuthenticationMessage::CleartextPassword => {
93 debug!("Server requested cleartext password");
94 self.send_message(FrontendMessage::PasswordMessage(
95 password.to_string(),
96 ))
97 .await?;
98 }
99 AuthenticationMessage::MD5Password(_) => {
100 return Err(anyhow!(
101 "MD5 authentication is not supported (insecure). \
102 Please configure PostgreSQL to use scram-sha-256 in pg_hba.conf"
103 ));
104 }
105 AuthenticationMessage::SASL(mechanisms) => {
106 if mechanisms.contains(&"SCRAM-SHA-256".to_string()) {
107 debug!("Server requested SCRAM-SHA-256 authentication");
108 let mut scram_client = ScramClient::new(user, password);
109
110 let client_first = scram_client.client_first_message();
112 self.send_sasl_initial_response("SCRAM-SHA-256", &client_first)
113 .await?;
114
115 loop {
117 let sasl_msg = self.read_message().await?;
118 match sasl_msg {
119 BackendMessage::Authentication(
120 AuthenticationMessage::SASLContinue(data),
121 ) => {
122 let server_first = String::from_utf8_lossy(&data);
123 scram_client
124 .process_server_first_message(&server_first)?;
125
126 let client_final =
127 scram_client.client_final_message()?;
128 self.send_sasl_response(&client_final).await?;
129 }
130 BackendMessage::Authentication(
131 AuthenticationMessage::SASLFinal(data),
132 ) => {
133 let server_final = String::from_utf8_lossy(&data);
134 scram_client.verify_server_final(&server_final)?;
135 debug!("SCRAM-SHA-256 authentication successful");
136 break;
137 }
138 BackendMessage::ErrorResponse(err) => {
139 return Err(anyhow!(
140 "SASL authentication failed: {}",
141 err.message
142 ));
143 }
144 _ => {
145 warn!("Unexpected message during SASL: {sasl_msg:?}");
146 }
147 }
148 }
149 } else {
150 return Err(anyhow!("No supported SASL mechanisms"));
151 }
152 }
153 _ => {
154 return Err(anyhow!("Unsupported authentication method"));
155 }
156 }
157 }
158 BackendMessage::ErrorResponse(err) => {
159 return Err(anyhow!("Authentication failed: {}", err.message));
160 }
161 _ => {
162 warn!("Unexpected message during authentication: {msg:?}");
163 }
164 }
165 }
166
167 loop {
169 let msg = self.read_message().await?;
170 match msg {
171 BackendMessage::BackendKeyData {
172 process_id,
173 secret_key,
174 } => {
175 self.process_id = Some(process_id);
176 self.secret_key = Some(secret_key);
177 debug!("Received backend key data: pid={process_id}");
178 }
179 BackendMessage::ParameterStatus { name, value } => {
180 debug!("Parameter: {name} = {value}");
181 self.parameters.insert(name, value);
182 }
183 BackendMessage::ReadyForQuery(status) => {
184 self.transaction_status = status;
185 debug!("Connection ready, status: {status:?}");
186 break;
187 }
188 BackendMessage::ErrorResponse(err) => {
189 return Err(anyhow!("Startup failed: {}", err.message));
190 }
191 BackendMessage::NoticeResponse(notice) => {
192 info!("Notice: {}", notice.message);
193 }
194 _ => {
195 warn!("Unexpected message during startup: {msg:?}");
196 }
197 }
198 }
199
200 Ok(())
201 }
202
203 pub async fn identify_system(&mut self) -> Result<HashMap<String, String>> {
204 debug!("Sending IDENTIFY_SYSTEM command");
205
206 self.send_message(FrontendMessage::Query("IDENTIFY_SYSTEM".to_string()))
207 .await?;
208
209 let mut system_info = HashMap::new();
210
211 loop {
212 let msg = self.read_message().await?;
213 match msg {
214 BackendMessage::RowDescription(_) => {
215 }
217 BackendMessage::DataRow(row) => {
218 if row.len() >= 4 {
220 if let Some(Some(systemid)) = row.first() {
221 system_info.insert(
222 "systemid".to_string(),
223 String::from_utf8_lossy(systemid).to_string(),
224 );
225 }
226 if let Some(Some(timeline)) = row.get(1) {
227 system_info.insert(
228 "timeline".to_string(),
229 String::from_utf8_lossy(timeline).to_string(),
230 );
231 }
232 if let Some(Some(xlogpos)) = row.get(2) {
233 system_info.insert(
234 "xlogpos".to_string(),
235 String::from_utf8_lossy(xlogpos).to_string(),
236 );
237 }
238 if let Some(Some(dbname)) = row.get(3) {
239 system_info.insert(
240 "dbname".to_string(),
241 String::from_utf8_lossy(dbname).to_string(),
242 );
243 }
244 }
245 }
246 BackendMessage::CommandComplete(_) => {
247 }
249 BackendMessage::ReadyForQuery(status) => {
250 self.transaction_status = status;
251 break;
252 }
253 BackendMessage::ErrorResponse(err) => {
254 return Err(anyhow!("IDENTIFY_SYSTEM failed: {}", err.message));
255 }
256 _ => {
257 warn!("Unexpected message during IDENTIFY_SYSTEM: {msg:?}");
258 }
259 }
260 }
261
262 Ok(system_info)
263 }
264
265 pub async fn create_replication_slot(
266 &mut self,
267 slot_name: &str,
268 temporary: bool,
269 ) -> Result<ReplicationSlotInfo> {
270 debug!("Creating replication slot: {slot_name}");
271
272 let query = if temporary {
273 format!("CREATE_REPLICATION_SLOT {slot_name} TEMPORARY LOGICAL pgoutput")
274 } else {
275 format!("CREATE_REPLICATION_SLOT {slot_name} LOGICAL pgoutput")
276 };
277
278 self.send_message(FrontendMessage::Query(query)).await?;
279
280 let mut slot_info = ReplicationSlotInfo {
281 slot_name: slot_name.to_string(),
282 consistent_point: String::new(),
283 snapshot_name: None,
284 output_plugin: "pgoutput".to_string(),
285 };
286
287 loop {
288 let msg = self.read_message().await?;
289 match msg {
290 BackendMessage::RowDescription(_) => {
291 }
293 BackendMessage::DataRow(row) => {
294 if row.len() >= 4 {
296 if let Some(Some(consistent_point)) = row.get(1) {
297 slot_info.consistent_point =
298 String::from_utf8_lossy(consistent_point).to_string();
299 }
300 if let Some(Some(snapshot_name)) = row.get(2) {
301 slot_info.snapshot_name =
302 Some(String::from_utf8_lossy(snapshot_name).to_string());
303 }
304 }
305 }
306 BackendMessage::CommandComplete(_) => {
307 }
309 BackendMessage::ReadyForQuery(status) => {
310 self.transaction_status = status;
311 break;
312 }
313 BackendMessage::ErrorResponse(err) => {
314 if err.message.contains("already exists") {
315 debug!("Replication slot already exists: {slot_name}");
316 return self.get_replication_slot_info(slot_name).await;
318 }
319 return Err(anyhow!("CREATE_REPLICATION_SLOT failed: {}", err.message));
320 }
321 _ => {
322 warn!("Unexpected message during CREATE_REPLICATION_SLOT: {msg:?}");
323 }
324 }
325 }
326
327 Ok(slot_info)
328 }
329
330 pub async fn get_replication_slot_info(
331 &mut self,
332 slot_name: &str,
333 ) -> Result<ReplicationSlotInfo> {
334 debug!("Using existing replication slot: {slot_name}");
337
338 Ok(ReplicationSlotInfo {
339 slot_name: slot_name.to_string(),
340 consistent_point: "0/0".to_string(), snapshot_name: None,
342 output_plugin: "pgoutput".to_string(),
343 })
344 }
345
346 pub async fn start_replication(
347 &mut self,
348 slot_name: &str,
349 start_lsn: Option<u64>,
350 options: HashMap<String, String>,
351 ) -> Result<()> {
352 debug!("Starting replication from slot: {slot_name}");
353
354 let mut query = format!("START_REPLICATION SLOT {slot_name} LOGICAL");
355
356 if let Some(lsn) = start_lsn {
357 query.push_str(&format!(" {}", format_lsn(lsn)));
358 } else {
359 query.push_str(" 0/0");
360 }
361
362 if !options.is_empty() {
363 query.push_str(" (");
364 let opts: Vec<String> = options.iter().map(|(k, v)| format!("{k} '{v}'")).collect();
365 query.push_str(&opts.join(", "));
366 query.push(')');
367 }
368
369 self.send_message(FrontendMessage::Query(query)).await?;
370
371 loop {
373 let msg = self.read_message().await?;
374 match msg {
375 BackendMessage::CopyBothResponse => {
376 debug!("Entered COPY BOTH mode for replication");
377 self.in_copy_mode = true;
378 break;
379 }
380 BackendMessage::ErrorResponse(err) => {
381 return Err(anyhow!("START_REPLICATION failed: {}", err.message));
382 }
383 BackendMessage::ReadyForQuery(_) => {
384 debug!("Received ReadyForQuery before entering COPY mode");
386 }
387 _ => {
388 debug!("Message during START_REPLICATION: {msg:?}");
389 }
390 }
391 }
392
393 Ok(())
394 }
395
396 pub async fn read_replication_message(&mut self) -> Result<BackendMessage> {
397 if !self.in_copy_mode {
398 return Err(anyhow!("Not in COPY mode"));
399 }
400
401 self.read_message().await
402 }
403
404 pub async fn send_standby_status(&mut self, status: StandbyStatusUpdate) -> Result<()> {
405 if !self.in_copy_mode {
406 return Err(anyhow!("Not in COPY mode"));
407 }
408
409 let timestamp = chrono::Utc::now().timestamp_micros() - 946684800000000; self.send_message(FrontendMessage::StandbyStatusUpdate {
412 write_lsn: status.write_lsn,
413 flush_lsn: status.flush_lsn,
414 apply_lsn: status.apply_lsn,
415 timestamp,
416 reply: if status.reply_requested { 1 } else { 0 },
417 })
418 .await
419 }
420
421 async fn send_message(&mut self, msg: FrontendMessage) -> Result<()> {
422 self.write_buffer.clear();
423 msg.encode(&mut self.write_buffer)?;
424
425 self.stream.write_all(&self.write_buffer).await?;
426 self.stream.flush().await?;
427
428 trace!("Sent message: {msg:?}");
429 Ok(())
430 }
431
432 async fn send_sasl_initial_response(&mut self, mechanism: &str, response: &str) -> Result<()> {
433 self.send_message(FrontendMessage::SASLInitialResponse {
434 mechanism: mechanism.to_string(),
435 data: response.as_bytes().to_vec(),
436 })
437 .await
438 }
439
440 async fn send_sasl_response(&mut self, response: &str) -> Result<()> {
441 self.send_message(FrontendMessage::SASLResponse(response.as_bytes().to_vec()))
442 .await
443 }
444
445 async fn read_message(&mut self) -> Result<BackendMessage> {
446 loop {
447 if let Some(msg) = self.try_parse_message()? {
449 trace!("Received message: {msg:?}");
450 return Ok(msg);
451 }
452
453 let mut temp_buf = vec![0u8; 4096];
455 let n = self.stream.read(&mut temp_buf).await?;
456 if n == 0 {
457 return Err(anyhow!("Connection closed by server"));
458 }
459
460 self.read_buffer.extend_from_slice(&temp_buf[..n]);
461 }
462 }
463
464 fn try_parse_message(&mut self) -> Result<Option<BackendMessage>> {
465 if self.read_buffer.len() < 5 {
466 return Ok(None); }
468
469 let msg_type = self.read_buffer[0];
470 let length = u32::from_be_bytes([
471 self.read_buffer[1],
472 self.read_buffer[2],
473 self.read_buffer[3],
474 self.read_buffer[4],
475 ]) as usize;
476
477 if length < 4 {
478 return Err(anyhow!("Invalid message length: {length}"));
479 }
480
481 let total_length = 1 + length; if self.read_buffer.len() < total_length {
484 return Ok(None); }
486
487 let body = self.read_buffer[5..total_length].to_vec();
489 self.read_buffer.advance(total_length);
490
491 let msg = parse_backend_message(msg_type, &body)?;
493 Ok(Some(msg))
494 }
495
496 pub async fn close(mut self) -> Result<()> {
497 if self.in_copy_mode {
498 let _ = self.send_message(FrontendMessage::CopyDone).await;
499 }
500 let _ = self.send_message(FrontendMessage::Terminate).await;
501 let _ = self.stream.shutdown().await;
502 Ok(())
503 }
504}
505
506fn format_lsn(lsn: u64) -> String {
507 format!("{:X}/{:X}", lsn >> 32, lsn & 0xFFFFFFFF)
508}
509
510#[allow(dead_code)]
511fn parse_lsn(lsn_str: &str) -> Result<u64> {
512 let parts: Vec<&str> = lsn_str.split('/').collect();
513 if parts.len() != 2 {
514 return Err(anyhow!("Invalid LSN format: {lsn_str}"));
515 }
516
517 let high = u64::from_str_radix(parts[0], 16)?;
518 let low = u64::from_str_radix(parts[1], 16)?;
519
520 Ok((high << 32) | low)
521}