mirror of
https://gitee.com/willfree/min-vpn-client_v2.git
synced 2026-10-04 19:42:00 +08:00
迁移注册登录接口
This commit is contained in:
1 parent
e3ad010e01
commit
0d226e5c36
20 files changed
+316
-329
No files matched your search
+2
-1
@@ -77,7 +77,8 @@ android {
|
||||
dependencies {
|
||||
implementation files('libs/gmhelper.jar')
|
||||
implementation files('libs/BaiduLBS_Android.jar')
|
||||
implementation files('libs/vpnjce-1.0.jar')
|
||||
implementation files('libs/min-dev-java-1.0-release.jar')
|
||||
// implementation files('libs/vpnjce-1.0.jar')
|
||||
|
||||
// 在依赖中添加对jre的依赖,以尝试解决android.jar中原生javax与java中的javax的冲突问题
|
||||
//noinspection GradlePath
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -21,7 +21,7 @@ import com.pkusz.min_vpn_client.activity.main.MainActivity;
|
||||
import com.pkusz.min_vpn_client.activity.register.RegisterActivity;
|
||||
import com.pkusz.min_vpn_client.activity.setting.SettingActivity;
|
||||
import com.pkusz.min_vpn_client.model.MINVpnSettingAPI;
|
||||
import com.pkusz.min_vpn_client.model.request.LoginRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalLoginRequest;
|
||||
import com.pkusz.min_vpn_client.utils.AccountValidatorRegexUtil;
|
||||
import com.pkusz.min_vpn_client.utils.Base64Util;
|
||||
import com.pkusz.min_vpn_client.utils.DigestUtilKt;
|
||||
@@ -176,7 +176,7 @@ public class LoginActivity extends MINVpnBaseActivity<LoginActivityPresenter>
|
||||
KeyManager.INSTANCE.initKeyChain("/"+username);
|
||||
|
||||
// 登录server(密码转md5)
|
||||
LoginRequest loginRequest=new LoginRequest();
|
||||
OriginalLoginRequest loginRequest=new OriginalLoginRequest();
|
||||
loginRequest.Prefix="/"+username;
|
||||
loginRequest.Password= DigestUtilKt.encodeToMD5(password);
|
||||
if(GlobalSetting.openBiometric){
|
||||
|
||||
+7
-7
@@ -10,7 +10,7 @@ package com.pkusz.min_vpn_client.activity.login;
|
||||
import android.content.Context;
|
||||
|
||||
import com.pkusz.min_vpn_client.activity.base.MINVpnBaseView;
|
||||
import com.pkusz.min_vpn_client.model.request.LoginRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalLoginRequest;
|
||||
|
||||
import cn.qjm253.quick_android_mvp.base.mvp.BaseModel;
|
||||
import cn.qjm253.quick_android_mvp.base.mvp.BasePresenter;
|
||||
@@ -23,9 +23,9 @@ public interface LoginActivityContract {
|
||||
}
|
||||
|
||||
interface Model extends BaseModel {
|
||||
void login(LoginRequest loginRequest);
|
||||
void loginWithSafetyInfo(LoginRequest loginRequest, Context context);
|
||||
void downloadRegisterFacePicture(String username,LoginRequest loginRequest, Context context);
|
||||
void login(OriginalLoginRequest loginRequest);
|
||||
void loginWithSafetyInfo(OriginalLoginRequest loginRequest, Context context);
|
||||
void downloadRegisterFacePicture(String username, OriginalLoginRequest loginRequest, Context context);
|
||||
}
|
||||
|
||||
abstract class Presenter extends BasePresenter<LoginActivityContract.View, LoginActivityContract.Model> {
|
||||
@@ -33,8 +33,8 @@ public interface LoginActivityContract {
|
||||
void loginSuccess(String successMsg){}
|
||||
void loginFailed(String errMsg){}
|
||||
|
||||
void login(LoginRequest loginRequest){}
|
||||
void loginWithSafetyInfo(LoginRequest loginRequest, Context context){}
|
||||
void downloadRegisterFacePicture(String username,LoginRequest loginRequest, Context context){}
|
||||
void login(OriginalLoginRequest loginRequest){}
|
||||
void loginWithSafetyInfo(OriginalLoginRequest loginRequest, Context context){}
|
||||
void downloadRegisterFacePicture(String username, OriginalLoginRequest loginRequest, Context context){}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ package com.pkusz.min_vpn_client.activity.login;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.pkusz.min_vpn_client.model.request.LoginRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalLoginRequest;
|
||||
|
||||
import common.LoggerHelper;
|
||||
|
||||
@@ -21,7 +21,7 @@ public class LoginActivityModel implements LoginActivityContract.Model{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void login(LoginRequest loginRequest) {
|
||||
public void login(OriginalLoginRequest loginRequest) {
|
||||
// 与MIS通信,获取返回信息
|
||||
// Request_API.INSTANCE.login()
|
||||
// 假定成功登录,则执行下面语句
|
||||
@@ -34,7 +34,7 @@ public class LoginActivityModel implements LoginActivityContract.Model{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loginWithSafetyInfo(LoginRequest loginRequest, Context context) {
|
||||
public void loginWithSafetyInfo(OriginalLoginRequest loginRequest, Context context) {
|
||||
// 内含百度定位、mac地址等信息
|
||||
boolean flag=true;
|
||||
if(flag){
|
||||
@@ -45,7 +45,7 @@ public class LoginActivityModel implements LoginActivityContract.Model{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadRegisterFacePicture(String username, LoginRequest loginRequest, Context context) {
|
||||
public void downloadRegisterFacePicture(String username, OriginalLoginRequest loginRequest, Context context) {
|
||||
LoggerHelper.warning("model.downloadRegisterFacePicture: 暂停人脸识别登录功能");
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -9,7 +9,7 @@ package com.pkusz.min_vpn_client.activity.login;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.pkusz.min_vpn_client.model.request.LoginRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalLoginRequest;
|
||||
|
||||
public class LoginActivityPresenter extends LoginActivityContract.Presenter{
|
||||
public LoginActivityPresenter(LoginActivity loginActivity){
|
||||
@@ -33,17 +33,17 @@ public class LoginActivityPresenter extends LoginActivityContract.Presenter{
|
||||
}
|
||||
|
||||
@Override
|
||||
void login(LoginRequest loginRequest) {
|
||||
void login(OriginalLoginRequest loginRequest) {
|
||||
mModel.login(loginRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
void loginWithSafetyInfo(LoginRequest loginRequest, Context context) {
|
||||
void loginWithSafetyInfo(OriginalLoginRequest loginRequest, Context context) {
|
||||
mModel.loginWithSafetyInfo(loginRequest, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
void downloadRegisterFacePicture(String username, LoginRequest loginRequest, Context context) {
|
||||
void downloadRegisterFacePicture(String username, OriginalLoginRequest loginRequest, Context context) {
|
||||
mModel.downloadRegisterFacePicture(username, loginRequest, context);
|
||||
}
|
||||
}
|
||||
+7
-8
@@ -10,8 +10,7 @@ package com.pkusz.min_vpn_client.activity.register;
|
||||
import android.content.Context;
|
||||
|
||||
import com.pkusz.min_vpn_client.activity.base.MINVpnBaseView;
|
||||
import com.pkusz.min_vpn_client.model.request.LoginRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.RegisterRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalRegisterRequest;
|
||||
|
||||
import cn.qjm253.quick_android_mvp.base.mvp.BaseModel;
|
||||
import cn.qjm253.quick_android_mvp.base.mvp.BasePresenter;
|
||||
@@ -23,16 +22,16 @@ public interface RegisterActivityContract {
|
||||
}
|
||||
|
||||
interface Model extends BaseModel {
|
||||
void register(RegisterRequest registerRequest);
|
||||
void registerWithSafetyInfo(RegisterRequest registerRequest, Context context);
|
||||
void uploadRegisterFacePicture(String username,RegisterRequest registerRequest, Context context);
|
||||
void register(OriginalRegisterRequest registerRequest);
|
||||
void registerWithSafetyInfo(OriginalRegisterRequest registerRequest, Context context);
|
||||
void uploadRegisterFacePicture(String username, OriginalRegisterRequest registerRequest, Context context);
|
||||
}
|
||||
|
||||
abstract class Presenter extends BasePresenter<RegisterActivityContract.View, RegisterActivityContract.Model> {
|
||||
void registerSuccess(){}
|
||||
void registerFailed(String errMsg){}
|
||||
void register(RegisterRequest registerRequest){}
|
||||
void registerWithSafetyInfo(RegisterRequest registerRequest, Context context){}
|
||||
void uploadRegisterFacePicture(String username,RegisterRequest registerRequest, Context context){}
|
||||
void register(OriginalRegisterRequest registerRequest){}
|
||||
void registerWithSafetyInfo(OriginalRegisterRequest registerRequest, Context context){}
|
||||
void uploadRegisterFacePicture(String username, OriginalRegisterRequest registerRequest, Context context){}
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -9,7 +9,7 @@ package com.pkusz.min_vpn_client.activity.register;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.pkusz.min_vpn_client.model.request.RegisterRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalRegisterRequest;
|
||||
|
||||
public class RegisterActivityModel implements RegisterActivityContract.Model{
|
||||
private RegisterActivityPresenter mPresenter;
|
||||
@@ -18,17 +18,17 @@ public class RegisterActivityModel implements RegisterActivityContract.Model{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(RegisterRequest registerRequest) {
|
||||
public void register(OriginalRegisterRequest registerRequest) {
|
||||
mPresenter.registerSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerWithSafetyInfo(RegisterRequest registerRequest, Context context) {
|
||||
public void registerWithSafetyInfo(OriginalRegisterRequest registerRequest, Context context) {
|
||||
mPresenter.registerSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadRegisterFacePicture(String username, RegisterRequest registerRequest, Context context) {
|
||||
public void uploadRegisterFacePicture(String username, OriginalRegisterRequest registerRequest, Context context) {
|
||||
// 废弃接口。
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -9,7 +9,7 @@ package com.pkusz.min_vpn_client.activity.register;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.pkusz.min_vpn_client.model.request.RegisterRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalRegisterRequest;
|
||||
|
||||
public class RegisterActivityPresenter extends RegisterActivityContract.Presenter{
|
||||
public RegisterActivityPresenter(RegisterActivity registerActivity){
|
||||
@@ -28,17 +28,17 @@ public class RegisterActivityPresenter extends RegisterActivityContract.Presente
|
||||
}
|
||||
|
||||
@Override
|
||||
void register(RegisterRequest registerRequest) {
|
||||
void register(OriginalRegisterRequest registerRequest) {
|
||||
mModel.register(registerRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
void registerWithSafetyInfo(RegisterRequest registerRequest, Context context) {
|
||||
void registerWithSafetyInfo(OriginalRegisterRequest registerRequest, Context context) {
|
||||
mModel.registerWithSafetyInfo(registerRequest,context);
|
||||
}
|
||||
|
||||
@Override
|
||||
void uploadRegisterFacePicture(String username, RegisterRequest registerRequest, Context context) {
|
||||
void uploadRegisterFacePicture(String username, OriginalRegisterRequest registerRequest, Context context) {
|
||||
mModel.uploadRegisterFacePicture(username, registerRequest, context);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import android.content.Context;
|
||||
|
||||
import com.pkusz.min_vpn_client.utils.EncryptedSharedPreferenceUtil;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public enum MINVpnSettingAPI {
|
||||
INSTANCE;
|
||||
|
||||
@@ -70,6 +72,12 @@ public enum MINVpnSettingAPI {
|
||||
// private Integer serverSwitch=0;
|
||||
// app主context,通过它可以获取持久化到本地的配置信息
|
||||
private Context context;
|
||||
// VMS发来证书的默认密码
|
||||
private final byte[] certPasswd="123456".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
public byte[] getVMSCertPasswd(){
|
||||
return certPasswd;
|
||||
}
|
||||
|
||||
public void init(Application applicationContext){
|
||||
this.context=applicationContext;
|
||||
|
||||
@@ -1,189 +0,0 @@
|
||||
//package com.pkusz.min_vpn_client.model;
|
||||
///*
|
||||
// * @Author: Wang Feng
|
||||
// * @Description:
|
||||
// * @Version: 1.0.0
|
||||
// * @Date: 17:22 2021/6/23
|
||||
// * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
// */
|
||||
///**
|
||||
// * 使用Java重写API。基于min-lib-java。原有API调不通。
|
||||
// */
|
||||
//
|
||||
//import java.io.IOException;
|
||||
//import java.net.InetSocketAddress;
|
||||
//import java.net.Socket;
|
||||
//import java.nio.charset.StandardCharsets;
|
||||
//
|
||||
//import com.pkusz.min_vpn_client.model.request.BaseRequest;
|
||||
//import com.pkusz.min_vpn_client.utils.*;
|
||||
//import common.LoggerHelper;
|
||||
//import component.Identifier;
|
||||
//import component.IdentifierComponent;
|
||||
//import logicface.LogicFace;
|
||||
//import logicface.LogicFaceException;
|
||||
//import packet.Interest;
|
||||
//
|
||||
//public enum Request_API {
|
||||
// INSTANCE;
|
||||
// private boolean isConnected = false;
|
||||
// private LogicFace face = null;
|
||||
//
|
||||
// private String globalPrefix="/mis";
|
||||
// private boolean tcpTestFinished=false;
|
||||
// private boolean tcpValid=true;
|
||||
//
|
||||
// private final String rsaPublicKey =
|
||||
// "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMI" +
|
||||
// "IBCgKCAQEArQpGd3+hMWsc+Wbrt8RnMd175NkLASAHjdq1" +
|
||||
// "W2mySrgzTEzo4wWegcxp/Jgau0Chu4Sy4vRXfqjv8hIuUqaTmd" +
|
||||
// "ETkmwv4RM6lTM+neDCV+vWolE+KlLAaBEPRW54EdkinIbLGeK69OW" +
|
||||
// "DVvAnkKVxma6WuZeVpbj6W76Cc90426OFEH8eHHWg8QlG1QX8Z+2A" +
|
||||
// "fO/SzRjL1UBvYNzMg3Yr1+5W/km8Zc/vj5z" +
|
||||
// "DBOh85dd9zvxh2lcRQeX0T9akdlygWyFc/MPwuka0Q24ie5EXC1z6KC" +
|
||||
// "JTa92cF8cj1Mnp7MLjosW5EqfN66wMpM5VMB7/0asufFP1u/B1rd0B7MJaDwIDAQAB";
|
||||
//
|
||||
// private String gmSM2PublicKey="";
|
||||
//
|
||||
// public Request_API updateGlobalPrefix(String prefix){
|
||||
// this.globalPrefix=prefix;
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 通过TcpTransport的方式连接到一个远端的NDN路由器
|
||||
// * * 1. 如果未连接到一个远端路由器,则尝试连接一个远端的路由器
|
||||
// * * 2. 如果已连接到一个路由器,则忽略本操作
|
||||
// * @param host
|
||||
// * @param port
|
||||
// * @return
|
||||
// */
|
||||
// public Request_API open(String host,Integer port){
|
||||
// if(face!=null){
|
||||
// this.close();
|
||||
// }
|
||||
// this.tcpConnectionTest(host,port);
|
||||
// face=new LogicFace();
|
||||
// LoggerHelper.severe("##################"+host+", "+port);
|
||||
// try {
|
||||
// face.initWithTcp(host,port);
|
||||
// System.out.println("create face: "+host+": "+port);
|
||||
// } catch (LogicFaceException e) {
|
||||
// System.out.println("failed to create face: "+host+": "+port);
|
||||
// }
|
||||
// isConnected=true;
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// public void tcpConnectionTest(String host,Integer port){
|
||||
// Thread t=new Thread(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// try {
|
||||
// Socket socket=new Socket();
|
||||
// InetSocketAddress inetSocketAddress=new InetSocketAddress(host,port);
|
||||
// socket.connect(inetSocketAddress,8000);
|
||||
// tcpTestFinished=true;
|
||||
// tcpValid=true;
|
||||
// LoggerHelper.fine("test not timeout");
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// tcpTestFinished=true;
|
||||
// tcpValid=false;
|
||||
// LoggerHelper.warning("test timeout");
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// t.start();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 断开与远端路由器的连接
|
||||
// * @return
|
||||
// */
|
||||
// public Request_API close(){
|
||||
// if(!isConnected){
|
||||
// return this;
|
||||
// }
|
||||
// face.shutDown();
|
||||
// tcpTestFinished=false;
|
||||
// isConnected=false;
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 统一构造兴趣包(自动填充command,uuid,username,自动对兴趣包进行签名)
|
||||
// */
|
||||
// private Interest commonGenerateInterest(String apiName,
|
||||
// BaseRequest requestData,
|
||||
// boolean canBePrefix,boolean mustBeRefresh,
|
||||
// double interestLifetime,String prefixAddition,
|
||||
// boolean needBeEncrypt){
|
||||
// try {
|
||||
// Interest interest=new Interest();
|
||||
// interest.setName(new Identifier(globalPrefix+apiName+prefixAddition));
|
||||
// interest.interestLifeTime.setInterestLifeTime((long) interestLifetime);
|
||||
// interest.canBePrefix.setCanBePrefix(canBePrefix);
|
||||
// interest.mustBeRefresh.setMustBeRefresh(mustBeRefresh);
|
||||
// LoggerHelper.fine("commonGenerateInterest-request data: "+requestData.toJson());
|
||||
// String result = "";
|
||||
// if(needBeEncrypt){
|
||||
// LoggerHelper.fine("commonGenerateInterest-需要加密");
|
||||
// // todo: 啥???
|
||||
// }else{
|
||||
// LoggerHelper.fine("commonGenerateInterest-不用加密");
|
||||
// result=requestData.toJson();
|
||||
// }
|
||||
// interest.payload.setValue(result.getBytes(StandardCharsets.UTF_8));
|
||||
//
|
||||
// // 使用KeyChain统一签名
|
||||
// KeyManager.INSTANCE.getKeyChain().signInterest(interest);
|
||||
// return interest;
|
||||
// } catch (Exception e) {
|
||||
// LoggerHelper.warning("commonGenerateInterest-error: "+e.getMessage());
|
||||
// e.printStackTrace();
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private Interest safelyGenerateInterest(String apiName,
|
||||
// BaseRequest requestData,
|
||||
// boolean canBePrefix,boolean mustBeRefresh,
|
||||
// double interestLifetime,String prefixAddition,
|
||||
// boolean needBeEncrypt,String safetyInfo){
|
||||
// try {
|
||||
// Interest interest=new Interest();
|
||||
// Identifier name=new Identifier(globalPrefix+apiName+prefixAddition);
|
||||
// name.append(new IdentifierComponent(safetyInfo));
|
||||
// interest.setName(name);
|
||||
//
|
||||
// interest.interestLifeTime.setInterestLifeTime((long) interestLifetime);
|
||||
// interest.canBePrefix.setCanBePrefix(canBePrefix);
|
||||
// interest.mustBeRefresh.setMustBeRefresh(mustBeRefresh);
|
||||
//
|
||||
// LoggerHelper.fine("commonGenerateInterest-request data: "+requestData.toJson());
|
||||
// String result = "";
|
||||
// if(needBeEncrypt){
|
||||
// LoggerHelper.fine("commonGenerateInterest-需要加密");
|
||||
// // todo: 啥???
|
||||
// }else{
|
||||
// LoggerHelper.fine("commonGenerateInterest-不用加密");
|
||||
// result=requestData.toJson();
|
||||
// LoggerHelper.fine("result: "+result);
|
||||
// }
|
||||
// interest.payload.setValue(result.getBytes(StandardCharsets.UTF_8));
|
||||
//
|
||||
// // 使用KeyChain统一签名
|
||||
// KeyManager.INSTANCE.getKeyChain().signInterest(interest);
|
||||
// return interest;
|
||||
// } catch (Exception e) {
|
||||
// LoggerHelper.warning("commonGenerateInterest-error: "+e.getMessage());
|
||||
// e.printStackTrace();
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//// public Observable<BaseResponse> easyRequest(){
|
||||
////
|
||||
//// }
|
||||
//}
|
||||
@@ -0,0 +1,199 @@
|
||||
package com.pkusz.min_vpn_client.model;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 支持与VMS进行TCP通信,以提供VPN客户端的注册、登录等网络请求功能
|
||||
* 实现参考自min-dev-java中example目录下的VPNLoginExample文件。
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:11 2021/7/29
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalLoginRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.OriginalRegisterRequest;
|
||||
import com.pkusz.min_vpn_client.utils.KeyManager;
|
||||
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
|
||||
import VMSConnection.BC_API;
|
||||
import VMSConnection.Model.LoginWithSignRequest;
|
||||
import VMSConnection.Model.RegisterWithSignRequest;
|
||||
import VMSConnection.TCPNet.Message.NetworkResponse;
|
||||
import VMSConnection.Utils.Base64Helper;
|
||||
import examples.KeyManagerExample;
|
||||
import minsecurity.certificate.cert.CertException;
|
||||
|
||||
public enum VMSRequestAPI {
|
||||
INSTANCE;
|
||||
|
||||
/**
|
||||
* 构造并发送VMS注册请求,返回服务器传回的应答码【0或null或其它非200,均属注册失败】
|
||||
* @return
|
||||
*/
|
||||
public Integer register(OriginalRegisterRequest original){
|
||||
// 1. 生成用户的公钥私钥(有则直接取出);
|
||||
this.generateForeverIdentity(original.username);
|
||||
|
||||
// 2. 签名(签名用户名) & 公钥(用户本地保存的公钥)
|
||||
byte[] registerSign=this.signBytes(original.username);
|
||||
String pubKey=new String(KeyManagerExample.INSTANCE.getKeyChain().getCurrentIdentity().getPubkey()
|
||||
.getBytes(),StandardCharsets.UTF_8);
|
||||
System.out.println("register pubkey: "+pubKey);
|
||||
|
||||
// 3. 构造注册请求包
|
||||
String timestamp= String.valueOf(System.currentTimeMillis());
|
||||
RegisterWithSignRequest request=new RegisterWithSignRequest(1,original.username,original.passwd,
|
||||
original.phoneNumber,original.inviteNumber,original.email,"dev","",
|
||||
pubKey,0,0,timestamp,registerSign);
|
||||
try {
|
||||
System.out.println("register request: "+request.toJson());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 4. 调用bc-api,发送注册请求到VMS:ip tcpPort sslPort VMSPubkey(可选,有默认key)
|
||||
BC_API bc_api=new BC_API(MINVpnSettingAPI.INSTANCE.getBlockChainIpAddress(),
|
||||
MINVpnSettingAPI.INSTANCE.getBcTcpPort(),MINVpnSettingAPI.INSTANCE.getBcSslPort());
|
||||
NetworkResponse networkResponse=bc_api.register(request);
|
||||
try {
|
||||
System.out.println("注册应答: "+networkResponse.toJson());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("注册应答码: "+networkResponse.Code);
|
||||
|
||||
// 5. 保存注册应答中的证书到本地
|
||||
boolean flag=false;
|
||||
try {
|
||||
this.importVMSCert(networkResponse.Data, MINVpnSettingAPI.INSTANCE.getVMSCertPasswd());
|
||||
}catch (Exception e){
|
||||
System.out.println("保存注册证书失败: "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("保存证书应答: "+flag);
|
||||
|
||||
return networkResponse.Code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造并发送VMS登录请求,返回服务器传回的应答码【0或null或其它非200,均属注册失败】
|
||||
* @param original
|
||||
* @return
|
||||
*/
|
||||
public Integer login(OriginalLoginRequest original){
|
||||
// 1. 生成用户的公钥私钥(有则直接取出)
|
||||
this.generateForeverIdentity(original.username);
|
||||
|
||||
// 2. 签名(签名用户名) & 证书(用户本地保存的证书,注册时接收到的VMS数据)
|
||||
byte[] loginSign= this.signBytes(original.username);
|
||||
String cert=this.exportVMSCert(MINVpnSettingAPI.INSTANCE.getVMSCertPasswd());
|
||||
|
||||
// 3. 构造登录请求包
|
||||
LoginWithSignRequest loginWithSignRequest=new LoginWithSignRequest(original.username,
|
||||
original.passwd,cert,loginSign);
|
||||
try {
|
||||
System.out.println("login request: "+loginWithSignRequest.toJson());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 4. 调用bc-api,发送登录请求到VMS:ip tcpPort sslPort VMSPubkey(可选,有默认key)
|
||||
BC_API bc_api=new BC_API(MINVpnSettingAPI.INSTANCE.getBlockChainIpAddress(),
|
||||
MINVpnSettingAPI.INSTANCE.getBcTcpPort(),MINVpnSettingAPI.INSTANCE.getBcSslPort());
|
||||
NetworkResponse networkResponse=bc_api.login(loginWithSignRequest);
|
||||
try {
|
||||
System.out.println("登录应答: "+networkResponse.toJson());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("登录应答码: "+networkResponse.Code);
|
||||
|
||||
return networkResponse.Code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成本地密钥,持久化存储到本地硬盘
|
||||
* @param username
|
||||
* @param identityPath
|
||||
*/
|
||||
private void generateForeverIdentity(String username,String identityPath){
|
||||
// identityName是将username进行base64编码,然后加上"/"
|
||||
String identityName="/"+ Base64Helper.Base64PlusEncode(username);
|
||||
KeyManager.INSTANCE.initKeyChain(identityName,identityPath);
|
||||
}
|
||||
private void generateForeverIdentity(String username){
|
||||
// identityName是将username进行base64编码,然后加上"/"
|
||||
String identityName="/"+ Base64Helper.Base64PlusEncode(username);
|
||||
KeyManager.INSTANCE.initKeyChain(identityName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用KeyChain的当前用户身份的私钥对指定字节数组进行签名
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
private byte[] signBytes(byte[] bytes){
|
||||
try {
|
||||
return KeyManager.INSTANCE.getKeyChain().getCurrentIdentity().sign(bytes);
|
||||
} catch (CryptoException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private byte[] signBytes(String bytes){
|
||||
return signBytes(bytes.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入进行用户注册时VMS返回的证书
|
||||
* @param certString
|
||||
* @param certPasswd 默认是123456
|
||||
* @return
|
||||
*/
|
||||
private boolean importVMSCert(byte[] certString,byte[] certPasswd){
|
||||
try {
|
||||
return KeyManager.INSTANCE.getKeyChain().getIdentifyManager()
|
||||
.importCert(certString,certPasswd);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private boolean importVMSCert(String certString,byte[] certPasswd){
|
||||
return importVMSCert(certString.getBytes(StandardCharsets.UTF_8),certPasswd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出指定身份标识的VMS证书
|
||||
* @param identityName
|
||||
* @param certPasswd 默认123456
|
||||
* @return
|
||||
*/
|
||||
private String exportVMSCert(String identityName,byte[] certPasswd){
|
||||
try {
|
||||
return KeyManager.INSTANCE.getKeyChain().getIdentifyManager()
|
||||
.dumpCert(identityName, certPasswd);
|
||||
} catch (NoSuchProviderException | InvalidAlgorithmParameterException
|
||||
| NoSuchPaddingException | InvalidKeyException | CertException
|
||||
| IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private String exportVMSCert(byte[] certPasswd){
|
||||
// 取出当前用户的身份标识名称
|
||||
String identityName=KeyManager.INSTANCE.getKeyChain().getCurrentIdentity()
|
||||
.getName();
|
||||
System.out.println("!!!: "+identityName);
|
||||
return exportVMSCert(identityName,certPasswd);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.pkusz.min_vpn_client.model.request;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:22 2021/6/23
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class BaseRequest {
|
||||
public String Command="base";
|
||||
public static String Type= "user-act";
|
||||
|
||||
public String toJson() throws IllegalAccessException {
|
||||
JSONObject jsonObject=new JSONObject();
|
||||
// 遍历属性值
|
||||
Class cls=this.getClass();
|
||||
Field[] fields=cls.getDeclaredFields();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
Field f=fields[i];
|
||||
f.setAccessible(true);
|
||||
// 将属性值的名字的属性值的值放入jsonObject
|
||||
jsonObject.put(f.getName(),f.get(this));
|
||||
}
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.pkusz.min_vpn_client.model.request;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:22 2021/6/23
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
public class LoginRequest extends BaseRequest{
|
||||
// 继承属性
|
||||
public String Type = BaseRequest.Type;
|
||||
public String Command = "Login";
|
||||
// 其他属性
|
||||
public String Prefix;
|
||||
public String Password;
|
||||
public String Timestamp = String.valueOf((System.currentTimeMillis() / 1000));
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.pkusz.min_vpn_client.model.request;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 登录请求的原始属性
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:22 2021/6/23
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
public class OriginalLoginRequest {
|
||||
public String username;
|
||||
public String passwd;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.pkusz.min_vpn_client.model.request;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 注册请求的原始属性
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:22 2021/6/23
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
public class OriginalRegisterRequest{
|
||||
public String username;
|
||||
public String passwd;
|
||||
public String phoneNumber;
|
||||
public String email;
|
||||
public String inviteNumber;
|
||||
|
||||
public OriginalRegisterRequest(String username, String passwd,
|
||||
String phoneNumber, String email, String inviteNumber) {
|
||||
this.username = username;
|
||||
this.passwd = passwd;
|
||||
this.phoneNumber = phoneNumber;
|
||||
this.email = email;
|
||||
this.inviteNumber = inviteNumber;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.pkusz.min_vpn_client.model.request;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:22 2021/6/23
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
public class RegisterRequest extends BaseRequest{
|
||||
// 继承属性
|
||||
public String Type = BaseRequest.Type;
|
||||
public String Command = "Registry";
|
||||
// 其他属性
|
||||
public String Pubkey;
|
||||
public String Prefix;
|
||||
public String Username;
|
||||
public String Realname;
|
||||
public String Phone;
|
||||
public String Iris;
|
||||
public String Action; // 邀请码
|
||||
public String Timestamp = String.valueOf((System.currentTimeMillis() / 1000));
|
||||
public Integer Level = 0; // (int 类型,0表示资源发布者,1表示资源浏览者)
|
||||
public String IDcard = "";
|
||||
public String AboutMe = "";
|
||||
public String Face = "";
|
||||
public String Print = "";
|
||||
public String Other = "";
|
||||
public String Sig = "";
|
||||
public String EMail = "";
|
||||
}
|
||||
@@ -70,6 +70,17 @@ public enum KeyManager {
|
||||
return getCertification(identityName,appPath);
|
||||
}
|
||||
|
||||
public Identity getIdentity(String identityName,String appKeyPath){
|
||||
return KeyManager.INSTANCE.initKeyChain(identityName,appKeyPath)
|
||||
.getKeyChain()
|
||||
.getCurrentIdentity();
|
||||
}
|
||||
|
||||
public KeyChain getKeyChain(String identityName,String appKeyPath){
|
||||
return KeyManager.INSTANCE.initKeyChain(identityName,appKeyPath)
|
||||
.getKeyChain();
|
||||
}
|
||||
|
||||
// 判断本地是否有某个标识名对应的证书
|
||||
public boolean isInKeyChain(String identityName,String appKeyPath) {
|
||||
KeyChain keyChain = null;
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
package com.pkusz.min_vpn_client.model;
|
||||
|
||||
import com.pkusz.min_vpn_client.model.request.BaseRequest;
|
||||
import com.pkusz.min_vpn_client.model.request.RegisterRequest;
|
||||
//import com.pkusz.min_vpn_client.model.request.BaseRequest;
|
||||
//import com.pkusz.min_vpn_client.model.request.OriginalRegisterRequest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestRequest {
|
||||
/**
|
||||
* 测试所用的org.json是调用android库的,而这里的测试不能调用它;
|
||||
* 所以最后这里使用的json解析是用的om.alibaba.fastjson这个库.
|
||||
*/
|
||||
@Test
|
||||
public void testBaseR2Json() {
|
||||
BaseRequest baseRequest=new BaseRequest();
|
||||
try {
|
||||
System.out.println(baseRequest.toJson());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRR2Json() {
|
||||
RegisterRequest request=new RegisterRequest();
|
||||
System.out.println(request.Type);
|
||||
try {
|
||||
System.out.println(request.toJson());
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * 测试所用的org.json是调用android库的,而这里的测试不能调用它;
|
||||
// * 所以最后这里使用的json解析是用的om.alibaba.fastjson这个库.
|
||||
// */
|
||||
// @Test
|
||||
// public void testBaseR2Json() {
|
||||
// BaseRequest baseRequest=new BaseRequest();
|
||||
// try {
|
||||
// System.out.println(baseRequest.toJson());
|
||||
// } catch (IllegalAccessException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void testRR2Json() {
|
||||
// OriginalRegisterRequest request=new OriginalRegisterRequest();
|
||||
// System.out.println(request.Type);
|
||||
// try {
|
||||
// System.out.println(request.toJson());
|
||||
// } catch (IllegalAccessException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in new issue
Block a user