Sfoglia il codice sorgente

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

master
kidjung 4 anni fa
parent
commit
47be1382bb
1 ha cambiato i file con 45 aggiunte e 56 eliminazioni
  1. 45
    56
      src/main.go

+ 45
- 56
src/main.go Vedi File

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

Loading…
Annulla
Salva