This commit is contained in:
polwex 2024-10-22 21:45:57 +07:00
parent f1791b8699
commit e68c675be2
9 changed files with 115 additions and 90 deletions

View File

@ -8,7 +8,7 @@ use kinode_process_lib::{
},
StatusCode,
},
println, set_state, Address, Message, Request,
kiprintln, set_state, Address, Message, Request,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
@ -121,7 +121,6 @@ pub fn load_state() -> State {
fn handle_message(our: &Address) -> anyhow::Result<()> {
let message = await_message()?;
if !message.is_request() {
return Err(anyhow::anyhow!("unexpected Response: {:?}", message));
}
@ -160,26 +159,27 @@ fn handle_http(our: &Address, request: IncomingHttpRequest) -> Result<()> {
"GET" => handle_get(our, request, &mut state),
"POST" => handle_post(our, request, &mut state),
_ => {
println!("cookies - got weird req - {:?}", request);
kiprintln!("cookies - got weird req - {:?}", request);
Ok(())
}
}
}
fn handle_get(our: &Address, req: IncomingHttpRequest, state: &mut State) -> anyhow::Result<()> {
// println!("got GET req - {:?}", req);
// kiprintln!("got GET req - {:?}", req);
let conc = format!("{}:{}{}", our.process(), our.package_id(), "/api");
let pat = req.bound_path(Some(conc.as_str()));
kiprintln!("got get {} {}", conc, pat);
// let pats = req.path()?;
// let pat = pats.as_str();
let uparams = req.url_params();
let qparams = req.query_params();
// println!("request path {}", pat);
// println!("request up {:?}", uparams);
// println!("request qp {:?}", qparams);
// kiprintln!("request path {}", pat);
// kiprintln!("request up {:?}", uparams);
// kiprintln!("request qp {:?}", qparams);
match pat {
"/debug" => {
println!("current state {:?}", state);
kiprintln!("current state {:?}", state);
}
"/all" => {
let res = UIRes::All(state.to_owned());
@ -219,7 +219,9 @@ fn handle_get(our: &Address, req: IncomingHttpRequest, state: &mut State) -> any
}
}
}
_ => {}
_ => {
kiprintln!("wtf");
}
}
send_response(StatusCode::CREATED, None, vec![]);
Ok(())
@ -238,7 +240,7 @@ fn parse_test() {
{"auth_multi":{"domain":".x.com","expirationDate":1736167622.15024,"hostOnly":false,"httpOnly":true,"name":"auth_multi","path":"/","sameSite":"lax","secure":true,"session":false,"storeId":"0","value":"\"1710606417324015616:604ed700cfb5c8a5d2665086503f1ec8b6032ef4\"","id":1},"auth_token":{"domain":".x.com","expirationDate":1736097942.573061,"hostOnly":false,"httpOnly":true,"name":"auth_token","path":"/","sameSite":"no_restriction","secure":true,"session":false,"storeId":"0","value":"2527de3d3719d900cd5658525e559d0966d86662","id":2},"ct0":{"domain":".x.com","expirationDate":1736097942.861119,"hostOnly":false,"httpOnly":false,"name":"ct0","path":"/","sameSite":"lax","secure":true,"session":false,"storeId":"0","value":"1461eb581cb824ea00b652d27a735f1abebe2b47de834cddd624afdbee20c4f033972cb63fd298d0db8cded9a75429df3907ced5bb375f12cf02100825b16f7c02a00253ca1e8883b60ace17ccea1622","id":3},"dnt":{"domain":".x.com","expirationDate":1736097942.572255,"hostOnly":false,"httpOnly":false,"name":"dnt","path":"/","sameSite":"no_restriction","secure":true,"session":false,"storeId":"0","value":"1","id":4},"external_referer":{"domain":".x.com","expirationDate":1721160063.509523,"hostOnly":false,"httpOnly":false,"name":"external_referer","path":"/","sameSite":"unspecified","secure":true,"session":false,"storeId":"0","value":"padhuUp37zjgzgv1mFWxJ12Ozwit7owX|0|8e8t2xd8A2w%3D","id":5},"guest_id":{"domain":".x.com","expirationDate":1736097942.861021,"hostOnly":false,"httpOnly":false,"name":"guest_id","path":"/","sameSite":"no_restriction","secure":true,"session":false,"storeId":"0","value":"v1%3A172054594269674803","id":6},"guest_id_ads":{"domain":".x.com","expirationDate":1736167616.625858,"hostOnly":false,"httpOnly":false,"name":"guest_id_ads","path":"/","sameSite":"no_restriction","secure":true,"session":false,"storeId":"0","value":"v1%3A172054594269674803","id":7},"guest_id_marketing":{"domain":".x.com","expirationDate":1736167616.625932,"hostOnly":false,"httpOnly":false,"name":"guest_id_marketing","path":"/","sameSite":"no_restriction","secure":true,"session":false,"storeId":"0","value":"v1%3A172054594269674803","id":8},"kdt":{"domain":".x.com","expirationDate":1736097942.572385,"hostOnly":false,"httpOnly":true,"name":"kdt","path":"/","sameSite":"unspecified","secure":true,"session":false,"storeId":"0","value":"iWdBqeAH3UcgpwzPxi6CZ2lRTk4Fqia3OR5VbiSo","id":9},"personalization_id":{"domain":".x.com","expirationDate":1736167616.625994,"hostOnly":false,"httpOnly":false,"name":"personalization_id","path":"/","sameSite":"no_restriction","secure":true,"session":false,"storeId":"0","value":"\"v1_O5kcdlrANZbUakXhbmlSTw==\"","id":10},"twid":{"domain":".x.com","expirationDate":1736167616.773335,"hostOnly":false,"httpOnly":false,"name":"twid","path":"/","sameSite":"no_restriction","secure":true,"session":false,"storeId":"0","value":"u%3D1809740330922831872","id":11},"d_prefs":{"domain":"x.com","hostOnly":true,"httpOnly":false,"name":"d_prefs","path":"/","sameSite":"unspecified","secure":true,"session":true,"storeId":"0","value":"MjoxLGNvbnNlbnRfdmVyc2lvbjoyLHRleHRfdmVyc2lvbjoxMDAw","id":12},"lang":{"domain":"x.com","hostOnly":true,"httpOnly":false,"name":"lang","path":"/","sameSite":"unspecified","secure":false,"session":true,"storeId":"0","value":"en","id":13}}
"#;
let pj = serde_json::from_str::<CookieMap>(str);
println!("json parsing test {:?}", pj);
kiprintln!("json parsing test {:?}", pj);
// let jason = r#"
// {
// "domain": ".x.com",
@ -256,16 +258,16 @@ fn parse_test() {
// }
// "#;
// let pj = serde_json::from_str::<Cookie>(jason);
// println!("json parsing test {:?}", pj);
// kiprintln!("json parsing test {:?}", pj);
// // let example: UIReq = UIReq::SetAPIKey(APIRes {
// // app: "foo".to_string(),
// // api_key: "bar".to_string(),
// // });
// // let value = serde_json::to_string(&example)?;
// // println!("expected json {}", value);
// // kiprintln!("expected json {}", value);
}
fn handle_post(our: &Address, req: IncomingHttpRequest, state: &mut State) -> anyhow::Result<()> {
println!("cokies post request!");
kiprintln!("cokies post request!");
// parse_test();
// let conc = format!("{}:{}{}", our.process(), our.package_id(), "/api");
@ -276,7 +278,7 @@ fn handle_post(our: &Address, req: IncomingHttpRequest, state: &mut State) -> an
// Fail silently if we can't parse the request
return Ok(());
};
println!("post body {:?}", post_request);
kiprintln!("post body {:?}", post_request);
match post_request {
UIReq::SetCookie { app, cookie } => {
@ -294,22 +296,26 @@ fn handle_post(our: &Address, req: IncomingHttpRequest, state: &mut State) -> an
Ok(())
}
call_init!(init);
fn init(our: Address) {
fn init_http() -> anyhow::Result<()> {
let mut http_server = HttpServer::new(5);
let http_config = HttpBindingConfig::default();
let ws_config = WsBindingConfig::default();
http_server.bind_ws_path("/", ws_config.clone());
// REST API
http_server.bind_http_path("/", http_config.clone());
http_server.bind_http_path("/api", http_config.clone());
http_server.bind_http_path("/api/all", http_config.clone());
http_server.bind_http_path("/api/app", http_config.clone());
http_server.bind_ws_path("/", ws_config.clone())?;
http_server.bind_http_path("/", http_config.clone())?;
http_server.bind_http_path("/api", http_config.clone())?;
http_server.bind_http_path("/api/all", http_config.clone())?;
http_server.bind_http_path("/api/app", http_config.clone())?;
Ok(())
}
fn init(our: Address) {
kiprintln!("begin cookies");
let _ = init_http();
loop {
match handle_message(&our) {
Ok(()) => {}
Err(e) => {
println!("error: {:?}", e);
kiprintln!("error: {:?}", e);
}
};
}

View File

@ -32,6 +32,7 @@
"request_networking": true,
"request_capabilities": [
"homepage:homepage:sys",
"vfs:distro:sys",
"http_server:distro:sys",
"http_client:distro:sys"
],

View File

@ -119,17 +119,21 @@ fn send_json(res: UIRes) -> Result<()> {
Ok(())
}
call_init!(init);
fn init(our: Address) {
println!("begin proxy");
println!("our {:?}", our);
println!("our {:?}", our.process());
fn init_http() -> anyhow::Result<()> {
let mut http_server = HttpServer::new(5);
let http_config = HttpBindingConfig::default();
let ws_config = WsBindingConfig::default();
http_server.bind_ws_path("/", ws_config.clone());
// REST API
http_server.bind_http_path("/api", http_config.clone());
http_server.bind_ws_path("/", ws_config.clone())?;
http_server.bind_http_path("/", http_config.clone())?;
http_server.bind_http_path("/api", http_config.clone())?;
Ok(())
}
fn init(our: Address) {
println!("begin proxy");
println!("our {:?}", our);
println!("our {:?}", our.process());
let _ = init_http();
loop {
match handle_message(&our) {
Ok(()) => {}

1
twittok/src/ICON Normal file

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@ use std::str::FromStr;
use kinode_process_lib::{
await_message, call_init, get_blob,
homepage::add_to_homepage,
http::{
server::{
send_response, send_ws_push, HttpBindingConfig, HttpServer, HttpServerRequest,
@ -14,6 +15,7 @@ use kinode_process_lib::{
};
use serde::{Deserialize, Serialize};
const ICON: &str = include_str!("ICON");
wit_bindgen::generate!({
path: "target/wit",
world: "tok-sortugdev-dot-os-v0",
@ -261,21 +263,25 @@ fn handle_message(
}
call_init!(init);
fn init(our: Address) {
println!("begin");
let mut message_archive: MessageArchive = HashMap::new();
let mut channel_id = 0;
fn init_http(our: &Address) -> anyhow::Result<()> {
let mut http_server = HttpServer::new(5);
let http_config = HttpBindingConfig::default();
let ws_config = WsBindingConfig::default();
http_server.bind_ws_path("/", ws_config.clone());
http_server.bind_ws_path("/", ws_config.clone())?;
// REST API
http_server.bind_http_path("/api", http_config.clone());
http_server.serve_ui(&our, "ui", vec!["/"], http_config.clone());
http_server.bind_http_path("/api", http_config.clone())?;
http_server.serve_ui(our, "ui", vec!["/"], http_config.clone())?;
add_to_homepage("抖推", Some(ICON), Some("/"), None);
Ok(())
}
fn init(our: Address) {
println!("begin");
let mut message_archive: MessageArchive = HashMap::new();
let mut channel_id = 0;
let _ = init_http(&our);
loop {
match handle_message(&our, &mut message_archive, &mut channel_id) {

View File

@ -10,6 +10,7 @@ import { useEffect, useState } from "react";
import { useHistory, Router, Route } from "./logic/router";
import { useQueryClient } from "@tanstack/react-query";
import { twatterCookieQ } from "./logic/queries";
import { PROCESS_ID } from "./logic/constants";
function App() {
const [loading, setLoading] = useState(true);
@ -19,16 +20,16 @@ function App() {
await qc.prefetchQuery(twatterCookieQ);
}
useEffect(() => {
init().then(_ => {
init().then((_) => {
setLoading(false);
});
pref();
}, []);
// const { init } = useGlobalState();
if (loading) return <img className="spinner gc" src={spinner} />
if (loading) return <img className="spinner gc" src={spinner} />;
return (
<>
<Router basePath="/">
<Router basePath={`/${PROCESS_ID}`}>
<Route path="/" component={Nav} />
<Route path="/cookies" component={Cookies} />
<Route path="/main" component={Loader} />
@ -48,7 +49,7 @@ function Nav() {
<a onClick={() => navigate("/main")} className="button">
Go Browse
</a>
<Test />
<Test />
</>
);
}

View File

@ -1,20 +1,17 @@
import {
COOKIE_PROCESS_ID,
PROXY_PROCESS_ID,
} from "./constants";
import { COOKIE_PROCESS_ID, NODE_URL, PROXY_PROCESS_ID } from "./constants";
import { APIRes, CookieMap, CookieRes, CookiesRes } from "./types";
export async function fetchAllCookies(): Promise<CookiesRes> {
const res = await getCookie(`/all`);
return res.All
return res.All;
}
export async function fetchCookie(name: string): Promise<CookieRes> {
const res = await getCookie(`/app?name=${name}&api=false`);
return res.App
return res.App;
}
export async function fetchAPIKey(name: string): Promise<APIRes> {
const res = await getCookie(`/app?name=${name}&api=true`);
return res.App
return res.App;
}
export async function saveAPIKey(app: string, value: string) {
const payload = { SetAPIKey: { app, api_key: value } };
@ -26,7 +23,7 @@ export async function saveCookie(app: string, value: CookieMap) {
}
async function getCookie(path: string) {
// const url = `${NODE_URL}/${COOKIE_PROCESS_ID}${path}`
// const url = `${NODE_URL}/${COOKIE_PROCESS_ID}/api${path}`;
const url = `/${COOKIE_PROCESS_ID}/api${path}`;
const res = await fetch(url);
const j = await res.json();
@ -53,10 +50,10 @@ export async function postProxy(payload: any, naked = false) {
// const url = `${NODE_URL}/${PROXY_PROCESS_ID}`
const url = `/${PROXY_PROCESS_ID}/api`;
const res = await fetch(url, opts);
if (naked) return res
console.log(res, "post proxy res")
if (naked) return res;
console.log(res, "post proxy res");
const j = await res.json();
console.log(j, "post proxy json")
console.log(j, "post proxy json");
return j;
}
async function getProxy() {
@ -66,7 +63,7 @@ async function getProxy() {
return res;
}
export async function saveWord(word: string){
export async function saveWord(word: string) {
// TODO
}

View File

@ -1,11 +1,13 @@
export const COOKIE_PROCESS_ID = "cookies:tok:sortugdev.os";
export const PROXY_PROCESS_ID = "proxy:tok:sortugdev.os";
export const NODE_URL = "http://localhost:8095";
export const PROCESS_ID = "twittok:tok:sortugdev.os";
export const NODE_URL = "http://localhost:8080";
export const TIMELINE_URL =
"https://x.com/i/api/graphql/A_qu1009UoeQToazaP4YCg/HomeTimeline";
export const translationPrompt = "You will be provided with a text in Thai, from Twitter, so it's rather casual in that internet slang way. Your task is to translate it into English. Please keep the tone of the original as much as possible.";
export const translationPrompt =
"You will be provided with a text in Thai, from Twitter, so it's rather casual in that internet slang way. Your task is to translate it into English. Please keep the tone of the original as much as possible.";
export const DefaultPrompts = {
translate: translationPrompt
}
translate: translationPrompt,
};

View File

@ -1,50 +1,57 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
/*
If you are developing a UI outside of a Kinode project,
comment out the following 2 lines:
*/
import manifest from '../pkg/manifest.json'
import metadata from '../metadata.json'
/*
IMPORTANT:
This must match the process name from pkg/manifest.json + pkg/metadata.json
The format is "/" + "process_name:package_name:publisher_node"
*/
const BASE_URL = `/${manifest[0].process_name}:${metadata.properties.package_name}:${metadata.properties.publisher}`;
const BASE_URL = "/twittok:tok:sortugdev.os";
const COOKIE_PROCESS_ID = "cookies:tok:sortugdev.os";
const PROXY_PROCESS_ID = "proxy:tok:sortugdev.os";
// This is the proxy URL, it must match the node you are developing against
const PROXY_URL = (process.env.VITE_NODE_URL || 'http://127.0.0.1:8080').replace('localhost', '127.0.0.1');
const PROXY_URL = (
process.env.VITE_NODE_URL || "http://127.0.0.1:8080"
).replace("localhost", "127.0.0.1");
console.log('process.env.VITE_NODE_URL', process.env.VITE_NODE_URL, PROXY_URL);
console.log("process.env.VITE_NODE_URL", process.env.VITE_NODE_URL, PROXY_URL);
export default defineConfig({
plugins: [react()],
base: BASE_URL,
build: {
rollupOptions: {
external: ['/our.js']
}
external: ["/our.js"],
},
},
server: {
open: true,
proxy: {
'/our': {
target: PROXY_URL,
changeOrigin: true,
},
[`${BASE_URL}/our.js`]: {
target: PROXY_URL,
changeOrigin: true,
rewrite: (path) => path.replace(BASE_URL, ''),
},
// This route will match all other HTTP requests to the backend
[`^${BASE_URL}/(?!(@vite/client|src/.*|node_modules/.*|@react-refresh|$))`]: {
"^.*": {
target: PROXY_URL,
changeOrigin: true,
},
// "/our": {
// target: PROXY_URL,
// changeOrigin: true,
// },
// [`^/${COOKIE_PROCESS_ID}/api.*`]: {
// target: PROXY_URL,
// changeOrigin: true,
// rewrite: (path) => path.replace(COOKIE_PROCESS_ID, ""),
// },
// [`/${PROXY_PROCESS_ID}`]: {
// target: PROXY_URL,
// changeOrigin: true,
// rewrite: (path) => path.replace(PROXY_PROCESS_ID, ""),
// },
// [`${BASE_URL}/our.js`]: {
// target: PROXY_URL,
// changeOrigin: true,
// rewrite: (path) => path.replace(BASE_URL, ""),
// },
// // This route will match all other HTTP requests to the backend
// [`^${BASE_URL}/(?!(@vite/client|src/.*|node_modules/.*|@react-refresh|$))`]:
// {
// target: PROXY_URL,
// changeOrigin: true,
// },
// '/example': {
// target: PROXY_URL,
// changeOrigin: true,
@ -62,6 +69,6 @@ export default defineConfig({
// });
// },
// },
}
}
},
},
});