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
use super::EventLoop;
use crate::types::parameters::AllowedUpdates;
use hyper::{
body::{Body, HttpBody},
Method, Request, Response,
};
use std::{
net::{IpAddr, Ipv4Addr},
num::NonZeroU32,
sync::Arc,
time::Duration,
};
mod http;
pub mod https;
pub use http::Http;
pub use https::Https;
#[must_use = "webhook does not start unless `start` is called"]
pub struct Webhook<'a> {
event_loop: EventLoop,
bind_to: IpAddr,
port: u16,
request_timeout: Duration,
updates_url: String,
url: &'a str,
ip_address: Option<IpAddr>,
certificate: Option<&'a str>,
max_connections: Option<NonZeroU32>,
allowed_updates: Option<AllowedUpdates>,
drop_pending_updates: bool,
}
impl<'a> Webhook<'a> {
pub(crate) fn new(event_loop: EventLoop, url: &'a str, port: u16) -> Self {
Self {
event_loop,
bind_to: IpAddr::V4(Ipv4Addr::LOCALHOST),
port,
request_timeout: Duration::from_secs(60),
updates_url: String::from("/"),
url,
ip_address: None,
certificate: None,
max_connections: None,
allowed_updates: None,
drop_pending_updates: false,
}
}
pub const fn bind_to(mut self, bind_to: IpAddr) -> Self {
self.bind_to = bind_to;
self
}
pub fn accept_updates_on(mut self, url: String) -> Self {
if !url.starts_with('/') {
panic!(
"[tbot] `Webhook::accept_connections_to` takes URLs starting \
only with `/`"
);
}
self.updates_url = url;
self
}
pub fn ip_address(mut self, ip_address: impl Into<IpAddr>) -> Self {
self.ip_address = Some(ip_address.into());
self
}
pub const fn certificate(mut self, certificate: &'a str) -> Self {
self.certificate = Some(certificate);
self
}
pub const fn max_connections(mut self, max: NonZeroU32) -> Self {
self.max_connections = Some(max);
self
}
pub const fn allowed_updates(mut self, updates: AllowedUpdates) -> Self {
self.allowed_updates = Some(updates);
self
}
pub const fn drop_pending_updates(mut self) -> Self {
self.drop_pending_updates = true;
self
}
pub const fn request_timeout(mut self, timeout: Duration) -> Self {
self.request_timeout = timeout;
self
}
pub const fn http(self) -> Http<'a> {
Http::new(self)
}
pub const fn https(
self,
#[cfg(feature = "tls")] identity: https::Identity,
#[cfg(feature = "rustls")] config: https::ServerConfig,
) -> Https<'a> {
Https::new(
self,
#[cfg(feature = "tls")]
identity,
#[cfg(feature = "rustls")]
config,
)
}
}
fn is_request_correct(request: &Request<Body>, updates_url: &str) -> bool {
let content_type = request.headers().get("Content-Type");
request.method() == Method::POST
&& request.uri() == updates_url
&& content_type.map(|x| x == "application/json") == Some(true)
}
async fn handle(
event_loop: Arc<EventLoop>,
request: Request<Body>,
updates_url: Arc<String>,
) -> Result<Response<Body>, hyper::Error> {
if is_request_correct(&request, &updates_url) {
let (parts, mut body) = request.into_parts();
let mut request = parts
.headers
.get("Content-Length")
.and_then(|x| x.to_str().ok().and_then(|x| x.parse().ok()))
.map_or_else(Vec::new, Vec::with_capacity);
while let Some(chunk) = body.data().await {
request.extend(chunk?);
}
match serde_json::from_slice(&request[..]) {
Ok(update) => event_loop.handle_update(update),
Err(error) => eprintln!(
"[tbot] Failed to parse an update: {error:?}. `tbot` will skip \
it, but this error means that `tbot`'s type deserialization \
doesn't match the Bot API. You should file an issue at \
https://gitlab.com/SnejUgal/tbot.",
),
}
}
Ok(Response::new(Body::empty()))
}