Exemplo n.º 1
0
func Parse(log *logging.Logger, data []uint8, conn net.Conn) {
	var cmd common.JSONMessage

	if err := json.Unmarshal(data, &cmd); err != nil {
		log.Debug("Error when parsing data: " + string(data))
		res := common.JSONResult{Result: "ko", Message: "Malformed command: " + err.Error()}
		res.Send(conn)
		return
	}
	log.Debug("Received data: " + string(data))

	switch cmd.Context {
	case "system":
		log.Debug("System context requested")
		switch cmd.Command.Name {
		case "exit":
			log.Debug("Exit command requested")
			res := common.JSONResult{Result: "ok", Message: "Client closed"}
			res.Send(conn)
			os.Exit(0)
		case "exec":
			log.Debug("Execute command requested")
			if err := common.ExecuteCMD(cmd.Command.Cmd); err != nil {
				log.Debug("Error when executing command: " + err.Error())
			}
			res := common.JSONResult{Result: "ok", Message: "Command executed"}
			res.Send(conn)
		default:
			log.Debug("Invalid command requested: " + cmd.Command.Name)
			res := common.JSONResult{Result: "ko", Message: "Command unknown: " + cmd.Command.Name}
			res.Send(conn)
		}
	case "file":
		log.Debug("File context requested")
		switch cmd.Command.Name {
		case "list":
			log.Debug("List command requested")
			files.List(log, conn, &cmd.Command)
		case "get":
			log.Debug("Get command requested")
			common.SendFile(cmd.Command.Element.Name, conn)
		case "put":
			log.Debug("Put command requested")
			files.Put(log, conn, &cmd.Command)
		default:
			log.Debug("Invalid command requested: " + cmd.Command.Name)
			res := common.JSONResult{Result: "ko", Message: "Command unknown: " + cmd.Command.Name}
			res.Send(conn)
		}
	default:
		log.Debug("Invalid context requested: " + cmd.Context)
		res := common.JSONResult{Result: "ko", Message: "Context unknown: " + cmd.Context}
		res.Send(conn)
	}
}
Exemplo n.º 2
0
func put(log *logging.Logger, section *common.Section, cfg *ini.File, cmd *common.JSONMessage) {
	var conn net.Conn
	var err error

	conn, err = network.GetSocket(cfg.Section(section.Name))
	if err != nil {
		log.Error("Error when opening connection with section " + section.Name)
		log.Debug("error: " + err.Error())
		return
	}
	defer conn.Close()

	cmd.Send(conn)

	if cmd.Command.Element.Type == "file" {
		transfered := dataset.Path(cfg, section, false) + string(filepath.Separator) + cmd.Command.Element.Name
		if err := common.SendFile(transfered, conn); err != nil {
			log.Debug("Error when sending file: ", err.Error())
		}

	}
}