Skip to content

Commit

Permalink
重签名任务调度器
Browse files Browse the repository at this point in the history
  • Loading branch information
togettoyou committed Feb 28, 2022
1 parent c7f377f commit f11b7fa
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 6 deletions.
2 changes: 2 additions & 0 deletions server/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"supersign/pkg"
"supersign/pkg/conf"
"supersign/pkg/log"
"supersign/pkg/sign"
"supersign/pkg/validatorer"

"github.com/spf13/pflag"
Expand All @@ -19,6 +20,7 @@ func setup() {
conf.Setup()
log.Setup(conf.Log.Level)
validatorer.Setup()
sign.Setup(log.New("sign").L(), 10)
//_ = redis.Setup(conf.Redis.DB, conf.Redis.Addr, conf.Redis.Password)
conf.OnChange(func() {
if err := pkg.Reset(); err != nil {
Expand Down
20 changes: 16 additions & 4 deletions server/internal/api/v1/download.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package v1

import (
"errors"
"fmt"
"net/http"
"path"
"supersign/internal/api"
"supersign/internal/model/req"
"supersign/pkg/conf"
"supersign/pkg/sign"
"text/template"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -118,17 +121,26 @@ func (d Download) Plist(c *gin.Context) {
if !d.MakeContext(c).ParseUri(&args) {
return
}
doneCache, ok := sign.DoneCache(args.UUID)
if !ok {
d.Resp(http.StatusNotFound, nil, false)
return
}
if !doneCache.Success {
d.Resp(http.StatusInternalServerError, errors.New(doneCache.Msg), false)
return
}
tmpl, err := template.New(args.UUID).Parse(plistTemp)
if d.HasErr(err) {
return
}
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename="+args.UUID+".plist")
if d.HasErr(tmpl.Execute(c.Writer, map[string]string{
"URL": "",
"BundleIdentifier": "",
"Version": "",
"Name": "",
"URL": fmt.Sprintf("%s/api/v1/download/tempipa/%s", conf.Server.URL, args.UUID),
"BundleIdentifier": doneCache.BundleIdentifier,
"Version": doneCache.Version,
"Name": doneCache.Name,
})) {
return
}
Expand Down
33 changes: 32 additions & 1 deletion server/internal/svc/apple_device.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package svc

import (
"path"
"supersign/internal/model"
"supersign/pkg/appstore"
"supersign/pkg/conf"
"supersign/pkg/e"
"supersign/pkg/sign"
"supersign/pkg/tools"

"github.com/google/uuid"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -118,5 +123,31 @@ func (a *AppleDevice) bindingAppleDeveloper(udid string, appleIPA *model.AppleIP
}

func (a *AppleDevice) signature(deviceID string, appleDeveloper *model.AppleDeveloper, appleIPA *model.AppleIPA) (string, error) {
return "", nil
// 获取mobileprovision描述文件
authorize := appstore.Authorize{
P8: appleDeveloper.P8,
Iss: appleDeveloper.Iss,
Kid: appleDeveloper.Kid,
}
profileUUID := uuid.New().String()
profileResponse, err := authorize.CreateProfile(profileUUID, appleDeveloper.BundleIds, appleDeveloper.CerID, deviceID)
if err != nil {
return "", err
}
mobileprovisionPath := path.Join(conf.Apple.TemporaryFilePath, profileUUID, "mobileprovision.mobileprovision")
err = tools.Base64ToFile(profileResponse.Data.Attributes.ProfileContent, mobileprovisionPath)
if err != nil {
return "", err
}
// 发布重签名任务
go sign.Push(&sign.Stream{
ProfileUUID: profileUUID,
Iss: appleDeveloper.Iss,
MobileprovisionPath: mobileprovisionPath,
IpaUUID: appleIPA.UUID,
BundleIdentifier: appleIPA.BundleIdentifier,
Version: appleIPA.Version,
Name: appleIPA.Name,
})
return profileUUID, nil
}
95 changes: 95 additions & 0 deletions server/pkg/sign/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package sign

import (
"path"
"supersign/pkg/conf"
"sync"

"go.uber.org/zap"
)

var signJob *job

type job struct {
logger *zap.Logger
streamCh chan *Stream
doneCache map[string]*done
mu sync.Mutex
}

type Stream struct {
ProfileUUID string
Iss string
MobileprovisionPath string
IpaUUID string
BundleIdentifier string
Version string
Name string
}

type done struct {
Success bool
Msg string
BundleIdentifier string
Version string
Name string
}

func Setup(logger *zap.Logger, maxJob int) {
signJob = &job{
logger: logger,
streamCh: make(chan *Stream, maxJob),
doneCache: make(map[string]*done, 0),
}
go func() {
for {
select {
case stream, ok := <-signJob.streamCh:
if !ok {
return
}
go func() {
signJob.logger.Info("开始执行重签名任务......")
err := run(
path.Join(conf.Apple.AppleDeveloperPath, stream.Iss, "pem.pem"),
path.Join(conf.Apple.AppleDeveloperPath, stream.Iss, "key.key"),
stream.MobileprovisionPath,
path.Join(conf.Apple.TemporaryFilePath, stream.ProfileUUID, "ipa.ipa"),
path.Join(conf.Apple.UploadFilePath, stream.IpaUUID, "ipa.ipa"),
)
if err != nil {
signJob.mu.Lock()
defer signJob.mu.Unlock()
signJob.doneCache[stream.ProfileUUID] = &done{
Success: false,
Msg: "重签名任务执行失败:" + err.Error(),
}
signJob.logger.Error("重签名任务执行失败:" + err.Error())
return
}
signJob.mu.Lock()
defer signJob.mu.Unlock()
signJob.doneCache[stream.ProfileUUID] = &done{
Success: true,
Msg: "重签名任务执行成功",
BundleIdentifier: stream.BundleIdentifier,
Version: stream.Version,
Name: stream.Name,
}
signJob.logger.Info("重签名任务执行成功")
}()
}
}
}()
}

func Push(stream *Stream) {
signJob.streamCh <- stream
}

func DoneCache(ProfileUUID string) (done *done, ok bool) {
signJob.mu.Lock()
defer signJob.mu.Unlock()
done, ok = signJob.doneCache[ProfileUUID]
return
}
2 changes: 1 addition & 1 deletion server/pkg/sign/zsign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sign

import "supersign/pkg/tools"

func Run(pemPath, keyPath, mobileprovisionPath, outputIPAPath, inputIPAPath string) error {
func run(pemPath, keyPath, mobileprovisionPath, outputIPAPath, inputIPAPath string) error {
return tools.CmdClient.Command("zsign",
"-c", pemPath,
"-k", keyPath,
Expand Down

0 comments on commit f11b7fa

Please sign in to comment.