banner
moeyy

moeyy

一条有远大理想的咸鱼。
github
mastodon
email

Build an HTTP server on an Android phone.

title: Building an HTTP Server on Android Phones
tags: []
id: '270'
categories:

    • Information
      date: 2019-07-19 10:13:25
      cover:
      ai: true

Are you confused by the title? Building an HTTP server on an Android phone???

Building an HTTP Server on Android Phones

Are you kidding me?

Yes, we are indeed building an HTTP server on an Android phone. When it comes to HTTP servers, the first thing that comes to mind is Apache or nginx. Can we run Apache or nginx on Android?

Of course, we cannot run these servers on Android phones. This time, we are running a simple HTTP server written in Golang on Android. Since Golang can be cross-compiled, I tried selecting the Linux operating system and the ARM CPU architecture, and then ran it on my phone. Surprisingly, an HTTP server was successfully running on my Android phone.

Building an HTTP Server on Android Phones

What a surprise!

Now, let's get back to the topic. In order to develop and write Golang, we need to set up the Golang development environment first. Here is the link to the Golang configuration. I developed it on Ubuntu and created a new file called "service.go".

  1 package main
  2 
  3 import (
  4         "net/http"
  5        )
  6 
  7 func main() {
  9     http.HandleFunc("/",myResponse)
 10     http.ListenAndServe("127.0.0.1:8888",nil)
 11 }
 12 
 13 func myResponse(w http.ResponseWriter,r* http.Request)  {
 14     w.Write([]byte("<html><center> <font size="40">hello I am go service</font></center></html>"))
 15 }

For those who are not familiar with Go, please note that do not press Enter randomly do not press Enter randomly do not press Enter randomly because Go is similar to Python and does not use ";" to end lines.

Let me explain briefly. The first parameter of http.HandleFunc("/",myResponse) is the URL for registering the HTTP service. Here, we fill in "/", so we can access it directly by typing localhost:8888 in the browser. If we fill in "/test", the URL for accessing it will be localhost/test:8888.

The first parameter of http.ListenAndServe("127.0.0.1:8888",nil) is the IP address and port to listen to, and the second parameter is set to nil.

Now, let's compile and run it. For running the program on the local machine, we can use the default compilation parameters by running go build service.go. Then, run ./service. Enter the URL in the browser to see if it can be accessed.

Building an HTTP Server on Android Phones

Chrome

Next, let's port this program to Android. The Android phone must have root access. If your phone is not rooted and you don't want to root it, you can use an emulator. Just change the CPU instruction set parameter. First, let's run it on the phone. My phone is Honor 6 with a Hisilicon 920 CPU. I found out that the instruction set for Hisilicon 920 is ARM32. Okay, let's compile it. Before compiling, let's rename the compiled program. Run GOOS="linux" GOARCH="arm" go build service.go to get an executable file. Use the file command to check the file type. file service

service: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped

We can see that this file is a 32-bit ARM program. Okay, let's copy it to the root directory of the Android phone and change the file permissions.

Building an HTTP Server on Android Phones

Screenshot_2018-04-08-19-18-11.png

Building an HTTP Server on Android Phones

Screenshot_2018-04-08-19-18-20.png

Then, run it in juiceSSH and enter the URL in the browser to check.

Building an HTTP Server on Android Phones

Screenshot_2018-04-08-19-19-43.png

No problem.

Next, let's compile it for the emulator. I'm using Genymotion with the Google Nexus 5 system. I searched and found out that Nexus 5 uses the Qualcomm Snapdragon 800 CPU. Here comes the problem. Snapdragon 800 uses the ARM32 instruction set, so theoretically, we can directly copy the program and run it. However, it shows an error message: /system/bin/sh: ./service_arm32: not executable: 32-bit ELF file. Then, I thought that the CPU is from the computer, so let's try compiling it for X64. Still not working. Let's try X86. Finally, it works. It turns out that the instructions need to match the CPU of the emulator. Run GOOS="linux" GOARCH="386" go build service.go and copy it to the emulator. Then, follow the same steps as on the phone: copy it to the root directory, change the file permissions, and run it using adb shell. Check if it can be accessed in the emulator's browser.

Building an HTTP Server on Android Phones

Genymotion

Alright, we have successfully run an HTTP server on Android.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.