Writing a Simple Magisk Module for Android

Introduction


One evening, I was tired of changing TTL after rebooting the device and thought about how to do this automatically. Android is Linux and it should have an analogue of systemd or it itself. In the process of research, my eyes fell on Magisk.


Surprisingly, developing a module for him was very simple.


Training


By quick google we find the default Magisk module . Do not be alarmed by the Archived repository. So conceived. Let's fork it. This will be our core module.


β”œβ”€β”€ META-INF β”‚  └── com β”‚  └── google β”‚  └── android β”‚  β”œβ”€β”€ update-binary β”‚  └── updater-script β”œβ”€β”€ README.md β”œβ”€β”€ common β”‚  β”œβ”€β”€ post-fs-data.sh β”‚  β”œβ”€β”€ service.sh β”‚  └── system.prop β”œβ”€β”€ install.sh β”œβ”€β”€ module.prop └── system └── placeholder 

The install.sh script is executed at the time of installation. The script post-fs-data.sh is executed synchronously with the download before loading the zygote . The service.sh script is run asynchronously after the system boots. More details here .


Modify


In the beginning, let's change module.prop . It is there that the overhead module information is located. It turned out like this:


 id=yotattlfix name=Yota TTL Fix version=v1 versionCode=1 author=lionzxy description=Change TTL on startup to 63 minMagisk=17000 

In the install.sh file, you need, firstly, to set to true those things that we will use:


 SKIPMOUNT=false PROPFILE=false POSTFSDATA=false LATESTARTSERVICE=true 

The file contains detailed comments on each line, so it’s not difficult to figure it out.


Next, I fixed the header during installation


 print_modname() { ui_print "*******************************" ui_print " Magisk Yota TTL Fix " ui_print "*******************************" } 

In addition, at this stage you can check various parameters. Here is the usual bash. Getprop will most likely help you: getprop ro.product.device . You can look at your device in /system/build.prop or /system/build.prop more details.


Well, add the code we need in common/service.sh


 iptables -t mangle -A POSTROUTING -j TTL --ttl-set 63 

We launch


You do not need to compile anything. Just zip it :) You can use Github


Download ZIP


Next, open Magisk-> Modules-> Add on our device and select our archive. If we try to run it straight this way, we will see a strange error.



Until I compared my module by byte with the working module with w3bsit3-dns.com, I did not understand where my error was.



For the test, copy the source code to our project. And it will work
Total:



Publication


Magisk has a catalog of modules. It would be nice to get there.


All modules are stored in the organization on GitHub github.com/Magisk-Modules-Repo
To get there you need to follow a number of conditions and create Issues in the main repository . However, be prepared to take it for a very, very long time




Source: https://habr.com/ru/post/478288/


All Articles