Skip to content

Commit c0dc98f

Browse files
committed
snapshot
1 parent 6723364 commit c0dc98f

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

main.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (a *AndroidAttestation) SaveToFile(filename string) error {
5757
return err
5858
}
5959
data = append([]byte(xml.Header), data...)
60-
return os.WriteFile(filename, data, 0o644)
60+
return os.WriteFile(filename, data, 0644)
6161
}
6262

6363
func LoadAttestationFromFile(filename string) (*AndroidAttestation, error) {
@@ -362,15 +362,39 @@ type App struct {
362362
caCert *x509.Certificate
363363
caKey interface{}
364364
reader *bufio.Reader
365+
tempDir string
366+
stepCounter int
365367
}
366368

367369
func NewApp() *App {
370+
tempDir, err := os.MkdirTemp("", "keybox_temp")
371+
if err != nil {
372+
fmt.Println("创建临时目录失败:", err)
373+
os.Exit(1)
374+
}
368375
return &App{
369-
reader: bufio.NewReader(os.Stdin),
376+
reader: bufio.NewReader(os.Stdin),
377+
tempDir: tempDir,
378+
stepCounter: 0,
379+
}
380+
}
381+
382+
func (app *App) saveIntermediate() {
383+
if app.attestation == nil {
384+
return
385+
}
386+
app.stepCounter++
387+
filename := filepath.Join(app.tempDir, fmt.Sprintf("keybox_step%d.xml", app.stepCounter))
388+
if err := app.attestation.SaveToFile(filename); err != nil {
389+
fmt.Println("保存中间文件失败:", err)
390+
} else {
391+
fmt.Println("保存中间文件到", filename)
370392
}
371393
}
372394

373395
func (app *App) RunInteractive() {
396+
fmt.Println("临时目录:", app.tempDir)
397+
fmt.Println("临时目录将保留,可手动删除")
374398
for {
375399
app.showMenu()
376400
choice, err := readLine("请输入选项:", app.reader)
@@ -422,6 +446,7 @@ func (app *App) importAttestation() {
422446
} else {
423447
app.attestation = a
424448
fmt.Println("成功加载 keybox.xml!")
449+
app.saveIntermediate()
425450
}
426451
}
427452

@@ -439,6 +464,8 @@ func (app *App) importCA() {
439464
err = app.importCANonInteractive(certFile, keyFile)
440465
if err != nil {
441466
fmt.Println("导入 CA 失败:", err)
467+
} else {
468+
app.saveIntermediate()
442469
}
443470
}
444471

@@ -456,6 +483,8 @@ func (app *App) generateCA() {
456483
err = app.generateCANonInteractive(subject, caType)
457484
if err != nil {
458485
fmt.Println("生成 CA 失败:", err)
486+
} else {
487+
app.saveIntermediate()
459488
}
460489
}
461490

@@ -477,6 +506,8 @@ func (app *App) generateSubCert() {
477506
err = app.generateSubCertNonInteractive(subject, subAlg)
478507
if err != nil {
479508
fmt.Println("生成子证书失败:", err)
509+
} else {
510+
app.saveIntermediate()
480511
}
481512
}
482513

@@ -511,6 +542,7 @@ func (app *App) saveAndExit() {
511542
} else {
512543
fmt.Println("成功保存到", outfile)
513544
}
545+
fmt.Println("临时目录保留,可手动删除:", app.tempDir)
514546
os.Exit(0)
515547
}
516548

@@ -694,14 +726,9 @@ func printUsage() {
694726

695727
func processOperations(args []string) {
696728
app := NewApp()
697-
tempDir, err := os.MkdirTemp("", "keybox_temp")
698-
if err != nil {
699-
fmt.Println("创建临时目录失败:", err)
700-
return
701-
}
702-
fmt.Println("临时目录:", tempDir)
729+
fmt.Println("临时目录:", app.tempDir)
703730
defer func() {
704-
fmt.Println("临时目录保留,可手动删除:", tempDir)
731+
fmt.Println("临时目录保留,可手动删除:", app.tempDir)
705732
}()
706733

707734
step := 0
@@ -722,6 +749,7 @@ func processOperations(args []string) {
722749
}
723750
app.attestation = a
724751
fmt.Println("成功加载 Attestation 数据")
752+
app.saveIntermediate()
725753
i += 2
726754
case "-importCA":
727755
if i+2 >= len(args) {
@@ -735,6 +763,7 @@ func processOperations(args []string) {
735763
fmt.Println("导入 CA 失败:", err)
736764
return
737765
}
766+
app.saveIntermediate()
738767
i += 3
739768
case "-generateCA":
740769
if i+1 >= len(args) {
@@ -752,6 +781,7 @@ func processOperations(args []string) {
752781
fmt.Println("生成 CA 失败:", err)
753782
return
754783
}
784+
app.saveIntermediate()
755785
i += 2
756786
case "-generateSubCert":
757787
if i+2 >= len(args) {
@@ -765,6 +795,7 @@ func processOperations(args []string) {
765795
fmt.Println("生成子证书失败:", err)
766796
return
767797
}
798+
app.saveIntermediate()
768799
i += 3
769800
case "-showAttestation":
770801
app.showAttestation()
@@ -793,14 +824,6 @@ func processOperations(args []string) {
793824
return
794825
}
795826
step++
796-
if app.attestation != nil {
797-
filename := filepath.Join(tempDir, fmt.Sprintf("keybox_step%d.xml", step))
798-
if err := app.attestation.SaveToFile(filename); err != nil {
799-
fmt.Println("保存中间文件失败:", err)
800-
} else {
801-
fmt.Println("保存中间文件到", filename)
802-
}
803-
}
804827
}
805828
}
806829

0 commit comments

Comments
 (0)