-
Notifications
You must be signed in to change notification settings - Fork 0
/
client100k.go
215 lines (185 loc) · 5.35 KB
/
client100k.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
httpApi "github.com/ipfs/go-ipfs-api"
core "github.com/ipfs/go-ipfs/core"
corenet "github.com/ipfs/go-ipfs/core/corenet"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
peer "gx/ipfs/QmZwZjMVGss5rqYsJVGy18gNbkTJffFyq2x1uJ4e4p3ZAt/go-libp2p-peer"
"github.com/golang-samples/revel/src/code.google.com/p/go.net/context"
)
const (
protocolAddress = "/42coffeecupfs/e-proxy-voting"
shellUrl = "localhost:5501"
)
type Bulletin struct {
Voting string `json:"voting"`
Vote string `json:"vote"`
Voter string `json:"voter"`
Datetime string `json:"datetime"`
PublicKey string `json:"publickey"`
}
type SignedBulletin struct {
Bulletin Bulletin `json:"bulletin"`
Signature string `json:"signature"`
}
// create ipfs object without links
func CreateIpfsObject(data string) *httpApi.IpfsObject {
obj := &httpApi.IpfsObject{Data: data}
return obj
}
func CreateBulletin(votingHash, voteHash, voterHash, nameOfPubKey string) Bulletin {
pubKey := strings.Replace(GetKey(nameOfPubKey), "\n", "_", -1) // replace all line breaks on the spaces
bulletinObj := Bulletin{
Voting: votingHash,
Vote: voteHash,
Voter: voterHash,
Datetime: time.Now().Format("2006-02-01T15:04:05.000Z"),
PublicKey: pubKey,
}
return bulletinObj
}
func CreateSignedBulletin(bulletin Bulletin, privKeyPEM string) string {
bulletinJson, err := json.Marshal(bulletin)
if err != nil {
panic(err)
}
signature := base64.StdEncoding.EncodeToString(JSONSign(string(bulletinJson), privKeyPEM))
signedBulletinObj := SignedBulletin{bulletin, signature}
signedBulletinJson, err := json.Marshal(signedBulletinObj)
if err != nil {
panic(err)
}
return string(signedBulletinJson)
}
func GetKey(filename string) string {
key, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return string(key)
}
func JSONSign(data, privKeyPEM string) []byte {
// Parse private key into rsa.PrivateKey
PEMBlock, _ := pem.Decode([]byte(privKeyPEM))
if PEMBlock == nil {
panic("Could not parse Private Key PEM")
}
if PEMBlock.Type != "RSA PRIVATE KEY" {
panic("Found wrong key type")
}
privkey, err := x509.ParsePKCS1PrivateKey(PEMBlock.Bytes)
if err != nil {
panic(err)
}
// Compute the sha1
h := sha1.New()
h.Write([]byte(data))
// Sign the data
signature, err := rsa.SignPKCS1v15(rand.Reader, privkey, crypto.SHA1, h.Sum(nil))
if err != nil {
panic(err)
}
// return the results
return signature
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Please give a peer ID as an argument")
return
}
if len(os.Args) < 3 {
fmt.Println("Please give a voting hash as an argument")
return
}
if len(os.Args) < 4 {
fmt.Println("Please give a vote hash as an argument")
return
}
if len(os.Args) < 5 {
fmt.Println("Please give a voter hash as an argument")
return
}
if len(os.Args) < 6 {
fmt.Println("Please give a name of pub key as an argument")
return
}
if len(os.Args) < 7 {
fmt.Println("Please give a name of priv key as an argument")
return
}
peerIDofHost := os.Args[1]
votingHash := os.Args[2]
voteHash := os.Args[3]
voterHash := os.Args[4]
nameOfPubKey := os.Args[5]
nameOfPrivKey := os.Args[6]
target, err := peer.IDB58Decode(peerIDofHost)
if err != nil {
panic(err)
}
// Basic ipfsnode setup
r, err := fsrepo.Open("~/.ipfsEvoxClient")
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cfg := &core.BuildCfg{
Repo: r,
Online: true,
}
nd, err := core.NewNode(ctx, cfg)
if err != nil {
panic(err)
}
for i := 1; i <= 100000; i++ {
fmt.Println("Connection with host, number: ", i)
connectionWithHost(votingHash, voteHash, voterHash, nameOfPubKey,
nameOfPrivKey, nd, target)
}
}
func connectionWithHost(votingHash, voteHash, voterHash, nameOfPubKey,
nameOfPrivKey string, nd *core.IpfsNode, target peer.ID) {
bulletin := CreateBulletin(votingHash, voteHash, voterHash, nameOfPubKey)
signedBulletin := CreateSignedBulletin(bulletin, GetKey(nameOfPrivKey))
ipfsObject := CreateIpfsObject(signedBulletin)
// save bulletin to ipfs
s := httpApi.NewShell(shellUrl)
hash, err := s.ObjectPut(ipfsObject)
if err != nil {
panic(err)
}
dataForHost := `{"Hash":"` + hash + `"}`
con, err := corenet.Dial(nd, target, protocolAddress)
if err != nil {
fmt.Println(err)
return
}
fmt.Fprintln(con, dataForHost) // send hash of bulletin to the server
// get data from response
buf := make([]byte, 256)
n, err := con.Read(buf)
if err != nil {
fmt.Println(err)
return
}
// show data and close script
response := string(buf[:n])
if response != "" {
fmt.Println(response)
return
}
}