Skip to content

Commit

Permalink
[mod] Add direct connection mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Nov 30, 2023
1 parent 1d556c1 commit 25f5513
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 51 deletions.
24 changes: 8 additions & 16 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::util;
/// Config
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
/// 是否启用代理, 若不启用, 默认使用镜像
/// 模式, 0: 镜像模式, 1: 直连模式, 2: 代理模式
///
/// Whether to enable the proxy, if not enabled, the default is to use the mirror
pub proxy: bool,
/// Mode, 0: mirror mode, 1: direct mode, 2: proxy mode
pub mode: usize,
/// 代理地址, 为空则直连
///
/// Proxy address, empty means direct
Expand All @@ -41,7 +41,7 @@ pub fn load() -> Config {
serde_json::from_str(&config).unwrap()
} else {
Config {
proxy: false,
mode: 0,
url: "".to_string(),
}
}
Expand All @@ -58,19 +58,11 @@ fn save(config: &Config) -> Result<()> {
Ok(())
}

/// 开启代理
/// 切换模式
///
/// enable proxy
pub fn enable_proxy() {
CONFIG.lock().proxy = true;
save(&CONFIG.lock()).unwrap();
}

/// 关闭代理
///
/// disable proxy
pub fn disable_proxy() {
CONFIG.lock().proxy = false;
/// switch mode
pub fn mode(mode: usize) {
CONFIG.lock().mode = mode;
save(&CONFIG.lock()).unwrap();
}

Expand Down
19 changes: 5 additions & 14 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,12 @@ fn get_config() -> Resp<Config> {
Ok(CONFIG.lock().clone()).into()
}

/// 开启代理
/// 切换模式
///
/// Enable proxy
/// Switch mode
#[tauri::command]
fn enable_proxy() {
config::enable_proxy();
}

/// 关闭代理
///
/// Disable proxy
#[tauri::command]
fn disable_proxy() {
config::disable_proxy();
fn switch_mode(mode: usize) {
config::mode(mode);
}

/// 设置代理地址
Expand All @@ -79,8 +71,7 @@ async fn main() {
copy,
translate,
get_config,
enable_proxy,
disable_proxy,
switch_mode,
set_proxy_url
])
.run(tauri::generate_context!())
Expand Down
16 changes: 9 additions & 7 deletions src-tauri/src/manager/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ pub async fn translate(context: &str) -> Result<TransVO> {
let context = encode(context);

let url = { CONFIG.lock().url.clone() };

if CONFIG.lock().proxy {
let proxy = reqwest::Proxy::all(&url)?;
let client = Client::builder().proxy(proxy).build().unwrap();
send(&client, "https://translate.googleapis.com", &lang, &context).await
} else {
send(&CLIENT, &mirror::one(), &lang, &context).await
let mode = CONFIG.lock().mode;
match mode {
0 => send(&CLIENT, mirror::one().as_str(), &lang, &context).await,
1 => send(&CLIENT, "https://translate.googleapis.com", &lang, &context).await,
_ => {
let proxy = reqwest::Proxy::all(&url)?;
let client = Client::builder().proxy(proxy).build().unwrap();
send(&client, "https://translate.googleapis.com", &lang, &context).await
}
}
}

Expand Down
55 changes: 41 additions & 14 deletions src/page/Setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import TopBar from "../components/TopBar"
import { Resp } from "../model/resp"

interface ConfigProps {
proxy: boolean
mode: number
url: string
}

const Setting = () => {
const main = getCurrent()

const [proxy, Proxy] = createSignal(false)
const [mode, Mode] = createSignal(0)
const [url, Url] = createSignal("")

main.listen("tauri://close-requested", async () => {
Expand All @@ -25,7 +25,7 @@ const Setting = () => {

onMount(async () => {
const resp = await invoke<Resp<ConfigProps>>("get_config")
Proxy(resp.data.proxy)
Mode(resp.data.mode)
Url(resp.data.url)
})

Expand All @@ -40,28 +40,55 @@ const Setting = () => {
<div class="setting-option">
<div
class="setting-option-item"
classList={{ "setting-option-item-selected": !proxy() }}
classList={{
"setting-option-item-selected": mode() == 0,
}}
onClick={async () => {
Proxy(false)
await invoke("disable_proxy")
}}>
Mode(0)
await invoke("switch_mode", {
mode: 0,
})
}}
>
镜像
</div>
<div
class="setting-option-item"
classList={{ "setting-option-item-selected": proxy() }}
classList={{
"setting-option-item-selected": mode() == 1,
}}
onClick={async () => {
Proxy(true)
await invoke("enable_proxy")
}}>
Mode(1)
await invoke("switch_mode", {
mode: 1,
})
}}
>
直连
</div>
<div
class="setting-option-item"
classList={{
"setting-option-item-selected": mode() == 2,
}}
onClick={async () => {
Mode(2)
await invoke("switch_mode", {
mode: 2,
})
}}
>
代理
</div>
</div>
<Switch fallback={"Need fix"}>
<Match when={!proxy()}>
<div class="setting-mirror">🎉Enjoy </div>
<Match when={mode() == 0}>
<div class="setting-mirror">🎉Enjoy mirror </div>
</Match>
<Match when={mode() == 1}>
<div class="setting-mirror">🎉Enjoy direct</div>
</Match>
<Match when={proxy()}>
<Match when={mode() == 2}>
<input
class="setting-proxy-url"
type="text"
Expand Down

0 comments on commit 25f5513

Please sign in to comment.