forked from openstf/STFService.apk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openstf#32 from thinkhy/rootPR
add query message to get root status of device
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
app/src/main/java/jp/co/cyberagent/stf/query/GetRootStatusResponder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package jp.co.cyberagent.stf.query; | ||
|
||
import android.content.Context; | ||
|
||
import com.google.protobuf.GeneratedMessageLite; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.InputStreamReader; | ||
|
||
import jp.co.cyberagent.stf.proto.Wire; | ||
|
||
public class GetRootStatusResponder extends AbstractResponder { | ||
public GetRootStatusResponder(Context context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
public GeneratedMessageLite respond(Wire.Envelope envelope) { | ||
boolean rootFlag = isDeviceRooted(); | ||
|
||
return Wire.Envelope.newBuilder() | ||
.setId(envelope.getId()) | ||
.setType(Wire.MessageType.GET_ROOT_STATUS) | ||
.setMessage(Wire.GetRootStatusResponse.newBuilder() | ||
.setSuccess(true) | ||
.setStatus(rootFlag) | ||
.build() | ||
.toByteString()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void cleanup() { | ||
// No-op | ||
} | ||
|
||
public static boolean isDeviceRooted() { | ||
return checkRootMethod1() || checkRootMethod2() || checkRootMethod3(); | ||
} | ||
|
||
private static boolean checkRootMethod1() { | ||
String buildTags = android.os.Build.TAGS; | ||
return buildTags != null && buildTags.contains("test-keys"); | ||
} | ||
|
||
private static boolean checkRootMethod2() { | ||
String[] paths = { "/system/app/Superuser.apk", "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su", | ||
"/system/bin/failsafe/su", "/data/local/su", "/su/bin/su"}; | ||
for (String path : paths) { | ||
if (new File(path).exists()) return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean checkRootMethod3() { | ||
Process process = null; | ||
BufferedReader in = null; | ||
try { | ||
process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" }); | ||
in = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
if (in.readLine() != null) return true; | ||
return false; | ||
} catch (Throwable t) { | ||
return false; | ||
} finally { | ||
if (in != null) { | ||
try { | ||
in.close(); | ||
} catch (Exception ignore) { | ||
// Nothing to do | ||
} | ||
} | ||
if (process != null) process.destroy(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters