Skip to content

Commit

Permalink
feat: add new /amfi/dev route to ipswd 🥃
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Apr 27, 2024
1 parent 3cc343e commit 7b4d529
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
86 changes: 86 additions & 0 deletions api/server/routes/idev/idev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
package idev

import (
"errors"
"fmt"
"net/http"
"time"

"github.com/blacktop/ipsw/api/types"
"github.com/blacktop/ipsw/internal/utils"
"github.com/blacktop/ipsw/pkg/usb"
"github.com/blacktop/ipsw/pkg/usb/amfi"
"github.com/blacktop/ipsw/pkg/usb/heartbeat"
"github.com/blacktop/ipsw/pkg/usb/lockdownd"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -57,3 +62,84 @@ func idevInfo(c *gin.Context) {

c.JSON(http.StatusOK, idevInfoResponse{Devices: dds})
}

func idevAmfiDev(c *gin.Context) {
udid := c.Query("udid")

ok, err := utils.IsDeveloperModeEnabled(udid)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: err.Error()})
return
}

if ok {
c.JSON(http.StatusOK, gin.H{"status": "Developer Mode is already enabled", "device": udid})
return
} else {
cli, err := amfi.NewClient(udid)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: err.Error()})
return
}
defer cli.Close()

if err := cli.EnableDeveloperMode(); err != nil {
if errors.Is(err, amfi.ErrPasscodeSet) {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: fmt.Errorf("cannot enabled Developer Mode when a pass-code is set: %w", err).Error()})
return
} else {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: err.Error()})
return
}
}

awake := make(chan bool)
defer close(awake)
errs := make(chan error)
defer close(errs)

go func() {
rebooting := false
for {
hb, err := heartbeat.NewClient(udid)
if err != nil {
rebooting = true
time.Sleep(1 * time.Second)
continue // ignore heartbeat connection errors (device may be rebooting)
}
beat, err := hb.Beat()
if err != nil {
errs <- fmt.Errorf("failed to start heartbeat: %w", err)
}
if rebooting && beat.Command == "Marco" { // REBOOTED
awake <- true
break
}
hb.Close()
time.Sleep(1 * time.Second)
}
}()

select {
case err := <-errs:
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: err.Error()})
return
case <-awake:
cli, err := amfi.NewClient(udid)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: err.Error()})
return
}
defer cli.Close()
if err := cli.EnableDeveloperModePostRestart(); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: err.Error()})
return
}
case <-time.After(time.Minute):
c.AbortWithStatusJSON(http.StatusInternalServerError, types.GenericError{Error: fmt.Errorf("device did not restart in time (1 minute)").Error()})
return
}
}

c.JSON(http.StatusOK, gin.H{"status": "Developer Mode enabled", "device": udid})
}
3 changes: 3 additions & 0 deletions api/server/routes/idev/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func AddRoutes(rg *gin.RouterGroup) {
// ig.GET("/afc/rm", handler) // TODO:
// ig.GET("/afc/tree", handler) // TODO:

// ig.GET("/amfi", handler) // TODO:
ig.GET("/amfi/dev", idevAmfiDev)

// ig.GET("/apps", handler) // TODO: implement this
// ig.GET("/apps/install", handler) // TODO: implement this
// ig.GET("/apps/ls", handler) // TODO: implement this
Expand Down

0 comments on commit 7b4d529

Please sign in to comment.