steamkit/steam/cdn/
server.rs1use std::fmt;
4use std::net::SocketAddr;
5
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
8pub enum ConnectionProtocol {
9 #[default]
11 Http,
12 Https,
14}
15
16#[derive(Clone, Debug, Default)]
18pub struct Server {
19 pub(crate) protocol: ConnectionProtocol,
21 pub(crate) host: Option<String>,
23 pub(crate) v_host: Option<String>,
25 pub(crate) port: i32,
27 pub(crate) server_type: Option<String>,
29 pub(crate) source_id: i32,
31 pub(crate) cell_id: u32,
33 pub(crate) load: i32,
35 pub(crate) weighted_load: f32,
37 pub(crate) num_entries: i32,
39 pub(crate) steam_china_only: bool,
41 pub(crate) use_as_proxy: bool,
43 pub(crate) proxy_request_path_template: Option<String>,
45 pub(crate) allowed_app_ids: Vec<u32>,
47 pub(crate) bypass_proxies_of_type: Vec<String>,
49}
50
51impl Server {
52 pub fn from_socket_addr(end_point: SocketAddr) -> Self {
55 let host = end_point.ip().to_string();
56
57 Server {
58 protocol: if end_point.port() == 443 {
59 ConnectionProtocol::Https
60 } else {
61 ConnectionProtocol::Http
62 },
63 host: Some(host.clone()),
64 v_host: Some(host),
65 port: i32::from(end_point.port()),
66 ..Server::default()
67 }
68 }
69
70 pub fn from_dns_end_point(host: &str, port: u16) -> Self {
73 Server {
74 protocol: if port == 443 {
75 ConnectionProtocol::Https
76 } else {
77 ConnectionProtocol::Http
78 },
79 host: Some(host.to_string()),
80 v_host: Some(host.to_string()),
81 port: i32::from(port),
82 ..Server::default()
83 }
84 }
85
86 pub fn protocol(&self) -> ConnectionProtocol {
88 self.protocol
89 }
90
91 pub fn host(&self) -> Option<&str> {
93 self.host.as_deref()
94 }
95
96 pub fn v_host(&self) -> Option<&str> {
98 self.v_host.as_deref()
99 }
100
101 pub fn port(&self) -> i32 {
103 self.port
104 }
105
106 pub fn server_type(&self) -> Option<&str> {
108 self.server_type.as_deref()
109 }
110
111 pub fn source_id(&self) -> i32 {
113 self.source_id
114 }
115
116 pub fn cell_id(&self) -> u32 {
118 self.cell_id
119 }
120
121 pub fn load(&self) -> i32 {
123 self.load
124 }
125
126 pub fn weighted_load(&self) -> f32 {
128 self.weighted_load
129 }
130
131 pub fn num_entries(&self) -> i32 {
133 self.num_entries
134 }
135
136 pub fn steam_china_only(&self) -> bool {
138 self.steam_china_only
139 }
140
141 pub fn use_as_proxy(&self) -> bool {
143 self.use_as_proxy
144 }
145
146 pub fn proxy_request_path_template(&self) -> Option<&str> {
148 self.proxy_request_path_template.as_deref()
149 }
150
151 pub fn allowed_app_ids(&self) -> &[u32] {
153 &self.allowed_app_ids
154 }
155
156 pub fn bypass_proxies_of_type(&self) -> &[String] {
158 &self.bypass_proxies_of_type
159 }
160}
161
162impl From<SocketAddr> for Server {
163 fn from(end_point: SocketAddr) -> Self {
165 Server::from_socket_addr(end_point)
166 }
167}
168
169impl fmt::Display for Server {
170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 write!(
173 f,
174 "{}:{} ({})",
175 self.host.as_deref().unwrap_or_default(),
176 self.port,
177 self.server_type.as_deref().unwrap_or_default()
178 )
179 }
180}