Skip to content

Commit

Permalink
feat: show more device info when install
Browse files Browse the repository at this point in the history
  • Loading branch information
bitxeno committed Sep 18, 2024
1 parent da65c4e commit 9ca478d
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 11 deletions.
49 changes: 49 additions & 0 deletions internal/manager/device_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manager

import (
"context"
"fmt"
"os/exec"
"strings"
Expand Down Expand Up @@ -52,6 +53,38 @@ func (dm *DeviceManager) GetDeviceByUDID(udid string) (*model.Device, bool) {
return nil, false
}

func (dm *DeviceManager) AppendProductInfo(dev *model.Device) {
timeout := 10 * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)

Check failure on line 58 in internal/manager/device_manager.go

View workflow job for this annotation

GitHub Actions / build (1.21.x)

lostcancel: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (govet)
cmd := exec.CommandContext(ctx, "ideviceinfo", "-u", dev.UDID, "-n")
cmd.Dir = app.Config.Server.DataDir
output, err := cmd.CombinedOutput()
if err != nil {
log.Err(err).Msgf("Error execute ideviceinfo: %s", dev.UDID)
return
}
lines := strings.Split(string(output), "\n")

for _, line := range lines {
var parts = strings.Split(line, ":")
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
switch key {
case "ProductType":
dev.ProductType = value
case "ProductVersion":
dev.ProductVersion = value
case "DeviceClass":
dev.ProductClass = value
case "DeviceName":
dev.Name = value
}
}
}

func (dm *DeviceManager) ReloadDevices() {
dm.devices.Range(func(k, v interface{}) bool {
dev := v.(model.Device)
Expand Down Expand Up @@ -178,6 +211,22 @@ func (dm *DeviceManager) CheckAfcServiceStatus(udid string) error {
return nil
}

func (dm *DeviceManager) CheckDeveloperMode(udid string) (bool, error) {
cmd := exec.Command("idevicedevmodectl", "list", "-u", udid, "-n")

data, err := cmd.CombinedOutput()
if err != nil {
return false, fmt.Errorf("%s%s", string(data), err.Error())
}

output := string(data)
if strings.Contains(output, "enabled") {
return true, nil
}

return false, nil
}

func (dm *DeviceManager) RestartUsbmuxd() error {
cmd := exec.Command("/etc/init.d/usbmuxd", "restart")
data, err := cmd.CombinedOutput()
Expand Down
12 changes: 12 additions & 0 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func GetDeviceByID(id string) (*model.Device, bool) {
return deviceManager.GetDeviceByID(id)
}

func AppendDeviceProductInfo(dev *model.Device) {
deviceManager.AppendProductInfo(dev)
}

func GetDeviceMountImageInfo(udid string) (*model.UsbmuxdImage, error) {
return deviceManager.GetMountImageInfo(udid)
}
Expand All @@ -26,10 +30,18 @@ func ScanDevices() {
deviceManager.Scan()
}

func CheckDeveloperMode(udid string) (bool, error) {
return deviceManager.CheckDeveloperMode(udid)
}

func CheckAfcServiceStatus(udid string) error {
return deviceManager.CheckAfcServiceStatus(udid)
}

func CheckDeviceStatus(udid string) error {
return nil
}

func RestartUsbmuxd() error {
return deviceManager.RestartUsbmuxd()
}
21 changes: 12 additions & 9 deletions internal/model/device.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package model

type Device struct {
ID string `json:"id"`
Name string `json:"name"`
ServiceName string `json:"service_name"`
IP string `json:"ip"`
MacAddr string `json:"mac_addr"`
UDID string `json:"udid"`
Status DeviceStatus `json:"status"`
Enable bool `json:"enable"`
Message string `json:"message"`
ID string `json:"id"`
Name string `json:"name"`
ServiceName string `json:"service_name"`
IP string `json:"ip"`
MacAddr string `json:"mac_addr"`
UDID string `json:"udid"`
Status DeviceStatus `json:"status"`
Enable bool `json:"enable"`
Message string `json:"message"`
ProductClass string `json:"product_class"`
ProductType string `json:"product_type"`
ProductVersion string `json:"product_version"`
}

const (
Expand Down
9 changes: 9 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ func MountDeveloperDiskImage(ctx context.Context, id string) error {
return nil
}

func CheckDeveloperMode(ctx context.Context, id string) (bool, error) {
device, ok := manager.GetDeviceByID(id)
if !ok {
return false, fmt.Errorf("device not found: %s", id)
}

return manager.CheckDeveloperMode(device.UDID)
}

func CheckAfcService(ctx context.Context, id string) error {
device, ok := manager.GetDeviceByID(id)
if !ok {
Expand Down
11 changes: 11 additions & 0 deletions web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func route(fi *fiber.App) {
api.Get("/devices/:id", func(c *fiber.Ctx) error {
id := c.Params("id")
if device, ok := manager.GetDeviceByID(id); ok {
manager.AppendDeviceProductInfo(device)
return c.Status(http.StatusOK).JSON(apiSuccess(device))
}

Expand All @@ -144,6 +145,16 @@ func route(fi *fiber.App) {
}
})

api.Post("/devices/:id/check/devmode", func(c *fiber.Ctx) error {
id := c.Params("id")

enabled, err := service.CheckDeveloperMode(c.Context(), id)
if err != nil {
return c.Status(http.StatusOK).JSON(apiSuccess(err.Error()))
}
return c.Status(http.StatusOK).JSON(apiSuccess(enabled))
})

api.Post("/devices/:id/check/afc", func(c *fiber.Ctx) error {
id := c.Params("id")

Expand Down
16 changes: 16 additions & 0 deletions web/static/src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ export default {
});
});
},
checkDeveloperMode: (id) => {
return new Promise((resolve, reject) => {
request({
url: `/api/devices/${id}/check/devmode`,
timeout: 30000,
method: "Post",
})
.then((res) => {
resolve(res.data ? "enabled" : "disabled");
})
.catch((err) => {
console.log(err);
resolve("-");
});
});
},
getDevices: (params) => {
return request({
url: "/api/devices",
Expand Down
9 changes: 7 additions & 2 deletions web/static/src/page/install/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,15 @@ export default {
_this.stopUpdateLog();
_this.startUpdateLog();
_this.log.output += "checking afc service status...\n";
_this.log.output += "checking device status...\n";
try {
_this.log.output += `product type: ${_this.device.product_type}\n`;
_this.log.output += `product version: ${_this.device.product_version}\n`;
let devmode = await api.checkDeveloperMode(_this.id);
_this.log.output += `developer mode: ${devmode}\n`;
await api.checkAfcService(_this.id);
_this.log.output += "afc service OK!\n";
_this.log.output += "afc service: OK!\n";
let formData = new FormData();
for (let i = 0; i < _this.files.length; i++) {
Expand Down

0 comments on commit 9ca478d

Please sign in to comment.