コード例 #1
0
ファイル: appclient.go プロジェクト: ericcapricorn/gateway
func loop() {
	body := make([]byte, 1024)
	// TODO using http client request the device
	var request, response common.Message
	for i := 0; i < 3600*1000; i++ {
		request.Header.PayloadLen = uint16(rand.Intn(30) + 1)
		request.Header.Version = 123
		request.Header.MsgCode = common.ZC_CODE_ZDESCRIBE
		request.Header.MsgId = uint8(i + 101)
		var j uint16
		for j = 0; j < request.Header.PayloadLen; j++ {
			body[j] = byte(j + 1)
		}
		old := time.Now().UTC()
		request.Payload = body[:request.Header.PayloadLen]
		fmt.Println("Write Request:", request.Header, body[:request.Header.PayloadLen])
		// check the message
		if response.Header.MsgCode != common.ZC_CODE_ERR {
			fmt.Println("Read Response:", response.Header, body[:response.Header.PayloadLen])
			for j = 0; j < response.Header.PayloadLen; j++ {
				common.Assert(response.Payload[j] == body[j], "check the response failed")
			}
			fmt.Println("request timeused:", request.Header.MsgId, old, time.Now().UTC())
		} else {
			fmt.Println("Send Request to Device Failed")
		}
		time.Sleep(time.Millisecond * 50)
	}
}
コード例 #2
0
func (this *GatewayServiceHandler) handleForwardRequest(req *zc.ZMsg, resp *zc.ZMsg) {
	domain := req.GetString("domain")
	subDomain := req.GetString("submain")
	deviceId := req.GetString("deviceid")
	Conn, find := this.device.Find(*server.NewDeviceGID(domain, subDomain, deviceId))
	if !find || Conn == nil {
		resp.SetErr(common.ErrDeviceForward.Error())
		log.Warningf("the device gateway not find:gid[%s:%s:%s]", domain, subDomain, deviceId)
		return
	}
	// convert the request to message by loading by so
	var request common.Message
	request.Header.Version = 1
	request.Header.MsgId = 1
	request.Header.MsgCode = 123
	payload, _ := req.GetPayload()
	request.Header.PayloadLen = uint16(len(payload))
	request.Payload = payload

	// receive the device response result
	result := make(chan common.Message, 32)
	defer close(result)
	err := Conn.(*server.Connection).SendRequest(&request, &result)
	if err != nil {
		resp.SetErr(err.Error())
		log.Warningf("forward message to dev failed:gid[%s:%s:%s], err[%v]", domain, subDomain, deviceId, err)
		return
	}
	log.Infof("forward message to device succ:code[%d], gid[%s:%s:%s]", request.Header.MsgCode, domain, subDomain, deviceId)

	// wait reponse from the device then response to the appclient
	response, err := this.waitResponse(request.Header.MsgId, result, common.APP_TIMEOUT)
	if err != nil {
		resp.SetErr(err.Error())
		log.Warningf("wait the current response failed:gid[%s:%s:%s], err[%v]", domain, subDomain, deviceId, err)
		return
	}
	log.Infof("forward device request succ:gid[%s:%s:%s]", domain, subDomain, deviceId)
	resp.PutObject("response", zc.ZObject{"body": response.Payload})
	resp.SetAck()
}
コード例 #3
0
ファイル: devclient.go プロジェクト: ericcapricorn/gateway
func loop() {
	//conn, err := net.Dial("tcp", "101.251.106.4:8384")
	//conn, err := net.Dial("tcp", "127.0.0.1:8384")
	conn, err := net.Dial("tcp", "192.168.1.114:8384")
	if err != nil {
		fmt.Println("Connect Error:", err)
		return
	}
	defer conn.Close()
	body := make([]byte, 1024)
	var report, request, response, empty common.Message
	// handshake step 1
	randKey := common.GenerateRandomKey(common.ZC_HS_MSG_LEN)
	request.Header.MsgCode = common.ZC_CODE_HANDSHAKE_1
	request.Header.PayloadLen = common.ZC_HS_MSG_LEN + common.ZC_HS_DEVICE_ID_LEN
	deviceId := "zzzzzzzzzzzz"
	fmt.Println("Generate random and id:", randKey, []byte(deviceId))
	request.Payload = make([]byte, request.Header.PayloadLen)
	copy(request.Payload[0:common.ZC_HS_MSG_LEN], randKey)
	copy(request.Payload[common.ZC_HS_MSG_LEN:request.Header.PayloadLen], []byte(deviceId))
	var contex common.EncryptContex
	contex.PublicKey = []byte("80138512665003396643737838315916663972728479914654754587175091902061894104953")
	contex.EncryptType = common.ZC_SEC_TYPE_RSA
	err = common.Send(conn, contex, &request, common.DEV_WRITE_TIMEOUT)
	if err != nil {
		fmt.Println("HANDSHAKE_1 failed:", err)
		return
	}
	fmt.Println("Write Handshake_1 succ")
	// handshake step 2
	contex.PrivateKey = common.PrivateKey
	err = common.Receive(conn, contex, &response, common.DEV_READ_TIMEOUT)
	if err != nil {
		fmt.Println("HANDSHAKE_2 failed:", err)
		return
	}
	sessionKey := make([]byte, common.ZC_HS_SESSION_KEY_LEN)
	copy(sessionKey, response.Payload[common.ZC_HS_MSG_LEN:])
	fmt.Println("Receive Handshake_2 Session Key:", response.Payload[:common.ZC_HS_MSG_LEN], sessionKey)

	// handshake step 3
	contex.EncryptType = common.ZC_SEC_TYPE_AES
	contex.SessionKey = sessionKey
	copy(request.Payload, randKey)
	request.Payload = request.Payload[0:common.ZC_HS_MSG_LEN]
	request.Header.PayloadLen = common.ZC_HS_MSG_LEN
	if len(request.Payload) != int(request.Header.PayloadLen) {
		fmt.Println("check payload len failed", len(request.Payload), request.Header.PayloadLen)
		return
	}
	request.Header.MsgCode = common.ZC_CODE_HANDSHAKE_3
	err = common.Send(conn, contex, &request, common.DEV_WRITE_TIMEOUT)
	if err != nil {
		fmt.Println("HANDSHAKE_3 failed:", err)
		return
	}
	fmt.Println("Write Handshake_3 succ")

	// handshake step 4
	err = common.Receive(conn, contex, &response, common.DEV_READ_TIMEOUT)
	if err != nil {
		fmt.Println("HANDSHAKE_4 failed:", err)
		return
	} else if !bytes.Equal(response.Payload, randKey) {
		fmt.Println("check random key failed")
		return
	} else {
		fmt.Println("CLOUD SAY HELLO WORLD TO DEV")
	}

	for i := 0; i < 3600; i++ {
		// step 1. send report message
		report.Header.PayloadLen = 0
		report.Header.Version = 123
		report.Header.MsgCode = common.ZC_CODE_HEARTBEAT
		report.Header.MsgId = uint8(i + 101)
		report.Payload = body[:report.Header.PayloadLen]
		err = common.Send(conn, contex, &report, common.DEV_WRITE_TIMEOUT)
		if err != nil {
			fmt.Println("Write Report Error:", err)
			return
		}
		// step 2. read the request
		err = common.Receive(conn, contex, &request, common.DEV_READ_TIMEOUT)
		if err != nil {
			fmt.Println("Receive Response Error:", err)
			return
		}
		fmt.Println("Read Request succ:", request.Header.MsgId, request.Payload)
		// step 3. echo empty message
		empty.Header.MsgId = request.Header.MsgId
		empty.Header.Version = request.Header.Version
		empty.Header.MsgCode = common.ZC_CODE_EMPTY
		empty.Header.PayloadLen = 0
		err = common.Send(conn, contex, &empty, common.DEV_WRITE_TIMEOUT)
		if err != nil {
			fmt.Println("Write Empty Response failed:", err)
			return
		}
		// fmt.Println("Write Empty Response", request.Header.MsgId)
		// step 4. write echo ack
		request.Header.MsgCode = common.ZC_CODE_ACK
		err = common.Send(conn, contex, &request, common.DEV_WRITE_TIMEOUT)
		if err != nil {
			fmt.Println("Write Response failed:", err)
			return
		}
		fmt.Println("Write Response:", request.Header.MsgId, request.Payload)
	}
}
コード例 #4
0
func (this *Connection) DeviceHandShake() (*DeviceGID, error) {
	var handShake common.Message
	// HANDSHAKE_1 using cloud private key
	this.contex.EncryptType = common.ZC_SEC_TYPE_RSA
	this.contex.PrivateKey = common.PrivateKey
	err := common.Receive(this.socket, this.contex, &handShake, common.DEV_READ_TIMEOUT)
	if err != nil {
		log.Warningf("read handShake_1 failed:err[%v]", err)
		return nil, err
	} else if handShake.Header.MsgCode != common.ZC_CODE_HANDSHAKE_1 {
		log.Warningf("check message code failed:code[%d]", handShake.Header.MsgCode)
		return nil, common.ErrInvalidMsg
	} else if handShake.Header.PayloadLen != (common.ZC_HS_MSG_LEN + common.ZC_HS_DEVICE_ID_LEN) {
		log.Warningf("check handshake step 1 failed:len[%d]", handShake.Header.PayloadLen)
		return nil, common.ErrInvalidMsg
	}
	fmt.Println("[DEV] HANDSHAKE_1 SUCC")

	// get device public key for rsa
	devRandom := handShake.Payload[0:common.ZC_HS_MSG_LEN]
	deviceId := handShake.Payload[common.ZC_HS_MSG_LEN : common.ZC_HS_MSG_LEN+common.ZC_HS_DEVICE_ID_LEN]
	log.Infof("Receive Dev Random and ID:", devRandom, deviceId)
	this.gid = DeviceGID{domain: "app", subDomain: "test", deviceId: string(deviceId)}
	device, err := this.deviceManager.Get(this.gid)
	if err != nil {
		log.Errorf("the device not valid:addr[%v], gid[%s]", this.socket.RemoteAddr(), this.gid.String())
		return nil, err
	}

	// HANDSHAKE_2 using device public key
	this.contex.EncryptType = common.ZC_SEC_TYPE_RSA
	this.contex.PublicKey = []byte(device.PublicKey())
	this.sessionKey = common.GenerateRandomKey(common.ZC_HS_SESSION_KEY_LEN)
	log.Infof("New session key:key[%v], dest[%v], gid[%s]", this.sessionKey, this.socket.RemoteAddr(), this.gid.String())
	handShake.Header.MsgId = this.getNewRequestId()
	var response common.Message
	response.Header.MsgCode = common.ZC_CODE_HANDSHAKE_2
	response.Header.PayloadLen = common.ZC_HS_MSG_LEN + common.ZC_HS_SESSION_KEY_LEN
	response.Payload = make([]byte, response.Header.PayloadLen)
	copy(response.Payload[0:common.ZC_HS_MSG_LEN], devRandom)
	copy(response.Payload[common.ZC_HS_MSG_LEN:response.Header.PayloadLen], this.sessionKey)
	err = common.Send(this.socket, this.contex, &response, common.DEV_WRITE_TIMEOUT)
	if err != nil {
		log.Warningf("Write ZC_CODE_HANDSHAKE_2 failed:err[%v]", err)
		return &this.gid, err
	}
	fmt.Println("[DEV] HANDSHAKE_2 SUCC")

	// HANDSHAKE_3 using session key
	this.contex.EncryptType = common.ZC_SEC_TYPE_AES
	this.contex.SessionKey = this.sessionKey
	err = common.Receive(this.socket, this.contex, &handShake, common.DEV_READ_TIMEOUT)
	if err != nil {
		log.Warningf("read handShake_3 failed:err[%v]", err)
		return &this.gid, err
	} else if handShake.Header.MsgCode != common.ZC_CODE_HANDSHAKE_3 {
		log.Warningf("check message code failed:code[%d]", handShake.Header.MsgCode)
		return &this.gid, common.ErrInvalidMsg
	} else if handShake.Header.PayloadLen != common.ZC_HS_MSG_LEN {
		log.Warningf("check handshake step 3 failed:len[%d]", handShake.Header.PayloadLen)
		return &this.gid, common.ErrInvalidMsg
	} else if !bytes.Equal(handShake.Payload, devRandom) {
		log.Warningf("check handshake content failed:payload[%v], random[%v]", handShake.Payload, devRandom)
		return &this.gid, common.ErrInvalidMsg
	}
	fmt.Println("[DEV] HANDSHAKE_3 SUCC")

	// HANDSHAKE_4 using session key
	this.contex.EncryptType = common.ZC_SEC_TYPE_AES
	this.contex.SessionKey = this.sessionKey
	handShake.Header.MsgId = this.getNewRequestId()
	handShake.Header.MsgCode = common.ZC_CODE_HANDSHAKE_4
	handShake.Header.PayloadLen = common.ZC_HS_MSG_LEN
	handShake.Payload = devRandom
	err = common.Send(this.socket, this.contex, &handShake, common.DEV_WRITE_TIMEOUT)
	if err != nil {
		log.Warningf("Write ZC_CODE_HANDSHAKE_4 failed:err[%v]", err)
		return &this.gid, err
	}
	fmt.Println("[DEV] HANDSHAKE_4 PASS, WELCOME:", this.socket.RemoteAddr())
	return &this.gid, err
}