forked from cloudflare/pingora
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_l4.rs
More file actions
169 lines (144 loc) · 4.84 KB
/
virtual_l4.rs
File metadata and controls
169 lines (144 loc) · 4.84 KB
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
//! This example demonstrates to how to implement a custom L4 connector
//! together with a virtual socket.
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;
use async_trait::async_trait;
use pingora_core::connectors::L4Connect;
use pingora_core::prelude::HttpPeer;
use pingora_core::protocols::l4::socket::SocketAddr as L4SocketAddr;
use pingora_core::protocols::l4::stream::Stream;
use pingora_core::protocols::l4::virt::{VirtualSocket, VirtualSocketStream};
use pingora_core::server::RunArgs;
use pingora_core::server::{configuration::ServerConf, Server};
use pingora_core::services::listening::Service;
use pingora_core::upstreams::peer::PeerOptions;
use pingora_error::Result;
use pingora_proxy::{http_proxy_service_with_name, prelude::*, HttpProxy, ProxyHttp};
use tokio::io::{AsyncRead, AsyncWrite};
/// Static virtual socket that serves a single HTTP request with a static response.
///
/// In real world use cases you would implement [`VirtualSocket`] for streams
/// that implement `AsyncRead + AsyncWrite`.
#[derive(Debug)]
struct StaticVirtualSocket {
content: Vec<u8>,
read_pos: usize,
}
impl StaticVirtualSocket {
fn new() -> Self {
let response = b"HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!";
Self {
content: response.to_vec(),
read_pos: 0,
}
}
}
impl AsyncRead for StaticVirtualSocket {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
debug_assert!(self.read_pos <= self.content.len());
let remaining = self.content.len() - self.read_pos;
if remaining == 0 {
return std::task::Poll::Ready(Ok(()));
}
let to_read = std::cmp::min(remaining, buf.remaining());
buf.put_slice(&self.content[self.read_pos..self.read_pos + to_read]);
self.read_pos += to_read;
std::task::Poll::Ready(Ok(()))
}
}
impl AsyncWrite for StaticVirtualSocket {
fn poll_write(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> std::task::Poll<std::io::Result<usize>> {
// Discard all writes
std::task::Poll::Ready(Ok(buf.len()))
}
fn poll_flush(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::io::Result<()>> {
std::task::Poll::Ready(Ok(()))
}
fn poll_shutdown(
self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::io::Result<()>> {
std::task::Poll::Ready(Ok(()))
}
}
impl VirtualSocket for StaticVirtualSocket {
fn set_socket_option(
&self,
_opt: pingora_core::protocols::l4::virt::VirtualSockOpt,
) -> std::io::Result<()> {
Ok(())
}
}
#[derive(Debug)]
struct VirtualConnector;
#[async_trait]
impl L4Connect for VirtualConnector {
async fn connect(&self, _addr: &L4SocketAddr) -> pingora_error::Result<Stream> {
Ok(Stream::from(VirtualSocketStream::new(Box::new(
StaticVirtualSocket::new(),
))))
}
}
struct VirtualProxy {
connector: Arc<dyn L4Connect + Send + Sync>,
}
impl VirtualProxy {
fn new() -> Self {
Self {
connector: Arc::new(VirtualConnector),
}
}
}
#[async_trait::async_trait]
impl ProxyHttp for VirtualProxy {
type CTX = ();
fn new_ctx(&self) -> Self::CTX {}
// Route everything to example.org unless the Host header is "virtual.test",
// in which case target the special virtual address 203.0.113.1:18080.
async fn upstream_peer(
&self,
_session: &mut Session,
_ctx: &mut Self::CTX,
) -> Result<Box<pingora_core::upstreams::peer::HttpPeer>> {
let mut options = PeerOptions::new();
options.custom_l4 = Some(self.connector.clone());
Ok(Box::new(HttpPeer {
_address: L4SocketAddr::Inet(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1)),
80,
)),
scheme: pingora_core::upstreams::peer::Scheme::HTTP,
sni: "example.org".to_string(),
proxy: None,
client_cert_key: None,
group_key: 0,
options,
}))
}
}
fn main() {
// Minimal server config
let conf = Arc::new(ServerConf::default());
// Build the service and set the default L4 connector
let mut svc: Service<HttpProxy<VirtualProxy>> =
http_proxy_service_with_name(&conf, VirtualProxy::new(), "virtual-proxy");
// Listen
let addr = "127.0.0.1:6196";
svc.add_tcp(addr);
let mut server = Server::new(None).unwrap();
server.add_service(svc);
let run = RunArgs::default();
eprintln!("Listening on {addr}, try: curl http://{addr}/");
server.run(run);
}