-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
100 lines (94 loc) · 2.14 KB
/
main.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
95
96
97
98
99
100
package main
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"
"time"
)
func main() {
privKeyBytes, _ := os.ReadFile(os.Args[2])
privKey, err := x509.ParsePKCS1PrivateKey(privKeyBytes)
if err != nil {
panic(err)
}
pubKeyBytes, _ := os.ReadFile(os.Args[3])
pubKey, err := x509.ParsePKCS1PublicKey(pubKeyBytes)
if err != nil {
panic(err)
}
ctx := context.TODO()
go func() { _ = runQBProblem(ctx, pubKey, os.Args[4:]) }()
fmt.Println(runMessageHandler(ctx, privKey, os.Args[1]))
}
func runMessageHandler(ctx context.Context, privateKey *rsa.PrivateKey, addr string) error {
mux := http.NewServeMux()
mux.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) {
encBytes, _ := io.ReadAll(r.Body)
decBytes, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, encBytes, nil)
if err == nil {
fmt.Println(string(decBytes))
}
})
server := &http.Server{Addr: addr, Handler: mux}
go func() {
<-ctx.Done()
server.Close()
}()
return server.ListenAndServe()
}
func runQBProblem(ctx context.Context, receiverKey *rsa.PublicKey, hosts []string) error {
queue := make(chan []byte, 256)
go func() {
pr, err := rsa.GenerateKey(rand.Reader, receiverKey.N.BitLen())
if err != nil {
panic(err)
}
for {
select {
case <-ctx.Done():
return
default:
if len(queue) == 0 {
encBytes, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, &pr.PublicKey, []byte("_"), nil)
if err == nil {
queue <- encBytes
}
}
}
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
default:
input, _, _ := bufio.NewReader(os.Stdin).ReadLine()
encBytes, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, receiverKey, input, nil)
if err == nil {
queue <- encBytes
}
}
}
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(5 * time.Second):
encBytes := <-queue
client := &http.Client{Timeout: time.Second}
for _, host := range hosts {
_, _ = client.Post(fmt.Sprintf("http://%s/push", host), "text/plain", bytes.NewBuffer(encBytes))
}
}
}
}