/
keygen.go
94 lines (77 loc) · 1.75 KB
/
keygen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package keygen
import (
"context"
"crypto/rand"
"crypto/rsa"
"fmt"
"log"
"sync"
"golang.org/x/crypto/ssh"
"github.com/julienschmidt/httprouter"
)
const maxHistory = 20
type KeyGen struct {
mu sync.Mutex
config Config
history []History
nextHistoryID int
nextWorkloadID int
cancelFunc context.CancelFunc
}
func New() *KeyGen {
kg := &KeyGen{
history: []History{},
}
return kg
}
func (kg *KeyGen) AddRoutes(router *httprouter.Router, base string) {
router.GET(base, kg.APIGet)
router.PUT(base, kg.APIPut)
}
func (kg *KeyGen) Restart() {
kg.mu.Lock()
defer kg.mu.Unlock()
// Cancel currently running workload
if kg.cancelFunc != nil {
kg.cancelFunc()
kg.cancelFunc = nil
}
if kg.config.Enable {
var ctx context.Context
ctx, kg.cancelFunc = context.WithCancel(context.Background())
if len(kg.config.MemQQueue) > 0 && len(kg.config.MemQServer) > 0 {
w := newMemQWorker(ctx, kg.config, kg.WorkloadOutput)
go w.startWork()
} else {
w := workload{
id: kg.nextWorkloadID,
c: kg.config,
ctx: ctx,
out: kg.WorkloadOutput,
}
kg.nextWorkloadID++
go w.startWork()
}
}
}
func (kg *KeyGen) WorkloadOutput(s string) {
kg.mu.Lock()
defer kg.mu.Unlock()
log.Print(s)
kg.history = append(kg.history, History{ID: kg.nextHistoryID, Data: s})
if len(kg.history) > maxHistory {
kg.history = kg.history[len(kg.history)-maxHistory:]
}
kg.nextHistoryID++
}
func generateKey() string {
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return fmt.Sprintf("Error generating key: %v", err)
}
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return fmt.Sprintf("Error generating ssh key; %v", err)
}
return ssh.FingerprintSHA256(pub)
}