-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
306 lines (271 loc) · 6.38 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// It connects to the peer specified by -peer.
// It accepts connections from peers and receives messages from them.
// When it sees a peer with an address it hasn't seen before, it makes a
// connection to that peer.
// It adds an ID field containing a random string to each outgoing message.
// When it recevies a message with an ID it hasn't seen before, it broadcasts
// that message to all connected peers.
//
package main
import (
"bufio"
"encoding/json"
"flag"
"log"
"net"
"os"
"sync"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"github.com/campoy/whispering-gophers/util"
"io"
"strings"
)
var (
listenAddr = flag.String("listen", "localhost:8000", "host:port to listen on")
peerAddr = flag.String("peer", "", "peer host:port")
myName = flag.String("myname", "Anonymous", "myname name")
aesKey = flag.String("aeskey", "very-secure-key0", "aeskey key")
self string
)
type Message struct {
ID string
Name string
Addr string
Body string
Recipient string
}
func main() {
flag.Parse()
aesKeyLen := len(*aesKey)
if aesKeyLen != 16 && aesKeyLen != 24 && aesKeyLen != 24 {
log.Println("Current AES key length:", aesKeyLen)
log.Fatal("Your AES key should be 16, 24 or 32 bytes long.")
}
log.Println("Using name:", *myName)
l, err := net.Listen("tcp", *listenAddr)
if err != nil {
log.Fatal(err)
}
self = l.Addr().String()
log.Println("Listening on", self)
if *peerAddr != "" {
go dial(*peerAddr)
}
go readInput()
for {
c, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go serve(c)
}
}
var peers = &Peers{m: make(map[string]chan<- Message)}
type Peers struct {
m map[string]chan<- Message
mu sync.RWMutex
}
// Add creates and returns a new channel for the given peer address.
// If an address already exists in the registry, it returns nil.
func (p *Peers) Add(addr string) <-chan Message {
p.mu.Lock()
defer p.mu.Unlock()
if _, ok := p.m[addr]; ok {
return nil
}
ch := make(chan Message)
p.m[addr] = ch
return ch
}
// Remove deletes the specified peer from the registry.
func (p *Peers) Remove(addr string) {
p.mu.Lock()
defer p.mu.Unlock()
delete(p.m, addr)
}
// List returns a slice of all active peer channels.
func (p *Peers) List() []chan<- Message {
p.mu.RLock()
defer p.mu.RUnlock()
l := make([]chan<- Message, 0, len(p.m))
for _, ch := range p.m {
l = append(l, ch)
}
return l
}
func broadcast(m Message) {
for _, ch := range peers.List() {
select {
case ch <- m:
default:
// Okay to drop messages sometimes.
}
}
}
func privateMessage(m Message, peer string) {
peers.mu.RLock()
defer peers.mu.RUnlock()
m.Body = encrypt([]byte(*aesKey), m.Body)
if _, ok := peers.m[peer]; ok {
select {
case peers.m[peer] <- m:
default:
// Okay to drop messages sometimes.
}
} else {
broadcast(m)
}
}
func getPrivateMessageRecipient(msg string) (string, bool) {
msgParse := strings.SplitN(msg, "|", 2)
if len(msgParse) > 1 {
return msgParse[0], true
} else {
return "", false
}
}
func serve(c net.Conn) {
defer c.Close()
d := json.NewDecoder(c)
for {
var m Message
err := d.Decode(&m)
if err != nil {
log.Println(err)
return
}
if strings.Contains(m.Body, "Spam") {
continue
}
if Seen(m.ID) {
continue
}
if m.Recipient != "" {
if m.Recipient == self {
m.Body = decrypt([]byte(*aesKey), m.Body)
log.Println("Incoming private connection from:", m.Name, m.Addr)
fmt.Printf("%#v\n", m)
} else {
broadcast(m)
}
} else {
log.Println("Incoming connection from:", m.Name, m.Addr)
fmt.Printf("%#v\n", m)
broadcast(m)
}
go dial(m.Addr)
}
}
func readInput() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
peer, ok := getPrivateMessageRecipient(s.Text())
m := Message{}
if ok {
m = Message{
ID: util.RandomID(),
Name: *myName,
Addr: self,
Body: strings.Replace(s.Text(), peer+"|", "", 2),
Recipient: peer,
}
privateMessage(m, peer)
} else {
m = Message{
ID: util.RandomID(),
Name: *myName,
Addr: self,
Body: s.Text(),
}
broadcast(m)
}
Seen(m.ID)
}
if err := s.Err(); err != nil {
log.Fatal(err)
}
}
func dial(addr string) {
if addr == self {
return // Don't try to dial self.
}
ch := peers.Add(addr)
if ch == nil {
return // Peer already connected.
}
defer peers.Remove(addr)
c, err := net.Dial("tcp", addr)
if err != nil {
log.Println(addr, err)
return
}
log.Println("Connected to", addr)
defer c.Close()
e := json.NewEncoder(c)
for m := range ch {
err := e.Encode(m)
if err != nil {
log.Println(addr, err)
return
}
}
}
var seenIDs = SeenMessages{sm: make(map[string]bool)}
type SeenMessages struct {
sm map[string]bool
mu sync.RWMutex
}
// Seen returns true if the specified id has been seen before.
// If not, it returns false and marks the given id as "seen".
func Seen(id string) bool {
seenIDs.mu.RLock()
defer seenIDs.mu.RUnlock()
if _, ok := seenIDs.sm[id]; ok {
return true
}
seenIDs.sm[id] = true
return false
}
// encrypt string to base64 crypto using AES
func encrypt(key []byte, text string) string {
// key := []byte(keyText)
plaintext := []byte(text)
block, err := aes.NewCipher(key)
if err != nil {
log.Println(err)
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
log.Println(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
// convert to base64
return base64.URLEncoding.EncodeToString(ciphertext)
}
// decrypt from base64 to decrypted string
func decrypt(key []byte, cryptoText string) string {
ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if len(ciphertext) < aes.BlockSize {
log.Println("ciphertext too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(ciphertext, ciphertext)
return fmt.Sprintf("%s", ciphertext)
}