diff --git a/app/utils/stream.ts b/app/utils/stream.ts index f8c272e42..dd665e71c 100644 --- a/app/utils/stream.ts +++ b/app/utils/stream.ts @@ -21,7 +21,12 @@ type StreamResponse = { export function fetch(url: string, options?: RequestInit): Promise { if (window.__TAURI__) { - const { signal, method = "GET", headers = {}, body = [] } = options || {}; + const { + signal, + method = "GET", + headers: _headers = {}, + body = [], + } = options || {}; let unlisten: Function | undefined; let request_id = 0; const ts = new TransformStream(); @@ -32,10 +37,10 @@ export function fetch(url: string, options?: RequestInit): Promise { writer.ready.then(() => { try { writer.releaseLock(); + ts.writable.close(); } catch (e) { console.error(e); } - ts.writable.close(); }); }; @@ -61,6 +66,19 @@ export function fetch(url: string, options?: RequestInit): Promise { }) .then((u: Function) => (unlisten = u)); + const headers = { + Accept: "*", + Connection: "close", + Origin: "http://localhost:3000", + Referer: "http://localhost:3000/", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "cross-site", + "User-Agent": navigator.userAgent, + }; + for (const item of new Headers(_headers || {})) { + headers[item[0]] = item[1]; + } return window.__TAURI__ .invoke("stream_fetch", { method, diff --git a/src-tauri/src/stream.rs b/src-tauri/src/stream.rs index 81710c733..97989ba7e 100644 --- a/src-tauri/src/stream.rs +++ b/src-tauri/src/stream.rs @@ -53,15 +53,33 @@ pub async fn stream_fetch( } let mut _headers = HeaderMap::new(); - for (key, value) in headers { - _headers.insert(key.parse::().unwrap(), value.parse().unwrap()); + for (key, value) in &headers { + _headers.insert(key.parse::().unwrap(), value.parse().unwrap()); } - let body = bytes::Bytes::from(body); - let response_future = Client::new().request( - method.parse::().map_err(|err| format!("failed to parse method: {}", err))?, + println!("method: {:?}", method); + println!("url: {:?}", url); + println!("headers: {:?}", headers); + println!("headers: {:?}", _headers); + + let method = method.parse::().map_err(|err| format!("failed to parse method: {}", err))?; + let client = Client::builder() + .user_agent("Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15") + .default_headers(_headers) + .build() + .map_err(|err| format!("failed to generate client: {}", err))?; + + let mut request = client.request( + method.clone(), url.parse::().map_err(|err| format!("failed to parse url: {}", err))? - ).headers(_headers).body(body).send(); + ); + + if method == reqwest::Method::POST { + let body = bytes::Bytes::from(body); + println!("body: {:?}", body); + request = request.body(body); + } + let response_future = request.send(); let res = response_future.await; let response = match res {