Repository navigation
Expand file tree
/
Copy pathopencode_service.rs
More file actions
638 lines (605 loc) · 22.5 KB
/
Copy pathopencode_service.rs
File metadata and controls
638 lines (605 loc) · 22.5 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! The running OpenCode service, reached without starting anything: its
//! owner-only registration names a loopback URL and password, `/api/info`
//! confirms the registered process still answers, and requests are plain
//! authenticated HTTP over `TcpStream`. Hex never runs an OpenCode
//! executable; when no service answers, OpenCode isn't running.
use std::fs;
use std::io::{Read, Write};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use color_eyre::eyre::{Result, WrapErr, eyre};
use serde::Deserialize;
/// What features say when no service answers.
pub(crate) const NOT_RUNNING: &str = "OpenCode isn't running";
const MAX_REGISTRATION_BYTES: u64 = 64 * 1024;
const MAX_REGISTRATIONS: usize = 16;
const MAX_RESPONSE_BYTES: usize = 16 * 1024 * 1024;
const INFO_DEADLINE: Duration = Duration::from_secs(2);
const CONNECT_DEADLINE: Duration = Duration::from_secs(2);
/// How often a blocked read checks cancellation and the deadline.
const READ_SLICE: Duration = Duration::from_millis(50);
/// One running service: where it listens and how to authenticate.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Service {
address: SocketAddr,
password: Option<String>,
}
#[derive(Debug)]
pub(crate) struct Response {
pub(crate) status: u16,
pub(crate) body: Vec<u8>,
}
/// The running service, or `None` when OpenCode isn't running. Reads only
/// registration files and asks each registered service for `/api/info`.
pub(crate) fn find() -> Option<Service> {
find_in(&state_dir()?)
}
/// Whether a service answers now; a short loopback check for availability.
pub(crate) fn running() -> bool {
find().is_some()
}
/// OpenCode's state directory on every OS: `$XDG_STATE_HOME` when set, else
/// `~/.local/state`, then `opencode` (Windows included).
fn state_dir() -> Option<PathBuf> {
let root = std::env::var_os("XDG_STATE_HOME")
.filter(|value| !value.is_empty())
.map(PathBuf::from)
.or_else(|| dirs::home_dir().map(|home| home.join(".local").join("state")))?;
Some(root.join("opencode"))
}
/// `service.json` (the stable, beta and dev channels) first, then other
/// channels' `service-<channel>.json`; the first that answers wins.
fn find_in(state: &Path) -> Option<Service> {
let mut others: Vec<PathBuf> = fs::read_dir(state)
.ok()?
.flatten()
.take(512)
.map(|entry| entry.path())
.filter(|path| {
path.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.starts_with("service-") && name.ends_with(".json"))
})
.collect();
others.sort();
std::iter::once(state.join("service.json"))
.chain(others)
.take(MAX_REGISTRATIONS)
.filter_map(|path| read_registration(&path))
.find_map(|registration| confirm(®istration))
}
#[derive(Deserialize)]
struct Registration {
url: String,
pid: u32,
#[serde(default)]
version: Option<String>,
#[serde(default)]
password: Option<String>,
}
fn read_registration(path: &Path) -> Option<Registration> {
let mut file = open_private(path)?;
let mut bytes = Vec::new();
Read::by_ref(&mut file)
.take(MAX_REGISTRATION_BYTES + 1)
.read_to_end(&mut bytes)
.ok()?;
if bytes.len() as u64 > MAX_REGISTRATION_BYTES {
return None;
}
serde_json::from_slice(&bytes).ok()
}
/// A regular registration file this user owns that no one else can read.
#[cfg(unix)]
fn open_private(path: &Path) -> Option<fs::File> {
use std::os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt};
let file = fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK)
.open(path)
.ok()?;
let metadata = file.metadata().ok()?;
(metadata.is_file()
&& metadata.uid() == unsafe { libc::geteuid() }
&& metadata.permissions().mode() & 0o077 == 0)
.then_some(file)
}
/// The profile folder's ACL keeps it to this user on Windows.
#[cfg(windows)]
fn open_private(path: &Path) -> Option<fs::File> {
let file = fs::File::open(path).ok()?;
file.metadata().ok()?.is_file().then_some(file)
}
/// The service a registration names, when it is on loopback and the process
/// answering `/api/info` is the registered one.
fn confirm(registration: &Registration) -> Option<Service> {
let service = Service {
address: loopback_address(®istration.url)?,
password: registration
.password
.clone()
.filter(|password| !password.is_empty()),
};
#[derive(Deserialize)]
struct Info {
pid: u32,
version: String,
}
let response = service
.request(
"GET",
"/api/info",
None,
None,
INFO_DEADLINE,
&AtomicBool::new(false),
)
.ok()?;
if response.status != 200 {
return None;
}
let info: Info = serde_json::from_slice(&response.body).ok()?;
(info.pid == registration.pid
&& registration
.version
.as_ref()
.is_none_or(|version| *version == info.version))
.then_some(service)
}
/// A registered `http://` URL's loopback socket; an unspecified address
/// (`0.0.0.0`, `::`) is reached through loopback. Anything else is refused.
fn loopback_address(url: &str) -> Option<SocketAddr> {
let url = url::Url::parse(url).ok()?;
if url.scheme() != "http"
|| !url.username().is_empty()
|| url.password().is_some()
|| url.path() != "/"
|| url.query().is_some()
|| url.fragment().is_some()
{
return None;
}
let ip = match url.host()? {
url::Host::Domain("localhost") => IpAddr::V4(Ipv4Addr::LOCALHOST),
url::Host::Ipv4(ip) if ip.is_unspecified() => IpAddr::V4(Ipv4Addr::LOCALHOST),
url::Host::Ipv6(ip) if ip.is_unspecified() => IpAddr::V6(Ipv6Addr::LOCALHOST),
url::Host::Ipv4(ip) if ip.is_loopback() => IpAddr::V4(ip),
url::Host::Ipv6(ip) if ip.is_loopback() => IpAddr::V6(ip),
_ => return None,
};
let port = url.port_or_known_default().filter(|port| *port != 0)?;
Some(SocketAddr::new(ip, port))
}
impl Service {
/// One request. `directory` scopes it to an OpenCode workspace (created
/// when first used). HTTP error statuses return normally; transport
/// failures, cancellation and the deadline are errors.
pub(crate) fn request(
&self,
method: &str,
path: &str,
body: Option<&[u8]>,
directory: Option<&Path>,
deadline: Duration,
cancelled: &AtomicBool,
) -> Result<Response> {
let started = Instant::now();
let remaining = || deadline.saturating_sub(started.elapsed());
let check = |remaining: Duration| -> Result<()> {
if cancelled.load(Ordering::Acquire) {
return Err(eyre!("OpenCode {path} was cancelled"));
}
if remaining.is_zero() {
return Err(eyre!(
"OpenCode {path} exceeded {} seconds",
deadline.as_secs()
));
}
Ok(())
};
check(remaining())?;
let mut head = format!(
"{method} {path} HTTP/1.1\r\nHost: {}\r\nAccept: application/json\r\nConnection: close\r\n",
self.address
);
if let Some(password) = &self.password {
if password.contains(['\r', '\n']) {
return Err(eyre!("OpenCode registration has an invalid password"));
}
head.push_str(&format!(
"Authorization: Basic {}\r\n",
base64(format!("opencode:{password}").as_bytes())
));
}
if let Some(directory) = directory {
head.push_str(&format!(
"x-opencode-directory: {}\r\n",
encode_uri_component(&workspace(directory)?)
));
}
if let Some(body) = body {
head.push_str(&format!(
"Content-Type: application/json\r\nContent-Length: {}\r\n",
body.len()
));
} else if method != "GET" {
head.push_str("Content-Length: 0\r\n");
}
head.push_str("\r\n");
let mut stream =
TcpStream::connect_timeout(&self.address, remaining().min(CONNECT_DEADLINE))
.map_err(|_| eyre!(NOT_RUNNING))?;
stream.set_write_timeout(Some(remaining().max(Duration::from_millis(1))))?;
stream
.write_all(head.as_bytes())
.and_then(|()| stream.write_all(body.unwrap_or_default()))
.wrap_err_with(|| format!("could not send OpenCode {path}"))?;
let mut bytes = Vec::new();
let mut buffer = [0; 64 * 1024];
loop {
check(remaining())?;
stream.set_read_timeout(Some(
remaining().clamp(Duration::from_millis(1), READ_SLICE),
))?;
match stream.read(&mut buffer) {
Ok(0) => break,
Ok(read) => {
bytes.extend_from_slice(&buffer[..read]);
if bytes.len() > MAX_RESPONSE_BYTES {
return Err(eyre!("OpenCode {path} answered more than 16 MiB"));
}
}
Err(error)
if matches!(
error.kind(),
std::io::ErrorKind::WouldBlock
| std::io::ErrorKind::TimedOut
| std::io::ErrorKind::Interrupted
) => {}
Err(error) => {
return Err(error).wrap_err_with(|| format!("could not read OpenCode {path}"));
}
}
}
parse_response(bytes).ok_or_else(|| eyre!("OpenCode {path} returned invalid HTTP"))
}
}
/// Creates Hex's OpenCode workspace on first use and returns the path
/// OpenCode should see.
fn workspace(directory: &Path) -> Result<String> {
fs::create_dir_all(directory).wrap_err_with(|| {
format!(
"could not create OpenCode workspace {}",
directory.display()
)
})?;
let directory = directory.canonicalize().wrap_err_with(|| {
format!(
"could not resolve OpenCode workspace {}",
directory.display()
)
})?;
let directory = directory
.to_str()
.ok_or_else(|| eyre!("OpenCode workspace path is not valid UTF-8"))?;
// canonicalize returns a verbatim path on Windows; OpenCode wants the drive form.
Ok(directory
.strip_prefix(r"\\?\")
.unwrap_or(directory)
.to_owned())
}
fn parse_response(bytes: Vec<u8>) -> Option<Response> {
let split = bytes.windows(4).position(|window| window == b"\r\n\r\n")?;
let head = std::str::from_utf8(&bytes[..split]).ok()?;
let mut lines = head.split("\r\n");
let status = lines.next()?.split_whitespace().nth(1)?.parse().ok()?;
let mut length = None;
let mut chunked = false;
for line in lines {
let (key, value) = line.split_once(':')?;
let value = value.trim();
if key.eq_ignore_ascii_case("content-length") {
length = Some(value.parse::<usize>().ok()?);
} else if key.eq_ignore_ascii_case("transfer-encoding") {
chunked = value.eq_ignore_ascii_case("chunked");
}
}
let body = &bytes[split + 4..];
let body = if chunked {
dechunk(body)?
} else if let Some(length) = length {
body.get(..length)?.to_vec()
} else {
body.to_vec()
};
Some(Response { status, body })
}
fn dechunk(mut body: &[u8]) -> Option<Vec<u8>> {
let mut out = Vec::new();
loop {
let line = body.windows(2).position(|window| window == b"\r\n")?;
let size = std::str::from_utf8(&body[..line]).ok()?;
let size = size.split(';').next()?.trim();
let size = usize::from_str_radix(size, 16).ok()?;
body = &body[line + 2..];
if size == 0 {
return Some(out);
}
out.extend_from_slice(body.get(..size)?);
body = body.get(size..)?.strip_prefix(b"\r\n")?;
}
}
fn base64(bytes: &[u8]) -> String {
const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let n = u32::from_be_bytes([
0,
chunk[0],
*chunk.get(1).unwrap_or(&0),
*chunk.get(2).unwrap_or(&0),
]);
let ch = |shift: u32| char::from(TABLE[((n >> shift) & 63) as usize]);
out.extend([
ch(18),
ch(12),
if chunk.len() > 1 { ch(6) } else { '=' },
if chunk.len() > 2 { ch(0) } else { '=' },
]);
}
out
}
fn encode_uri_component(value: &str) -> String {
let mut encoded = String::with_capacity(value.len());
for byte in value.bytes() {
if byte.is_ascii_alphanumeric() {
encoded.push(char::from(byte));
} else {
encoded.push('%');
encoded.push(char::from(b"0123456789ABCDEF"[(byte >> 4) as usize]));
encoded.push(char::from(b"0123456789ABCDEF"[(byte & 0x0f) as usize]));
}
}
encoded
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use std::io::{BufRead, BufReader};
use std::net::TcpListener;
use std::thread;
/// The headers and body of each request a fixture received.
pub(crate) type Received = thread::JoinHandle<Vec<(String, Vec<u8>)>>;
/// A service fixture answering each `(request line, status, body)` in
/// order; returns its URL and what it received.
pub(crate) fn serve(routes: Vec<(String, u16, String)>) -> (String, Received) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/", listener.local_addr().unwrap());
let server = thread::spawn(move || {
routes
.into_iter()
.map(|(start, status, response)| {
let (mut stream, _) = listener.accept().unwrap();
stream
.set_read_timeout(Some(Duration::from_secs(5)))
.unwrap();
let mut reader = BufReader::new(&mut stream);
let mut headers = String::new();
loop {
let mut line = String::new();
assert!(reader.read_line(&mut line).unwrap() > 0);
if line == "\r\n" {
break;
}
headers.push_str(&line);
}
assert!(
headers.starts_with(&format!("{start} HTTP/1.1\r\n")),
"{headers}"
);
let length: usize = headers
.lines()
.find_map(|line| {
let (key, value) = line.split_once(':')?;
key.eq_ignore_ascii_case("content-length")
.then(|| value.trim().parse().unwrap())
})
.unwrap_or(0);
let mut body = vec![0; length];
reader.read_exact(&mut body).unwrap();
write!(
stream,
"HTTP/1.1 {status} Fixture\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{response}",
response.len()
)
.unwrap();
(headers, body)
})
.collect()
});
(url, server)
}
pub(crate) fn fixture(url: &str) -> Service {
Service {
address: loopback_address(url).unwrap(),
password: Some("fixture-password".into()),
}
}
fn state_root(label: &str) -> PathBuf {
let root = std::env::temp_dir().join(format!(
"hex-opencode-service-{label}-{}-{:?}",
std::process::id(),
thread::current().id()
));
let _ = fs::remove_dir_all(&root);
fs::create_dir_all(&root).unwrap();
root
}
fn register(state: &Path, name: &str, url: &str, pid: u32) {
let path = state.join(name);
fs::write(
&path,
serde_json::json!({
"id": "fixture", "version": "2.0.0", "url": url, "pid": pid,
"password": "fixture-password"
})
.to_string(),
)
.unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap();
}
}
#[test]
fn a_registered_service_is_confirmed_by_its_pid() {
let state = state_root("confirm");
let info = r#"{"pid":42,"version":"2.0.0"}"#.to_owned();
let (url, server) = serve(vec![("GET /api/info".into(), 200, info)]);
register(&state, "service.json", &url, 42);
let service = find_in(&state).unwrap();
let (headers, _) = server.join().unwrap().remove(0);
assert!(headers.contains("Authorization: Basic b3BlbmNvZGU6Zml4dHVyZS1wYXNzd29yZA==\r\n"));
assert_eq!(service, fixture(&url));
fs::remove_dir_all(state).unwrap();
}
#[test]
fn a_stale_registration_means_opencode_isnt_running() {
let state = state_root("stale");
assert_eq!(find_in(&state), None);
let info = r#"{"pid":7,"version":"2.0.0"}"#.to_owned();
let (url, server) = serve(vec![("GET /api/info".into(), 200, info)]);
register(&state, "service.json", &url, 42);
assert_eq!(find_in(&state), None);
server.join().unwrap();
let closed = TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/", closed.local_addr().unwrap());
drop(closed);
register(&state, "service.json", &url, 42);
assert_eq!(find_in(&state), None);
fs::remove_dir_all(state).unwrap();
}
#[cfg(unix)]
#[test]
fn a_registration_others_can_read_is_ignored() {
use std::os::unix::fs::PermissionsExt;
let state = state_root("private");
register(&state, "service.json", "http://127.0.0.1:9/", 42);
let path = state.join("service.json");
fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();
assert!(read_registration(&path).is_none());
fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap();
assert!(read_registration(&path).is_some());
fs::remove_dir_all(state).unwrap();
}
#[test]
fn only_loopback_http_endpoints_are_used() {
assert_eq!(
loopback_address("http://0.0.0.0:4096"),
Some("127.0.0.1:4096".parse().unwrap())
);
assert_eq!(
loopback_address("http://localhost:4096/"),
Some("127.0.0.1:4096".parse().unwrap())
);
assert_eq!(
loopback_address("http://[::]:4096"),
Some("[::1]:4096".parse().unwrap())
);
for refused in [
"https://127.0.0.1:4096",
"http://192.168.1.2:4096",
"http://example.com:4096",
"http://user:pass@127.0.0.1:4096",
"http://127.0.0.1:4096/prefix",
"http://127.0.0.1:0",
] {
assert_eq!(loopback_address(refused), None, "{refused}");
}
}
#[test]
fn requests_carry_the_workspace_and_decode_chunked_answers() {
let root = state_root("workspace").join("scope %2F caf\u{00e9}");
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/", listener.local_addr().unwrap());
let server = thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
let mut reader = BufReader::new(&mut stream);
let mut headers = String::new();
loop {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
if line == "\r\n" {
break;
}
headers.push_str(&line);
}
stream
.write_all(
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n",
)
.unwrap();
headers
});
let response = fixture(&url)
.request(
"GET",
"/api/model",
None,
Some(&root),
Duration::from_secs(5),
&AtomicBool::new(false),
)
.unwrap();
let workspace = root.canonicalize().unwrap();
let headers = server.join().unwrap();
assert!(headers.contains(&format!(
"x-opencode-directory: {}\r\n",
encode_uri_component(workspace.to_str().unwrap())
)));
assert_eq!(response.status, 200);
assert_eq!(response.body, b"hello world");
fs::remove_dir_all(root.parent().unwrap()).unwrap();
}
#[test]
fn a_silent_service_is_bounded_by_cancellation_and_the_deadline() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/", listener.local_addr().unwrap());
let service = fixture(&url);
let started = Instant::now();
let error = service
.request(
"GET",
"/api/info",
None,
None,
Duration::from_millis(200),
&AtomicBool::new(false),
)
.unwrap_err();
assert!(error.to_string().contains("exceeded"), "{error}");
let cancelled = AtomicBool::new(false);
thread::scope(|scope| {
scope.spawn(|| {
thread::sleep(Duration::from_millis(100));
cancelled.store(true, Ordering::Release);
});
let error = service
.request(
"GET",
"/api/info",
None,
None,
Duration::from_secs(30),
&cancelled,
)
.unwrap_err();
assert!(error.to_string().contains("cancelled"), "{error}");
});
assert!(started.elapsed() < Duration::from_secs(5));
drop(listener);
}
}