ferogram_connect/proxy.rs
1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2//
3// ferogram: async Telegram MTProto client in Rust
4// https://github.com/ankit-chaubey/ferogram
5//
6// Licensed under either the MIT License or the Apache License 2.0.
7// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
8// https://github.com/ankit-chaubey/ferogram
9//
10// Feel free to use, modify, and share this code.
11// Please keep this notice when redistributing.
12
13use tokio::net::TcpStream;
14
15use crate::error::ConnectError;
16use crate::transport_kind::TransportKind;
17
18/// Decoded MTProxy configuration.
19#[derive(Clone, Debug)]
20pub struct MtProxyConfig {
21 /// Proxy server hostname or IP.
22 pub host: String,
23 /// Proxy server port.
24 pub port: u16,
25 /// Raw secret bytes.
26 pub secret: Vec<u8>,
27 /// Transport variant; pass this as `config.transport`.
28 pub transport: TransportKind,
29}
30
31impl MtProxyConfig {
32 /// Open a TCP connection to the MTProxy host:port.
33 pub async fn connect(&self) -> Result<TcpStream, ConnectError> {
34 let addr = format!("{}:{}", self.host, self.port);
35 tracing::debug!("[ferogram] MTProxy TCP connect -> {addr}");
36 TcpStream::connect(&addr).await.map_err(ConnectError::Io)
37 }
38
39 /// Socket address string `"host:port"`.
40 pub fn addr(&self) -> String {
41 format!("{}:{}", self.host, self.port)
42 }
43}