func OnRecvMsg()

in agent/channel/channel_manager.go [245:363]


func OnRecvMsg(Msg string, ChannelType int) string {
	log.GetLogger().Infoln("kick msg:", Msg)

	// legacy code for websocket kick data proc.
	if ChannelType == ChannelWebsocketType {
		if update.IsCriticalActionRunning() {
			return "reject:" + Msg
		}
		if Msg == "kick_vm" {
			go func() {
				taskengine.Fetch(true, "", taskengine.NormalTaskType)			}()
			return "accept:" + Msg
		} else if strings.Contains(Msg, "kick_vm agent deregister") {
			hybrid.UnRegister(true)
		}

		handle := kickvmhandle.ParseOption(Msg)
		valid_cmd := false
		if handle != nil {
			if handle.CheckAction() == true {
				valid_cmd = true
				go func() {
					handle.DoAction()
				}()
			}
		}
		if valid_cmd == false {
			return "unknow:" + Msg
		} else {
			return "accept:" + Msg
		}

	}

	if !gjson.Valid(Msg) {
		return BuildInvalidRet("invalid json1")
	}
	execute := gjson.Get(Msg, "execute").String()
	if execute == "guest-sync" {
		gshellCheck := GshellCheck{}
		err := json.Unmarshal([]byte(Msg), &gshellCheck)
		if err != nil {
			return BuildInvalidRet("invalid json: " + err.Error())
		}
		gshellCheckReply := GshellCheckReply{
			Return: gshellCheck.Arguments.ID,
		}
		retStr, _ := json.Marshal(gshellCheckReply)
		return string(retStr)
	} else if execute == "guest-command" {
		gshellCmd := GshellCmd{}
		err := json.Unmarshal([]byte(Msg), &gshellCmd)
		if err != nil {
			return BuildInvalidRet("invalid guest-command json: " + err.Error())
		}
		if update.IsCriticalActionRunning() {
			gshellCmdReply := GshellCmdReply{}
			gshellCmdReply.Return.Result = 7
			gshellCmdReply.Return.CmdOutput = "agent is busy"
			retStr, _ := json.Marshal(gshellCmdReply)
			return string(retStr)
		}
		if gshellCmd.Arguments.Cmd == "kick_vm" {
			go func() {
				taskengine.Fetch(true, "", taskengine.NormalTaskType)			}()
			gshellCmdReply := GshellCmdReply{}
			gshellCmdReply.Return.Result = 8
			gshellCmdReply.Return.CmdOutput = "execute kick_vm success"
			gshellCmdReply.Return.Netcheck = LastNetcheckReply()
			retStr, _ := json.Marshal(gshellCmdReply)
			return string(retStr)
		} else {
			handle := kickvmhandle.ParseOption(gshellCmd.Arguments.Cmd)
			valid_cmd := false
			if handle != nil {
				if handle.CheckAction() == true {
					valid_cmd = true
					go func() {
						handle.DoAction()
					}()
				}
			}
			if valid_cmd == false {
				gshellCmdReply := GshellCmdReply{}
				gshellCmdReply.Return.Result = 6
				gshellCmdReply.Return.CmdOutput = "invalid command"
				retStr, _ := json.Marshal(gshellCmdReply)
				return string(retStr)
			} else {
				gshellCmdReply := GshellCmdReply{}
				gshellCmdReply.Return.Result = 8
				gshellCmdReply.Return.CmdOutput = "execute kick_vm success"
				gshellCmdReply.Return.Netcheck = LastNetcheckReply()
				retStr, _ := json.Marshal(gshellCmdReply)
				return string(retStr)
			}
		}
	} else if execute == "guest-shutdown" {
		gshellShutdown := GshellShutdown{}
		err := json.Unmarshal([]byte(Msg), &gshellShutdown)
		if err != nil {
			return BuildInvalidRet("invalid guest-shutdown command: " + err.Error())
		}
		gshellCmdReply := GshellCmdReply{}
		gshellCmdReply.Return.Result = 8
		gshellCmdReply.Return.CmdOutput = "execute command success"
		retStr, _ := json.Marshal(gshellCmdReply)
		reboot := false
		if gshellShutdown.Arguments.Mode == powerutil.PowerdownMode {
		} else if gshellShutdown.Arguments.Mode == powerutil.RebootMode {
			reboot = true
		} else {
			return BuildInvalidRet("invalid guest-shutdown command")
		}
		powerutil.Shutdown(reboot)
		return string(retStr)
	}
	return BuildInvalidRet("invalid command")
}