mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
feat: add configurable Cursor TAB routing
This commit is contained in:
1 parent
8a4fc5076a
commit
0f23a9a9c3
23 files changed
+649
-73
No files matched your search
@@ -9,7 +9,10 @@ use parking_lot::RwLock;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{store::Store, Error, Result};
|
||||
use crate::{
|
||||
store::{Store, TabMode, TabSettings},
|
||||
Error, Result,
|
||||
};
|
||||
|
||||
use self::{ca::CaManager, proxy::ProxyRuntime};
|
||||
|
||||
@@ -65,6 +68,7 @@ struct Inner {
|
||||
ca: CaManager,
|
||||
ca_initialization: Mutex<()>,
|
||||
backend_addr: RwLock<Option<SocketAddr>>,
|
||||
tab_mode: Arc<RwLock<TabMode>>,
|
||||
proxy: Mutex<ProxyRuntime>,
|
||||
}
|
||||
|
||||
@@ -76,6 +80,7 @@ impl CursorHarness {
|
||||
ca: CaManager::managed()?,
|
||||
ca_initialization: Mutex::new(()),
|
||||
backend_addr: RwLock::new(None),
|
||||
tab_mode: Arc::new(RwLock::new(TabMode::default())),
|
||||
proxy: Mutex::new(ProxyRuntime::default()),
|
||||
}),
|
||||
})
|
||||
@@ -138,6 +143,12 @@ impl CursorHarness {
|
||||
self.status().await
|
||||
}
|
||||
|
||||
pub async fn set_tab_settings(&self, settings: TabSettings) -> Result<TabSettings> {
|
||||
let saved = self.inner.store.set_tab_settings(settings).await?;
|
||||
*self.inner.tab_mode.write() = saved.mode;
|
||||
Ok(saved)
|
||||
}
|
||||
|
||||
async fn enable(&self) -> Result<()> {
|
||||
if !matches!(self.inner.ca.state()?, CaState::Ready) {
|
||||
return Err(Error::Config(
|
||||
@@ -158,7 +169,15 @@ impl CursorHarness {
|
||||
}
|
||||
let ca = self.inner.ca.load()?;
|
||||
let requested_port = self.inner.store.port_settings().await?.proxy_port;
|
||||
let (url, actual_port) = proxy.start(backend_addr, ca, requested_port).await?;
|
||||
*self.inner.tab_mode.write() = self.inner.store.tab_settings().await?.mode;
|
||||
let (url, actual_port) = proxy
|
||||
.start(
|
||||
backend_addr,
|
||||
ca,
|
||||
requested_port,
|
||||
self.inner.tab_mode.clone(),
|
||||
)
|
||||
.await?;
|
||||
if let Err(error) = self.inner.store.set_proxy_port(actual_port).await {
|
||||
proxy.stop().await;
|
||||
return Err(error);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::net::SocketAddr;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use hudsucker::{
|
||||
certificate_authority::RcgenAuthority,
|
||||
@@ -8,7 +8,13 @@ use hudsucker::{
|
||||
};
|
||||
use tokio::{net::TcpListener, sync::oneshot, task::JoinHandle};
|
||||
|
||||
use crate::{cursor::proxy::UPSTREAM_URL_HEADER, Error, Result};
|
||||
use parking_lot::RwLock;
|
||||
|
||||
use crate::{
|
||||
cursor::{proxy::UPSTREAM_URL_HEADER, tab::is_tab_path},
|
||||
store::TabMode,
|
||||
Error, Result,
|
||||
};
|
||||
|
||||
use super::ca::LoadedCa;
|
||||
|
||||
@@ -33,6 +39,7 @@ impl ProxyRuntime {
|
||||
backend: SocketAddr,
|
||||
ca: LoadedCa,
|
||||
requested_port: u16,
|
||||
tab_mode: Arc<RwLock<TabMode>>,
|
||||
) -> Result<(String, u16)> {
|
||||
if let Some(url) = self.url() {
|
||||
return Ok((url, self.port.unwrap_or_default()));
|
||||
@@ -45,7 +52,7 @@ impl ProxyRuntime {
|
||||
.with_listener(listener)
|
||||
.with_ca(authority)
|
||||
.with_rustls_connector(aws_lc_rs::default_provider())
|
||||
.with_http_handler(CursorRelay { backend })
|
||||
.with_http_handler(CursorRelay { backend, tab_mode })
|
||||
.with_graceful_shutdown(async move {
|
||||
let _ = done.await;
|
||||
})
|
||||
@@ -89,6 +96,7 @@ async fn bind_proxy_listener(requested_port: u16) -> Result<TcpListener> {
|
||||
#[derive(Clone)]
|
||||
struct CursorRelay {
|
||||
backend: SocketAddr,
|
||||
tab_mode: Arc<RwLock<TabMode>>,
|
||||
}
|
||||
|
||||
impl HttpHandler for CursorRelay {
|
||||
@@ -98,7 +106,8 @@ impl HttpHandler for CursorRelay {
|
||||
mut request: Request<Body>,
|
||||
) -> RequestOrResponse {
|
||||
let original = request.uri().clone();
|
||||
if is_cursor_host(original.host().unwrap_or_default()) && is_local_path(original.path()) {
|
||||
let locally_routed = should_route_locally(original.path(), *self.tab_mode.read());
|
||||
if is_cursor_host(original.host().unwrap_or_default()) && locally_routed {
|
||||
if let Ok(value) = original.to_string().parse() {
|
||||
request.headers_mut().insert(UPSTREAM_URL_HEADER, value);
|
||||
}
|
||||
@@ -157,6 +166,10 @@ fn is_local_path(path: &str) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
fn should_route_locally(path: &str, tab_mode: TabMode) -> bool {
|
||||
is_local_path(path) || (is_tab_path(path) && tab_mode != TabMode::Direct)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -179,5 +192,17 @@ mod tests {
|
||||
"/aiserver.v1.AnalyticsService/BootstrapStatsig"
|
||||
));
|
||||
assert!(!is_local_path("/unrelated"));
|
||||
assert!(should_route_locally(
|
||||
"/aiserver.v1.AiService/StreamCpp",
|
||||
TabMode::Public
|
||||
));
|
||||
assert!(should_route_locally(
|
||||
"/aiserver.v1.AiService/StreamCpp",
|
||||
TabMode::Custom
|
||||
));
|
||||
assert!(!should_route_locally(
|
||||
"/aiserver.v1.AiService/StreamCpp",
|
||||
TabMode::Direct
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user