Skip to main content

cqlsh_rs/driver/
proxy_address_translator.rs

1//! Proxy address translator.
2//!
3//! When connecting through a proxy or load balancer (e.g., AWS NLB, PrivateLink),
4//! the driver discovers internal node IPs from `system.peers` that are unreachable
5//! from the client. This translator remaps all peer addresses to the original
6//! contact point, ensuring all connections go through the proxy.
7
8use std::net::SocketAddr;
9
10use async_trait::async_trait;
11use scylla::errors::TranslationError;
12use scylla::policies::address_translator::{AddressTranslator, UntranslatedPeer};
13
14/// An [`AddressTranslator`] that redirects all peer connections to the original
15/// contact point address. Used when the cluster is accessed through a proxy.
16///
17/// All discovered node addresses are translated to `proxy_address`, ensuring
18/// the driver only connects through the proxy endpoint.
19#[derive(Debug, Clone)]
20pub struct ProxyAddressTranslator {
21    /// The proxy/contact point address to route all connections through.
22    proxy_address: SocketAddr,
23}
24
25impl ProxyAddressTranslator {
26    /// Create a new translator that routes all connections to `proxy_address`.
27    pub fn new(proxy_address: SocketAddr) -> Self {
28        Self { proxy_address }
29    }
30}
31
32#[async_trait]
33impl AddressTranslator for ProxyAddressTranslator {
34    async fn translate_address(
35        &self,
36        _untranslated_peer: &UntranslatedPeer,
37    ) -> Result<SocketAddr, TranslationError> {
38        Ok(self.proxy_address)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use std::net::{IpAddr, Ipv4Addr};
46
47    fn sock(ip: [u8; 4], port: u16) -> SocketAddr {
48        SocketAddr::new(IpAddr::V4(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3])), port)
49    }
50
51    #[test]
52    fn creates_with_correct_address() {
53        let proxy_addr = sock([18, 208, 144, 200], 9042);
54        let translator = ProxyAddressTranslator::new(proxy_addr);
55        assert_eq!(translator.proxy_address, proxy_addr);
56    }
57}