Skip to content

Commit

Permalink
add ipk and firmware upgrade api
Browse files Browse the repository at this point in the history
  • Loading branch information
rwby-ovo committed Sep 8, 2022
1 parent 3441159 commit bf2cdff
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 5 deletions.
72 changes: 71 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"embed"
"encoding/json"
"fmt"
jsoniter "github.com/json-iterator/go"
"io/fs"
"io/ioutil"
"net"
Expand All @@ -16,6 +15,8 @@ import (
"strings"
"time"

jsoniter "github.com/json-iterator/go"

"rttys/cache"
"rttys/config"
"rttys/utils"
Expand Down Expand Up @@ -970,6 +971,75 @@ func apiStart(br *broker) {
go hiSynchShuntConf(br, devid, "")
})

// 设备固件/软件升级(需要设备在线) action=ipk|firmware devid=设备id
r.POST("/hi/device/upgrade/:action/:devid", func(c *gin.Context) {
action := c.Param("action")
devid := c.Param("devid")
onlyid := devidGetOnlyid(br, devid)

db, err := hi.InstanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
c.Status(http.StatusInternalServerError)
return
}

_, authErr := userAuth(c, db, devid)
if authErr != nil {
c.JSON(http.StatusOK, gin.H{
"ret": 0,
"msg": "Authentication failed",
"data": gin.H{
"error": authErr.Error(),
},
})
return
}

if len(onlyid) == 0 {
c.JSON(http.StatusOK, gin.H{
"ret": 0,
"msg": "设备不在线",
"data": nil,
})
return
}

content, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.Status(http.StatusBadRequest)
return
}
callUrl := jsoniter.Get(content, "call_url").ToString()
path := jsoniter.Get(content, "path").ToString()
if action == "ipk" {
c.JSON(http.StatusOK, gin.H{
"ret": 1,
"msg": "success",
"data": gin.H{
"token": hiDeviceIpkUpgrade(br, devid, path, callUrl),
},
})
return
}
if action == "firmware" {
c.JSON(http.StatusOK, gin.H{
"ret": 1,
"msg": "success",
"data": gin.H{
"token": hiDeviceFirmwareUpgrade(br, devid, path, callUrl),
},
})
return
}

c.JSON(http.StatusOK, gin.H{
"ret": 0,
"msg": "action error",
"data": nil,
})
})

// WG action=set|cancel|get devid=设备id
r.POST("/hi/wg/:action/:devid", func(c *gin.Context) {
action := c.Param("action")
Expand Down
35 changes: 31 additions & 4 deletions hi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gopkg.in/errgo.v2/fmt/errors"
"gorm.io/gorm"
"rttys/hi"
"rttys/hi/xrsa"
"rttys/version"
Expand All @@ -15,6 +11,11 @@ import (
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gopkg.in/errgo.v2/fmt/errors"
"gorm.io/gorm"

"rttys/utils"

"github.com/nahid/gohttp"
Expand Down Expand Up @@ -218,6 +219,32 @@ func hiRebootDevice(br *broker, devid string) string {
return hiExecBefore(br, db, devid, "#!/bin/sh\nreboot", "")
}

// 固件升级
func hiDeviceFirmwareUpgrade(br *broker, devid string, path string, callback string) string {
if len(br.cfg.HiApiUrl) == 0 {
log.Info().Msgf("api url is empty")
return ""
}
db, err := hi.InstanceDB(br.cfg.DB)
if err != nil {
return ""
}
return hiExecBefore(br, db, devid, hi.FirmwareUpgradeCmd(path), callback)
}

// ipk软件升级
func hiDeviceIpkUpgrade(br *broker, devid string, path string, callback string) string {
if len(br.cfg.HiApiUrl) == 0 {
log.Info().Msgf("api url is empty")
return ""
}
db, err := hi.InstanceDB(br.cfg.DB)
if err != nil {
return ""
}
return hiExecBefore(br, db, devid, hi.IpkUpgradeCmd(path), callback)
}

// 执行之前
func hiExecBefore(br *broker, db *gorm.DB, devid, cmd, callback string) string {
onlyid := devidGetOnlyid(br, devid)
Expand Down
16 changes: 16 additions & 0 deletions hi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ type StaticLeasesModel struct {
Name string `json:"name"`
}

func IpkUpgradeCmd(path string) string {
var cmds []string
cmds = append(cmds, "#!/bin/sh")
cmds = append(cmds, fmt.Sprintf("curl -4 -s -o /tmp/speedbox.ipk '%s' >/dev/null", path))
cmds = append(cmds, "opkg install /tmp/speedbox.ipk")
return strings.Join(cmds, "\n")
}

func FirmwareUpgradeCmd(path string) string {
var cmds []string
cmds = append(cmds, "#!/bin/sh")
cmds = append(cmds, fmt.Sprintf("curl -4 -s -o /tmp/firmware.img '%s' >/dev/null", path))
cmds = append(cmds, "sysupgrade /tmp/firmware.img -y")
return strings.Join(cmds, "\n")
}

func WireguardCmd(wg WgModel) string {
var cmds []string
//
Expand Down

0 comments on commit bf2cdff

Please sign in to comment.