Commit c5491608 authored by Administrator's avatar Administrator

update

parent 934b29df
......@@ -36,7 +36,6 @@
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:name="com.virjar.g4proxy.utils.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
......@@ -64,6 +63,16 @@
</intent-filter>
</service>
<service
android:name="com.virjar.g4proxy.service.SchedulerTaskService"
tools:ignore="ExportedService">
<intent-filter android:priority="1000">
<action android:name="com.virjar.g4proxy.service.scheuler" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
<!--开机自启服务-->
<receiver
android:name="com.virjar.g4proxy.StartOnBootBroadcastReceiver"
......@@ -83,11 +92,11 @@
<!--&lt;!&ndash;覆盖安装后唤醒service&ndash;&gt;-->
<!--<receiver android:name="com.virjar.g4proxy.SelfStartReceiver">-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.PACKAGE_REPLACED" />-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.PACKAGE_REPLACED" />-->
<!--<data android:scheme="package" />-->
<!--</intent-filter>-->
<!--<data android:scheme="package" />-->
<!--</intent-filter>-->
<!--</receiver>-->
<service
......
package com.virjar.g4proxy;
public class Constant {
public static final String TAG = "G4ProxyAgent";
public static final String sharedPreferenceFile = "config";
}
......@@ -109,14 +109,6 @@ public class HttpProxyService extends Service {
String clientKey = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
Log.i("weijia", "start G4Proxy front service");
Launcher.startHttpProxyService(3128);
// Log.i("weijia", "start private network forward task");
//
// ProxyClient.start(Constants.g4ProxyServerHost_1, Constants.g4ProxyServerPort, clientKey);
//
// ProxyClient.start(Constants.g4ProxyServerHost_2, Constants.g4ProxyServerPort, clientKey);
//新的代理服务器实现
LogbackConfig.config();
......
package com.virjar.g4proxy;
import android.app.ActivityManager;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.tencent.mm.R;
import com.virjar.g4proxy.service.SchedulerTaskService;
import com.virjar.g4proxy.utils.AccessAuthority;
import com.virjar.g4proxy.utils.DeviceInfoHolder;
import com.virjar.g4proxy.utils.DeviceMessageUtil;
import com.virjar.g4proxy.utils.HttpClientUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import top.wuhaojie.installerlibrary.AutoInstaller;
public class MainActivity extends AppCompatActivity {
public LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -44,20 +28,34 @@ public class MainActivity extends AppCompatActivity {
TextView textView = findViewById(R.id.text);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String clientKey = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
textView.setText("device: " + clientKey + "\n");
textView.setText("device: " + clientKey + "\n\n");
//待上传的设备信息:devicesMsg
JSONObject deviceMsg = DeviceMessageUtil.getDeviceMessage(locationManager, this, this);
JSONObject deviceMsg = DeviceMessageUtil.getDeviceMessage(this);
textView.append(deviceMsg.toString());
startService(new Intent(this, AppUpdateService.class));
startService(new Intent(this, SchedulerTaskService.class));
startService(new Intent(this, HttpProxyService.class));
AutoInstaller.getDefault(this).autoConfigAccessibilitySetting(this);
}
@Override
protected void onResume() {
super.onResume();
DeviceInfoHolder.initDeviceInfo(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//不给权限的话,循环申请
DeviceInfoHolder.initDeviceInfo(this);
}
}
......@@ -5,15 +5,17 @@ import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import com.virjar.g4proxy.service.SchedulerTaskService;
/**
* Created by liuzhaoce on 2019/4/14.
*/
public class SelfStartReceiver extends BroadcastReceiver{
// 这里我们先依靠slave进行守护保活
public class SelfStartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
......@@ -22,19 +24,19 @@ public class SelfStartReceiver extends BroadcastReceiver{
Uri data = intent.getData();
if (data == null){
if (data == null) {
return;
}
//取得安装的Apk的包名,只在该app覆盖安装后自启动
String installedPkgName = data.getSchemeSpecificPart();
if (action == null){
if (action == null) {
return;
}
if((action.equals(Intent.ACTION_PACKAGE_ADDED)
|| action.equals(Intent.ACTION_PACKAGE_REPLACED)) && installedPkgName.equals(localPkgName)){
if ((action.equals(Intent.ACTION_PACKAGE_ADDED)
|| action.equals(Intent.ACTION_PACKAGE_REPLACED)) && installedPkgName.equals(localPkgName)) {
Intent launchAppUpdateService = new Intent(context, AppUpdateService.class);
Intent launchAppUpdateService = new Intent(context, SchedulerTaskService.class);
launchAppUpdateService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent launchHttpProxyService = new Intent(context, HttpProxyService.class);
......
......@@ -6,6 +6,8 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.virjar.g4proxy.service.SchedulerTaskService;
/**
* Created by virjar on 2018/8/23.
*/
......@@ -16,7 +18,7 @@ public class StartOnBootBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.i("weijia", "receive start service broadcast");
context.startService(new Intent(context, AppUpdateService.class));
context.startService(new Intent(context, SchedulerTaskService.class));
context.startService(new Intent(context, HttpProxyService.class));
......
package com.virjar.g4proxy;
package com.virjar.g4proxy.bean;
/**
* Created by liuzhaoce on 2019/4/11.
......
package com.virjar.g4proxy;
package com.virjar.g4proxy.service;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
......@@ -11,11 +10,13 @@ import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.virjar.g4proxy.Constant;
import com.virjar.g4proxy.utils.DeviceMessageUtil;
import com.virjar.g4proxy.utils.HttpClientUtils;
import org.json.JSONException;
......@@ -23,61 +24,76 @@ import org.json.JSONObject;
import java.io.IOException;
import javax.annotation.Nullable;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;
import top.wuhaojie.installerlibrary.AutoInstaller;
public class AppUpdateService extends Service {
public class SchedulerTaskService extends Service {
//查询更新的间隔 (单位:分钟)
public static final int updateInterval = 10;
public static final String TAG = "AppUpdateService";
public static final String getNewestModifiedApkUrl = "http://www.scumall.com:5597/hermes/ModifiedApp/getNewestModifiedApk?appPackageName=";
private long lastCheckUpdateTimestamp = 0;
@Nullable
@Override
public IBinder onBind(Intent intent) {
startService();
checkAndRunTask();
return null;
}
@Override
public void onCreate() {
registerAlarm();
checkAndRunTask();
super.onCreate();
startService();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startService();
checkAndRunTask();
return START_STICKY;
}
private void startService() {
private void checkAndRunTask() {
if (System.currentTimeMillis() - lastCheckUpdateTimestamp < (updateInterval * 1000 * 60) / 3) {
return;
}
lastCheckUpdateTimestamp = System.currentTimeMillis();
runAppInstallTask();
runUploadMessageTask();
}
private void runUploadMessageTask() {
JSONObject deviceMessage = DeviceMessageUtil.getDeviceMessage(this);
}
private void runAppInstallTask() {
final String packageName = getApplicationInfo().packageName;
Request request = HttpClientUtils.getRequest(getNewestModifiedApkUrl + packageName);
HttpClientUtils.getClient().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@Nullable Call call, @Nullable IOException e) {
Log.e("get request", "get downloadUrl failed", e);
public void onFailure(@javax.annotation.Nullable Call call, @javax.annotation.Nullable IOException e) {
Log.e(Constant.TAG, "get downloadUrl failed", e);
}
@Override
public void onResponse(@Nullable Call call, @Nullable Response response) {
public void onResponse(@javax.annotation.Nullable Call call, @javax.annotation.Nullable Response response) {
try {
handleResponse(response, packageName);
} catch (JSONException | PackageManager.NameNotFoundException | IOException e) {
......@@ -85,25 +101,14 @@ public class AppUpdateService extends Service {
}
}
});
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int time = 1000 * 60 * updateInterval;
//获取系统开机至今所经历的毫秒
long startTime = SystemClock.elapsedRealtime() + time;
Intent intent = new Intent(this, AppUpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
if (alarmManager == null){
return;
}
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, startTime, pendingIntent);
}
private void doInstall(final String url) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.e(TAG, "installing");
AutoInstaller installer = AutoInstaller.getDefault(AppUpdateService.this);
Log.e(Constant.TAG, "installing");
AutoInstaller installer = AutoInstaller.getDefault(SchedulerTaskService.this);
installer.installFromUrl(url);
installer.setOnStateChangedListener(new AutoInstaller.OnStateChangedListener() {
@Override
......@@ -118,12 +123,12 @@ public class AppUpdateService extends Service {
@Override
public void onNeed2OpenService() {
Toast.makeText(AppUpdateService.this, "请打开辅助功能服务", Toast.LENGTH_SHORT).show();
Toast.makeText(SchedulerTaskService.this, "请打开辅助功能服务", Toast.LENGTH_SHORT).show();
}
@Override
public void needPermission() {
Toast.makeText(AppUpdateService.this, "需要申请存储空间权限", Toast.LENGTH_SHORT).show();
Toast.makeText(SchedulerTaskService.this, "需要申请存储空间权限", Toast.LENGTH_SHORT).show();
}
});
}
......@@ -132,25 +137,37 @@ public class AppUpdateService extends Service {
public void handleResponse(Response response, String packageName) throws JSONException, PackageManager.NameNotFoundException, IOException {
if (response.body() == null){
if (response.body() == null) {
return;
}
String responseJson = response.body().string();
Log.i(Constant.TAG, "app update config service response: " + responseJson);
JSONObject jsonObject = new JSONObject(responseJson);
jsonObject = jsonObject.optJSONObject("data");
String downloadUrl = jsonObject.optString("downloadUrl");
long versionCode = jsonObject.optLong("versionCode");
Log.i("app_update", "downloadUrl: " + downloadUrl);
Log.i("app_update", "up-to-date versionCode: " + versionCode);
Log.i(Constant.TAG, "downloadUrl: " + downloadUrl);
Log.i(Constant.TAG, "up-to-date versionCode: " + versionCode);
PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
if (packageInfo.versionCode < versionCode) {
Log.i(Constant.TAG, "the app need update,with download url: " + downloadUrl);
doInstall(downloadUrl);
}
}
@Override
public void onDestroy() {
super.onDestroy();
private void registerAlarm() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int time = 1000 * 60 * updateInterval;
//获取系统开机至今所经历的毫秒
long startTime = SystemClock.elapsedRealtime() + time;
Intent intent = new Intent(this, SchedulerTaskService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
if (alarmManager == null) {
return;
}
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, startTime, pendingIntent);
}
}
package com.virjar.g4proxy.utils;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import com.virjar.g4proxy.Constant;
import com.virjar.g4proxy.MainActivity;
public class DeviceInfoHolder {
private static String imei;
private static String androidId;
public static void initDeviceInfo(MainActivity activity) {
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
Log.e(Constant.TAG, "checkSelfPermission ACCESS_FINE_LOCATION did not show.");
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AccessAuthority.ACCESS_FINE_LOCATION_REQUEST_CODE.getRequestCode());
}
return;
}
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Log.e(Constant.TAG, "checkSelfPermission WRITE_EXTERNAL_STORAGE did not show.");
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, AccessAuthority.WRITE_EXTERNAL_STORAGE_REQUEST_CODE.getRequestCode());
}
return;
}
if (restoreInfo(activity)) {
return;
}
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE)) {
Log.e(Constant.TAG, "checkSelfPermission READ_PHONE_STATE did not show.");
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, AccessAuthority.READ_PHONE_STATE_REQUEST_CODE.getRequestCode());
}
return;
}
try {
TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager == null) {
Log.e(Constant.TAG, "can not get telephonyManager instance");
return;
}
@SuppressLint("HardwareIds") String imeiCopy = telephonyManager.getDeviceId();
if (!TextUtils.isEmpty(imeiCopy)) {
imei = imeiCopy;
}
androidId = Settings.System.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
save(activity);
} catch (Exception e) {
Log.e(Constant.TAG, "getIMEI failed!", e);
}
}
private static boolean restoreInfo(Context context) {
if (imei != null && androidId != null) {
return true;
}
SharedPreferences sharedPreferences = context.getSharedPreferences(Constant.sharedPreferenceFile, Context.MODE_PRIVATE);
String imei = sharedPreferences.getString("imei", null);
if (imei == null) {
return false;
}
DeviceInfoHolder.imei = imei;
String androidId = sharedPreferences.getString("androidId", null);
if (androidId == null) {
return false;
}
DeviceInfoHolder.androidId = androidId;
return true;
}
private static void save(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(Constant.sharedPreferenceFile, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("imei", imei);
edit.putString("androidId", androidId);
edit.apply();
}
public static String getCachedImei(Context context) {
restoreInfo(context);
return imei;
}
public static String getCachedAndroidId(Context context) {
restoreInfo(context);
return androidId;
}
}
package com.virjar.g4proxy.utils;
import android.app.Application;
import android.content.Context;
/**
* Created by liuzhaoce on 2019/4/12.
*/
public class MyApplication extends Application {
private static Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
context=getApplicationContext();
}
public static Context getContext() {
return context;
}
}
\ No newline at end of file
......@@ -13,6 +13,7 @@ import android.provider.Settings;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.tencent.mm.BuildConfig;
......@@ -175,6 +176,13 @@ public class AutoInstaller extends Handler {
}
}
public void autoConfigAccessibilitySetting(Context context) {
if (!isAccessibilitySettingsOn(mContext)) {
toAccessibilityService();
Toast.makeText(context, "请打开辅助模式", Toast.LENGTH_LONG).show();
}
}
private boolean permissionDenied() {
if (Build.VERSION.SDK_INT >= 23) {
String[] permissions = {
......@@ -194,7 +202,7 @@ public class AutoInstaller extends Handler {
private void toAccessibilityService() {
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment