优化代码
This commit is contained in:
@@ -1,15 +1,16 @@
|
|||||||
|
use std::{io, thread};
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
use std::net::UdpSocket;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{io, thread};
|
|
||||||
|
|
||||||
use console::style;
|
use console::style;
|
||||||
|
use windows_service::Error;
|
||||||
use windows_service::service::{
|
use windows_service::service::{
|
||||||
ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType, ServiceState, ServiceType,
|
ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType, ServiceState, ServiceType,
|
||||||
};
|
};
|
||||||
use windows_service::service_manager::{ServiceManager, ServiceManagerAccess};
|
use windows_service::service_manager::{ServiceManager, ServiceManagerAccess};
|
||||||
use windows_service::Error;
|
|
||||||
use switch::core::{Config, Switch};
|
use switch::core::{Config, Switch};
|
||||||
|
|
||||||
use crate::{BaseArgs, Commands, config};
|
use crate::{BaseArgs, Commands, config};
|
||||||
@@ -56,6 +57,10 @@ pub fn main0(base_args: BaseArgs) {
|
|||||||
if admin_check() {
|
if admin_check() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
// 允许应用通过防火墙
|
||||||
|
let _udp = UdpSocket::bind("0.0.0.0:0").unwrap();
|
||||||
|
}
|
||||||
match config::default_config(args) {
|
match config::default_config(args) {
|
||||||
Ok(start_config) => {
|
Ok(start_config) => {
|
||||||
match service_state() {
|
match service_state() {
|
||||||
@@ -260,7 +265,7 @@ fn install(path: PathBuf, auto: bool) -> Result<(), Error> {
|
|||||||
let service_info = ServiceInfo {
|
let service_info = ServiceInfo {
|
||||||
name: OsString::from(SERVICE_NAME),
|
name: OsString::from(SERVICE_NAME),
|
||||||
display_name: OsString::from("switch service v1"),
|
display_name: OsString::from("switch service v1"),
|
||||||
service_type: ServiceType::OWN_PROCESS,
|
service_type: SERVICE_TYPE,
|
||||||
start_type,
|
start_type,
|
||||||
error_control: ServiceErrorControl::Normal,
|
error_control: ServiceErrorControl::Normal,
|
||||||
executable_path: service_path.into(),
|
executable_path: service_path.into(),
|
||||||
@@ -294,7 +299,7 @@ fn change(auto: bool) -> Result<(), Error> {
|
|||||||
let service_info = ServiceInfo {
|
let service_info = ServiceInfo {
|
||||||
name: OsString::from(SERVICE_NAME),
|
name: OsString::from(SERVICE_NAME),
|
||||||
display_name: config.display_name,
|
display_name: config.display_name,
|
||||||
service_type: ServiceType::OWN_PROCESS,
|
service_type: SERVICE_TYPE,
|
||||||
start_type,
|
start_type,
|
||||||
error_control: config.error_control,
|
error_control: config.error_control,
|
||||||
executable_path: config.executable_path,
|
executable_path: config.executable_path,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ use switch::core::{Config, Switch};
|
|||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
use crate::windows::config::read_config;
|
use crate::windows::config::read_config;
|
||||||
|
use crate::windows::SERVICE_NAME;
|
||||||
|
|
||||||
define_windows_service!(ffi_service_main, switch_service_main);
|
define_windows_service!(ffi_service_main, switch_service_main);
|
||||||
pub fn switch_service_main(_arguments: Vec<OsString>) {
|
pub fn switch_service_main(_arguments: Vec<OsString>) {
|
||||||
@@ -50,7 +51,7 @@ fn service_main() -> windows_service::Result<()> {
|
|||||||
// Register system service event handler.
|
// Register system service event handler.
|
||||||
// The returned status handle should be used to report service status changes to the system.
|
// The returned status handle should be used to report service status changes to the system.
|
||||||
let status_handle =
|
let status_handle =
|
||||||
service_control_handler::register(crate::windows::SERVICE_NAME, event_handler)?;
|
service_control_handler::register(SERVICE_NAME, event_handler)?;
|
||||||
|
|
||||||
// Tell the system that service is running
|
// Tell the system that service is running
|
||||||
status_handle.set_service_status(ServiceStatus {
|
status_handle.set_service_status(ServiceStatus {
|
||||||
@@ -131,5 +132,5 @@ fn start_switch() -> switch::Result<Arc<Switch>> {
|
|||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
log::info!("以服务的方式启动");
|
log::info!("以服务的方式启动");
|
||||||
service_dispatcher::start("switch-service", ffi_service_main).unwrap();
|
service_dispatcher::start(SERVICE_NAME, ffi_service_main).unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ pub fn create_tun(
|
|||||||
.address(address)
|
.address(address)
|
||||||
.netmask(netmask)
|
.netmask(netmask)
|
||||||
.mtu(1420)
|
.mtu(1420)
|
||||||
// .queues(2)
|
// .queues(2) 用多个队列有兼容性问题
|
||||||
.up();
|
.up();
|
||||||
//
|
//
|
||||||
// config.platform(|config| {
|
// config.platform(|config| {
|
||||||
|
|||||||
Reference in New Issue
Block a user