Skip to content

Commit

Permalink
Support default impl for RemoteVerify
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Aug 17, 2017
1 parent 3fec5d8 commit 53617d2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ dependencies {
annotationProcessor "org.lzh.compiler.parceler:parceler-compiler:$PARCELER_VERSION"
compile "org.lzh.compiler.parceler:parceler-api:$PARCELER_VERSION"

compile "com.github.yjfnypeu.Router:router-host:$ROUTER_VERSION"
annotationProcessor "com.github.yjfnypeu.Router:router-compiler:$ROUTER_VERSION"
// compile "com.github.yjfnypeu.Router:router-host:$ROUTER_VERSION"
// annotationProcessor "com.github.yjfnypeu.Router:router-compiler:$ROUTER_VERSION"

// compile project(':host')
// annotationProcessor project(':compiler')
compile project(':host')
annotationProcessor project(':compiler')
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/lzh/nonview/router/demo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onCreate() {
RouterConfiguration.get().setCallback(new DefaultCallback());

// 启动远程服务。一般在插件化环境下使用。
// RouterConfiguration.get().startHostService("com.lzh.nonview.router.demo", this);
RouterConfiguration.get().startHostService("com.lzh.nonview.router.demo", this);
// 对应于启动远程服务操作。可设置此远程数据创建者。也应在插件化环境下使用。
// RouterConfiguration.get().setRemoteFactory(new RemoteFactory());
// 当默认的动作路由启动方式不能满足你项目需要时。通过定制此接口来做替换
Expand Down
30 changes: 30 additions & 0 deletions host/src/main/java/com/lzh/nonview/router/host/DefaultVerify.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lzh.nonview.router.host;

import android.content.Context;
import android.os.Binder;
import android.util.Log;

/**
* <b>Default impl for {@link RemoteVerify}</b>
*
* <p>This class to ensure that the remote-client should have the same package name with service.
*/
final class DefaultVerify implements RemoteVerify{

@Override
public boolean verify(Context context) throws Exception {
String packageName = context.getPackageName();
int uid = Binder.getCallingUid();
String[] packages = context.getPackageManager().getPackagesForUid(uid);
if (packages == null) {
packages = new String[0];
}
for (String pack: packages) {
if (packageName.equals(pack)) {
return true;
}
}
Log.e("DefaultVerify", String.format("The client with uid %s connected failed:", uid));
return false;
}
}
11 changes: 5 additions & 6 deletions host/src/main/java/com/lzh/nonview/router/host/RemoteVerify.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.lzh.nonview.router.host;

import android.app.Service;
import android.os.Binder;
import android.content.Context;

/**
* Provide an interface for security verification.
*/
public interface RemoteVerify {
/**
* Verify the plug-in you are connecting to.
* @param service The remote service.
* @param binder The remote binder
* Verify the client you are connecting to.
* @param context The application context to provide managers.
* @return returns true if safety
* @throws Exception error occurs
*/
boolean verify(Service service, Binder binder);
boolean verify(Context context) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@
package com.lzh.nonview.router.host;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;

import com.lzh.nonview.router.protocol.IService;
import com.lzh.nonview.router.module.RemoteRule;
import com.lzh.nonview.router.protocol.IService;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -36,12 +32,11 @@

/**
* Remote service to store route rules.
*
* @author haoge
*/
public class RouterHostService extends Service{

private static RemoteVerify verify;
private static RemoteVerify verify = new DefaultVerify();

public static void setVerify(RemoteVerify verify) {
RouterHostService.verify = verify;
Expand Down Expand Up @@ -86,12 +81,16 @@ public RemoteRule getActivityRule(Uri uri) throws RemoteException {

@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
// check for security verification
if (verify != null && !verify.verify(RouterHostService.this, this)) {
try {
// check for security verification
if (verify != null && !verify.verify(getApplicationContext())) {
return false;
}
return super.onTransact(code, data, reply, flags);
} catch (Exception e) {
e.printStackTrace();
return false;
}

return super.onTransact(code, data, reply, flags);
}
};

Expand Down

0 comments on commit 53617d2

Please sign in to comment.