forked from googleforgames/quilkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.rs
173 lines (152 loc) · 5.49 KB
/
filters.rs
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
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
time::Duration,
};
use tokio::time::timeout;
use quilkin::{
config::Filter,
filters::{Debug, StaticFilter},
net::endpoint::Endpoint,
test::{load_test_filters, AddressType, TestHelper},
};
#[tokio::test]
#[cfg_attr(target_os = "macos", ignore)]
async fn test_filter() {
let mut t = TestHelper::default();
load_test_filters();
// create an echo server as an endpoint.
let echo = t.run_echo_server(AddressType::Random).await;
// create server configuration
let server_config = std::sync::Arc::new(quilkin::Config::default_non_agent());
server_config.filters.store(
quilkin::filters::FilterChain::try_create([Filter {
name: "TestFilter".to_string(),
label: None,
config: None,
}])
.map(std::sync::Arc::new)
.unwrap(),
);
server_config
.clusters
.modify(|clusters| clusters.insert_default([Endpoint::new(echo.clone())].into()));
let server_port = t.run_server(server_config, None, None).await;
// create a local client
let client_config = std::sync::Arc::new(quilkin::Config::default_non_agent());
client_config.clusters.modify(|clusters| {
clusters.insert_default(
[Endpoint::new(
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), server_port).into(),
)]
.into(),
)
});
client_config.filters.store(
quilkin::filters::FilterChain::try_create([Filter {
name: "TestFilter".to_string(),
label: None,
config: None,
}])
.map(std::sync::Arc::new)
.unwrap(),
);
// Run client proxy.
let client_port = t.run_server(client_config, None, None).await;
// let's send the packet
let (mut recv_chan, socket) = t.open_socket_and_recv_multiple_packets().await;
// game_client
let local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), client_port);
tracing::info!(address = %local_addr, "Sending hello");
socket.send_to(b"hello", &local_addr).await.unwrap();
let result = timeout(Duration::from_secs(5), recv_chan.recv())
.await
.unwrap()
.unwrap();
// since we don't know the ephemeral ip addresses in use, we'll search for
// substrings for the results we expect that the TestFilter will inject in
// the round-tripped packets.
assert_eq!(
2,
result.matches("odr").count(),
"Should be 2 read calls in {}",
result
);
assert_eq!(
2,
result.matches("our").count(),
"Should be 2 write calls in {}",
result
);
}
#[tokio::test]
#[cfg_attr(target_os = "macos", ignore)]
async fn debug_filter() {
let mut t = TestHelper::default();
// handy for grabbing the configuration name
let factory = Debug::factory();
// create an echo server as an endpoint.
let echo = t.run_echo_server(AddressType::Random).await;
tracing::trace!(%echo, "running echo server");
// create server configuration
let server_config = std::sync::Arc::new(quilkin::Config::default_non_agent());
server_config
.clusters
.modify(|clusters| clusters.insert_default([Endpoint::new(echo.clone())].into()));
server_config.filters.store(
quilkin::filters::FilterChain::try_create([Filter {
name: factory.name().into(),
label: None,
config: Some(serde_json::json!({ "id": "server", })),
}])
.map(std::sync::Arc::new)
.unwrap(),
);
let server_port = t.run_server(server_config, None, None).await;
// create a local client
let client_config = std::sync::Arc::new(quilkin::Config::default_non_agent());
client_config.clusters.modify(|clusters| {
clusters.insert_default(
[Endpoint::new(
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), server_port).into(),
)]
.into(),
)
});
client_config.filters.store(
quilkin::filters::FilterChain::try_create([Filter {
name: factory.name().into(),
label: None,
config: Some(serde_json::json!({ "id": "client" })),
}])
.map(std::sync::Arc::new)
.unwrap(),
);
let client_port = t.run_server(client_config, None, None).await;
// let's send the packet
let (mut recv_chan, socket) = t.open_socket_and_recv_multiple_packets().await;
// game client
let local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), client_port);
tracing::info!(address = %local_addr, "Sending hello");
socket.send_to(b"hello", &local_addr).await.unwrap();
// since the debug filter doesn't change the data, it should be exactly the same
let value = timeout(Duration::from_millis(500), recv_chan.recv())
.await
.unwrap()
.unwrap();
assert_eq!("hello", value);
}