futu_backend/auth/commconfig/
accessors.rs1use crate::auth::UserAttribution;
4
5use super::types::CommonConfigSnapshot;
6
7use super::parsers::is_web_identity;
8use super::types::{
9 CONN_WEB_AU, CONN_WEB_CA, CONN_WEB_CN, CONN_WEB_HK, CONN_WEB_JP, CONN_WEB_MY, CONN_WEB_SG,
10 CONN_WEB_US,
11};
12
13pub fn forced_ip_for_attribution(
14 snapshot: &CommonConfigSnapshot,
15 attr: UserAttribution,
16 now_ts: i64,
17) -> Option<(String, u16)> {
18 snapshot.forced_ip.get(&attr).and_then(|entry| {
19 if entry.is_valid(now_ts) {
20 Some((entry.ip.clone(), entry.port))
21 } else {
22 tracing::debug!(
23 ?attr,
24 expire_ts = entry.expire_ts,
25 now_ts,
26 "commconfig: forced_ip expired, ignored"
27 );
28 None
29 }
30 })
31}
32
33pub fn ips_for_attribution(
36 snapshot: &CommonConfigSnapshot,
37 attr: UserAttribution,
38) -> Vec<(String, u16)> {
39 snapshot
40 .guaranteed_ip
41 .get(&attr)
42 .cloned()
43 .unwrap_or_default()
44}
45
46pub fn ips_for_broker(snapshot: &CommonConfigSnapshot, broker_id: u32) -> Vec<(String, u16)> {
55 snapshot
56 .guaranteed_ip_broker
57 .get(&broker_id)
58 .cloned()
59 .unwrap_or_default()
60}
61
62pub fn ips_for_web_identity(snapshot: &CommonConfigSnapshot, identity: u32) -> Vec<(String, u16)> {
68 snapshot
69 .guaranteed_ip_web
70 .get(&identity)
71 .cloned()
72 .unwrap_or_default()
73}
74
75pub fn webtcp_hardcoded_addrs(identity: u32) -> &'static [(&'static str, u16)] {
89 match identity {
90 CONN_WEB_CN => &[
91 ("119.91.245.213", 443),
92 ("119.91.245.125", 443),
93 ("119.91.245.213", 9595),
94 ("119.91.245.125", 9595),
95 ],
96 CONN_WEB_HK => &[
97 ("101.32.198.103", 443),
98 ("43.135.64.109", 443),
99 ("101.32.198.103", 9595),
100 ("43.135.64.109", 9595),
101 ],
102 CONN_WEB_US => &[
103 ("49.51.78.82", 443),
104 ("170.106.62.85", 443),
105 ("49.51.78.82", 9595),
106 ("170.106.62.85", 9595),
107 ],
108 CONN_WEB_SG => &[
109 ("101.32.173.177", 443),
110 ("101.33.49.67", 443),
111 ("101.32.173.177", 9595),
112 ("101.33.49.67", 9595),
113 ],
114 CONN_WEB_AU => &[
115 ("54.206.243.201", 443),
116 ("3.104.68.90", 443),
117 ("54.206.243.201", 9595),
118 ("3.104.68.90", 9595),
119 ],
120 CONN_WEB_JP => &[
121 ("43.163.254.232", 443),
122 ("43.163.252.131", 443),
123 ("43.163.254.232", 9595),
124 ("43.163.252.131", 9595),
125 ],
126 CONN_WEB_MY => &[
127 ("47.254.245.70", 443),
128 ("47.254.254.140", 443),
129 ("47.254.245.70", 9595),
130 ("47.254.254.140", 9595),
131 ],
132 CONN_WEB_CA => &[
133 ("15.157.179.115", 443),
134 ("15.157.83.146", 443),
135 ("15.157.179.115", 9595),
136 ("15.157.83.146", 9595),
137 ],
138 _ => &[],
139 }
140}
141
142pub fn webtcp_addrs_for_identity(
145 snapshot: &CommonConfigSnapshot,
146 identity: u32,
147) -> Vec<(String, u16)> {
148 let dynamic = ips_for_web_identity(snapshot, identity);
149 if !dynamic.is_empty() {
150 return dynamic;
151 }
152 webtcp_hardcoded_addrs(identity)
153 .iter()
154 .map(|(ip, port)| ((*ip).to_string(), *port))
155 .collect()
156}
157
158pub fn default_webtcp_identity_for_client_type(client_type: u8) -> u32 {
164 if client_type == 60 {
165 CONN_WEB_US
166 } else {
167 CONN_WEB_HK
168 }
169}
170
171pub fn broker_auth_webtcp_identity(snapshot: &CommonConfigSnapshot, client_type: u8) -> u32 {
178 snapshot
179 .web_conn_identity
180 .filter(|identity| is_web_identity(*identity))
181 .unwrap_or_else(|| default_webtcp_identity_for_client_type(client_type))
182}
183
184pub fn delay_until_next_refresh(next_refresh_ts: i64, now: i64) -> u64 {
193 const MIN_DELAY: u64 = 300; const MAX_DELAY: u64 = 7200; const DEFAULT_DELAY: u64 = 1800; if next_refresh_ts <= 0 {
197 return DEFAULT_DELAY;
198 }
199 let diff = next_refresh_ts - now;
200 if diff <= 0 {
201 return DEFAULT_DELAY;
202 }
203 (diff as u64).clamp(MIN_DELAY, MAX_DELAY)
204}