Android 13 动态启用或禁用IPV6

介绍

客户想要通过APK来控制IPV6的启用和禁用,这里我们通过广播的方式来让客户控制IPV6。

效果展示

adb shell

ifconfig

这里我们用debug软件,将下面节点置为1 如图ipv6已被禁用了 

echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6

修改

接下来我们通过代码控制,动态注册广播

custom.action.intent.ipv6.on   //禁用ipv6

custom.action.intent.ipv6.off   //启用ipv6

路径:frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

//*/soda water.20232328.ipv6 switch
import java.io.BufferedWriter;
import java.io.FileWriter;
//*/


    //我们在 init 中动态注册广播
    public void init(Context context, IWindowManager windowManager,
            WindowManagerFuncs windowManagerFuncs) {

        //*/soda water.20232327.ipv6 switch
        filter = new IntentFilter();
        filter.addAction("custom.action.intent.ipv6.on");
        filter.addAction("custom.action.intent.ipv6.off");
        context.registerReceiver(Ipv6Receiver, filter);
        //*/

    }



    //此处我们监听广播 这里定义广播
    //*/soda water.20232327.ipv6 switch
    BroadcastReceiver Ipv6Receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("custom.action.intent.ipv6.on".equals(intent.getAction())) {
            setIpv6(true);
            Log.d("soda water","on");
            }else if ("custom.action.intent.ipv6.off".equals(intent.getAction())){
            setIpv6(false);
            Log.d("soda water","off");
            }
        }
    };

    private static void setIpv6(Boolean commod) {
        BufferedWriter bufWriter;
        try {
            bufWriter = new BufferedWriter(new FileWriter("/proc/sys/net/ipv6/conf/all/disable_ipv6"));
            Log.d("soda water","one");
            bufWriter.write(commod?"1":"0");
            Log.d("soda water","two");
            bufWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //*/

接下来我们尝试用apk发送广播

public class MainActivity extends AppCompatActivity {
    private ToggleButton tg_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tg_button = findViewById(R.id.bt_switch);
        tg_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Intent intent = new Intent();
                if (b) {
                    intent.setAction("custom.action.intent.ipv6.on");
                    sendBroadcast(intent);
                } else {
                    intent.setAction("custom.action.intent.ipv6.off");
                    sendBroadcast(intent);
                }
            }
        });

    }
}

验证发现缺少avc写入的权限

adb shell

logcat | grep avc

12-26 19:52:25.616  1310  1310 W system_server: type=1400 audit(0.0:193): avc: denied { write } for name="disable_ipv6" dev="proc" ino=27000 scontext=
u:r:system_server:s0 tcontext=u:object_r:proc_net:s0 tclass=file permissive=0

上面显示 proc_net 在 system_server 中没有 write 权限

在如下路径补上如下代码 此时在此发送广播发现可以动态控制ipv6的启用和禁用了

路径:system/sepolicy/private/system_server.te

allow system_server proc_net:file {open write getattr};

相关推荐

  1. android 14.0 控制wifi启用禁用功能实现

    2023-12-29 10:22:02       38 阅读
  2. Android12 禁用adb

    2023-12-29 10:22:02       25 阅读
  3. 通过CMake的option启用禁用特定功能

    2023-12-29 10:22:02       40 阅读
  4. Android 13屏蔽Activity包的手势禁止滑动退出

    2023-12-29 10:22:02       58 阅读
  5. android 13.0 framework禁用系统所有通知

    2023-12-29 10:22:02       57 阅读
  6. 如何在 Ubuntu 上启用 IPv6

    2023-12-29 10:22:02       33 阅读
  7. Android 10-13,默认屏幕亮度80%100%

    2023-12-29 10:22:02       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-29 10:22:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-29 10:22:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-29 10:22:02       87 阅读
  4. Python语言-面向对象

    2023-12-29 10:22:02       96 阅读

热门阅读

  1. 用VSCode Remote-SSH做Docker环境中的开发

    2023-12-29 10:22:02       61 阅读
  2. centos 编译安装 icu

    2023-12-29 10:22:02       67 阅读
  3. SQL高级:递归查询

    2023-12-29 10:22:02       56 阅读
  4. HIVE笔记

    2023-12-29 10:22:02       55 阅读
  5. Nginx屏蔽垃圾邮件骚扰IP的方法

    2023-12-29 10:22:02       61 阅读
  6. LeetCode-827. 最大人工岛

    2023-12-29 10:22:02       69 阅读
  7. Windows Server 2012R2密钥

    2023-12-29 10:22:02       75 阅读
  8. Cookie与Session详解

    2023-12-29 10:22:02       72 阅读
  9. 32单片机按键扫描 实现长短按

    2023-12-29 10:22:02       68 阅读