//curl -i -X GET 'localhost:9090/sshPort[/2200]'
func (this *SshPortController) Get() {
	portNum := this.Ctx.Input.Params[":objectId"]
	dbconn, err := models.InitDbConn(0)
	if err != nil {
		logs.Error("db init error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshPortM := new(models.SshPortManage)
	//当没有传入具体的port值时,输出全部sshport对象
	if portNum == "" {
		sshPortObs, _ := sshPortM.QueryAll(dbconn, 0)
		this.Data["json"] = sshPortObs
		this.ServeJson()
		this.StopRun()
	}

	//输出portNum对应的sshPort信息
	sshPortOb, err := sshPortM.Query(dbconn, portNum, 0)
	if err != nil {
		logs.Error("ssh port query error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	this.Data["json"] = sshPortOb
	this.ServeJson()
}
//curl localhost:9090/sshKeys/123456
func (this *SshKeysController) Get() {
	uid := this.Ctx.Input.Params[":objectId"]
	if uid == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}

	dbconn, err := models.InitDbConn(0)
	if err != nil {
		logs.Error("db init error!", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshKeysM := new(models.SshKeysManage)
	sshKeysOb, err := sshKeysM.GetAll(dbconn, uid, 0)
	if err != nil {
		logs.Error("sshkeysM getall error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	this.Data["json"] = sshKeysOb
	this.ServeJson()
}
//curl -i -X GET localhost:9090/sshRules/9999
func (this *SshRulesController) GetByUid() {
	uid := this.Ctx.Input.Params[":uid"]
	if uid == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}

	dbconn, err := models.InitDbConn(0)
	if err != nil {
		logs.Error("init db conn error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshRulesM := new(models.SshRuleManage)
	rulelist, err := sshRulesM.QueryByUid(dbconn, uid, 0)
	if err != nil {
		logs.Error("ssh rule query by uid error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	this.Data["json"] = rulelist
	this.ServeJson()
}
func (this *SshProxyServerController) Get() {
	host := this.Ctx.Input.Params[":objectId"]
	if host == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}

	dbconn, err := models.InitDbConn(0)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshProxyM := new(models.SshProxyManage)
	sshProxyOb, err := sshProxyM.Query(dbconn, host, 0)
	if err != nil {
		logs.Error("ssh proxy query error:", err, "logid:", 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	logs.Normal("ssh post ok!", sshProxyOb, "logid:", 0)
	this.Data["json"] = sshProxyOb
	this.ServeJson()
}
//curl localhost:9090/sshKeys -X PUT -i -d '{"uid":5544,"keyname":"testliuyanglong","publickey":"d","logid":"444444"}'
func (this *SshKeysController) Put() {
	requestBody := string(this.Ctx.Input.CopyBody())
	var sshKeysOb models.SshKeys
	err := json.Unmarshal(this.Ctx.Input.CopyBody(), &sshKeysOb)
	if err != nil {
		//fmt.Println(err)
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, err := models.GetLogId(this.Ctx.Input.CopyBody())
	if err != nil {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("param is:", requestBody, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()
	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)
	sshKeysM := new(models.SshKeysManage)
	err = sshKeysM.Update(dbconn, sshKeysOb, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("ssh keys update error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}

	//reload ssh public key
	err = this.reloadSshPublicKeys(dbconn, strconv.Itoa(sshKeysOb.Uid), logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("reload ssh public key error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("ssh key put ok", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}
//curl -i -X GET localhost:9090/sshRules/9999/aiaiia
func (this *SshRulesController) DeleteByContainer() {
	uid := this.Ctx.Input.Params[":uid"]
	containerName := this.Ctx.Input.Params[":container"]
	if uid == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, _ := this.GetInt("logid")
	if logid == 0 {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("delete param:", "uid:", uid, "containerName:", containerName, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn err:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)

	sshRulesM := new(models.SshRuleManage)
	delrule, _ := sshRulesM.Query(dbconn, uid, containerName, logid)
	err = sshRulesM.Delete(dbconn, uid, containerName, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("delete err:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}

	err = models.DeleteContainerUserFromProxy([]models.SshRule{delrule}, logid)
	if err != nil {
		logs.Error("Delete single Container From Proxy error:", err, "logid:", logid)
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", logid)
	}

	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)

	logs.Normal("delete OK!", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}
//curl -i -X POST localhost:9090/sshRules/9999 -d '{"name":"aiaiia","port":4242,"rule":"~~~~","uid":9999}'
func (this *SshRulesController) Post() {
	var sshRulesOb models.SshRule
	requestBody := string(this.Ctx.Input.RequestBody)

	err := json.Unmarshal(this.Ctx.Input.RequestBody, &sshRulesOb)
	if err != nil {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, err := models.GetLogId(this.Ctx.Input.RequestBody)
	if err != nil {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("post data:", string(requestBody), "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)

	sshRulesM := new(models.SshRuleManage)
	err = sshRulesM.Insert(dbconn, sshRulesOb, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("ssh rules insert error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}

	if err = this.reloadRules(dbconn, sshRulesOb, logid); err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("reload rules error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("post OK!", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
}
func (this *SshPortController) GetBestUsePort(logid int64) (models.SshPort, error) {
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn err:", err, "logid:", logid)
		return models.SshPort{}, err
	}
	defer dbconn.Close()

	sshPortM := new(models.SshPortManage)
	portOb, err := sshPortM.GetBestUsePort(dbconn, logid)
	if err != nil {
		logs.Error("ssh port get best userport error:", err, "logid:", logid)
	}
	logs.Normal("get best use port ok!", portOb, "logid:", logid)
	return portOb, err
}
func (this *SshPortController) UpdateNumber(port, num string, isplus bool, logid int64) error {
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn err:", err, "logid:", logid)
		return err
	}
	defer dbconn.Close()

	sshPortM := new(models.SshPortManage)
	err = sshPortM.Update(dbconn, port, num, isplus, logid)
	if err != nil {
		logs.Error("ssh port update error:", err, "logid:", logid)
		return err
	}
	logs.Normal("ssh updatenumber OK", logid)
	return nil
}
func (this *SshProxyServerController) Delete() {
	host := this.Ctx.Input.Params[":objectId"]
	if host == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, _ := this.GetInt("logid")
	if logid == 0 {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("delete param:", "host:", host, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)

	sshProxyM := new(models.SshProxyManage)
	err = sshProxyM.Delete(dbconn, host, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("ssh proxy delete err:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("ssh proxy delete OK!", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}
//curl -i -X DELETE 'localhost:9090/sshKeys/4444444?logid=4223'
func (this *SshKeysController) Delete() {
	uid := this.Ctx.Input.Params[":objectId"]
	keyname := this.Ctx.Input.Params[":keyname"]
	if uid == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, _ := this.GetInt("logid")
	if logid == 0 {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("delete param:", "uid:", uid, "keyname:", keyname, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()
	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)
	sshKeysM := new(models.SshKeysManage)
	if keyname != "" {
		err = sshKeysM.DeleteByKey(dbconn, uid, keyname, logid)
		if err != nil {
			dbconn.Exec("ROLLBACK")
			logs.Normal("ROLLBACK", "logid:", logid)
			logs.Error("delete by key error:", err, "logid:", logid)
			this.Ctx.Output.SetStatus(500)
			this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
			this.StopRun()
		}
	} else {
		err = sshKeysM.Delete(dbconn, uid, logid)
		if err != nil {
			dbconn.Exec("ROLLBACK")
			logs.Normal("ROLLBACK", "logid:", logid)
			logs.Error("delete by uid error:", err, "logid:", logid)
			this.Ctx.Output.SetStatus(500)
			this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
			this.StopRun()
		}
	}

	//reload ssh public key
	err = this.reloadSshPublicKeys(dbconn, uid, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("reload ssh public key error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("delete over", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}