Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
添加hostname检查支持
Browse files Browse the repository at this point in the history
  • Loading branch information
neargle committed Mar 31, 2018
1 parent 98bb10a commit eb06275
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions web/conf/app-config-sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ perloadcount = 500
# Debug : 7
loglevel=6

# 设置hostname, 如果没设置则不会验证
# 如果设置了,只有该host可以访问web页面,多个host以逗号隔开
ylhostname = ""

# 后台登录用户名
username = "yulong"
# passwordhex为登录密码的32位md5,默认密码为(带句号): All_life_is_a_game_of_luck.
Expand Down
15 changes: 13 additions & 2 deletions web/controllers/agentapi.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package controllers

import "net/url"

import (
"net/url"
"strings"
"yulong-hids/web/models"
"yulong-hids/web/utils"

"github.com/astaxie/beego"
"gopkg.in/mgo.v2/bson"
Expand All @@ -18,6 +18,17 @@ type AgentApiController struct {
// Get agent will get publickey content and serverlist here
func (c *AgentApiController) Get() {

// check hostname
hostname := beego.AppConfig.String("ylhostname")
allowHosts := strings.Split(hostname, ",")
if hostname != "" && !utils.StringInSlice(c.Ctx.Input.Host(), allowHosts) {
beego.Error("Hostname not correct.")
c.Ctx.Output.SetStatus(403)
c.Data["json"] = "Forbidden"
c.ServeJSON()
return
}

currentURL := c.Ctx.Request.RequestURI

if strings.Contains(currentURL, "publickey") {
Expand Down
12 changes: 12 additions & 0 deletions web/controllers/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"strings"
"yulong-hids/web/settings"
"yulong-hids/web/utils"

Expand All @@ -17,6 +18,17 @@ type BaseController struct {
// Prepare access Control, 2FA, csrf check and other security options
func (c *BaseController) Prepare() {

// check hostname
hostname := beego.AppConfig.String("ylhostname")
allowHosts := strings.Split(hostname, ",")
if hostname != "" && !utils.StringInSlice(c.Ctx.Input.Host(), allowHosts) {
beego.Error("Hostname not correct.")
c.Ctx.Output.SetStatus(403)
c.Data["json"] = "Forbidden"
c.ServeJSON()
return
}

// only https be allowed
HTTPSOnly, _ := beego.AppConfig.Bool("OnlyHTTPS")
if HTTPSOnly && c.Ctx.Input.Scheme() != "https" &&
Expand Down

0 comments on commit eb06275

Please sign in to comment.