This commit is contained in:
arraykeys
2019-08-08 17:13:34 +08:00
parent e8e5966a8c
commit f0bf2d5fec
2885 changed files with 1195993 additions and 12 deletions
@@ -0,0 +1,135 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"fmt"
"strings"
"time"
validation "github.com/go-ozzo/ozzo-validation"
)
type AreaController struct {
BaseController
}
func (this *AreaController) Delete() {
areaModel := models.Area{}
areaId := this.GetString("area_id")
err := areaModel.Delete(areaId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *AreaController) Add() {
areaModel := models.Area{}
if this.Ctx.Input.IsPost() {
_, data := this.getAreaFromPost(false)
_, err := areaModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
regionModel := models.Region{}
regions, err := regionModel.GetSubRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["action"] = "add"
this.viewLayout("area/form", "form")
}
}
func (this *AreaController) Edit() {
areaModel := models.Area{}
areaId := this.GetString("area_id")
if this.Ctx.Input.IsPost() {
_, data := this.getAreaFromPost(true)
_, err := areaModel.Update(areaId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
area, err := areaModel.GetAreaByAreaId(areaId)
if err != nil {
this.JsonError(err, "")
}
if len(area) == 0 {
this.ViewError("Area不存在")
}
regionModel := models.Region{}
regions, err := regionModel.GetSubRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["area"] = area
this.Data["action"] = "edit"
this.viewLayout("area/form", "form")
}
}
func (this *AreaController) List() {
column := strings.Trim(this.GetString("type", ""), " ")
keyword := strings.Trim(this.GetString("id", ""), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
areaModel := models.Area{}
var areas = []map[string]string{}
var areaCount = 0
if keyword == "" {
areaCount, err = areaModel.CountAreas()
areas, err = areaModel.GetAreasByLimit(limit, pageSize)
} else {
areaCount, err = areaModel.CountAreasByID(column, keyword)
areas, err = areaModel.GetAreasByIDAndLimit(column, keyword, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["areas"] = areas
this.Data["page"] = utils.NewMisc().Page(areaCount, page, pageSize, fmt.Sprintf("/web/area/list?page={page}&type=%s&id=%s", column, keyword))
this.viewLayoutTitle("用户列表", "area/list", "form")
}
func (this *AreaController) getAreaFromPost(isUpdate bool) (areaId string, area map[string]interface{}) {
area = map[string]interface{}{
"name": this.GetString("name"),
"cs_type": this.GetString("cs_type"),
"is_forbidden": this.GetString("is_forbidden"),
}
errs := validation.Errors{
"区域名称": validation.Validate(area["name"],
validation.Required.Error("不能为空")),
"类型": validation.Validate(area["cs_type"],
validation.Required.Error("不能为空"),
validation.In("server", "client").Error("错误"),
),
"访问控制": validation.Validate(area["is_forbidden"],
validation.Required.Error("不能为空"),
validation.In("0", "1").Error("错误"),
),
}
err := errs.Filter()
if err != nil {
this.JsonError(err)
}
if isUpdate {
area["update_time"] = time.Now().Unix()
} else {
area["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,24 @@
package controllers
import (
system "anytunnel/at-admin/app/controllers"
)
const moduleName = "web"
type BaseController struct {
system.BaseAdminController
}
func (this *BaseController) viewLayoutTitle(title, viewName, layout string) {
this.ViewLayoutTitle(moduleName, title, viewName, layout)
}
func (this *BaseController) viewLayout(viewName, layout string) {
this.ViewLayout(moduleName, viewName, layout)
}
func (this *BaseController) view(viewName string) {
this.View(moduleName, viewName)
}
func (this *BaseController) viewTitle(title, viewName string) {
this.ViewTitle(moduleName, title, viewName)
}
@@ -0,0 +1,148 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"regexp"
"strings"
"time"
validation "github.com/go-ozzo/ozzo-validation"
)
type ClientController struct {
BaseController
}
func (this *ClientController) Reset() {
clientModel := models.Client{}
clientId := this.GetString("client_id")
err := clientModel.Offline(clientId)
if err != nil {
this.JsonError(err)
}
err = clientModel.Reset(clientId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *ClientController) Delete() {
clientModel := models.Client{}
clientId := this.GetString("client_id")
hasTunnel, err := clientModel.HasTunnelRef(clientId)
if err != nil {
this.JsonError(err)
}
if hasTunnel {
this.JsonError("Tunnel引用,不能删除")
}
err = clientModel.Delete(clientId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *ClientController) Add() {
clientModel := models.Client{}
if this.Ctx.Input.IsPost() {
_, data := this.getClientFromPost(false)
_, err := clientModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
this.Data["action"] = "add"
this.viewLayout("client/form", "form")
}
}
func (this *ClientController) Edit() {
clientModel := models.Client{}
clientId := this.GetString("client_id")
if this.Ctx.Input.IsPost() {
_, data := this.getClientFromPost(true)
_, err := clientModel.Update(clientId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
client, err := clientModel.GetClientByClientId(clientId)
if err != nil {
this.JsonError(err, "")
}
if len(client) == 0 {
this.ViewError("Client不存在")
}
this.Data["client"] = client
this.Data["action"] = "edit"
this.viewLayout("client/form", "form")
}
}
func (this *ClientController) List() {
userID := strings.Trim(this.GetString("user_id"), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
clientModel := models.Client{}
var clients = []map[string]string{}
var clientCount = 0
if userID == "" {
clientCount, err = clientModel.CountClients()
clients, err = clientModel.GetClientsByLimit(limit, pageSize)
} else {
clientCount, err = clientModel.CountClientsByUserID(userID)
clients, err = clientModel.GetClientsByUserIDAndLimit(userID, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["userID"] = userID
this.Data["clients"] = clients
this.Data["page"] = utils.NewMisc().Page(clientCount, page, pageSize, "/web/client/list?page={page}&user_id="+userID)
this.viewLayoutTitle("Client列表", "client/list", "form")
}
func (this *ClientController) getClientFromPost(isUpdate bool) (clientId string, client map[string]interface{}) {
port, _ := this.GetInt("local_port")
client = map[string]interface{}{
"name": this.GetString("name"),
"local_host": this.GetString("local_host"),
"local_port": port,
"user_id": 0,
"is_delete": 0,
}
errs := validation.Errors{
"名称": validation.Validate(client["name"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^.{1,15}$")).Error("名称长度必须是1-15字符")),
"本地网络Host": validation.Validate(client["local_host"],
validation.Required.Error("不能为空")),
"本地网络端口": validation.Validate(client["local_port"],
validation.Required.Error("不能为空"),
validation.Min(1).Error("最小值1"),
validation.Max(65535).Error("最大值65535")),
}
err := errs.Filter()
if err != nil {
this.JsonError(err)
}
if isUpdate {
client["update_time"] = time.Now().Unix()
} else {
client["token"] = utils.NewMisc().RandString(32)
client["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,202 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
common "anytunnel/at-common"
"encoding/json"
"fmt"
"regexp"
"time"
"github.com/astaxie/beego"
validation "github.com/go-ozzo/ozzo-validation"
is "github.com/go-ozzo/ozzo-validation/is"
)
type ClusterController struct {
BaseController
}
func (this *ClusterController) Statistic() {
clusterID := this.GetString("cluster_id")
if this.Ctx.Input.IsGet() {
this.Data["clusterID"] = clusterID
this.viewLayout("cluster/statistic", "default")
} else {
clusterModel := models.Cluster{}
cluster, err := clusterModel.GetClusterByClusterId(clusterID)
if err != nil {
this.JsonError(err)
}
clusterIP := cluster["ip"]
url := fmt.Sprintf("https://%s:%s/traffic/count", clusterIP, beego.AppConfig.String("cluster.api.port"))
body, _, err := common.HttpGet(url)
if err != nil {
this.JsonError(err)
}
status := map[string]interface{}{}
err = json.Unmarshal(body, &status)
if err != nil {
this.JsonError(err)
}
_data, ok := status["data"]
if !ok {
this.JsonError("no data")
}
data := _data.(map[string]interface{})
rs, err := models.DB.Query(models.DB.AR().From("cluster").Where(map[string]interface{}{
"cluster_id": clusterID,
}))
if err != nil {
this.JsonError(err)
}
if rs.Len() == 0 {
this.JsonError("no cluster")
}
data["cluster"] = rs.Row()
status["data"] = data
bytes, _ := json.Marshal(status)
this.Ctx.WriteString(string(bytes))
}
}
func (this *ClusterController) Forbidden() {
clusterModel := models.Cluster{}
clusterId := this.GetString("cluster_id")
err := clusterModel.Forbidden(clusterId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *ClusterController) Review() {
clusterModel := models.Cluster{}
clusterId := this.GetString("cluster_id")
err := clusterModel.Review(clusterId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *ClusterController) Delete() {
clusterModel := models.Cluster{}
clusterId := this.GetString("cluster_id")
HasTunnel, err := clusterModel.HasTunnel(clusterId)
if err != nil {
this.JsonError(err)
}
if HasTunnel {
this.JsonError("Tunnel引用非空,不能删除")
}
err = clusterModel.Delete(clusterId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *ClusterController) Add() {
clusterModel := models.Cluster{}
if this.Ctx.Input.IsPost() {
_, data := this.getClusterFromPost(false)
_, err := clusterModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
regionModel := models.Region{}
regions, err := regionModel.GetSubRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["action"] = "add"
this.viewLayout("cluster/form", "form")
}
}
func (this *ClusterController) Edit() {
clusterModel := models.Cluster{}
clusterId := this.GetString("cluster_id")
if this.Ctx.Input.IsPost() {
_, data := this.getClusterFromPost(true)
_, err := clusterModel.Update(clusterId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
cluster, err := clusterModel.GetClusterByClusterId(clusterId)
if err != nil {
this.JsonError(err, "")
}
if len(cluster) == 0 {
this.ViewError("Cluster不存在")
}
regionModel := models.Region{}
regions, err := regionModel.GetSubRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["cluster"] = cluster
this.Data["action"] = "edit"
this.viewLayout("cluster/form", "form")
}
}
func (this *ClusterController) List() {
clusterModel := models.Cluster{}
clusters, err := clusterModel.GetAllClusters()
if err != nil {
this.ViewError(err.Error())
}
regionModel := models.Region{}
regions, err := regionModel.GetSubRegions()
if err != nil {
this.ViewError(err.Error())
}
r := map[string]string{
"region_id": "0",
}
regions = append(regions, r)
this.Data["regions"] = regions
this.Data["clusters"] = clusters
this.view("cluster/list")
}
func (this *ClusterController) getClusterFromPost(isUpdate bool) (clusterId string, cluster map[string]interface{}) {
cluster = map[string]interface{}{
"name": this.GetString("name"),
"ip": this.GetString("ip"),
"region_id": this.GetString("region_id"),
}
errs := validation.Errors{
"名称": validation.Validate(cluster["name"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^.{1,15}$")).Error("长度必须是1-15字符")),
"IP": validation.Validate(cluster["ip"],
validation.Required.Error("不能为空"),
is.IPv4),
"区域": validation.Validate(cluster["region_id"],
validation.Match(regexp.MustCompile("^[1-9][0-9]*$")).Error("格式错误")),
}
err := errs.Filter()
if err != nil {
this.JsonError(err)
}
if isUpdate {
cluster["update_time"] = time.Now().Unix()
} else {
cluster["is_disable"] = 1
cluster["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,50 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"fmt"
"strings"
)
type ConnController struct {
BaseController
}
func (this *ConnController) List() {
orderBy := strings.Trim(this.GetString("orderby"), " ")
if orderBy == "" {
orderBy = "count"
}
col := strings.Trim(this.GetString("col"), " ")
keyword := strings.Trim(this.GetString("keyword"), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
connModel := models.Conn{}
var conns = []map[string]string{}
var connCount = 0
if keyword == "" {
connCount, err = connModel.CountConns()
conns, err = connModel.GetConnsByLimit(limit, pageSize, orderBy)
} else {
connCount, err = connModel.CountConnsByUserID(col, keyword)
conns, err = connModel.GetConnsByUserIDAndLimit(col, keyword, orderBy, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["conns"] = conns
this.Data["col"] = col
this.Data["keyword"] = keyword
this.Data["orderby"] = orderBy
this.Data["p"] = page
this.Data["page"] = utils.NewMisc().Page(connCount, page, pageSize, fmt.Sprintf("/web/conn/list?page={page}&col=%s&keyword=%s&orderby=%s", col, keyword, orderBy))
this.viewLayoutTitle("隧道连接数列表", "conn/list", "form")
}
@@ -0,0 +1,137 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"fmt"
"strings"
"time"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type IpListController struct {
BaseController
}
func (this *IpListController) Delete() {
ip_listModel := models.IpList{}
ip_listId := this.GetString("ip_list_id")
err := ip_listModel.Delete(ip_listId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *IpListController) Add() {
ip_listModel := models.IpList{}
if this.Ctx.Input.IsPost() {
_, data := this.getIpListFromPost(false)
_, err := ip_listModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
regionModel := models.Region{}
regions, err := regionModel.GetSubRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["action"] = "add"
this.viewLayout("ip_list/form", "form")
}
}
func (this *IpListController) Edit() {
ip_listModel := models.IpList{}
ip_listId := this.GetString("ip_list_id")
if this.Ctx.Input.IsPost() {
_, data := this.getIpListFromPost(true)
_, err := ip_listModel.Update(ip_listId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
ip_list, err := ip_listModel.GetIpListByIpListId(ip_listId)
if err != nil {
this.JsonError(err, "")
}
if len(ip_list) == 0 {
this.ViewError("IpList不存在")
}
regionModel := models.Region{}
regions, err := regionModel.GetSubRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["ip_list"] = ip_list
this.Data["action"] = "edit"
this.viewLayout("ip_list/form", "form")
}
}
func (this *IpListController) List() {
column := strings.Trim(this.GetString("type", ""), " ")
keyword := strings.Trim(this.GetString("id", ""), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
ip_listModel := models.IpList{}
var ip_lists = []map[string]string{}
var ip_listCount = 0
if keyword == "" {
ip_listCount, err = ip_listModel.CountIpLists()
ip_lists, err = ip_listModel.GetIpListsByLimit(limit, pageSize)
} else {
ip_listCount, err = ip_listModel.CountIpListsByID(column, keyword)
ip_lists, err = ip_listModel.GetIpListsByIDAndLimit(column, keyword, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["ip_lists"] = ip_lists
this.Data["page"] = utils.NewMisc().Page(ip_listCount, page, pageSize, fmt.Sprintf("/web/ip_list/list?page={page}&type=%s&id=%s", column, keyword))
this.viewLayoutTitle("用户列表", "ip_list/list", "form")
}
func (this *IpListController) getIpListFromPost(isUpdate bool) (ip_listId string, ip_list map[string]interface{}) {
ip_list = map[string]interface{}{
"ip": this.GetString("ip"),
"cs_type": this.GetString("cs_type"),
"is_forbidden": this.GetString("is_forbidden"),
}
errs := validation.Errors{
"IP": validation.Validate(ip_list["ip"],
validation.Required.Error("不能为空"),
is.IPv4.Error("格式错误")),
"类型": validation.Validate(ip_list["cs_type"],
validation.Required.Error("不能为空"),
validation.In("server", "client").Error("错误"),
),
"访问控制": validation.Validate(ip_list["is_forbidden"],
validation.Required.Error("不能为空"),
validation.In("0", "1").Error("错误"),
),
}
err := errs.Filter()
if err != nil {
this.JsonError(err)
}
if isUpdate {
ip_list["update_time"] = time.Now().Unix()
} else {
ip_list["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,44 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"fmt"
"strings"
)
type OnlineController struct {
BaseController
}
func (this *OnlineController) List() {
cs := strings.Trim(this.GetString("cs", ""), " ")
column := strings.Trim(this.GetString("type", ""), " ")
keyword := strings.Trim(this.GetString("keyword", ""), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
onlineModel := models.Online{}
var onlines = []map[string]string{}
var onlineCount = 0
if keyword == "" {
onlineCount, err = onlineModel.CountOnlines(cs)
onlines, err = onlineModel.GetOnlinesByLimit(cs, limit, pageSize)
} else {
onlineCount, err = onlineModel.CountOnlinesByID(cs, column, keyword)
onlines, err = onlineModel.GetOnlinesByIDAndLimit(cs, column, keyword, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["cs"] = cs
this.Data["onlines"] = onlines
this.Data["page"] = utils.NewMisc().Page(onlineCount, page, pageSize, fmt.Sprintf("/web/online/list?page={page}&type=%s&keyword=%s&cs=%s", column, keyword, cs))
this.viewLayoutTitle(cs+"列表", "online/list", "form")
}
@@ -0,0 +1,135 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"fmt"
"regexp"
"strings"
"time"
validation "github.com/go-ozzo/ozzo-validation"
)
type PackageController struct {
BaseController
}
func (this *PackageController) List() {
column := "user_id"
keyword := strings.Trim(this.GetString("keyword", ""), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
packageModel := models.PackageModel{}
var packages = []map[string]string{}
var packageCount = 0
if keyword == "" {
packageCount, err = packageModel.CountPackages()
packages, err = packageModel.GetPackagesByLimit(limit, pageSize)
} else {
packageCount, err = packageModel.CountPackagesByID(column, keyword)
packages, err = packageModel.GetPackagesByIDAndLimit(column, keyword, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["packages"] = packages
this.Data["page"] = utils.NewMisc().Page(packageCount, page, pageSize, fmt.Sprintf("/web/package/list?page={page}&type=%s&keyword=%s", column, keyword))
this.viewLayoutTitle("流量列表", "package/list", "form")
}
func (this *PackageController) Add() {
packageModel := models.PackageModel{}
if this.Ctx.Input.IsPost() {
_, data := this.getPackageFromPost(false)
_, err := packageModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
this.Data["action"] = "add"
this.viewLayout("package/form", "form")
}
}
func (this *PackageController) Edit() {
packageModel := models.PackageModel{}
packageId := this.GetString("package_id")
if this.Ctx.Input.IsPost() {
_, data := this.getPackageFromPost(true)
_, err := packageModel.Update(packageId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
_package, err := packageModel.GetPackageByPackageId(packageId)
if err != nil {
this.JsonError(err, "")
}
if len(_package) == 0 {
this.ViewError("Package不存在")
}
this.Data["package"] = _package
this.Data["action"] = "edit"
this.viewLayout("package/form", "form")
}
}
func (this *PackageController) getPackageFromPost(isUpdate bool) (packageId string, _package map[string]interface{}) {
_package = map[string]interface{}{
"comment": this.GetString("comment"),
"bytes_left": this.GetString("bytes_left"),
"user_id": this.GetString("user_id"),
"start_time": this.GetString("start_time"),
"end_time": this.GetString("end_time"),
}
errs := validation.Errors{
"来源": validation.Validate(_package["comment"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^.{1,10}$")).Error("长度必须是1-10字符")),
"字节数": validation.Validate(_package["bytes_left"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^[0-9]+$")).Error("必须是数字")),
"用户ID": validation.Validate(_package["user_id"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^[1-9][0-9]*$")).Error("必须是数字")),
"生效时间": validation.Validate(_package["start_time"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)).Error("格式错误")),
"过期时间": validation.Validate(_package["end_time"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)).Error("格式错误")),
}
err := errs.Filter()
if err != nil {
this.JsonError(err)
}
userModel := models.User{}
user, err := userModel.GetUserByUserId(_package["user_id"].(string))
if err != nil {
this.JsonError(err)
}
if len(user) == 0 {
this.JsonError("用户不存在")
}
_start, _ := time.ParseInLocation("2006-01-02 15:04:05", _package["start_time"].(string)+" 00:00:00", time.Local)
_end, _ := time.ParseInLocation("2006-01-02 15:04:05", _package["end_time"].(string)+" 23:59:59", time.Local)
_package["start_time"] = _start.Unix()
_package["end_time"] = _end.Unix()
if isUpdate {
_package["update_time"] = time.Now().Unix()
} else {
_package["bytes_total"] = _package["bytes_left"]
_package["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,124 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"regexp"
"time"
validation "github.com/go-ozzo/ozzo-validation"
)
type RegionController struct {
BaseController
}
func (this *RegionController) Delete() {
regionModel := models.Region{}
regionId := this.GetString("region_id")
hasRole, err := regionModel.HasRole(regionId)
if err != nil {
this.JsonError(err)
}
if hasRole {
this.JsonError("角色引用非空,不能删除")
}
HasCluster, err := regionModel.HasCluster(regionId)
if err != nil {
this.JsonError(err)
}
if HasCluster {
this.JsonError("Cluster引用非空,不能删除")
}
HasSubRegion, err := regionModel.HasSubRegion(regionId)
if err != nil {
this.JsonError(err)
}
if HasSubRegion {
this.JsonError("子区域非空,不能删除")
}
err = regionModel.Delete(regionId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *RegionController) Add() {
regionModel := models.Region{}
if this.Ctx.Input.IsPost() {
_, data := this.getRegionFromPost(false)
_, err := regionModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
regions, err := regionModel.GetTopRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["action"] = "add"
this.viewLayout("region/form", "form")
}
}
func (this *RegionController) Edit() {
regionModel := models.Region{}
regionId := this.GetString("region_id")
if this.Ctx.Input.IsPost() {
_, data := this.getRegionFromPost(true)
_, err := regionModel.Update(regionId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
region, err := regionModel.GetRegionByRegionId(regionId)
if err != nil {
this.JsonError(err, "")
}
if len(region) == 0 {
this.ViewError("区域不存在")
}
regions, err := regionModel.GetTopRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.Data["region"] = region
this.Data["action"] = "edit"
this.viewLayout("region/form", "form")
}
}
func (this *RegionController) List() {
regionModel := models.Region{}
regions, err := regionModel.GetAllRegions()
if err != nil {
this.ViewError(err.Error())
}
this.Data["regions"] = regions
this.view("region/list")
}
func (this *RegionController) getRegionFromPost(isUpdate bool) (regionId string, region map[string]interface{}) {
region = map[string]interface{}{
"name": this.GetString("name"),
"parent_id": this.GetString("parent_id"),
"is_delete": 0,
}
err := validation.Validate(region["name"],
validation.Required.Error("名称不能为空"),
validation.Match(regexp.MustCompile("^.{1,15}$")).Error("名称长度必须是1-15字符"))
if err != nil {
this.JsonError(err.Error())
}
if isUpdate {
region["update_time"] = time.Now().Unix()
} else {
region["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,155 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"regexp"
"strings"
"time"
validation "github.com/go-ozzo/ozzo-validation"
)
type RoleController struct {
BaseController
}
func (this *RoleController) Delete() {
roleModel := models.Role{}
roleId := this.GetString("role_id")
// if roleId == "0" {
// this.JsonError("系统角色不能删除")
// }
if roleId == "1" {
this.JsonError("默认角色不能删除")
}
hasUser, err := roleModel.HasUser(roleId)
if err != nil {
this.JsonError(err)
}
if hasUser {
this.JsonError("角色用户非空,不能删除")
}
err = roleModel.SetRegions(roleId, []string{})
if err != nil {
this.JsonError(err)
}
err = roleModel.Delete(roleId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *RoleController) Add() {
roleModel := models.Role{}
if this.Ctx.Input.IsPost() {
_, data := this.getRoleFromPost(false)
_, err := roleModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
this.Data["action"] = "add"
this.viewLayout("role/form", "form")
}
}
func (this *RoleController) Edit() {
roleModel := models.Role{}
roleId := this.GetString("role_id")
if this.Ctx.Input.IsPost() {
_, data := this.getRoleFromPost(true)
_, err := roleModel.Update(roleId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
role, err := roleModel.GetRoleByRoleId(roleId)
if err != nil {
this.JsonError(err, "")
}
if len(role) == 0 {
this.ViewError("角色不存在")
}
this.Data["tunnel_mode_arr"] = strings.Split(role["tunnel_mode"], ",")
this.Data["role"] = role
this.Data["action"] = "edit"
this.viewLayout("role/form", "form")
}
}
func (this *RoleController) List() {
roleModel := models.Role{}
roles, err := roleModel.GetAllRoles()
if err != nil {
this.ViewError(err.Error())
}
this.Data["roles"] = roles
this.view("role/list")
}
func (this *RoleController) Regions() {
roleModel := models.Role{}
roleID := this.GetString("role_id")
if this.Ctx.Input.IsPost() {
regionIds := this.GetStrings("region-ids")
err := roleModel.SetRegions(roleID, regionIds)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
regionModel := models.Region{}
regions, err := regionModel.GetAllRegions()
if err != nil {
this.ViewError(err.Error())
}
regionIds, err := roleModel.GetRoleRegionIds(roleID)
if err != nil {
this.ViewError(err.Error())
}
this.Data["regionIds"] = regionIds
this.Data["roleId"] = this.GetString("role_id")
this.Data["regions"] = regions
this.view("role/region")
}
}
func (this *RoleController) getRoleFromPost(isUpdate bool) (roleId string, role map[string]interface{}) {
role = map[string]interface{}{
"name": this.GetString("name"),
"server_area": this.GetString("server_area"),
"client_area": this.GetString("client_area"),
"bandwidth": this.GetString("bandwidth"),
"is_delete": 0,
}
errs := validation.Errors{
"名称": validation.Validate(role["name"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^.{1,15}$")).Error("长度必须是1-15字符")),
"Server区域": validation.Validate(role["server_area"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^(china|foreign|all)$")).Error("错误")),
"Client区域": validation.Validate(role["client_area"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^(china|foreign|all)$")).Error("错误")),
"带宽": validation.Validate(role["bandwidth"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^(0|([1-9][0-9]*))$")).Error("必须是大于等于0的整数")),
}
err := errs.Filter()
if err != nil {
this.JsonError(err)
}
role["tunnel_mode"] = strings.Join(this.GetStrings("tunnel_mode"), ",")
if isUpdate {
role["update_time"] = time.Now().Unix()
} else {
role["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,137 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"regexp"
"strings"
"time"
validation "github.com/go-ozzo/ozzo-validation"
)
type ServerController struct {
BaseController
}
func (this *ServerController) Reset() {
serverModel := models.Server{}
serverId := this.GetString("server_id")
err := serverModel.Offline(serverId)
if err != nil {
this.JsonError(err)
}
err = serverModel.Reset(serverId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *ServerController) Delete() {
serverModel := models.Server{}
serverId := this.GetString("server_id")
hasTunnel, err := serverModel.HasTunnelRef(serverId)
if err != nil {
this.JsonError(err)
}
if hasTunnel {
this.JsonError("Tunnel引用,不能删除")
}
err = serverModel.Delete(serverId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *ServerController) Add() {
serverModel := models.Server{}
if this.Ctx.Input.IsPost() {
_, data := this.getServerFromPost(false)
_, err := serverModel.Insert(data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
this.Data["action"] = "add"
this.viewLayout("server/form", "form")
}
}
func (this *ServerController) Edit() {
serverModel := models.Server{}
serverId := this.GetString("server_id")
if this.Ctx.Input.IsPost() {
_, data := this.getServerFromPost(true)
_, err := serverModel.Update(serverId, data)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
} else {
server, err := serverModel.GetServerByServerId(serverId)
if err != nil {
this.JsonError(err, "")
}
if len(server) == 0 {
this.ViewError("Server不存在")
}
this.Data["server"] = server
this.Data["action"] = "edit"
this.viewLayout("server/form", "form")
}
}
func (this *ServerController) List() {
userID := strings.Trim(this.GetString("user_id"), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
serverModel := models.Server{}
var servers = []map[string]string{}
var serverCount = 0
if userID == "" {
serverCount, err = serverModel.CountServers()
servers, err = serverModel.GetServersByLimit(limit, pageSize)
} else {
serverCount, err = serverModel.CountServersByUserID(userID)
servers, err = serverModel.GetServersByUserIDAndLimit(userID, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["userID"] = userID
this.Data["servers"] = servers
this.Data["page"] = utils.NewMisc().Page(serverCount, page, pageSize, "/web/server/list?page={page}&user_id="+userID)
this.viewLayoutTitle("Server列表", "server/list", "form")
}
func (this *ServerController) getServerFromPost(isUpdate bool) (serverId string, server map[string]interface{}) {
server = map[string]interface{}{
"name": this.GetString("name"),
// "user_id": this.GetString("user_id"),
"user_id": 0,
"is_delete": 0,
}
err := validation.Validate(server["name"],
validation.Required.Error("名称不能为空"),
validation.Match(regexp.MustCompile("^.{1,15}$")).Error("名称长度必须是1-15字符"))
if err != nil {
this.JsonError(err.Error())
}
if isUpdate {
server["update_time"] = time.Now().Unix()
} else {
server["token"] = utils.NewMisc().RandString(32)
server["create_time"] = time.Now().Unix()
}
return
}
@@ -0,0 +1,42 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"fmt"
"strings"
)
type TunnelController struct {
BaseController
}
func (this *TunnelController) List() {
column := strings.Trim(this.GetString("type", ""), " ")
keyword := strings.Trim(this.GetString("id", ""), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
tunnelModel := models.Tunnel{}
var tunnels = []map[string]string{}
var tunnelCount = 0
if keyword == "" {
tunnelCount, err = tunnelModel.CountTunnels()
tunnels, err = tunnelModel.GetTunnelsByLimit(limit, pageSize)
} else {
tunnelCount, err = tunnelModel.CountTunnelsByID(column, keyword)
tunnels, err = tunnelModel.GetTunnelsByIDAndLimit(column, keyword, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
this.Data["tunnels"] = tunnels
this.Data["page"] = utils.NewMisc().Page(tunnelCount, page, pageSize, fmt.Sprintf("/web/tunnel/list?page={page}&type=%s&id=%s", column, keyword))
this.viewLayoutTitle("用户列表", "tunnel/list", "form")
}
@@ -0,0 +1,275 @@
package controllers
import (
"anytunnel/at-admin/app/modules/web/models"
"anytunnel/at-admin/app/utils"
"regexp"
"strings"
"time"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type UserController struct {
BaseController
}
func (this *UserController) Forbidden() {
userModel := models.User{}
userId := this.GetString("user_id")
// if userId == "0" {
// this.JsonError("系统用户不能禁用")
// }
err := userModel.Forbidden(userId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
func (this *UserController) Review() {
userModel := models.User{}
userId := this.GetString("user_id")
// if userId == "0" {
// this.JsonError("系统用户不能操作")
// }
err := userModel.Review(userId)
if err != nil {
this.JsonError(err)
}
this.JsonSuccess("")
}
// func (this *UserController) ChangePassword() {
// userModel := models.User{}
// if this.Ctx.Input.IsPost() {
// newpassword := this.GetString("password")
// oldpassword := this.GetString("password_old")
// errs := validation.Errors{
// "旧密码": validation.Validate(oldpassword,
// validation.Required.Error("不能为空")),
// "新密码": validation.Validate(newpassword,
// validation.Required.Error("不能为空"),
// validation.Match(regexp.MustCompile("^([0-9]+[a-zA-Z]+[_]*){1,16}$")).Error("必须同时包含数字和字母,且1-15字符")),
// }
// err := errs.Filter()
// if err != nil {
// this.JsonError(err)
// }
// err = userModel.ChangePassword(this.loginUser["user_id"], newpassword, oldpassword)
// if err != nil {
// this.JsonError("修改密码失败:" + err.Error())
// }
// this.JsonSuccess("")
// } else {
// this.viewLayout("user/changepassword", "form")
// }
// }
func (this *UserController) Add() {
return
userModel := models.User{}
roleModel := models.Role{}
userRoleModel := models.UserRole{}
if this.Ctx.Input.IsPost() {
_, data := this.getUserFromPost(false)
username := this.GetString("username")
roleIds := this.GetStrings("role_ids", []string{})
if len(roleIds) == 0 {
this.JsonError("没有选择角色")
}
HasUsername, err := userModel.HasUsername(username)
if err != nil {
this.JsonError(err)
}
if HasUsername {
this.JsonError("用户名已经存在")
}
userId, err := userModel.Insert(data)
if err != nil {
this.JsonError("添加用户失败:" + err.Error())
}
//添加用户与角色对应关系
_, err = userRoleModel.Insert(utils.NewConvert().IntToString(userId, 10), roleIds)
if err != nil {
this.JsonError("添加用户角色失败:" + err.Error())
}
this.JsonSuccess("")
} else {
roles := []map[string]string{}
allRoles, _ := roleModel.GetAllRoles()
for _, allRole := range allRoles {
role := allRole
role["is_default"] = "0"
roles = append(roles, role)
}
this.Data["action"] = "add"
this.Data["roles"] = roles
this.viewLayout("user/form", "form")
}
}
func (this *UserController) Edit() {
userModel := models.User{}
roleModel := models.Role{}
userRoleModel := models.UserRole{}
userId := this.GetString("user_id")
if this.Ctx.Input.IsPost() {
// _, data := this.getUserFromPost(true)
// username := this.GetString("username")
roleIds := this.GetStrings("role_ids", []string{})
if len(roleIds) == 0 {
this.JsonError("没有选择角色")
}
// HasSameUsername, err := userModel.HasSameUsername(userId, username)
// if err != nil {
// this.JsonError(err)
// }
// if HasSameUsername {
// this.JsonError("用户名已经存在")
// }
// _, err = userModel.Update(userId, data)
// if err != nil {
// this.JsonError("修改用户失败:" + err.Error())
// }
//添加用户与角色对应关系
_, err := userRoleModel.Insert(userId, roleIds)
if err != nil {
this.JsonError("修改用户角色失败:" + err.Error())
}
this.JsonSuccess("")
} else {
roles := []map[string]string{}
user, err := userModel.GetUserByUserId(userId)
allRoles, _ := roleModel.GetAllRoles()
userRoles, _ := userRoleModel.GetUserRolesByUserId(userId)
for _, allRole := range allRoles {
role := allRole
if len(userRoles) == 0 {
role["is_default"] = "0"
} else {
for _, userRoles := range userRoles {
if allRole["role_id"] == userRoles["role_id"] {
role["is_default"] = "1"
break
}
role["is_default"] = "0"
}
}
roles = append(roles, role)
}
if err != nil {
this.JsonError(err, "")
}
if len(user) == 0 {
this.JsonError("用户不存在")
}
this.Data["user"] = user
this.Data["roles"] = roles
this.Data["action"] = "edit"
this.viewLayout("user/form", "form")
}
}
func (this *UserController) getUserFromPost(isUpdate bool) (userId string, user map[string]interface{}) {
userModel := models.User{}
user = map[string]interface{}{
"username": this.GetString("username"),
"nickname": this.GetString("nickname"),
"email": this.GetString("email"),
}
errs := validation.Errors{
"邮箱": validation.Validate(user["email"],
validation.Required.Error("不能为空"),
is.Email.Error("格式错误")),
"昵称": validation.Validate(user["nickname"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^.{1,15}$")).Error("长度必须是1-15字符")),
//"角色": validation.Validate(user["role_ids"]),
// validation.Required.Error("没有选择角色"),
}
if !isUpdate {
errs["用户名"] = validation.Validate(user["username"],
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^[0-9_a-zA-Z]{1,15}$")).Error("只能包含数字字母和下划线,且1-15字符"))
errs["密码"] = validation.Validate(this.GetString("password"),
validation.Required.Error("不能为空"),
validation.Match(regexp.MustCompile("^([0-9]+[a-zA-Z]+[_]*){1,16}$")).Error("必须同时包含数字和字母,且1-15字符"))
}
err := errs.Filter()
if err != nil {
this.JsonError(err)
}
if isUpdate {
if this.GetString("password") != "" {
user["password"] = userModel.EncodePassword(this.GetString("password"))
}
user["update_time"] = time.Now().Unix()
delete(user, "username")
} else {
user["is_forbidden"] = 0
user["password"] = userModel.EncodePassword(this.GetString("password"))
user["create_time"] = time.Now().Unix()
}
return
}
func (this *UserController) List() {
keyword := strings.Trim(this.GetString("keyword", ""), " ")
page, err := this.GetInt("page", 1)
if err != nil {
page = 1
}
//每页的条数
pageSize := 10
limit := (page - 1) * pageSize
userModel := models.User{}
var users = []map[string]string{}
var userCount = 0
if keyword == "" {
userCount, err = userModel.CountUsers()
users, err = userModel.GetUsersByLimit(limit, pageSize)
} else {
userCount, err = userModel.CountUsersByKeyword(keyword)
users, err = userModel.GetUsersByKeywordAndLimit(keyword, limit, pageSize)
}
if err != nil {
this.ViewError(err.Error())
}
roleModel := models.Role{}
userRoles := map[string]string{}
//用户角色
for _, user := range users {
userId := user["user_id"]
var names = ""
roles, err := roleModel.GetRolesByUserId(userId)
if err != nil {
this.ViewError(err.Error())
}
for _, role := range roles {
names += "," + role["name"]
}
userRoles[user["user_id"]] = strings.Replace(names, ",", "", 1)
}
this.Data["users"] = users
this.Data["userRoles"] = userRoles
this.Data["keyword"] = keyword
this.Data["page"] = utils.NewMisc().Page(userCount, page, pageSize, "/user/list?page={page}")
this.viewLayoutTitle("用户列表", "user/list", "form")
}