之前我们介绍过EasyNVR硬件设备如何开启使用https,大家也可以了解一下。关于EasyNVR的管理平台EasyNVS,也是支持开启https的。本文就介绍一下EasyNVS视频管理平台添加https证书配置的步骤。
1、首先需要先添加两个上传证书的接口:
/** * @api {post} /api/v1/uploadfullchain 上传fullchain.pem证书 * @apiGroup channel * @apiUse simpleSuccess */ func (h *APIHandler) Uploadfullchain(c *gin.Context) { support := ".pem,.key,.crt,.cer" file, err := c.FormFile("file") if err != nil { err = fmt.Errorf("获取上传文件错误, %v", err) c.AbortWithStatusJSON(400, err.Error()) return } ext := filepath.Ext(file.Filename) reg := regexp.MustCompile("(?i)(" + strings.Join(strings.Split(support, ","), "|") + ")$") if !reg.Match([]byte(ext)) { c.AbortWithStatusJSON(400, "上传数据格式错误!") return } var dest = "" if runtime.GOARCH == "arm" { dest = filepath.Join(utils.ExcelDataDir(), fmt.Sprintf("%s", file.Filename)) } else { dest = filepath.Join(utils.SslDir(), fmt.Sprintf("%s", file.Filename)) } err = c.SaveUploadedFile(file, dest) if ext == ".key" || ext == ".pem" || ext == ".crt" || ext == ".cer" { utils.SaveToConf("https", map[string]string{ "ssl_cert_file": dest, }) } if err != nil { err = fmt.Errorf("保存上传文件错误, %v", err) c.AbortWithStatusJSON(500, err.Error()) return } c.IndentedJSON(200, "OK") h.Restart(c) } /** * @api {post} /api/v1/uploadprivkey 上传privkey * @apiGroup channel * @apiUse simpleSuccess */ func (h *APIHandler) Uploadprivkey(c *gin.Context) { support := ".pem,.key,.crt,.cer" file, err := c.FormFile("file") if err != nil { err = fmt.Errorf("获取上传文件错误, %v", err) c.AbortWithStatusJSON(400, err.Error()) return } ext := filepath.Ext(file.Filename) reg := regexp.MustCompile("(?i)(" + strings.Join(strings.Split(support, ","), "|") + ")$") if !reg.Match([]byte(ext)) { c.AbortWithStatusJSON(400, "上传数据格式错误!") return } var dest = "" if runtime.GOARCH == "arm" { dest = filepath.Join(utils.ExcelDataDir(), fmt.Sprintf("%s", file.Filename)) } else { dest = filepath.Join(utils.SslDir(), fmt.Sprintf("%s", file.Filename)) } err = c.SaveUploadedFile(file, dest) if ext == ".key" || ext == ".pem" || ext == ".crt" || ext == ".cer" { utils.SaveToConf("https", map[string]string{ "ssl_key_file": dest, }) } if err != nil { err = fmt.Errorf("保存上传文件错误, %v", err) c.AbortWithStatusJSON(500, err.Error()) return } c.IndentedJSON(200, "OK") h.Restart(c) }
2、然后设置开启https配置的接口:
func (h *APIHandler) SetSslConfig(c *gin.Context) { type Form struct { HttpsPort uint Https bool CrtFile string KeyFile string } var form = Form{} if err := c.Bind(&form); err != nil { return } _port := utils.Conf().Section("https").Key("port").MustUint(443) if _port != form.HttpsPort && utils.IsPortInUse(int(form.HttpsPort)) { c.AbortWithStatusJSON(http.StatusBadRequest, "HTTPS 本地端口 已占用") return } _Https := "0" if form.Https { _Https = "1" } utils.SaveToConf("https", map[string]string{ "port": fmt.Sprintf("%d", form.HttpsPort), "ssl_cert_file": fmt.Sprintf("%s", form.CrtFile), "ssl_key_file": fmt.Sprintf("%s", form.KeyFile), "http_ssl": _Https, }) if _port != form.HttpsPort { API.RestartChan <- true } c.IndentedJSON(http.StatusOK, "ok") }
3、最后还需要添加一个获取是否开启https配置的接口:
func (h *APIHandler) GetSslConfig(c *gin.Context) { sec := utils.Conf().Section("https") ack := map[string]interface{}{ "HttpsPort": sec.Key("port").MustString("443"), "CrtFile": sec.Key("ssl_cert_file").MustString(""), "KeyFile": sec.Key("ssl_key_file").MustString(""), "Https": sec.Key("http_ssl").MustBool(false), } c.IndentedJSON(http.StatusOK, ack) }
配置完成后,EasyNVS访问即可通过https进入。
有想了解EasyNVS的用户,可以直接留言联系我们。如果还想了解更多视频相关的解决方案,可进入TSINGSEE青犀视频查阅浏览,青犀视频全线产品都已经支持H265编码视频,可实战测试,欢迎咨询了解。