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
b5bb39ef
There was an error fetching the commit references. Please try again later.
Commit
b5bb39ef
authored
3 years ago
by
GyDi
Browse files
Options
Downloads
Patches
Plain Diff
feat: put new profile to clash by default
parent
98bc9b07
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src-tauri/src/main.rs
+10
-1
10 additions, 1 deletion
src-tauri/src/main.rs
src-tauri/src/utils/clash.rs
+57
-2
57 additions, 2 deletions
src-tauri/src/utils/clash.rs
with
67 additions
and
3 deletions
src-tauri/src/main.rs
+
10
−
1
View file @
b5bb39ef
...
...
@@ -10,7 +10,7 @@ mod config;
mod
events
;
mod
utils
;
use
crate
::
events
::
state
::
ClashInfoState
;
use
crate
::
{
events
::
state
::
ClashInfoState
,
utils
::
clash
::
put_clash_profile
}
;
use
std
::
sync
::{
Arc
,
Mutex
};
use
tauri
::{
api
,
CustomMenuItem
,
Manager
,
SystemTray
,
SystemTrayEvent
,
SystemTrayMenu
,
SystemTrayMenuItem
,
...
...
@@ -68,6 +68,15 @@ fn main() -> std::io::Result<()> {
utils
::
init
::
init_app
(
app
.package_info
());
// run clash sidecar
let
info
=
utils
::
clash
::
run_clash_bin
(
&
app
.handle
());
// update the profile
let
info_copy
=
info
.clone
();
tauri
::
async_runtime
::
spawn
(
async
move
{
match
put_clash_profile
(
&
info_copy
)
.await
{
Ok
(
_
)
=>
{}
Err
(
err
)
=>
log
::
error!
(
"failed to put config for `{}`"
,
err
),
};
});
app
.manage
(
ClashInfoState
(
Arc
::
new
(
Mutex
::
new
(
info
))));
app
.run
(|
app_handle
,
e
|
match
e
{
...
...
This diff is collapsed.
Click to expand it.
src-tauri/src/utils/clash.rs
+
57
−
2
View file @
b5bb39ef
extern
crate
log
;
use
crate
::{
config
::
read_clash_controller
,
config
::
{
read_clash_controller
,
read_profiles
},
events
::
emit
::{
clash_start
,
ClashInfoPayload
},
utils
::
app_home_dir
,
};
use
reqwest
::
header
::
HeaderMap
;
use
std
::{
collections
::
HashMap
,
env
::
temp_dir
,
fs
};
use
tauri
::{
api
::
process
::{
Command
,
CommandEvent
},
AppHandle
,
...
...
@@ -58,6 +60,59 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
};
clash_start
(
app_handle
,
&
payload
);
payload
}
/// Update the clash profile firstly
pub
async
fn
put_clash_profile
(
payload
:
&
ClashInfoPayload
)
->
Result
<
(),
String
>
{
let
profile
=
{
let
profiles
=
read_profiles
();
let
current
=
profiles
.current
.unwrap_or
(
0u32
)
as
usize
;
match
profiles
.items
{
Some
(
items
)
=>
{
if
items
.len
()
==
0
{
return
Err
(
"can not read profiles"
.to_string
());
}
let
idx
=
if
current
<
items
.len
()
{
current
}
else
{
0
};
items
[
idx
]
.clone
()
}
None
=>
{
return
Err
(
"can not read profiles"
.to_string
());
}
}
};
// generate temp profile
let
file_name
=
match
profile
.file
{
Some
(
file_name
)
=>
file_name
.clone
(),
None
=>
{
return
Err
(
"the profile item should have `file` field"
.to_string
());
}
};
let
file_path
=
app_home_dir
()
.join
(
"profiles"
)
.join
(
file_name
);
let
temp_path
=
temp_dir
()
.join
(
"clash-verge-runtime.yaml"
);
if
!
file_path
.exists
()
{
return
Err
(
format!
(
"the profile `{:?}` not exists"
,
file_path
));
}
fs
::
copy
(
file_path
,
temp_path
.clone
())
.unwrap
();
let
server
=
payload
.controller
.clone
()
.unwrap
()
.server
.unwrap
();
let
server
=
format!
(
"http://{}/configs"
,
server
);
let
mut
headers
=
HeaderMap
::
new
();
headers
.insert
(
"Content-Type"
,
"application/json"
.parse
()
.unwrap
());
let
mut
data
=
HashMap
::
new
();
data
.insert
(
"path"
,
temp_path
.as_os_str
()
.to_str
()
.unwrap
());
let
client
=
reqwest
::
Client
::
new
();
match
client
.put
(
server
)
.headers
(
headers
)
.json
(
&
data
)
.send
()
.await
{
Ok
(
_
)
=>
Ok
(()),
Err
(
err
)
=>
Err
(
format!
(
"request failed with status `{}`"
,
err
.status
()
.unwrap
()
)),
}
}
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