@@ -14,7 +14,14 @@ import {
1414 usePluginsStore ,
1515 useRulesetsStore ,
1616} from '@/stores'
17- import { ignoredError , stringifyNoFolding , message , confirm } from '@/utils'
17+ import {
18+ ignoredError ,
19+ stringifyNoFolding ,
20+ message ,
21+ confirm ,
22+ APP_TITLE ,
23+ getTaskSchXmlString ,
24+ } from '@/utils'
1825
1926// Permissions Helper
2027export const SwitchPermissions = async ( enable : boolean ) => {
@@ -38,20 +45,24 @@ export const SwitchPermissions = async (enable: boolean) => {
3845 appPath ,
3946 '/f' ,
4047 ]
41- await Exec ( 'reg' , args )
48+ await Exec ( 'reg' , args , { Convert : true } )
4249}
4350
4451export const CheckPermissions = async ( ) => {
4552 const { appPath } = useEnvStore ( ) . env
4653 try {
47- const out = await Exec ( 'reg' , [
48- 'query' ,
49- 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers' ,
50- '/v' ,
51- appPath ,
52- '/t' ,
53- 'REG_SZ' ,
54- ] )
54+ const out = await Exec (
55+ 'reg' ,
56+ [
57+ 'query' ,
58+ 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers' ,
59+ '/v' ,
60+ appPath ,
61+ '/t' ,
62+ 'REG_SZ' ,
63+ ] ,
64+ { Convert : true } ,
65+ )
5566 return out . includes ( 'RunAsAdmin' )
5667 } catch {
5768 return false
@@ -349,25 +360,33 @@ export const GetSystemProxy = async () => {
349360 const { os } = useEnvStore ( ) . env
350361 try {
351362 if ( os === 'windows' ) {
352- const out1 = await Exec ( 'reg' , [
353- 'query' ,
354- 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings' ,
355- '/v' ,
356- 'ProxyEnable' ,
357- '/t' ,
358- 'REG_DWORD' ,
359- ] )
363+ const out1 = await Exec (
364+ 'reg' ,
365+ [
366+ 'query' ,
367+ 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings' ,
368+ '/v' ,
369+ 'ProxyEnable' ,
370+ '/t' ,
371+ 'REG_DWORD' ,
372+ ] ,
373+ { Convert : true } ,
374+ )
360375
361376 if ( / R E G _ D W O R D \s + 0 x 0 / . test ( out1 ) ) return ''
362377
363- const out2 = await Exec ( 'reg' , [
364- 'query' ,
365- 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings' ,
366- '/v' ,
367- 'ProxyServer' ,
368- '/t' ,
369- 'REG_SZ' ,
370- ] )
378+ const out2 = await Exec (
379+ 'reg' ,
380+ [
381+ 'query' ,
382+ 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings' ,
383+ '/v' ,
384+ 'ProxyServer' ,
385+ '/t' ,
386+ 'REG_SZ' ,
387+ ] ,
388+ { Convert : true } ,
389+ )
371390
372391 const regex = / P r o x y S e r v e r \s + R E G _ S Z \s + ( \S + ) /
373392 const match = out2 . match ( regex )
@@ -551,21 +570,62 @@ export const GetSystemOrKernelProxy = async () => {
551570 return proxy_cache . proxyPromise
552571}
553572
554- export const QuerySchTask = async ( taskName : string ) => {
555- await Exec ( 'Schtasks' , [ '/Query' , '/TN' , taskName , '/XML' ] , { Convert : true } )
573+ // Auto-start
574+ export const IsAutoStartEnabled = async ( ) => {
575+ const { os } = useEnvStore ( ) . env
576+ let isAutoStart = false
577+ if ( os === 'windows' ) {
578+ isAutoStart = await Exec ( 'Schtasks' , [ '/Query' , '/TN' , APP_TITLE , '/XML' ] , { Convert : true } )
579+ . then ( ( ) => true )
580+ . catch ( ( ) => false )
581+ } else if ( os === 'darwin' ) {
582+ isAutoStart = await Exec ( 'osascript' , [
583+ '-e' ,
584+ `tell application "System Events" to get exists login item "${ APP_TITLE } "` ,
585+ ] )
586+ . then ( ( res ) => res . includes ( 'true' ) )
587+ . catch ( ( ) => false )
588+ } else if ( os === 'linux' ) {
589+ // TODO
590+ }
591+ return isAutoStart
556592}
557593
558- export const CreateSchTask = async ( taskName : string , xmlPath : string ) => {
559- const fn = useEnvStore ( ) . env . isPrivileged ? Exec : RunWithPowerShell
560- await fn ( 'SchTasks' , [ '/Create' , '/F' , '/TN' , taskName , '/XML' , xmlPath ] , {
561- admin : true ,
562- hidden : true ,
563- } )
594+ export const EnableAutoStart = async ( delay = 10 ) => {
595+ const { os, appPath, basePath, isPrivileged } = useEnvStore ( ) . env
596+ if ( os === 'windows' ) {
597+ const xmlPath = await AbsolutePath ( 'data/.cache/tasksch.xml' )
598+ const xmlContent = getTaskSchXmlString ( appPath , delay )
599+ await WriteFile ( xmlPath , xmlContent )
600+ const fn = isPrivileged ? Exec : RunWithPowerShell
601+ await fn ( 'SchTasks' , [ '/Create' , '/F' , '/TN' , APP_TITLE , '/XML' , xmlPath ] , {
602+ admin : true ,
603+ hidden : true ,
604+ } )
605+ } else if ( os === 'darwin' ) {
606+ const path = basePath . replace ( '/Contents/MacOS' , '' )
607+ await Exec ( 'osascript' , [
608+ '-e' ,
609+ `tell application "System Events" to make login item at end with properties {name:"${ APP_TITLE } ", path:"${ path } "}` ,
610+ ] )
611+ } else if ( os === 'linux' ) {
612+ // TODO
613+ }
564614}
565615
566- export const DeleteSchTask = async ( taskName : string ) => {
567- const fn = useEnvStore ( ) . env . isPrivileged ? Exec : RunWithPowerShell
568- await fn ( 'SchTasks' , [ '/Delete' , '/F' , '/TN' , taskName ] , { admin : true , hidden : true } )
616+ export const DisableAutoStart = async ( ) => {
617+ const { os, isPrivileged } = useEnvStore ( ) . env
618+ if ( os === 'windows' ) {
619+ const fn = isPrivileged ? Exec : RunWithPowerShell
620+ await fn ( 'SchTasks' , [ '/Delete' , '/F' , '/TN' , APP_TITLE ] , { admin : true , hidden : true } )
621+ } else if ( os === 'darwin' ) {
622+ await Exec ( 'osascript' , [
623+ '-e' ,
624+ `tell application "System Events" to delete login item "${ APP_TITLE } "` ,
625+ ] )
626+ } else if ( os === 'linux' ) {
627+ // TODO
628+ }
569629}
570630
571631// Others
0 commit comments