Exemplo n.º 1
0
func handleClient(conn net.Conn, config_obj *read_config.ReadConfig) {
	// close connection on exit
	defer conn.Close()
	pkt_rcv, _ := common.ReceivePacket(conn)

	cmd := string(pkt_rcv.CommandBuffer[:common.GetLen(pkt_rcv.CommandBuffer[:])])
	pkt_rcv_crc32value := pkt_rcv.CRC32Value

	if crc32, _ := common.DoCRC32(&pkt_rcv); crc32 != pkt_rcv_crc32value {
		fmt.Println("WARNING: CRC not matching", crc32, pkt_rcv_crc32value)
	}

	pkt_send := common.PrepareToSend(cmd, common.RESPONSE_PACKET)

	if pkt_send.ResultCode == common.STATE_UNKNOWN { //its a response, but not to the HELLO_COMMAND
		if config_obj.IsCommandAllowed(cmd) {
			str_cmd := config_obj.GetCommand(cmd)
			fmt.Println("executing:", str_cmd)
			return_id, return_stdout := common.ExecuteCommand(str_cmd)
			pkt_send.ResultCode = return_id
			copy(pkt_send.CommandBuffer[:], return_stdout)
			pkt_send.CRC32Value, _ = common.DoCRC32(&pkt_send)
		} else {
			pkt_send.ResultCode = common.STATE_CRITICAL
		}
	}

	err := common.SendPacket(conn, pkt_send)
	common.CheckError(err)
}