1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
use crate::handle::{ClientHandle, ServerHandle};
use crate::target_configure::{ClientTargetConfig, ServerTargetConfig};
use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Formatter},
marker::PhantomData,
net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr},
str::FromStr,
};
use tokio::net::lookup_host;
const DEFAULT_PORT: u16 = 8080;
#[derive(Debug, Serialize, Deserialize)]
pub struct TcpServerTarget<Client, Server>
where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
/// Client Config
client_cfg: Option<ClientTargetConfig>,
/// Server Config
server_cfg: Option<ServerTargetConfig>,
/// Server port
port: u16,
/// Bind addr
bind_addr: IpAddr,
/// Client Phantom Data
_client: PhantomData<Client>,
/// Server Phantom Data
_server: PhantomData<Server>,
}
impl<Client, Server> Default for TcpServerTarget<Client, Server>
where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
fn default() -> Self {
Self {
client_cfg: None,
server_cfg: None,
port: DEFAULT_PORT,
bind_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
_client: PhantomData,
_server: PhantomData,
}
}
}
impl<Client, Server> From<SocketAddr> for TcpServerTarget<Client, Server>
where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
/// Convert SocketAddr to TcpServerTarget
fn from(value: SocketAddr) -> Self {
Self {
port: value.port(),
bind_addr: value.ip(),
..Self::default()
}
}
}
impl<Client, Server> From<TcpServerTarget<Client, Server>> for SocketAddr
where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
/// Convert TcpServerTarget to SocketAddr
fn from(val: TcpServerTarget<Client, Server>) -> Self {
SocketAddr::new(val.bind_addr, val.port)
}
}
impl<Client, Server> Display for TcpServerTarget<Client, Server>
where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.bind_addr, self.port)
}
}
impl<Client, Server> TcpServerTarget<Client, Server>
where
Client: ClientHandle<Server>,
Server: ServerHandle<Client>,
{
/// Create target by address
pub fn from_addr(addr: impl Into<IpAddr>, port: impl Into<u16>) -> Self {
Self {
port: port.into(),
bind_addr: addr.into(),
..Self::default()
}
}
/// Try to create target by string
pub fn from_str<'a>(addr_str: impl Into<&'a str>) -> Result<Self, AddrParseError> {
let socket_addr = SocketAddr::from_str(addr_str.into());
match socket_addr {
Ok(socket_addr) => Ok(Self::from_addr(socket_addr.ip(), socket_addr.port())),
Err(err) => Err(err),
}
}
/// Try to create target by domain name
pub async fn from_domain<'a>(domain: impl Into<&'a str>) -> Result<Self, std::io::Error> {
match domain_to_addr(domain).await {
Ok(domain_addr) => Ok(Self::from(domain_addr)),
Err(e) => Err(e),
}
}
/// Set client config
pub fn client_cfg(mut self, config: ClientTargetConfig) -> Self {
self.client_cfg = Some(config);
self
}
/// Set server config
pub fn server_cfg(mut self, config: ServerTargetConfig) -> Self {
self.server_cfg = Some(config);
self
}
/// Add client config
pub fn add_client_cfg(&mut self, config: ClientTargetConfig) {
self.client_cfg = Some(config);
}
/// Add server config
pub fn add_server_cfg(&mut self, config: ServerTargetConfig) {
self.server_cfg = Some(config);
}
/// Get client config ref
pub fn get_client_cfg(&self) -> Option<&ClientTargetConfig> {
self.client_cfg.as_ref()
}
/// Get server config ref
pub fn get_server_cfg(&self) -> Option<&ServerTargetConfig> {
self.server_cfg.as_ref()
}
/// Get SocketAddr of TcpServerTarget
pub fn get_addr(&self) -> SocketAddr {
SocketAddr::new(self.bind_addr, self.port)
}
}
/// Parse Domain Name to IpAddr via DNS
async fn domain_to_addr<'a>(domain: impl Into<&'a str>) -> Result<SocketAddr, std::io::Error> {
let domain = domain.into();
let default_port: u16 = DEFAULT_PORT;
if let Ok(socket_addr) = domain.parse::<SocketAddr>() {
return Ok(match socket_addr.ip() {
IpAddr::V4(_) => socket_addr,
IpAddr::V6(_) => SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), socket_addr.port()),
});
}
if let Ok(_v6_addr) = domain.parse::<std::net::Ipv6Addr>() {
return Ok(SocketAddr::new(
IpAddr::V4(Ipv4Addr::LOCALHOST),
default_port,
));
}
let (host, port_str) = if let Some((host, port)) = domain.rsplit_once(':') {
(host.trim_matches(|c| c == '[' || c == ']'), Some(port))
} else {
(domain, None)
};
let port = port_str
.and_then(|p| p.parse::<u16>().ok())
.map(|p| p.clamp(0, u16::MAX))
.unwrap_or(default_port);
let mut socket_iter = lookup_host((host, 0)).await?;
if let Some(addr) = socket_iter.find(|addr| addr.is_ipv4()) {
return Ok(SocketAddr::new(addr.ip(), port));
}
Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port))
}
|