防代理 防vpn访问
先上结构
新增文件
直接上代码 新增目录以及文件
AppContext.java
package com.你的包名;
import android.app.Application;
import android.content.Context;
public class AppContext extends Application {
private static Context context;
@Override
public void onCreate()
{
context = getApplicationContext();
}
public static Context getContext()
{
return context;
}
}
PreventCaught.java
package com.你的包名;
import android.widget.Toast;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactMethod;
import android.os.Build;
import android.text.TextUtils;
import com.biue.me.rnfilter.AppContext;
import android.content.Context;
import java.util.HashMap;
import java.util.Map;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Collections;
import java.util.List;
/**
* 原生模块
*/
public class PreventCaught extends ReactContextBaseJavaModule {
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
public PreventCaught(ReactApplicationContext reactContext) {
super(reactContext);
}
/**
*
* @return js调用的模块名
*/
@Override
public String getName() {
return "PreventCaught";
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
return constants;
}
@ReactMethod
public void showText(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
/**
* 使用ReactMethod注解,使这个方法被js调用
* @param message 文本
* @param duration 时长
*/
@ReactMethod
public void checkProxy(Callback success,Callback error) {
try {
if(isWifiProxy(AppContext.getContext()) || isVpnUsed()){
success.invoke("代理");
}else {
success.invoke("非代理");
}
}
catch (Exception e){
error.invoke("错误");
}
}
// 判断是否为代理
private boolean isWifiProxy(Context context) {
final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
String proxyAddress;
int proxyPort;
if (IS_ICS_OR_LATER) {
proxyAddress = System.getProperty("http.proxyHost");
String portStr = System.getProperty("http.proxyPort");
proxyPort = Integer.parseInt((portStr != null ? portStr : "-1"));
} else {
proxyAddress = android.net.Proxy.getHost(context);
proxyPort = android.net.Proxy.getPort(context);
}
return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1);
}
/**
* 是否正在使用VPN
*/
public static boolean isVpnUsed() {
try {
List<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces());
if(nis != null) {
for (NetworkInterface intf : nis) {
if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
continue;
}
if ("tun0".equals(intf.getName()) || "ppp0".equals(intf.getName())){
return true; // The VPN is up
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
}
PreventCaughtPackage.java
package com.你的包名;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PreventCaughtPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new PreventCaught(reactContext));
return modules;
}
}
修改文件
MainApplication.java
引入新增的文件
AndroidManifest.xml
获取网络状态权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在请求时进行判断
引入Platform ,NativeModules
import { Platform ,NativeModules} from 'react-native';
// 判断是否为代理
let checkProxy = () => {
return new Promise((resolve, reject) => {
// 如果为代理直接跳出
NativeModules.PreventCaught.checkProxy((success) => {
if(success === '代理'){
resolve(true);
} else {
resolve(false);
}
}, (error) => {
reject(true);
});
});
};
调用接口时进行判断