Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Clash Verge
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
many-archive
Clash Verge
Commits
7108d5f3
There was an error fetching the commit references. Please try again later.
Unverified
Commit
7108d5f3
authored
3 years ago
by
GyDi
Browse files
Options
Downloads
Patches
Plain Diff
fix: enhanced profile consistency
parent
ef47a749
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src-tauri/src/cmds.rs
+11
-17
11 additions, 17 deletions
src-tauri/src/cmds.rs
src-tauri/src/core/clash.rs
+48
-35
48 additions, 35 deletions
src-tauri/src/core/clash.rs
src-tauri/src/utils/resolve.rs
+2
-5
2 additions, 5 deletions
src-tauri/src/utils/resolve.rs
with
61 additions
and
57 deletions
src-tauri/src/cmds.rs
+
11
−
17
View file @
7108d5f3
use
crate
::{
use
crate
::{
core
::{
ClashInfo
,
PrfItem
,
Profiles
,
VergeConfig
},
core
::{
ClashInfo
,
PrfItem
,
Profiles
,
VergeConfig
},
ret_err
,
states
::{
ClashState
,
ProfilesState
,
VergeState
},
states
::{
ClashState
,
ProfilesState
,
VergeState
},
utils
::{
dirs
,
sysopt
::
SysProxyConfig
},
utils
::{
dirs
,
sysopt
::
SysProxyConfig
},
wrap_err
,
};
};
use
crate
::{
ret_err
,
wrap_err
};
use
anyhow
::
Result
;
use
anyhow
::
Result
;
use
serde_yaml
::
Mapping
;
use
serde_yaml
::
Mapping
;
use
std
::{
path
::
PathBuf
,
process
::
Command
};
use
std
::{
path
::
PathBuf
,
process
::
Command
};
...
@@ -80,7 +79,7 @@ pub async fn update_profile(
...
@@ -80,7 +79,7 @@ pub async fn update_profile(
// reactivate the profile
// reactivate the profile
if
Some
(
index
)
==
profiles
.get_current
()
{
if
Some
(
index
)
==
profiles
.get_current
()
{
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
wrap_err!
(
clash
.activate
(
&
profiles
))
?
;
wrap_err!
(
clash
.activate
(
&
profiles
,
false
))
?
;
}
}
Ok
(())
Ok
(())
...
@@ -97,7 +96,7 @@ pub fn select_profile(
...
@@ -97,7 +96,7 @@ pub fn select_profile(
wrap_err!
(
profiles
.put_current
(
index
))
?
;
wrap_err!
(
profiles
.put_current
(
index
))
?
;
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
wrap_err!
(
clash
.activate
(
&
profiles
))
wrap_err!
(
clash
.activate
(
&
profiles
,
false
))
}
}
/// change the profile chain
/// change the profile chain
...
@@ -108,16 +107,13 @@ pub fn change_profile_chain(
...
@@ -108,16 +107,13 @@ pub fn change_profile_chain(
clash_state
:
State
<
'_
,
ClashState
>
,
clash_state
:
State
<
'_
,
ClashState
>
,
profiles_state
:
State
<
'_
,
ProfilesState
>
,
profiles_state
:
State
<
'_
,
ProfilesState
>
,
)
->
Result
<
(),
String
>
{
)
->
Result
<
(),
String
>
{
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
let
mut
clash
=
clash_state
.0
.lock
()
.unwrap
();
let
mut
profiles
=
profiles_state
.0
.lock
()
.unwrap
();
let
mut
profiles
=
profiles_state
.0
.lock
()
.unwrap
();
profiles
.put_chain
(
chain
);
profiles
.put_chain
(
chain
);
clash
.set_window
(
app_handle
.get_window
(
"main"
));
app_handle
wrap_err!
(
clash
.activate_enhanced
(
&
profiles
,
false
))
.get_window
(
"main"
)
.map
(|
win
|
wrap_err!
(
clash
.activate_enhanced
(
&
profiles
,
win
,
false
)));
Ok
(())
}
}
/// manually exec enhanced profile
/// manually exec enhanced profile
...
@@ -127,14 +123,12 @@ pub fn enhance_profiles(
...
@@ -127,14 +123,12 @@ pub fn enhance_profiles(
clash_state
:
State
<
'_
,
ClashState
>
,
clash_state
:
State
<
'_
,
ClashState
>
,
profiles_state
:
State
<
'_
,
ProfilesState
>
,
profiles_state
:
State
<
'_
,
ProfilesState
>
,
)
->
Result
<
(),
String
>
{
)
->
Result
<
(),
String
>
{
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
let
mut
clash
=
clash_state
.0
.lock
()
.unwrap
();
let
profiles
=
profiles_state
.0
.lock
()
.unwrap
();
let
profiles
=
profiles_state
.0
.lock
()
.unwrap
();
app_handle
clash
.set_window
(
app_handle
.get_window
(
"main"
));
.get_window
(
"main"
)
.map
(|
win
|
wrap_err!
(
clash
.activate_enhanced
(
&
profiles
,
win
,
false
)));
Ok
((
))
wrap_err!
(
clash
.activate_enhanced
(
&
profiles
,
false
))
}
}
/// delete profile item
/// delete profile item
...
@@ -148,7 +142,7 @@ pub fn delete_profile(
...
@@ -148,7 +142,7 @@ pub fn delete_profile(
if
wrap_err!
(
profiles
.delete_item
(
index
))
?
{
if
wrap_err!
(
profiles
.delete_item
(
index
))
?
{
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
let
clash
=
clash_state
.0
.lock
()
.unwrap
();
wrap_err!
(
clash
.activate
(
&
profiles
))
?
;
wrap_err!
(
clash
.activate
(
&
profiles
,
false
))
?
;
}
}
Ok
(())
Ok
(())
...
@@ -291,7 +285,7 @@ pub fn patch_verge_config(
...
@@ -291,7 +285,7 @@ pub fn patch_verge_config(
wrap_err!
(
clash
.tun_mode
(
tun_mode
.unwrap
()))
?
;
wrap_err!
(
clash
.tun_mode
(
tun_mode
.unwrap
()))
?
;
clash
.update_config
();
clash
.update_config
();
wrap_err!
(
clash
.activate
(
&
profiles
))
?
;
wrap_err!
(
clash
.activate
(
&
profiles
,
false
))
?
;
}
}
Ok
(())
Ok
(())
...
...
This diff is collapsed.
Click to expand it.
src-tauri/src/core/clash.rs
+
48
−
35
View file @
7108d5f3
...
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
...
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
use
serde_yaml
::{
Mapping
,
Value
};
use
serde_yaml
::{
Mapping
,
Value
};
use
std
::{
collections
::
HashMap
,
time
::
Duration
};
use
std
::{
collections
::
HashMap
,
time
::
Duration
};
use
tauri
::
api
::
process
::{
Command
,
CommandChild
,
CommandEvent
};
use
tauri
::
api
::
process
::{
Command
,
CommandChild
,
CommandEvent
};
use
tauri
::
Window
;
use
tokio
::
time
::
sleep
;
use
tokio
::
time
::
sleep
;
#[derive(Default,
Debug,
Clone,
Deserialize,
Serialize)]
#[derive(Default,
Debug,
Clone,
Deserialize,
Serialize)]
...
@@ -23,7 +24,6 @@ pub struct ClashInfo {
...
@@ -23,7 +24,6 @@ pub struct ClashInfo {
pub
secret
:
Option
<
String
>
,
pub
secret
:
Option
<
String
>
,
}
}
#[derive(Debug)]
pub
struct
Clash
{
pub
struct
Clash
{
/// maintain the clash config
/// maintain the clash config
pub
config
:
Mapping
,
pub
config
:
Mapping
,
...
@@ -33,6 +33,9 @@ pub struct Clash {
...
@@ -33,6 +33,9 @@ pub struct Clash {
/// clash sidecar
/// clash sidecar
pub
sidecar
:
Option
<
CommandChild
>
,
pub
sidecar
:
Option
<
CommandChild
>
,
/// save the main window
pub
window
:
Option
<
Window
>
,
}
}
impl
Clash
{
impl
Clash
{
...
@@ -44,6 +47,7 @@ impl Clash {
...
@@ -44,6 +47,7 @@ impl Clash {
config
,
config
,
info
,
info
,
sidecar
:
None
,
sidecar
:
None
,
window
:
None
,
}
}
}
}
...
@@ -114,6 +118,11 @@ impl Clash {
...
@@ -114,6 +118,11 @@ impl Clash {
}
}
}
}
/// save the main window
pub
fn
set_window
(
&
mut
self
,
win
:
Option
<
Window
>
)
{
self
.window
=
win
;
}
/// run clash sidecar
/// run clash sidecar
pub
fn
run_sidecar
(
&
mut
self
)
->
Result
<
()
>
{
pub
fn
run_sidecar
(
&
mut
self
)
->
Result
<
()
>
{
let
app_dir
=
dirs
::
app_home_dir
();
let
app_dir
=
dirs
::
app_home_dir
();
...
@@ -156,7 +165,7 @@ impl Clash {
...
@@ -156,7 +165,7 @@ impl Clash {
self
.update_config
();
self
.update_config
();
self
.drop_sidecar
()
?
;
self
.drop_sidecar
()
?
;
self
.run_sidecar
()
?
;
self
.run_sidecar
()
?
;
self
.activate
(
profiles
)
self
.activate
(
profiles
,
false
)
}
}
/// update the clash info
/// update the clash info
...
@@ -241,12 +250,15 @@ impl Clash {
...
@@ -241,12 +250,15 @@ impl Clash {
self
.save_config
()
self
.save_config
()
}
}
/// activate the profile
/// generate a new profile to the temp_dir
/// then put the path to the clash core
fn
_activate
(
info
:
ClashInfo
,
config
:
Mapping
)
->
Result
<
()
>
{
fn
_activate
(
info
:
ClashInfo
,
config
:
Mapping
)
->
Result
<
()
>
{
let
temp_path
=
dirs
::
profiles_temp_path
();
let
temp_path
=
dirs
::
profiles_temp_path
();
config
::
save_yaml
(
temp_path
.clone
(),
&
config
,
Some
(
"# Clash Verge Temp File"
))
?
;
config
::
save_yaml
(
temp_path
.clone
(),
&
config
,
Some
(
"# Clash Verge Temp File"
))
?
;
tauri
::
async_runtime
::
spawn
(
async
move
{
tauri
::
async_runtime
::
spawn
(
async
move
{
let
server
=
info
.server
.
clone
()
.
unwrap
();
let
server
=
info
.server
.unwrap
();
let
server
=
format!
(
"http://{server}/configs"
);
let
server
=
format!
(
"http://{server}/configs"
);
let
mut
headers
=
HeaderMap
::
new
();
let
mut
headers
=
HeaderMap
::
new
();
...
@@ -263,22 +275,20 @@ impl Clash {
...
@@ -263,22 +275,20 @@ impl Clash {
// retry 5 times
// retry 5 times
for
_
in
0
..
5
{
for
_
in
0
..
5
{
match
reqwest
::
ClientBuilder
::
new
()
.no_proxy
()
.build
()
{
match
reqwest
::
ClientBuilder
::
new
()
.no_proxy
()
.build
()
{
Ok
(
client
)
=>
match
client
Ok
(
client
)
=>
{
.put
(
&
server
)
let
builder
=
client
.put
(
&
server
)
.headers
(
headers
.clone
())
.json
(
&
data
);
.headers
(
headers
.clone
())
.json
(
&
data
)
match
builder
.send
()
.await
{
.send
()
Ok
(
resp
)
=>
{
.await
if
resp
.status
()
!=
204
{
{
log
::
error!
(
"failed to activate clash for status
\"
{}
\"
"
,
resp
.status
());
Ok
(
resp
)
=>
{
}
if
resp
.status
()
!=
204
{
// do not retry
log
::
error!
(
"failed to activate clash for status
\"
{}
\"
"
,
resp
.status
())
;
break
;
}
}
// do not retry
Err
(
err
)
=>
log
::
error!
(
"failed to activate for `{err}`"
),
break
;
}
}
Err
(
err
)
=>
log
::
error!
(
"failed to activate for `{err}`"
),
}
},
Err
(
err
)
=>
log
::
error!
(
"failed to activate for `{err}`"
),
Err
(
err
)
=>
log
::
error!
(
"failed to activate for `{err}`"
),
}
}
sleep
(
Duration
::
from_millis
(
500
))
.await
;
sleep
(
Duration
::
from_millis
(
500
))
.await
;
...
@@ -288,26 +298,14 @@ impl Clash {
...
@@ -288,26 +298,14 @@ impl Clash {
Ok
(())
Ok
(())
}
}
/// activate the profile
/// enhanced profiles mode
pub
fn
activate
(
&
self
,
profiles
:
&
Profiles
)
->
Result
<
()
>
{
/// only change the enhanced profiles
let
info
=
self
.info
.clone
();
pub
fn
activate_enhanced
(
&
self
,
profiles
:
&
Profiles
,
delay
:
bool
)
->
Result
<
()
>
{
let
mut
config
=
self
.config
.clone
();
if
self
.window
.is_none
()
{
let
gen_map
=
profiles
.gen_activate
()
?
;
bail!
(
"failed to get the main window"
);
for
(
key
,
value
)
in
gen_map
.into_iter
()
{
config
.insert
(
key
,
value
);
}
}
Self
::
_activate
(
info
,
config
)
let
win
=
self
.window
.clone
()
.unwrap
();
}
/// enhanced profiles mode
pub
fn
activate_enhanced
(
&
self
,
profiles
:
&
Profiles
,
win
:
tauri
::
Window
,
delay
:
bool
,
)
->
Result
<
()
>
{
let
event_name
=
help
::
get_uid
(
"e"
);
let
event_name
=
help
::
get_uid
(
"e"
);
let
event_name
=
format!
(
"enhanced-cb-{event_name}"
);
let
event_name
=
format!
(
"enhanced-cb-{event_name}"
);
...
@@ -344,6 +342,21 @@ impl Clash {
...
@@ -344,6 +342,21 @@ impl Clash {
Ok
(())
Ok
(())
}
}
/// activate the profile
/// auto activate enhanced profile
pub
fn
activate
(
&
self
,
profiles
:
&
Profiles
,
delay
:
bool
)
->
Result
<
()
>
{
let
gen_map
=
profiles
.gen_activate
()
?
;
let
info
=
self
.info
.clone
();
let
mut
config
=
self
.config
.clone
();
for
(
key
,
value
)
in
gen_map
.into_iter
()
{
config
.insert
(
key
,
value
);
}
Self
::
_activate
(
info
,
config
)
?
;
self
.activate_enhanced
(
profiles
,
delay
)
}
}
}
impl
Default
for
Clash
{
impl
Default
for
Clash
{
...
...
This diff is collapsed.
Click to expand it.
src-tauri/src/utils/resolve.rs
+
2
−
5
View file @
7108d5f3
...
@@ -24,12 +24,9 @@ pub fn resolve_setup(app: &App) {
...
@@ -24,12 +24,9 @@ pub fn resolve_setup(app: &App) {
log_if_err!
(
clash
.run_sidecar
());
log_if_err!
(
clash
.run_sidecar
());
*
profiles
=
Profiles
::
read_file
();
*
profiles
=
Profiles
::
read_file
();
log_if_err!
(
clash
.activate
(
&
profiles
));
match
app
.get_window
(
"main"
)
{
clash
.set_window
(
app
.get_window
(
"main"
));
Some
(
win
)
=>
log_if_err!
(
clash
.activate_enhanced
(
&
profiles
,
win
,
true
)),
log_if_err!
(
clash
.activate
(
&
profiles
,
true
));
None
=>
log
::
error!
(
"failed to get window for enhanced profiles"
),
};
verge
.init_sysproxy
(
clash
.info.port
.clone
());
verge
.init_sysproxy
(
clash
.info.port
.clone
());
// enable tun mode
// enable tun mode
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment