Quellcode durchsuchen

[fix] 랜덤 키 생성시 시드 주입

master
kidjung vor 4 Jahren
Ursprung
Commit
47be1382bb
1 geänderte Dateien mit 45 neuen und 56 gelöschten Zeilen
  1. 45
    56
      src/main.go

+ 45
- 56
src/main.go Datei anzeigen

@@ -1,4 +1,5 @@
1 1
 package main
2
+
2 3
 // TODO: 등록확인 되기 전에는 PM,SM 전송 불가하게 만들 것, 실행파일로 만든 후, AWS 이용해서 여러개 마이크로서비스 상태에서 테스트 해볼것
3 4
 
4 5
 import (
@@ -16,6 +17,7 @@ import (
16 17
 	"strconv"
17 18
 	"strings"
18 19
 	"syscall"
20
+	"time"
19 21
 	"unicode"
20 22
 )
21 23
 
@@ -57,8 +59,8 @@ type SubscriptionMsg struct {
57 59
 
58 60
 type PublishMsg struct {
59 61
 	Message
60
-	Topic []int64 //대주제
61
-	Value []int64 //Topic 의 세부적인 내용
62
+	Topic   []int64 //대주제
63
+	Value   []int64 //Topic 의 세부적인 내용
62 64
 	Content []int64 // 내용
63 65
 }
64 66
 
@@ -66,24 +68,21 @@ type WithdrawMsg struct {
66 68
 	Message
67 69
 }
68 70
 
69
-
70
-
71 71
 type Receiver struct {
72 72
 	thisNodeAddr string
73 73
 	microService MicroService
74 74
 }
75 75
 
76 76
 type MicroService struct {
77
-
78 77
 	ClientAddr string
79 78
 	PrivateKey int64
80
-	ShareKey int64
79
+	ShareKey   int64
81 80
 	// 메세지 미들웨어에 연결되어 있는가
82 81
 	IsConnected chan bool
83
-	MMAddress string
82
+	MMAddress   string
84 83
 }
85 84
 
86
-func (receiver Receiver) Receive(args Args, reply *Reply) error{
85
+func (receiver Receiver) Receive(args Args, reply *Reply) error {
87 86
 	switch args.Kind {
88 87
 
89 88
 	case KSM:
@@ -165,7 +164,7 @@ func (receiver Receiver) Receive(args Args, reply *Reply) error{
165 164
 	return nil
166 165
 }
167 166
 
168
-func (microService MicroService) Receive(msg MsgUnit){
167
+func (microService MicroService) Receive(msg MsgUnit) {
169 168
 	var msg_type = msg.CheckType()
170 169
 	//메세지 타입에 따라 다르게 처리
171 170
 	switch msg_type {
@@ -174,7 +173,7 @@ func (microService MicroService) Receive(msg MsgUnit){
174 173
 
175 174
 	case PM: //Publish msg
176 175
 		fmt.Println("----- received PM ------")
177
-		DecryptionMsg(msg.(PublishMsg),microService.ShareKey, microService.PrivateKey)
176
+		DecryptionMsg(msg.(PublishMsg), microService.ShareKey, microService.PrivateKey)
178 177
 		fmt.Println("------------------------")
179 178
 
180 179
 	case SM: //Subscription msg
@@ -194,7 +193,6 @@ func (microService MicroService) Receive(msg MsgUnit){
194 193
 	return
195 194
 }
196 195
 
197
-
198 196
 func (msg SubscriptionMsg) ConvertToJson() ([]byte, error) {
199 197
 	js := msg
200 198
 	jsonBytes, err := json.Marshal(js)
@@ -231,7 +229,7 @@ func CreatePubMsg(msg Message, topic string, value string, content string) Publi
231 229
 	//fmt.Print("Topic length ")
232 230
 	//fmt.Println(len(intArr))
233 231
 	//fmt.Println(len(toPubMsg.Topic))
234
-	var topicInt [] int64
232
+	var topicInt []int64
235 233
 	for index := 0; index < len(intArr); index++ {
236 234
 		//toPubMsg.Topic = append(toPubMsg.Topic, int64(intArr[index]))
237 235
 		topicInt = append(topicInt, int64(intArr[index]))
@@ -239,9 +237,9 @@ func CreatePubMsg(msg Message, topic string, value string, content string) Publi
239 237
 	//fmt.Println(len(toPubMsg.Topic))
240 238
 
241 239
 	intArr = []rune(value)
242
-	var valueInt [] int64
243
-	strInt64, _ :=  strconv.ParseInt(value, 10, 64)
244
-	if unicode.IsDigit(intArr[0]){
240
+	var valueInt []int64
241
+	strInt64, _ := strconv.ParseInt(value, 10, 64)
242
+	if unicode.IsDigit(intArr[0]) {
245 243
 		valueInt = append(valueInt, strInt64)
246 244
 	} else {
247 245
 		for index := 0; index < len(intArr); index++ {
@@ -251,14 +249,13 @@ func CreatePubMsg(msg Message, topic string, value string, content string) Publi
251 249
 	}
252 250
 
253 251
 	intArr = []rune(content)
254
-	var contentInt [] int64
252
+	var contentInt []int64
255 253
 	for index := 0; index < len(intArr); index++ {
256 254
 		//toPubMsg.content = append(toPubMsg.content, int64(intArr[index]))
257 255
 		contentInt = append(contentInt, int64(intArr[index]))
258 256
 	}
259 257
 
260
-
261
-	return PublishMsg{msg,topicInt,valueInt,contentInt}
258
+	return PublishMsg{msg, topicInt, valueInt, contentInt}
262 259
 }
263 260
 
264 261
 func CreateSubMsg(msg Message, topic string, value string, operator string, isAlpha bool) SubscriptionMsg {
@@ -269,13 +266,13 @@ func CreateSubMsg(msg Message, topic string, value string, operator string, isAl
269 266
 	//fmt.Print("Topic length ")
270 267
 	//fmt.Println(len(intArr))
271 268
 	//fmt.Println(len(toPubMsg.Topic))
272
-	var topicInt [] int64
269
+	var topicInt []int64
273 270
 	for index := 0; index < len(intArr); index++ {
274 271
 		//toPubMsg.Topic = append(toPubMsg.Topic, int64(intArr[index]))
275 272
 		topicInt = append(topicInt, int64(intArr[index]))
276 273
 	}
277 274
 	//fmt.Println(len(toPubMsg.Topic))
278
-	var valueInt [] int64
275
+	var valueInt []int64
279 276
 	if isAlpha {
280 277
 		intArr = []rune(value)
281 278
 
@@ -285,7 +282,7 @@ func CreateSubMsg(msg Message, topic string, value string, operator string, isAl
285 282
 		}
286 283
 	} else {
287 284
 		stringSlice := strings.Split(value, " ")
288
-		for index:=0; index < len(stringSlice); index++ {
285
+		for index := 0; index < len(stringSlice); index++ {
289 286
 			strInt64, _ := strconv.ParseInt(stringSlice[index], 10, 64)
290 287
 			valueInt = append(valueInt, strInt64)
291 288
 		}
@@ -293,7 +290,6 @@ func CreateSubMsg(msg Message, topic string, value string, operator string, isAl
293 290
 	var operatorSlice []string
294 291
 	operatorSlice = strings.Split(operator, " ")
295 292
 
296
-
297 293
 	//intArr = []rune(content)
298 294
 	//var contentInt [] int64
299 295
 	//for index := 0; index < len(intArr); index++ {
@@ -301,14 +297,12 @@ func CreateSubMsg(msg Message, topic string, value string, operator string, isAl
301 297
 	//	contentInt = append(contentInt, int64(intArr[index]))
302 298
 	//}
303 299
 
304
-
305
-	return SubscriptionMsg{msg,topicInt,valueInt,operatorSlice, isAlpha}
300
+	return SubscriptionMsg{msg, topicInt, valueInt, operatorSlice, isAlpha}
306 301
 }
307 302
 
308
-
309 303
 type Args struct { // 매개변수
310
-	JsonMsg [] byte
311
-	Kind int
304
+	JsonMsg []byte
305
+	Kind    int
312 306
 }
313 307
 
314 308
 type Reply struct { // 리턴값
@@ -316,15 +310,16 @@ type Reply struct { // 리턴값
316 310
 }
317 311
 
318 312
 func generateRanUint64() uint64 {
313
+	rand.Seed(time.Now().UnixNano())
319 314
 	return uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
320 315
 }
321 316
 
322
-func main(){
317
+func main() {
323 318
 
324 319
 	var argu string
325 320
 	for _, v := range os.Args {
326 321
 		if strings.Index(v, "-mma") == 0 {
327
-			argu = strings.Replace(v, "-mma=", "",-1)
322
+			argu = strings.Replace(v, "-mma=", "", -1)
328 323
 			break
329 324
 		}
330 325
 	}
@@ -334,7 +329,6 @@ func main(){
334 329
 		return
335 330
 	}
336 331
 
337
-
338 332
 	args := new(Args)
339 333
 	reply := new(Reply)
340 334
 
@@ -343,11 +337,11 @@ func main(){
343 337
 	privateKey := int64(generateRanUint64())
344 338
 	shareKey := int64(9999)
345 339
 
346
-	microService := MicroService{currentIP,privateKey, shareKey, make(chan bool), MMAddress}
347
-	receiver := Receiver{thisNodeAddr: currentIP,microService: microService}
340
+	microService := MicroService{currentIP, privateKey, shareKey, make(chan bool), MMAddress}
341
+	receiver := Receiver{thisNodeAddr: currentIP, microService: microService}
348 342
 
349
-	color.Blue("<current machine address : " + currentIP +  "> <MM address : " + MMAddress + ">")
350
-	color.Blue("<private key : " + strconv.FormatUint(uint64(privateKey), 10)+">")
343
+	color.Blue("<current machine address : " + currentIP + "> <MM address : " + MMAddress + ">")
344
+	color.Blue("<private key : " + strconv.FormatUint(uint64(privateKey), 10) + ">")
351 345
 
352 346
 	errReg := rpc.Register(receiver)
353 347
 	if errReg != nil {
@@ -355,7 +349,7 @@ func main(){
355 349
 		return
356 350
 	}
357 351
 
358
-	client, err := rpc.Dial("tcp", microService.MMAddress + ":8160") // RPC 서버에 연결
352
+	client, err := rpc.Dial("tcp", microService.MMAddress+":8160") // RPC 서버에 연결
359 353
 	if err != nil {
360 354
 		fmt.Println(err)
361 355
 		return
@@ -364,11 +358,10 @@ func main(){
364 358
 
365 359
 	go Listen()
366 360
 
367
-
368 361
 	// Register 메세지 생성
369 362
 	message := Message{From: microService.ClientAddr, Version: "1", Time: "1", Kind: RM}
370 363
 	regMsg := RegisterMsg{message, microService.PrivateKey}
371
-	RJsonMsg,_ := regMsg.ConvertToJson()
364
+	RJsonMsg, _ := regMsg.ConvertToJson()
372 365
 	args.JsonMsg = RJsonMsg
373 366
 	args.Kind = RM
374 367
 	err = client.Call("Receiver.MmReceive", args, reply)
@@ -379,16 +372,15 @@ func main(){
379 372
 	}
380 373
 	color.Yellow("MM: " + reply.CompleteLog)
381 374
 
382
-
383 375
 	// 연결 되면 채널로 연결 확인이 되어야 다음 단계로 넘어감
384
-	checkConnect := <- microService.IsConnected
376
+	checkConnect := <-microService.IsConnected
385 377
 	_ = checkConnect
386 378
 	fmt.Scanln()
387 379
 	fmt.Println("***** sending Subscription messages *****")
388 380
 
389 381
 	/*
390
-	파일에서 subscription 읽어서 subscription 보내기
391
-	 **/
382
+		파일에서 subscription 읽어서 subscription 보내기
383
+		 **/
392 384
 	subFile, err := os.Open("subscription.txt")
393 385
 	if err != nil {
394 386
 		log.Fatalf("Error when opening file: %s", err)
@@ -397,14 +389,14 @@ func main(){
397 389
 	fileScanner := bufio.NewScanner(subFile)
398 390
 	var subscriptionText string
399 391
 	numSub := 0
400
-	for fileScanner.Scan(){
392
+	for fileScanner.Scan() {
401 393
 		subscriptionText = fileScanner.Text()
402 394
 		subscriptionTextSlice := strings.Split(subscriptionText, "/")
403 395
 		subTopic := subscriptionTextSlice[0]
404 396
 		subValue := subscriptionTextSlice[1]
405 397
 		subOperator := subscriptionTextSlice[2]
406 398
 		var subIsAlpha bool
407
-		if subscriptionTextSlice[3] == "true"{
399
+		if subscriptionTextSlice[3] == "true" {
408 400
 			subIsAlpha = true
409 401
 		} else if subscriptionTextSlice[3] == "false" {
410 402
 			subIsAlpha = false
@@ -416,15 +408,15 @@ func main(){
416 408
 		subMsg := CreateSubMsg(message, subTopic, subValue, subOperator, subIsAlpha)
417 409
 		//subMsg := CreateSubMsg(message, "soccer", "player", "==", true)
418 410
 		//fmt.Println(pubMsg)
419
-		subMsg = EncryptionSubMsg(subMsg,microService.ShareKey,microService.PrivateKey)
420
-		jsonMsg,_ := subMsg.ConvertToJson()
411
+		subMsg = EncryptionSubMsg(subMsg, microService.ShareKey, microService.PrivateKey)
412
+		jsonMsg, _ := subMsg.ConvertToJson()
421 413
 		args.JsonMsg = jsonMsg
422 414
 		args.Kind = subMsg.Kind
423 415
 		//fmt.Println(subMsg)
424 416
 
425 417
 		//fmt.Println(string(args.JsonMsg))
426 418
 		err = client.Call("Receiver.MmReceive", args, reply)
427
-		log.Println("SM sent! #:",numSub)
419
+		log.Println("SM sent! #:", numSub)
428 420
 		if err != nil {
429 421
 			fmt.Println(err)
430 422
 			return
@@ -445,7 +437,7 @@ func main(){
445 437
 	fileScanner2 := bufio.NewScanner(pubFile)
446 438
 	var publishText string
447 439
 	numPub := 0
448
-	for fileScanner2.Scan(){
440
+	for fileScanner2.Scan() {
449 441
 		publishText = fileScanner2.Text()
450 442
 		publishTextSlice := strings.Split(publishText, "/")
451 443
 		//fmt.Println(publishTextSlice)
@@ -453,20 +445,19 @@ func main(){
453 445
 		pubValue := publishTextSlice[1]
454 446
 		pubContent := publishTextSlice[2]
455 447
 
456
-
457 448
 		message = Message{From: microService.ClientAddr, Version: "1", Time: "1", Kind: PM}
458 449
 		pubMsg := CreatePubMsg(message, pubTopic, pubValue, pubContent)
459 450
 		//subMsg := CreateSubMsg(message, "soccer", "player", "==", true)
460 451
 		//fmt.Println("Pub.txt",pubMsg)
461
-		pubMsg = EncryptionPubMsg(pubMsg,microService.ShareKey,microService.PrivateKey)
462
-		jsonMsg,_ := pubMsg.ConvertToJson()
452
+		pubMsg = EncryptionPubMsg(pubMsg, microService.ShareKey, microService.PrivateKey)
453
+		jsonMsg, _ := pubMsg.ConvertToJson()
463 454
 		args.JsonMsg = jsonMsg
464 455
 		args.Kind = pubMsg.Kind
465 456
 		//fmt.Println(subMsg)
466 457
 
467 458
 		//fmt.Println(string(args.JsonMsg))
468 459
 		err = client.Call("Receiver.MmReceive", args, reply)
469
-		log.Println("PM sent! #:",numPub)
460
+		log.Println("PM sent! #:", numPub)
470 461
 		if err != nil {
471 462
 			fmt.Println(err)
472 463
 			return
@@ -494,7 +485,7 @@ func main(){
494 485
 	return
495 486
 }
496 487
 
497
-func withDraw(msg Message, client *rpc.Client){
488
+func withDraw(msg Message, client *rpc.Client) {
498 489
 	msg.Kind = WM
499 490
 	wdMsg := WithdrawMsg{msg}
500 491
 	var args Args
@@ -515,7 +506,6 @@ func EncryptionPubMsg(msg PublishMsg, gyKey int64, privateKey int64) PublishMsg
515 506
 		msg.Content[index] = msg.Content[index] + gyKey + privateKey
516 507
 	}
517 508
 
518
-
519 509
 	return msg
520 510
 }
521 511
 
@@ -551,7 +541,7 @@ func DecryptionMsg(msg PublishMsg, gyKey int64, privateKey int64) {
551 541
 		runeArr = append(runeArr, rune(int(msg.Value[index])))
552 542
 	}
553 543
 
554
-	if len(runeArr) == 1{
544
+	if len(runeArr) == 1 {
555 545
 		fmt.Println("Value is: ", runeArr)
556 546
 	} else {
557 547
 		fmt.Println("Value is:", string(runeArr))
@@ -582,9 +572,8 @@ func Listen() {
582 572
 	}
583 573
 	defer l.Close()
584 574
 
585
-
586 575
 	for {
587 576
 		conn, _ := l.Accept()
588 577
 		go rpc.ServeConn(conn)
589 578
 	}
590
-}
579
+}

Laden…
Abbrechen
Speichern