package com.cqu.android.bean;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import com.cqu.android.Activity.R;
public class FireWall {
/** special application UID used to indicate "any application" */
public static final int SPECIAL_UID_ANY = -10;
/** special application UID used to indicate the Linux Kernel */
public static final int SPECIAL_UID_KERNEL = -11;
/** root script filename */
private static final String SCRIPT_FILE = "droidwall.sh";
private static List<Integer> blackList=new LinkedList<Integer>();
public static void initBlackList(){
}
/**
* Purge and re-add all rules (internal implementation).
* @param ctx application context (mandatory)
* @param uidsWifi list of selected UIDs for WIFI to allow or disallow (depending on the working mode)
* @param uids3g list of selected UIDs for 2G/3G to allow or disallow (depending on the working mode)
* @param showErrors indicates if errors should be alerted
*/
public static String applyIptablesRulesImpl(Context ctx, List<Integer> uids3g,boolean showErrors) {
if (ctx == null) {
return "";
}
assertBinaries(ctx, showErrors);
final String ITFS_3G[] = {"rmnet+","pdp+","ppp+","uwbr+","wimax+","vsnet+"};
//final String ITFS_3G[] = {"rmnet+","pdp+","ppp+","uwbr+"};
final boolean whitelist = false;
final boolean blacklist = !whitelist;
final StringBuilder script = new StringBuilder();
try {
script.append(scriptHeader(ctx));
script.append("" +
"$IPTABLES --version || exit 1\n" +
"# Create the droidwall chains if necessary\n" +
"$IPTABLES -L droidwall >/dev/null 2>/dev/null || $IPTABLES --new droidwall || exit 2\n" +
"$IPTABLES -L droidwall-3g >/dev/null 2>/dev/null || $IPTABLES --new droidwall-3g || exit 3\n" +
"$IPTABLES -L droidwall-wifi >/dev/null 2>/dev/null || $IPTABLES --new droidwall-wifi || exit 4\n" +
"$IPTABLES -L droidwall-reject >/dev/null 2>/dev/null || $IPTABLES --new droidwall-reject || exit 5\n" +
"# Add droidwall chain to OUTPUT chain if necessary\n" +
"$IPTABLES -L OUTPUT | $GREP -q droidwall || $IPTABLES -A OUTPUT -j droidwall || exit 6\n" +
"# Flush existing rules\n" +
"$IPTABLES -F droidwall || exit 7\n" +
"$IPTABLES -F droidwall-3g || exit 8\n" +
"$IPTABLES -F droidwall-wifi || exit 9\n" +
"$IPTABLES -F droidwall-reject || exit 10\n" +
"");
// Check if logging is enabled
script.append("" +
"# Create the reject rule (log disabled)\n" +
"$IPTABLES -A droidwall-reject -j REJECT || exit 11\n" +
"");
script.append("# Main rules (per interface)\n");
for (final String itf : ITFS_3G) {
script.append("$IPTABLES -A droidwall -o ").append(itf).append(" -j droidwall-3g || exit\n");
}
script.append("# Filtering rules\n");
final String targetRule = (whitelist ? "RETURN" : "droidwall-reject");
final boolean any_3g = uids3g.indexOf(SPECIAL_UID_ANY) >= 0;
if (any_3g) {
if (blacklist) {
/* block any application on this interface */
script.append("$IPTABLES -A droidwall-3g -j ").append(targetRule).append(" || exit\n");
}
} else {
/* release/block individual applications on this interface */
for (final Integer uid : uids3g) {
if (uid >= 0) script.append("$IPTABLES -A droidwall-3g -m owner --uid-owner ").append(uid).append(" -j ").append(targetRule).append(" || exit\n");
}
}
if (whitelist) {
if (!any_3g) {
if (uids3g.indexOf(SPECIAL_UID_KERNEL) >= 0) {
script.append("# hack to allow kernel packets on white-list\n");
script.append("$IPTABLES -A droidwall-3g -m owner --uid-owner 0:999999999 -j droidwall-reject || exit\n");
} else {
script.append("$IPTABLES -A droidwall-3g -j droidwall-reject || exit\n");
}
}
} else {
if (uids3g.indexOf(SPECIAL_UID_KERNEL) >= 0) {
script.append("# hack to BLOCK kernel packets on black-list\n");
script.append("$IPTABLES -A droidwall-3g -m owner --uid-owner 0:999999999 -j RETURN || exit\n");
script.append("$IPTABLES -A droidwall-3g -j droidwall-reject || exit\n");
}
}
final StringBuilder res = new StringBuilder();
Log.v("lck",script.toString());
int code=runScript(ctx, script.toString(), res,40000,true);
if(showErrors&&code<0){
alert(ctx, ctx.getResources().getString(R.string.iptables_applyRule_error) + "\n");
}
return res.toString();
} catch (Exception e) {
}
return "error";
}
/**
* Display a simple alert box
* @param ctx context
* @param msg message
*/
public static void alert(Context ctx, CharSequence msg) {
if (ctx != null) {
new AlertDialog.Builder(ctx)
.setNeutralButton(android.R.string.ok, null)
.setMessage(msg)
.show();
}
}
/**
* Check if we have root access
* @param ctx mandatory context
* @param showErrors indicates if errors should be alerted
* @return boolean true if we have root
*/
public static boolean hasRootAccess(Context ctx, boolean showErrors) {
final StringBuilder res = new StringBuilder();
try {
// Run an empty script just to check root access
if (runScript(ctx, "exit 0", res,-1,true) == 0) {
return true;
}
} catch (Exception e) {
}
if (showErrors) {
alert(ctx, ctx.getResources().getString(R.string.iptables_hasNotRoot_error) + res.toString());
}
return false;
}
/**
* 清除所有规则
* @return 执行结果信息
*/
public static boolean purgeIptables(Context ctx, boolean showErrors) {
StringBuilder res = new StringBuilder();
try {
assertBinaries(ctx,
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android网络流量监控软件源码 (256个子文件)
all-wcprops 414B
resources.ap_ 1.11MB
melcome.apk 1.32MB
busybox_g1 115KB
jarlist.cache 120B
MonitoringService.class 11KB
FireWall.class 10KB
DatabaseAdapter.class 10KB
startActivity.class 10KB
ProDatabase.class 8KB
keepInTime.class 8KB
AbstractDemoChart.class 7KB
setting.class 7KB
mainPage.class 6KB
MonitoringService$2.class 6KB
statist.class 5KB
SetTime.class 5KB
Round.class 5KB
mainPage$1.class 5KB
MonitoringService$1.class 5KB
R$id.class 5KB
netList.class 5KB
FlowChart.class 4KB
keepInTime$1.class 4KB
MonthChart.class 4KB
onGestureListener.class 4KB
netList$AppAdapter.class 4KB
proFlowSearch.class 3KB
applicationList.class 3KB
statist$4.class 3KB
proFlowSearch$4.class 3KB
FireWall$ScriptRunner.class 3KB
TrafficMonitoring.class 3KB
applicationList$MyTask.class 3KB
netList$1$1.class 3KB
Contact.class 3KB
Left.class 3KB
statist$3.class 3KB
proFlowSearch$5.class 3KB
setting$5$1.class 3KB
applicationList$AppAdapter.class 3KB
AppInfo.class 3KB
setting$4$1.class 3KB
startActivity$MyOnPageChangeListener.class 2KB
setting$5.class 2KB
netList$1.class 2KB
setting$6.class 2KB
WeekChart.class 2KB
startActivity$MyPagerAdapter.class 2KB
setting$4.class 2KB
setting$3.class 2KB
R$string.class 2KB
Parameter.class 2KB
setting$3$1.class 2KB
aboutus.class 2KB
Programme.class 2KB
netList$1$1$1.class 2KB
BootBroadcastReceiver.class 2KB
Round$1.class 2KB
R$drawable.class 1KB
setting$1.class 1KB
setting$2.class 1KB
onGestureListener$Current.class 1KB
MonitoringService$3.class 1KB
netList$2.class 1KB
setting$6$1.class 1KB
DatabaseAdapter$DatabaseHelper.class 1KB
ProDatabase$DatabaseHelper.class 1KB
R$layout.class 1KB
Application.class 1KB
applicationList$1.class 1KB
MyDatePickerDialog.class 1KB
liuliangjiankong.class 1KB
statist$MonthChartListener.class 1KB
statist$WeekChartListener.class 1KB
statist$DayChartListener.class 1KB
startActivity$MyOnClickListener.class 1KB
proFlowSearch$2.class 1006B
proFlowSearch$1.class 1006B
liuliangjiankong$1.class 986B
statist$1.class 929B
statist$2.class 929B
startActivity$1.class 856B
proFlowSearch$3.class 835B
MonitoringService$MyBinder.class 765B
onGestureListener$SnsConstant.class 682B
R.class 675B
R$raw.class 479B
R$anim.class 442B
IChart.class 360B
R$attr.class 358B
BuildConfig.class 357B
.classpath 475B
Thumbs.db 54KB
classes.dex 784KB
entries 744B
iptables_g1 127KB
iptables_n1 131KB
android-support-v4.jar 341KB
android-support-v4-74e1318c5546dc3afa10def76f66d61c.jar 132KB
共 256 条
- 1
- 2
- 3
与梦想齐飞
- 粉丝: 3
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页