| 750251b2 Andy McCall ↶ |
1 |
|
| 2 |
|
| 2b5ee371 Andy McCall ↶ |
3 |
|
| 4 |
|
| 5 |
|
| 750251b2 Andy McCall ↶ |
6 |
|
| 7 |
|
| 8 |
|
| e02bf0cd Andy McCall ↶ |
9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 750251b2 Andy McCall ↶ |
13 |
|
| 51d4862a Andy McCall ↶ |
14 |
pub mod witness; |
| 15 |
|
| 750251b2 Andy McCall ↶ |
16 |
use axum::body::Bytes; |
| 17 |
use axum::extract::{Path, State}; |
| e02bf0cd Andy McCall ↶ |
18 |
use axum::http::{header, HeaderMap, Method, StatusCode}; |
| 750251b2 Andy McCall ↶ |
19 |
use axum::response::{IntoResponse, Response}; |
| e02bf0cd Andy McCall ↶ |
20 |
use axum::routing::any; |
| 750251b2 Andy McCall ↶ |
21 |
use axum::Router; |
| 22 |
use oxy_core::protocol::{wire, CONTENT_TYPE}; |
| 23 |
use oxy_core::repo::Repo; |
| 24 |
use std::collections::HashMap; |
| 25 |
use std::path::PathBuf; |
| 26 |
use std::sync::{Arc, Mutex}; |
| 27 |
|
| 28 |
#[derive(Clone, Debug)] |
| 29 |
pub struct ServerConfig { |
| 30 |
|
| 31 |
pub root: PathBuf, |
| 32 |
|
| 33 |
pub tokens: Vec<String>, |
| 2b5ee371 Andy McCall ↶ |
34 |
|
| 35 |
pub tls: Option<TlsFiles>, |
| 750251b2 Andy McCall ↶ |
36 |
} |
| 37 |
|
| 2b5ee371 Andy McCall ↶ |
38 |
#[derive(Clone, Debug)] |
| 39 |
pub struct TlsFiles { |
| 40 |
pub cert: PathBuf, |
| 41 |
pub key: PathBuf, |
| 42 |
} |
| 43 |
|
| e02bf0cd Andy McCall ↶ |
44 |
|
| 45 |
|
| 46 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 47 |
pub enum Access { |
| 48 |
Read, |
| 49 |
Push, |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub trait Backend: Send + Sync + 'static { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
fn open(&self, name: &str) -> Result<Repo, Failure>; |
| 60 |
|
| 61 |
|
| 62 |
fn authorize(&self, name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure>; |
| 63 |
|
| 64 |
|
| 7aa2dc93 Andy McCall ↶ |
65 |
|
| 66 |
|
| 76f75250 Andy McCall ↶ |
67 |
fn after_push(&self, _name: &str, _report: &oxy_core::repo::PushReport, _headers: &HeaderMap) {} |
| f8c69576 Andy McCall ↶ |
68 |
|
| 642fa213 Andy McCall ↶ |
69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
fn push_notes( |
| 76 |
&self, |
| 77 |
_name: &str, |
| 78 |
_report: &oxy_core::repo::PushReport, |
| 79 |
_headers: &HeaderMap, |
| 80 |
) -> Vec<(String, String)> { |
| 81 |
Vec::new() |
| 82 |
} |
| 83 |
|
| 67a4fe1a Andy McCall ↶ |
84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
fn vet_push( |
| 95 |
&self, |
| 96 |
_name: &str, |
| 97 |
_repo: &Repo, |
| 98 |
_bundle: &oxy_core::protocol::Bundle, |
| 99 |
_updates: &[oxy_core::protocol::RefUpdate], |
| 100 |
_headers: &HeaderMap, |
| 101 |
) -> Result<(), Failure> { |
| 102 |
Ok(()) |
| 103 |
} |
| 104 |
|
| 4b9d0677 Andy McCall ↶ |
105 |
|
| 106 |
|
| 107 |
|
| 108 |
fn add_log(&self, _name: &str, _refs: &mut oxy_core::protocol::RefsResponse) {} |
| 109 |
|
| ee130dd9 Andy McCall ↶ |
110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
fn adjust_refs( |
| 117 |
&self, |
| 118 |
_name: &str, |
| 119 |
repo: &Repo, |
| 120 |
refs: &mut oxy_core::protocol::RefsResponse, |
| 121 |
headers: &HeaderMap, |
| 122 |
) { |
| 123 |
tailor_refs(repo, refs, headers); |
| 124 |
} |
| 125 |
|
| b63fc7a7 Andy McCall ↶ |
126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
fn vet_tag_withdrawal(&self, _name: &str, _tag: &str) -> Result<(), String> { |
| 132 |
Ok(()) |
| 133 |
} |
| 134 |
|
| f8c69576 Andy McCall ↶ |
135 |
|
| 136 |
|
| 137 |
fn default_bookmark(&self, _name: &str) -> Option<String> { |
| 138 |
None |
| 139 |
} |
| e02bf0cd Andy McCall ↶ |
140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
pub struct Server { |
| 145 |
backend: Box<dyn Backend>, |
| 750251b2 Andy McCall ↶ |
146 |
locks: Mutex<HashMap<String, Arc<Mutex<()>>>>, |
| 147 |
} |
| 148 |
|
| e02bf0cd Andy McCall ↶ |
149 |
|
| 150 |
#[derive(Debug)] |
| 151 |
pub struct Failure(pub StatusCode, pub String); |
| 750251b2 Andy McCall ↶ |
152 |
|
| 153 |
impl IntoResponse for Failure { |
| 154 |
fn into_response(self) -> Response { |
| 155 |
(self.0, self.1).into_response() |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
impl From<oxy_core::repo::RepoError> for Failure { |
| 160 |
fn from(e: oxy_core::repo::RepoError) -> Failure { |
| be451356 Andy McCall ↶ |
161 |
use oxy_core::repo::RepoError; |
| 162 |
|
| 163 |
let status = match &e { |
| 164 |
RepoError::Io(_) | RepoError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 165 |
_ => StatusCode::BAD_REQUEST, |
| 166 |
}; |
| 167 |
Failure(status, e.to_string()) |
| 750251b2 Andy McCall ↶ |
168 |
} |
| 169 |
} |
| 170 |
|
| e02bf0cd Andy McCall ↶ |
171 |
|
| 172 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 173 |
pub enum Endpoint { |
| 174 |
Capabilities, |
| 175 |
Refs, |
| 176 |
Negotiate, |
| 177 |
Fetch, |
| 178 |
Objects, |
| 179 |
Checkpoint, |
| 80b069fa Andy McCall ↶ |
180 |
CheckpointIndex, |
| e02bf0cd Andy McCall ↶ |
181 |
Shallow, |
| 182 |
Push, |
| 183 |
} |
| 184 |
|
| 185 |
impl Endpoint { |
| 186 |
pub fn parse(name: &str) -> Option<Endpoint> { |
| 187 |
Some(match name { |
| 188 |
"capabilities" => Endpoint::Capabilities, |
| 189 |
"refs" => Endpoint::Refs, |
| 190 |
"negotiate" => Endpoint::Negotiate, |
| 191 |
"fetch" => Endpoint::Fetch, |
| 192 |
"objects" => Endpoint::Objects, |
| 193 |
"checkpoint" => Endpoint::Checkpoint, |
| 80b069fa Andy McCall ↶ |
194 |
"checkpoint-index" => Endpoint::CheckpointIndex, |
| e02bf0cd Andy McCall ↶ |
195 |
"shallow" => Endpoint::Shallow, |
| 196 |
"push" => Endpoint::Push, |
| 197 |
_ => return None, |
| 198 |
}) |
| 199 |
} |
| 200 |
|
| 201 |
pub fn name(self) -> &'static str { |
| 202 |
match self { |
| 203 |
Endpoint::Capabilities => "capabilities", |
| 204 |
Endpoint::Refs => "refs", |
| 205 |
Endpoint::Negotiate => "negotiate", |
| 206 |
Endpoint::Fetch => "fetch", |
| 207 |
Endpoint::Objects => "objects", |
| 208 |
Endpoint::Checkpoint => "checkpoint", |
| 80b069fa Andy McCall ↶ |
209 |
Endpoint::CheckpointIndex => "checkpoint-index", |
| e02bf0cd Andy McCall ↶ |
210 |
Endpoint::Shallow => "shallow", |
| 211 |
Endpoint::Push => "push", |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
pub fn method(self) -> Method { |
| 217 |
match self { |
| 218 |
Endpoint::Capabilities | Endpoint::Refs => Method::GET, |
| 219 |
_ => Method::POST, |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
fn access(self) -> Access { |
| 224 |
match self { |
| 225 |
Endpoint::Push => Access::Push, |
| 226 |
_ => Access::Read, |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 750251b2 Andy McCall ↶ |
231 |
fn ok(body: Vec<u8>) -> Response { |
| 232 |
([(header::CONTENT_TYPE, CONTENT_TYPE)], body).into_response() |
| 233 |
} |
| 234 |
|
| e02bf0cd Andy McCall ↶ |
235 |
pub fn valid_repo_name(name: &str) -> bool { |
| 750251b2 Andy McCall ↶ |
236 |
!name.is_empty() |
| 237 |
&& !name.starts_with('.') |
| 75167672 Andy McCall |
238 |
&& name |
| 239 |
.chars() |
| 240 |
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-')) |
| 750251b2 Andy McCall ↶ |
241 |
} |
| 242 |
|
| e02bf0cd Andy McCall ↶ |
243 |
impl Server { |
| 244 |
pub fn new(backend: impl Backend) -> Arc<Server> { |
| 75167672 Andy McCall |
245 |
Arc::new(Server { |
| 246 |
backend: Box::new(backend), |
| 247 |
locks: Mutex::new(HashMap::new()), |
| 248 |
}) |
| e02bf0cd Andy McCall ↶ |
249 |
} |
| 250 |
|
| 251 |
pub fn backend(&self) -> &dyn Backend { |
| 252 |
self.backend.as_ref() |
| 253 |
} |
| 254 |
|
| 255 |
fn lock_for(&self, name: &str) -> Arc<Mutex<()>> { |
| 75167672 Andy McCall |
256 |
self.locks |
| 257 |
.lock() |
| 258 |
.unwrap() |
| 259 |
.entry(name.to_string()) |
| 260 |
.or_default() |
| 261 |
.clone() |
| e02bf0cd Andy McCall ↶ |
262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 75167672 Andy McCall |
266 |
pub async fn handle( |
| 267 |
self: &Arc<Self>, |
| 268 |
name: String, |
| 269 |
endpoint: Endpoint, |
| 270 |
method: &Method, |
| 271 |
headers: &HeaderMap, |
| 272 |
body: Bytes, |
| 273 |
) -> Response { |
| e02bf0cd Andy McCall ↶ |
274 |
if *method != endpoint.method() { |
| 75167672 Andy McCall |
275 |
return Failure( |
| 276 |
StatusCode::METHOD_NOT_ALLOWED, |
| 277 |
format!("{} is {}", endpoint.name(), endpoint.method()), |
| 278 |
) |
| 279 |
.into_response(); |
| e02bf0cd Andy McCall ↶ |
280 |
} |
| 281 |
if let Err(f) = self.backend.authorize(&name, endpoint.access(), headers) { |
| 282 |
return f.into_response(); |
| 283 |
} |
| 75167672 Andy McCall |
284 |
let claimed = headers |
| 285 |
.get(oxy_core::protocol::REPO_ID_HEADER) |
| 286 |
.and_then(|v| v.to_str().ok()) |
| 287 |
.and_then(hex_id); |
| e02bf0cd Andy McCall ↶ |
288 |
let server = Arc::clone(self); |
| 76f75250 Andy McCall ↶ |
289 |
let request_headers = headers.clone(); |
| be451356 Andy McCall ↶ |
290 |
let repo = name.clone(); |
| be0161a8 Andy McCall ↶ |
291 |
let result = tokio::task::spawn_blocking(move || { |
| be451356 Andy McCall ↶ |
292 |
server.respond(&repo, endpoint, body, claimed, &request_headers) |
| be0161a8 Andy McCall ↶ |
293 |
}) |
| 294 |
.await; |
| be451356 Andy McCall ↶ |
295 |
let failure = match result { |
| 296 |
Ok(Ok(r)) => return r, |
| 297 |
Ok(Err(f)) => f, |
| 298 |
Err(e) => Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), |
| 299 |
}; |
| 300 |
|
| 301 |
let f = Failure( |
| 302 |
failure.0, |
| 303 |
format!("{} of {name}: {}", endpoint.name(), failure.1), |
| 304 |
); |
| 305 |
eprintln!("oxy-server: {}: {}", f.0.as_u16(), f.1); |
| 306 |
f.into_response() |
| e02bf0cd Andy McCall ↶ |
307 |
} |
| 308 |
|
| 05e71ed6 Andy McCall ↶ |
309 |
fn respond( |
| 310 |
&self, |
| 311 |
name: &str, |
| 312 |
endpoint: Endpoint, |
| 313 |
body: Bytes, |
| 314 |
claimed: Option<[u8; 16]>, |
| 76f75250 Andy McCall ↶ |
315 |
headers: &HeaderMap, |
| 05e71ed6 Andy McCall ↶ |
316 |
) -> Result<Response, Failure> { |
| e02bf0cd Andy McCall ↶ |
317 |
let r = self.backend.open(name)?; |
| 318 |
Ok(match endpoint { |
| 319 |
Endpoint::Capabilities => ok(wire::encode_capabilities(r.id()?)), |
| 14edbaaf Andy McCall ↶ |
320 |
Endpoint::Refs => { |
| 321 |
let mut refs = r.remote_refs()?; |
| 322 |
if let Some(d) = self.backend.default_bookmark(name) { |
| 323 |
refs.default_bookmark = Some(d); |
| 324 |
} |
| 4b9d0677 Andy McCall ↶ |
325 |
self.backend.add_log(name, &mut refs); |
| ee130dd9 Andy McCall ↶ |
326 |
self.backend.adjust_refs(name, &r, &mut refs, headers); |
| 14edbaaf Andy McCall ↶ |
327 |
ok(wire::encode_refs(r.id()?, &refs)) |
| 328 |
} |
| e02bf0cd Andy McCall ↶ |
329 |
Endpoint::Negotiate => { |
| 330 |
let (have, want) = wire::decode_negotiate(&body)?; |
| 331 |
ok(wire::encode_hashes(&r.negotiate(&have, &want)?)) |
| 332 |
} |
| 333 |
Endpoint::Fetch => { |
| 334 |
let (changes, max_blob) = wire::decode_fetch(&body)?; |
| 75167672 Andy McCall |
335 |
ok(wire::encode_bundle( |
| 336 |
&r.bundle_for_filtered(&changes, max_blob)?, |
| 337 |
)) |
| e02bf0cd Andy McCall ↶ |
338 |
} |
| 339 |
Endpoint::Objects => { |
| 340 |
let refs = wire::decode_hashes(&body)?; |
| 75167672 Andy McCall |
341 |
let bundle = oxy_core::protocol::Bundle { |
| 342 |
objects: r.objects_for(&refs)?, |
| 343 |
..Default::default() |
| 344 |
}; |
| e02bf0cd Andy McCall ↶ |
345 |
ok(wire::encode_bundle(&bundle)) |
| 346 |
} |
| 347 |
Endpoint::Checkpoint => { |
| 348 |
let heads = wire::decode_hashes(&body)?; |
| 349 |
let lock = self.lock_for(name); |
| 350 |
let _guard = lock.lock().unwrap(); |
| f3811eb2 Andy McCall ↶ |
351 |
let bundle = r.checkpoint_bundle_for(&heads)?; |
| 352 |
let _t = oxy_core::timing::phase("serve: encode the checkpoint"); |
| 353 |
ok(wire::encode_bundle(&bundle)) |
| e02bf0cd Andy McCall ↶ |
354 |
} |
| 80b069fa Andy McCall ↶ |
355 |
Endpoint::CheckpointIndex => { |
| 356 |
let heads = wire::decode_hashes(&body)?; |
| 357 |
let lock = self.lock_for(name); |
| 358 |
let _guard = lock.lock().unwrap(); |
| 359 |
ok(wire::encode_bundle(&r.checkpoint_index_for(&heads)?)) |
| 360 |
} |
| e02bf0cd Andy McCall ↶ |
361 |
Endpoint::Shallow => { |
| 362 |
let (heads, depth) = wire::decode_shallow(&body)?; |
| 363 |
ok(wire::encode_hashes(&r.shallow_changes(&heads, depth)?)) |
| 364 |
} |
| 365 |
Endpoint::Push => { |
| 366 |
let (bundle, updates, policy, tags) = wire::decode_push(&body)?; |
| 67a4fe1a Andy McCall ↶ |
367 |
|
| 368 |
|
| 369 |
self.backend |
| 370 |
.vet_push(name, &r, &bundle, &updates, headers)?; |
| e02bf0cd Andy McCall ↶ |
371 |
let lock = self.lock_for(name); |
| 372 |
let _guard = lock.lock().unwrap(); |
| 75167672 Andy McCall |
373 |
if r.is_unbound() { |
| 374 |
|
| 375 |
match claimed { |
| 376 |
Some(id) => r.bind_id(id)?, |
| 377 |
None => { |
| 378 |
return Err(Failure( |
| 379 |
StatusCode::BAD_REQUEST, |
| 05e71ed6 Andy McCall ↶ |
380 |
format!( |
| 381 |
"this repository is unbound; push with a client that sends {}", |
| 382 |
oxy_core::protocol::REPO_ID_HEADER |
| 383 |
), |
| 75167672 Andy McCall |
384 |
)) |
| 385 |
} |
| 386 |
} |
| f8c69576 Andy McCall ↶ |
387 |
} |
| 388 |
let protected = self.backend.default_bookmark(name); |
| 389 |
let (refused, updates): (Vec<_>, Vec<_>) = updates.into_iter().partition(|u| { |
| 390 |
u.new_state.is_zero() && protected.as_deref() == Some(u.name.as_str()) |
| 391 |
}); |
| b63fc7a7 Andy McCall ↶ |
392 |
let mut kept_tags = Vec::new(); |
| 393 |
let mut withdrawals_refused = Vec::new(); |
| 394 |
for t in tags { |
| 395 |
match t |
| 396 |
.is_withdrawal() |
| 397 |
.then(|| self.backend.vet_tag_withdrawal(name, &t.name)) |
| 398 |
{ |
| 399 |
Some(Err(reason)) => withdrawals_refused.push((t.name, reason)), |
| 400 |
_ => kept_tags.push(t), |
| 401 |
} |
| 402 |
} |
| 403 |
let mut report = r.receive_push_report(bundle, updates, policy, kept_tags)?; |
| cb8b8082 Andy McCall ↶ |
404 |
|
| 405 |
|
| b63fc7a7 Andy McCall ↶ |
406 |
if !refused.is_empty() || !withdrawals_refused.is_empty() { |
| cb8b8082 Andy McCall ↶ |
407 |
report |
| 408 |
.results |
| 409 |
.retain(|r| r.name != oxy_core::repo::OBJECTS_RESULT); |
| 410 |
} |
| f8c69576 Andy McCall ↶ |
411 |
for u in refused { |
| 412 |
report.results.push(oxy_core::protocol::RefResult { |
| 413 |
name: u.name.clone(), |
| 414 |
ok: false, |
| 415 |
reason: format!( |
| 416 |
"{:?} is this repository's default bookmark and cannot be deleted", |
| 417 |
u.name |
| 418 |
), |
| b63fc7a7 Andy McCall ↶ |
419 |
}); |
| 420 |
} |
| 421 |
for (tag, reason) in withdrawals_refused { |
| 422 |
report.results.push(oxy_core::protocol::RefResult { |
| 423 |
name: format!("tags/{tag}"), |
| 424 |
ok: false, |
| 425 |
reason, |
| f8c69576 Andy McCall ↶ |
426 |
}); |
| be451356 Andy McCall ↶ |
427 |
} |
| 428 |
for r in report.results.iter().filter(|r| !r.ok) { |
| 429 |
eprintln!( |
| 430 |
"oxy-server: push to {name}: {} refused: {}", |
| 431 |
r.name, r.reason |
| 432 |
); |
| 642fa213 Andy McCall ↶ |
433 |
} |
| 434 |
for (reference, note) in self.backend.push_notes(name, &report, headers) { |
| 435 |
if let Some(r) = report |
| 436 |
.results |
| 437 |
.iter_mut() |
| 438 |
.find(|r| r.ok && r.name == reference) |
| 439 |
{ |
| 440 |
r.reason = if r.reason.is_empty() { |
| 441 |
note |
| 442 |
} else { |
| 443 |
format!("{}; {note}", r.reason) |
| 444 |
}; |
| 445 |
} |
| 75167672 Andy McCall |
446 |
} |
| 76f75250 Andy McCall ↶ |
447 |
self.backend.after_push(name, &report, headers); |
| 7aa2dc93 Andy McCall ↶ |
448 |
ok(wire::encode_push_response(&report.results)) |
| e02bf0cd Andy McCall ↶ |
449 |
} |
| 450 |
}) |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
pub struct DirBackend { |
| 457 |
pub root: PathBuf, |
| 458 |
pub tokens: Vec<String>, |
| 459 |
} |
| 460 |
|
| 461 |
impl Backend for DirBackend { |
| 750251b2 Andy McCall ↶ |
462 |
fn open(&self, name: &str) -> Result<Repo, Failure> { |
| 463 |
if !valid_repo_name(name) { |
| 464 |
return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 465 |
} |
| e02bf0cd Andy McCall ↶ |
466 |
let dir = self.root.join(name); |
| 750251b2 Andy McCall ↶ |
467 |
if !dir.join(".oxy").is_dir() { |
| 468 |
return Err(Failure(StatusCode::NOT_FOUND, "no such repository".into())); |
| 469 |
} |
| 470 |
Repo::open(&dir).map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())) |
| 471 |
} |
| 472 |
|
| 8c30d814 Andy McCall ↶ |
473 |
fn default_bookmark(&self, name: &str) -> Option<String> { |
| 474 |
self.open(name).ok().map(|r| r.config.bookmark.clone()) |
| 475 |
} |
| 476 |
|
| e02bf0cd Andy McCall ↶ |
477 |
fn authorize(&self, _name: &str, access: Access, headers: &HeaderMap) -> Result<(), Failure> { |
| 478 |
if access == Access::Read || self.tokens.is_empty() { |
| 750251b2 Andy McCall ↶ |
479 |
return Ok(()); |
| 480 |
} |
| e02bf0cd Andy McCall ↶ |
481 |
match bearer_token(headers) { |
| 482 |
Some(t) if self.tokens.iter().any(|x| x == t) => Ok(()), |
| 75167672 Andy McCall |
483 |
_ => Err(Failure( |
| 484 |
StatusCode::UNAUTHORIZED, |
| 485 |
"a valid bearer token is required to push".into(), |
| 486 |
)), |
| 750251b2 Andy McCall ↶ |
487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 75167672 Andy McCall |
491 |
fn hex_id(h: &str) -> Option<[u8; 16]> { |
| 492 |
let h = h.trim(); |
| 493 |
if h.len() != 32 { |
| 494 |
return None; |
| 495 |
} |
| 496 |
let mut out = [0u8; 16]; |
| 497 |
for (i, byte) in out.iter_mut().enumerate() { |
| 498 |
*byte = u8::from_str_radix(&h[2 * i..2 * i + 2], 16).ok()?; |
| 499 |
} |
| 500 |
Some(out) |
| ee130dd9 Andy McCall ↶ |
501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
pub fn client_features(headers: &HeaderMap) -> Option<&str> { |
| 506 |
headers |
| 507 |
.get(oxy_core::protocol::FEATURES_HEADER) |
| 508 |
.and_then(|v| v.to_str().ok()) |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
pub fn tailor_refs(repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) { |
| 514 |
repo.refs_for_client(refs, client_features(headers)); |
| 75167672 Andy McCall |
515 |
} |
| 516 |
|
| e02bf0cd Andy McCall ↶ |
517 |
|
| 518 |
pub fn bearer_token(headers: &HeaderMap) -> Option<&str> { |
| 75167672 Andy McCall |
519 |
headers |
| 520 |
.get(header::AUTHORIZATION) |
| 521 |
.and_then(|v| v.to_str().ok()) |
| 522 |
.and_then(|v| v.strip_prefix("Bearer ")) |
| 523 |
.map(str::trim) |
| b3c5a59a Andy McCall ↶ |
524 |
} |
| 525 |
|
| 75167672 Andy McCall |
526 |
async fn dispatch( |
| 527 |
State(server): State<Arc<Server>>, |
| 528 |
Path((repo, endpoint)): Path<(String, String)>, |
| 529 |
method: Method, |
| 530 |
headers: HeaderMap, |
| 531 |
body: Bytes, |
| 532 |
) -> Response { |
| e02bf0cd Andy McCall ↶ |
533 |
match Endpoint::parse(&endpoint) { |
| 534 |
Some(e) => server.handle(repo, e, &method, &headers, body).await, |
| 535 |
None => Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(), |
| 536 |
} |
| 750251b2 Andy McCall ↶ |
537 |
} |
| 538 |
|
| e30c081a Andy McCall ↶ |
539 |
|
| 540 |
|
| 541 |
|
| 542 |
pub const BODY_LIMIT: usize = if usize::BITS > 32 { |
| 543 |
4 << 30 |
| 544 |
} else { |
| 545 |
usize::MAX |
| 546 |
}; |
| 547 |
|
| e02bf0cd Andy McCall ↶ |
548 |
|
| 750251b2 Andy McCall ↶ |
549 |
pub fn router(cfg: ServerConfig) -> Router { |
| 75167672 Andy McCall |
550 |
let server = Server::new(DirBackend { |
| 551 |
root: cfg.root, |
| 552 |
tokens: cfg.tokens, |
| 553 |
}); |
| e30c081a Andy McCall ↶ |
554 |
|
| 555 |
|
| 75167672 Andy McCall |
556 |
Router::new() |
| 557 |
.route("/{repo}/oxy/v1/{endpoint}", any(dispatch)) |
| e30c081a Andy McCall ↶ |
558 |
.layer(axum::extract::DefaultBodyLimit::max(BODY_LIMIT)) |
| 75167672 Andy McCall |
559 |
.with_state(server) |
| 750251b2 Andy McCall ↶ |
560 |
} |
| 561 |
|
| 2b5ee371 Andy McCall ↶ |
562 |
|
| 563 |
|
| 750251b2 Andy McCall ↶ |
564 |
pub async fn serve(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 565 |
listener.set_nonblocking(true)?; |
| 2b5ee371 Andy McCall ↶ |
566 |
match cfg.tls.clone() { |
| 567 |
Some(tls) => { |
| 568 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 75167672 Andy McCall |
569 |
let rustls_cfg = |
| 570 |
axum_server::tls_rustls::RustlsConfig::from_pem_file(&tls.cert, &tls.key) |
| 571 |
.await |
| 572 |
.map_err(|e| std::io::Error::other(format!("loading TLS files: {e}")))?; |
| 573 |
axum_server::from_tcp_rustls(listener, rustls_cfg)? |
| 574 |
.serve(router(cfg).into_make_service()) |
| 2b5ee371 Andy McCall ↶ |
575 |
.await |
| 576 |
} |
| 577 |
None => { |
| 578 |
let listener = tokio::net::TcpListener::from_std(listener)?; |
| 579 |
axum::serve(listener, router(cfg)).await |
| 580 |
} |
| 581 |
} |
| 750251b2 Andy McCall ↶ |
582 |
} |
| 583 |
|
| 584 |
|
| 585 |
pub fn run_blocking(listener: std::net::TcpListener, cfg: ServerConfig) -> std::io::Result<()> { |
| 75167672 Andy McCall |
586 |
let rt = tokio::runtime::Builder::new_multi_thread() |
| 587 |
.enable_all() |
| 588 |
.build()?; |
| 750251b2 Andy McCall ↶ |
589 |
rt.block_on(serve(listener, cfg)) |
| 590 |
} |