mirror of
https://gitee.com/willfree/min-vpn-client_v2.git
synced 2026-10-04 19:42:00 +08:00
增加支持读取安卓设备信息,并转换为长度大约在10左右的设备标识,用于登录
This commit is contained in:
1 parent
0d9fdb2e8f
commit
70b161eb17
6 files changed
+178
-39
No files matched your search
@@ -10,6 +10,9 @@ package com.pkusz.min_vpn_client;
|
||||
import android.app.Application;
|
||||
|
||||
import com.pkusz.min_vpn_client.model.MINVpnSettingAPI;
|
||||
import com.pkusz.min_vpn_client.model.info.SafetyInfo;
|
||||
import com.pkusz.min_vpn_client.utils.SafetyInfoUtil;
|
||||
import com.pkusz.min_vpn_client.utils.UuidUtil;
|
||||
import com.pkusz.min_vpn_client.utils.gmutil.sm4.Android_SM4;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -23,6 +26,8 @@ import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
public class APP extends Application {
|
||||
public static String machineInfo ="Android_";
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
@@ -32,6 +37,13 @@ public class APP extends Application {
|
||||
System.out.println(MINVpnSettingAPI.INSTANCE.toString());
|
||||
// 加载BC
|
||||
loadRightBC();
|
||||
// 加载设备信息
|
||||
loadMachineInfo();
|
||||
}
|
||||
|
||||
public void loadMachineInfo(){
|
||||
machineInfo += UuidUtil.getAndroidDeviceUuid(this);
|
||||
System.out.println("本机唯一标识符:"+machineInfo);
|
||||
}
|
||||
|
||||
private boolean loadRightBC(){
|
||||
|
||||
@@ -29,6 +29,8 @@ import com.pkusz.min_vpn_client.test.TestSigned;
|
||||
import com.pkusz.min_vpn_client.utils.AccountValidatorRegexUtil;
|
||||
import com.pkusz.min_vpn_client.utils.DigestUtilKt;
|
||||
import com.pkusz.min_vpn_client.utils.KeyManager;
|
||||
import com.pkusz.min_vpn_client.utils.SafetyInfoUtil;
|
||||
import com.pkusz.min_vpn_client.utils.UuidUtil;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -79,6 +81,8 @@ public class LoginActivity extends MINVpnBaseActivity<LoginActivityPresenter>
|
||||
etPassword=findViewById(R.id.etPassword);
|
||||
|
||||
initView();
|
||||
// 测试唯一标识
|
||||
// System.out.println(UuidUtil.getAndroidDeviceUuid(this));
|
||||
}
|
||||
|
||||
private void initView(){
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.ArrayList;
|
||||
|
||||
public enum MINVpnSettingAPI {
|
||||
INSTANCE;
|
||||
|
||||
// 虽然不确定下面这个字符串为啥要放在目的标识的最后一个组件中,也没有测试不放会怎样,但猜测它是SafetyInfo转成的String
|
||||
public static String defraudString = "eyJNSUQiOiIiLCJNYWMiOiIiLCJCZWlEb3UiOiIiLCJHUFMiOiIiLCJTSU0iOiIifQ";
|
||||
|
||||
// 将信息持久化到本地的KEY
|
||||
|
||||
@@ -8,12 +8,14 @@ package com.pkusz.min_vpn_client.model;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import com.pkusz.min_vpn_client.APP;
|
||||
import com.pkusz.min_vpn_client.model.OriginalRequest.OriginalGetVpnInfoRequest;
|
||||
import com.pkusz.min_vpn_client.model.OriginalRequest.OriginalLoginRequest;
|
||||
import com.pkusz.min_vpn_client.model.OriginalRequest.OriginalRegisterRequest;
|
||||
import com.pkusz.min_vpn_client.model.Response.VpnServerInfoList;
|
||||
import com.pkusz.min_vpn_client.utils.KeyManager;
|
||||
import com.pkusz.min_vpn_client.utils.StringUtilForCPacketEncoding;
|
||||
import com.pkusz.min_vpn_client.utils.UuidUtil;
|
||||
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
|
||||
@@ -140,10 +142,9 @@ public enum VMSRequestAPI {
|
||||
}
|
||||
|
||||
// 3. 构造登录请求包
|
||||
String machineInfo = APP.machineInfo;
|
||||
LoginWithSignRequest loginWithSignRequest=new LoginWithSignRequest(original.username,
|
||||
original.passwd,cert,loginSign,
|
||||
"Android_"+System.currentTimeMillis()+"_"
|
||||
+StringUtilForCPacketEncoding.getFixLenthString(5));
|
||||
original.passwd,cert,loginSign,machineInfo);
|
||||
try {
|
||||
System.out.println("login request: "+loginWithSignRequest.toJson());
|
||||
} catch (IllegalAccessException e) {
|
||||
|
||||
@@ -38,43 +38,44 @@ public class SafetyInfoUtil {
|
||||
public static BDLocation lastLocation = null;
|
||||
public static LocationClient locationClient = null;
|
||||
|
||||
public static Location getLocation(Context context) {
|
||||
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
|
||||
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
Toast.makeText(context, "请开启GPS导航", Toast.LENGTH_SHORT).show();
|
||||
return null;
|
||||
}
|
||||
|
||||
// List<String> providerlist = locationManager.getAllProviders();
|
||||
// showProvider(providerlist);
|
||||
|
||||
Location locGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
// while(locGPS == null) {
|
||||
// locGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
// public static Location getLocation(Context context) {
|
||||
// LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
//
|
||||
// if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
// Toast.makeText(context, "请开启GPS导航", Toast.LENGTH_SHORT).show();
|
||||
// return null;
|
||||
// }
|
||||
return locGPS;
|
||||
}
|
||||
//
|
||||
//// List<String> providerlist = locationManager.getAllProviders();
|
||||
//// showProvider(providerlist);
|
||||
//
|
||||
// Location locGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
//// while(locGPS == null) {
|
||||
//// locGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
//// }
|
||||
// return locGPS;
|
||||
// }
|
||||
|
||||
public static Location getLastKnownLocation(Context context) {
|
||||
LocationManager mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
Toast.makeText(context, "请开启GPS导航", Toast.LENGTH_SHORT).show();
|
||||
return null;
|
||||
}
|
||||
List<String> providers = mLocationManager.getProviders(true);
|
||||
Location bestLocation = null;
|
||||
for (String provider : providers) {
|
||||
Location l = mLocationManager.getLastKnownLocation(provider);
|
||||
if (l == null) {
|
||||
continue;
|
||||
}
|
||||
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
|
||||
// Found best last known location: %s", l);
|
||||
bestLocation = l;
|
||||
}
|
||||
}
|
||||
return bestLocation;
|
||||
return null;
|
||||
// LocationManager mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||
// if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
// Toast.makeText(context, "请开启GPS导航", Toast.LENGTH_SHORT).show();
|
||||
// return null;
|
||||
// }
|
||||
// List<String> providers = mLocationManager.getProviders(true);
|
||||
// Location bestLocation = null;
|
||||
// for (String provider : providers) {
|
||||
// Location l = mLocationManager.getLastKnownLocation(provider);
|
||||
// if (l == null) {
|
||||
// continue;
|
||||
// }
|
||||
// if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
|
||||
// // Found best last known location: %s", l);
|
||||
// bestLocation = l;
|
||||
// }
|
||||
// }
|
||||
// return bestLocation;
|
||||
}
|
||||
|
||||
private static void showProvider(List<String> list){
|
||||
@@ -312,7 +313,6 @@ public class SafetyInfoUtil {
|
||||
return Base64Util.BlocChainEncode(originSafetyInfo);
|
||||
}
|
||||
|
||||
|
||||
public static SafetyInfo getSafetyInfo(Context context) {
|
||||
Location loc = getLastKnownLocation(context);
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package com.pkusz.min_vpn_client.utils;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:54 2021/11/3
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
|
||||
import org.zz.gmhelper.SM3Util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
import VMSConnection.Utils.Base64Helper;
|
||||
|
||||
public class UuidUtil {
|
||||
/**
|
||||
* 获取安卓设备唯一标识
|
||||
* 参考:https://www.cnblogs.com/qixingchao/p/11652408.html
|
||||
* @return
|
||||
*/
|
||||
public static String getAndroidDeviceUuid(Context context){
|
||||
//获得AndroidId(无需权限)
|
||||
String androidid = getAndroidId(context);
|
||||
//获得设备序列号(无需权限)
|
||||
String serial = getSERIAL();
|
||||
//获得硬件uuid(根据硬件相关属性,生成uuid)(无需权限)
|
||||
String uuid = getDeviceUUID().replace("-", "");
|
||||
System.out.println("------------------------");
|
||||
System.out.println(androidid);
|
||||
System.out.println(serial);
|
||||
System.out.println(uuid);
|
||||
System.out.println("------------------------");
|
||||
// 综合以上信息,经过sm3哈希取前16位
|
||||
String allInfo=androidid+serial+uuid+"android";
|
||||
return getSM3Hash8AndBase64(allInfo.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* 哈希取前8位,再做base64编码
|
||||
* @param rawBytes
|
||||
* @return
|
||||
*/
|
||||
public static String getSM3Hash8AndBase64(byte[] rawBytes){
|
||||
System.out.println("sm3哈希之前的密钥:"+new String(rawBytes));
|
||||
byte[] hashRes= SM3Util.hash(rawBytes);
|
||||
byte[] hashRes8=new byte[8];
|
||||
System.arraycopy(hashRes,0,hashRes8,0,8);
|
||||
System.out.println("sm3哈希得到的密钥:"+hashRes8.length+" , "+new String(hashRes8));
|
||||
System.out.println("sm3哈希得到的密钥:"+ Arrays.toString(hashRes8));
|
||||
// base64编码
|
||||
String resString= Base64Helper.Base64BasicEncode(hashRes8);
|
||||
System.out.println("sm3取八位+base64编码:"+resString);
|
||||
return resString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得设备的AndroidId
|
||||
*
|
||||
* @param context 上下文
|
||||
* @return 设备的AndroidId
|
||||
*/
|
||||
@SuppressLint("HardwareIds")
|
||||
private static String getAndroidId(Context context) {
|
||||
try {
|
||||
return Settings.Secure.getString(context.getContentResolver(),
|
||||
Settings.Secure.ANDROID_ID);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得设备序列号(如:WTK7N16923005607), 个别设备无法获取
|
||||
*
|
||||
* @return 设备序列号
|
||||
*/
|
||||
@SuppressLint("HardwareIds")
|
||||
private static String getSERIAL() {
|
||||
try {
|
||||
return Build.SERIAL;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得设备硬件uuid
|
||||
* 使用硬件信息,计算出一个随机数
|
||||
*
|
||||
* @return 设备硬件uuid
|
||||
*/
|
||||
@SuppressLint("HardwareIds")
|
||||
private static String getDeviceUUID() {
|
||||
try {
|
||||
@SuppressLint("HardwareIds") String dev = "3883756" +
|
||||
Build.BOARD.length() % 10 +
|
||||
Build.BRAND.length() % 10 +
|
||||
Build.DEVICE.length() % 10 +
|
||||
Build.HARDWARE.length() % 10 +
|
||||
Build.ID.length() % 10 +
|
||||
Build.MODEL.length() % 10 +
|
||||
Build.PRODUCT.length() % 10 +
|
||||
Build.SERIAL.length() % 10;
|
||||
return new UUID(dev.hashCode(),
|
||||
Build.SERIAL.hashCode()).toString();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user