介紹
客戶想要通過(guò)APK來(lái)控制IPV6的啟用和禁用,這里我們通過(guò)廣播的方式來(lái)讓客戶控制IPV6。
效果展示
adb shell
ifconfig
這里我們用debug軟件,將下面節(jié)點(diǎn)置為1 如圖ipv6已被禁用了?
echo 1 >?/proc/sys/net/ipv6/conf/all/disable_ipv6
修改
接下來(lái)我們通過(guò)代碼控制,動(dòng)態(tài)注冊(cè)廣播
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;
//*/
//我們?cè)?init 中動(dòng)態(tài)注冊(cè)廣播
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);
//*/
}
//此處我們監(jiān)聽(tīng)廣播 這里定義廣播
//*/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();
}
}
//*/
接下來(lái)我們嘗試用apk發(fā)送廣播
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);
}
}
});
}
}
驗(yàn)證發(fā)現(xiàn)缺少avc寫(xiě)入的權(quán)限
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 中沒(méi)有?write 權(quán)限
在如下路徑補(bǔ)上如下代碼 此時(shí)在此發(fā)送廣播發(fā)現(xiàn)可以動(dòng)態(tài)控制ipv6的啟用和禁用了文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-808572.html
路徑:system/sepolicy/private/system_server.te文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-808572.html
allow system_server proc_net:file {open write getattr};
到了這里,關(guān)于Android 13 動(dòng)態(tài)啟用或禁用IPV6的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!