@@ -260,7 +260,7 @@ public Integer call() throws RemoteException {
260260
261261@ CommandLine .Command (name = "modules" , subcommands = {ListModulesCommand .class , SetModulesCommand .class })
262262class ModulesCommand implements Runnable {
263- @ CommandLine .ParentCommand
263+ @ CommandLine .ParentCommand
264264 Main parent ;
265265
266266 @ CommandLine .Option (names = {"-h" , "--help" , "help" }, usageHelp = true , description = "display this help message" )
@@ -428,7 +428,7 @@ public Integer call() throws RemoteException {
428428
429429@ CommandLine .Command (name = "scope" , subcommands = {ListScopeCommand .class , SetScopeCommand .class })
430430class ScopeCommand implements Runnable {
431- @ CommandLine .ParentCommand
431+ @ CommandLine .ParentCommand
432432 Main parent ;
433433
434434 @ CommandLine .Option (names = {"-h" , "--help" , "help" }, usageHelp = true , description = "display this help message" )
@@ -665,7 +665,7 @@ public Integer call() throws RemoteException {
665665
666666@ CommandLine .Command (name = CMDNAME , subcommands = {LogCommand .class , LoginCommand .class , BackupCommand .class , ModulesCommand .class , RestoreCommand .class , ScopeCommand .class , StatusCommand .class , RevokePinCommand .class }, version = "0.3" )
667667public class Main implements Runnable {
668- @ CommandLine .Option (names = {"-p" , "--pin" }, description = "Authentication PIN for the CLI." , scope = CommandLine .ScopeType .INHERIT )
668+ @ CommandLine .Option (names = {"-p" , "--pin" }, description = "Authentication PIN for the CLI." , scope = CommandLine .ScopeType .INHERIT )
669669 String pin ;
670670
671671 @ CommandLine .Option (names = {"-V" , "--version" , "version" }, versionHelp = true , description = "display version info" )
@@ -683,13 +683,13 @@ public Main() {
683683 }
684684
685685 public static void main (String [] args ) {
686- System .exit (new CommandLine (new Main ())
687- .setExecutionExceptionHandler ((ex , commandLine , parseResult ) -> {
688- commandLine .getErr ().println (ex .getMessage ());
689- return ex instanceof SecurityException ? ERRCODES .AUTH_FAILED .ordinal () : ERRCODES .REMOTE_ERROR .ordinal ();
690- })
691- .execute (args ));
692- }
686+ System .exit (new CommandLine (new Main ())
687+ .setExecutionExceptionHandler ((ex , commandLine , parseResult ) -> {
688+ commandLine .getErr ().println (ex .getMessage ());
689+ return ex instanceof SecurityException ? ERRCODES .AUTH_FAILED .ordinal () : ERRCODES .REMOTE_ERROR .ordinal ();
690+ })
691+ .execute (args ));
692+ }
693693
694694 public void run () {
695695 throw new CommandLine .ParameterException (spec .commandLine (), "Missing required subcommand" );
@@ -713,7 +713,7 @@ public int handleExecutionException(Exception ex, CommandLine commandLine, Parse
713713 }
714714
715715 public final ICLIService getManager () {
716- if (objManager == null ) {
716+ if (objManager == null ) {
717717 try {
718718 objManager = connectToService ();
719719 if (objManager == null ) {
@@ -728,68 +728,68 @@ public final ICLIService getManager() {
728728 return objManager ;
729729 }
730730
731- private ICLIService connectToService () throws RemoteException {
732- // 1. Check for credentials provided by the user via arguments or environment.
733- // We store this in a separate variable to remember if the user even tried to provide a PIN.
734- String initialPin = this .pin ; // `this.pin` is populated by picocli from the --pin arg
735- if (initialPin == null ) {
736- initialPin = System .getenv ("LSPOSED_CLI_PIN" );
737- }
738- // `this.pin` will be used for the actual connection attempts.
739- this .pin = initialPin ;
740-
741- // 2. Connect to the basic application service binder (boilerplate).
742- var activityService = ServiceManager .getService ("activity" );
743- if (activityService == null ) throw new RemoteException ("Could not get activity service." );
744-
745- var binder = new Binder ();
746- Parcel data = Parcel .obtain ();
747- data .writeInterfaceToken ("LSPosed" );
748- data .writeInt (2 );
749- data .writeString ("lsp-cli:" + org .lsposed .lspd .util .SignInfo .CLI_UUID );
750- data .writeStrongBinder (binder );
751- Parcel reply = Parcel .obtain ();
752-
753- if (!activityService .transact (1598837584 , data , reply , 0 )) {
754- throw new RemoteException ("Transaction to activity service failed." );
755- }
756-
757- reply .readException ();
758- var serviceBinder = reply .readStrongBinder ();
759- if (serviceBinder == null ) throw new RemoteException ("Daemon did not return a service binder." );
760-
761- var service = ILSPApplicationService .Stub .asInterface (serviceBinder );
762- var lstBinder = new ArrayList <IBinder >(1 );
763-
764- // 3. First attempt: Authenticate with the credentials we have (which could be null).
765- service .requestCLIBinder (this .pin , lstBinder );
766-
767- // 4. Recovery step: If the first attempt failed, we have no PIN, AND we're in an
768- // interactive shell, then prompt the user as a last resort.
769- if (lstBinder .isEmpty () && this .pin == null && System .console () != null ) {
770- System .err .println ("Authentication required." );
771- char [] pinChars = System .console ().readPassword ("Enter CLI PIN: " );
772- if (pinChars != null ) {
773- this .pin = new String (pinChars );
774- // Second attempt: Retry with the PIN from the interactive prompt.
775- service .requestCLIBinder (this .pin , lstBinder );
776- }
777- }
778-
779- // 5. Final check and smart error reporting.
780- if (lstBinder .isEmpty ()) {
781- String errorMessage ;
782- if (initialPin == null ) {
783- // The user never provided a PIN. The daemon requires one. Guide the user.
784- errorMessage = "Authentication required. Use --pin, set LSPOSED_CLI_PIN, or use an interactive shell." ;
785- } else {
786- // The user provided a PIN, but it was rejected by the daemon.
787- errorMessage = "Authentication failed. The provided PIN is incorrect or CLI is disabled in the Manager app." ;
788- }
789- throw new SecurityException (errorMessage );
790- }
791-
792- // If we reach here, we are successful.
793- return ICLIService .Stub .asInterface (lstBinder .get (0 ));
794- }
731+ private ICLIService connectToService () throws RemoteException {
732+ // 1. Check for credentials provided by the user via arguments or environment.
733+ // We store this in a separate variable to remember if the user even tried to provide a PIN.
734+ String initialPin = this .pin ; // `this.pin` is populated by picocli from the --pin arg
735+ if (initialPin == null ) {
736+ initialPin = System .getenv ("LSPOSED_CLI_PIN" );
737+ }
738+ // `this.pin` will be used for the actual connection attempts.
739+ this .pin = initialPin ;
740+
741+ // 2. Connect to the basic application service binder (boilerplate).
742+ var activityService = ServiceManager .getService ("activity" );
743+ if (activityService == null ) throw new RemoteException ("Could not get activity service." );
744+
745+ var binder = new Binder ();
746+ Parcel data = Parcel .obtain ();
747+ data .writeInterfaceToken ("LSPosed" );
748+ data .writeInt (2 );
749+ data .writeString ("lsp-cli:" + org .lsposed .lspd .util .SignInfo .CLI_UUID );
750+ data .writeStrongBinder (binder );
751+ Parcel reply = Parcel .obtain ();
752+
753+ if (!activityService .transact (1598837584 , data , reply , 0 )) {
754+ throw new RemoteException ("Transaction to activity service failed." );
755+ }
756+
757+ reply .readException ();
758+ var serviceBinder = reply .readStrongBinder ();
759+ if (serviceBinder == null ) throw new RemoteException ("Daemon did not return a service binder." );
760+
761+ var service = ILSPApplicationService .Stub .asInterface (serviceBinder );
762+ var lstBinder = new ArrayList <IBinder >(1 );
763+
764+ // 3. First attempt: Authenticate with the credentials we have (which could be null).
765+ service .requestCLIBinder (this .pin , lstBinder );
766+
767+ // 4. Recovery step: If the first attempt failed, we have no PIN, AND we're in an
768+ // interactive shell, then prompt the user as a last resort.
769+ if (lstBinder .isEmpty () && this .pin == null && System .console () != null ) {
770+ System .err .println ("Authentication required." );
771+ char [] pinChars = System .console ().readPassword ("Enter CLI PIN: " );
772+ if (pinChars != null ) {
773+ this .pin = new String (pinChars );
774+ // Second attempt: Retry with the PIN from the interactive prompt.
775+ service .requestCLIBinder (this .pin , lstBinder );
776+ }
777+ }
778+
779+ // 5. Final check and smart error reporting.
780+ if (lstBinder .isEmpty ()) {
781+ String errorMessage ;
782+ if (initialPin == null ) {
783+ // The user never provided a PIN. The daemon requires one. Guide the user.
784+ errorMessage = "Authentication required. Use --pin, set LSPOSED_CLI_PIN, or use an interactive shell." ;
785+ } else {
786+ // The user provided a PIN, but it was rejected by the daemon.
787+ errorMessage = "Authentication failed. The provided PIN is incorrect or CLI is disabled in the Manager app." ;
788+ }
789+ throw new SecurityException (errorMessage );
790+ }
791+
792+ // If we reach here, we are successful.
793+ return ICLIService .Stub .asInterface (lstBinder .get (0 ));
794+ }
795795}
0 commit comments