Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
add /info api
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Nov 9, 2017
1 parent 682aebb commit 683061d
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 103 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ $ curl $DEVICE_URL/version
# expect example: 0.0.2
```

## 获取设备信息
```bash
$ curl $DEVICE_URL/info
{
"udid": "bf755cab-ff:ff:ff:ff:ff:ff-SM901",
"serial": "bf755cab",
"brand": "SMARTISAN",
"model": "SM901",
"hwaddr": "ff:ff:ff:ff:ff:ff",
"agentVersion": "dev"
}
```

## 安装应用
```bash
$ curl -X POST -d url="http://some-host/some.apk" $DEVICE_URL/install
Expand Down
88 changes: 10 additions & 78 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,19 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" href="/assets/terminal.ico?v=1">
<title>atx-agent terminal</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/xterm.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/addons/fullscreen/fullscreen.min.css">
<style>
* {
margin: 0;
}

html,
body {
height: 100%;
}

#xterm {
height: 100%;
background-color: black;
padding-left: 5px;
}
</style>
<title>ATX-Agent</title>
</head>

<body>
<div id="xterm"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/xterm.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/addons/fit/fit.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/addons/fullscreen/fullscreen.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery.ba-resize.min.js"></script>
<script>
var term;
var websocket = new WebSocket("ws://" + location.host + "/term");
websocket.binaryType = "arraybuffer";

function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
websocket.onopen = function(evt) {
term = new Terminal({
screenKeys: true,
useStyle: true,
cursorBlink: true,
});

term.on('data', function(data) {
websocket.send(new TextEncoder().encode("\x00" + data));
});
term.on('resize', function(evt) {
websocket.send(new TextEncoder().encode("\x01" + JSON.stringify({
cols: evt.cols,
rows: evt.rows
})))
});
term.on('title', function(title) {
document.title = title;
});

term.open(document.getElementById('xterm'));
term.fit();
$("#xterm").resize(function() {
term.fit()
})

websocket.onmessage = function(evt) {
if (evt.data instanceof ArrayBuffer) {
term.write(ab2str(evt.data));
} else {
alert(evt.data)
}
}
websocket.onclose = function(evt) {
term.write("Session terminated");
term.destroy();
}
websocket.onerror = function(evt) {
if (typeof console.log == "function") {
console.log(evt)
}
}
}
</script>
<h1>Hello ATX-Agent</h1>
<ul>
<li>
<a href="/term">Web terminal</a>
</li>
<li>
<a href="/info">API /info</a>
</li>
</ul>
</body>

</html>
91 changes: 91 additions & 0 deletions assets/terminal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" href="/assets/terminal.ico?v=1">
<title>atx-agent terminal</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/xterm.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/addons/fullscreen/fullscreen.min.css">
<style>
* {
margin: 0;
}

html,
body {
height: 100%;
}

#xterm {
height: 100%;
background-color: black;
padding-left: 5px;
}
</style>
</head>

<body>
<div id="xterm"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/xterm.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/addons/fit/fit.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/addons/fullscreen/fullscreen.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery.ba-resize.min.js"></script>
<script>
var term;
var websocket = new WebSocket("ws://" + location.host + "/term");
websocket.binaryType = "arraybuffer";

function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
websocket.onopen = function(evt) {
term = new Terminal({
screenKeys: true,
useStyle: true,
cursorBlink: true,
});

term.on('data', function(data) {
websocket.send(new TextEncoder().encode("\x00" + data));
});
term.on('resize', function(evt) {
websocket.send(new TextEncoder().encode("\x01" + JSON.stringify({
cols: evt.cols,
rows: evt.rows
})))
});
term.on('title', function(title) {
document.title = title;
});

term.open(document.getElementById('xterm'));
term.fit();
$("#xterm").resize(function() {
term.fit()
})

websocket.onmessage = function(evt) {
if (evt.data instanceof ArrayBuffer) {
term.write(ab2str(evt.data));
} else {
alert(evt.data)
}
}
websocket.onclose = function(evt) {
term.write("Session terminated");
term.destroy();
}
websocket.onerror = function(evt) {
if (typeof console.log == "function") {
console.log(evt)
}
}
}
</script>
</body>

</html>
43 changes: 31 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,11 @@ var (

func getProperty(name string) string {
propOnce.Do(func() {
properties = make(map[string]string)
propOutput, err := runShell("getprop")
var err error
properties, err = androidutils.Properties()
if err != nil {
panic(err)
}
re := regexp.MustCompile(`\[(.*?)\]:\s*\[(.*?)\]`)
matches := re.FindAllStringSubmatch(string(propOutput), -1)
for _, m := range matches {
var key = m[1]
var val = m[2]
properties[key] = val
}
})
return properties[name]
}
Expand Down Expand Up @@ -167,11 +160,11 @@ func installAPK(path string) error {
}
out, err := runShell(cmds...)
if err != nil {
matches := regexp.MustCompile(`Failure \[([\w_]+)\]`).FindStringSubmatch(string(out))
matches := regexp.MustCompile(`Failure \[([\w_ ]+)\]`).FindStringSubmatch(string(out))
if len(matches) > 0 {
return errors.Wrap(err, matches[0])
}
return err
return errors.Wrap(err, string(out))
}
return nil
}
Expand Down Expand Up @@ -413,9 +406,23 @@ func currentUserName() string {
return ""
}

func renderHTML(w http.ResponseWriter, filename string) {
file, err := Assets.Open(filename)
if err != nil {
http.Error(w, "404 page not found", 404)
return
}
content, _ := ioutil.ReadAll(file)
template.Must(template.New(filename).Parse(string(content))).Execute(w, nil)
}

func ServeHTTP(lis net.Listener) error {
m := mux.NewRouter()

m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
renderHTML(w, "index.html")
})

m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
file, err := Assets.Open("index.html")
if err != nil {
Expand Down Expand Up @@ -576,7 +583,19 @@ func ServeHTTP(lis net.Listener) error {
}()
})

m.HandleFunc("/term", handleTerminalWebsocket)
m.HandleFunc("/term", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Upgrade") == "websocket" {
handleTerminalWebsocket(w, r)
return
}
renderHTML(w, "terminal.html")
})

m.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
info := getDeviceInfo()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(info)
})

screenshotFilename := "/data/local/tmp/minicap-screenshot.jpg"
if username := currentUserName(); username != "" {
Expand Down
3 changes: 3 additions & 0 deletions term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import (
)

func handleTerminalWebsocket(w http.ResponseWriter, r *http.Request) {
// for k, v := range r.Header {
// log.Println(k, v)
// }
io.WriteString(w, "not support windows")
}
29 changes: 16 additions & 13 deletions tunnelproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ func runTunnelProxy(serverAddr string) {
}
}

func getDeviceInfo() *proto.DeviceInfo {
devInfo := &proto.DeviceInfo{
Serial: getProperty("ro.serialno"),
Brand: getProperty("ro.product.brand"),
Model: getProperty("ro.product.model"),
AgentVersion: version,
}
devInfo.HWAddr, _ = androidutils.HWAddrWLAN()

// Udid is ${Serial}-${MacAddress}-${model}
udid := getProperty("ro.serialno") + "-" + devInfo.HWAddr + "-" + getProperty("ro.product.model")
devInfo.Udid = udid
return devInfo
}

type VersionResponse struct {
ServerVersion string `json:"version"`
AgentVersion string `json:"atx-agent"`
Expand Down Expand Up @@ -68,19 +83,7 @@ func unsafeRunTunnelProxy(serverAddr string) error {
defer ws.Close()
log.Printf("server connected")

props, _ := androidutils.Properties()
devInfo := &proto.DeviceInfo{
Serial: props["ro.serialno"],
Brand: props["ro.product.brand"],
Model: props["ro.product.model"],
AgentVersion: version,
}
devInfo.HWAddr, _ = androidutils.HWAddrWLAN()

// Udid is ${Serial}-${MacAddress}-${model}
udid := props["ro.serialno"] + "-" + devInfo.HWAddr + "-" + props["ro.product.model"]
devInfo.Udid = udid

devInfo := getDeviceInfo()
ws.WriteJSON(proto.CommonMessage{
Type: proto.DeviceInfoMessage,
Data: devInfo,
Expand Down

0 comments on commit 683061d

Please sign in to comment.