修改ip pool策略

This commit is contained in:
bjdgyc 2023-04-19 18:12:50 +08:00
parent bc9248e16b
commit 638c601c02
15 changed files with 452 additions and 9919 deletions

View File

@ -60,7 +60,7 @@ AnyLink 服务端仅在 CentOS 7、CentOS 8、Ubuntu 18.04、Ubuntu 20.04 测试
### 自行编译安装 ### 自行编译安装
> 需要提前安装好 golang >= 1.19 和 nodejs >= 14.x 和 pnpm >= v7.x > 需要提前安装好 golang >= 1.19 和 nodejs >= 14.x 和 yarn >= v1.22.x
```shell ```shell
git clone https://github.com/bjdgyc/anylink.git git clone https://github.com/bjdgyc/anylink.git

View File

@ -19,11 +19,10 @@ cd $cpath/web
#npm install --registry=https://registry.npm.taobao.org #npm install --registry=https://registry.npm.taobao.org
#npm install #npm install
#npm run build #npm run build
#yarn install
#yarn run build
pnpm i --registry=https://registry.npmmirror.com yarn install --registry=https://registry.npmmirror.com
pnpm build yarn run build
RETVAL $? RETVAL $?

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"github.com/bjdgyc/anylink/dbdata" "github.com/bjdgyc/anylink/dbdata"
"github.com/bjdgyc/anylink/sessdata"
) )
func UserIpMapList(w http.ResponseWriter, r *http.Request) { func UserIpMapList(w http.ResponseWriter, r *http.Request) {
@ -80,6 +81,8 @@ func UserIpMapSet(w http.ResponseWriter, r *http.Request) {
return return
} }
sessdata.IpAllSet(v)
RespSucess(w, nil) RespSucess(w, nil)
} }
@ -93,11 +96,20 @@ func UserIpMapDel(w http.ResponseWriter, r *http.Request) {
return return
} }
data := dbdata.IpMap{Id: id} var data dbdata.IpMap
err := dbdata.Del(&data) err := dbdata.One("Id", id, &data)
if err != nil { if err != nil {
RespError(w, RespInternalErr, err) RespError(w, RespInternalErr, err)
return return
} }
err = dbdata.Del(&data)
if err != nil {
RespError(w, RespInternalErr, err)
return
}
sessdata.IpAllDel(&data)
RespSucess(w, nil) RespSucess(w, nil)
} }

View File

@ -77,6 +77,8 @@ type ServerConfig struct {
IptablesNat bool `json:"iptables_nat"` IptablesNat bool `json:"iptables_nat"`
Compression bool `json:"compression"` // bool Compression bool `json:"compression"` // bool
NoCompressLimit int `json:"no_compress_limit"` // int NoCompressLimit int `json:"no_compress_limit"` // int
DisplayError bool `json:"display_error"`
} }
func initServerCfg() { func initServerCfg() {

View File

@ -48,7 +48,7 @@ var configs = []config{
{Typ: cfgStr, Name: "default_group", Usage: "默认用户组", ValStr: "one"}, {Typ: cfgStr, Name: "default_group", Usage: "默认用户组", ValStr: "one"},
{Typ: cfgStr, Name: "default_domain", Usage: "要发布的默认域", ValStr: ""}, {Typ: cfgStr, Name: "default_domain", Usage: "要发布的默认域", ValStr: ""},
{Typ: cfgInt, Name: "ip_lease", Usage: "IP租期(秒)", ValInt: 1209600}, {Typ: cfgInt, Name: "ip_lease", Usage: "IP租期(秒)", ValInt: 86400},
{Typ: cfgInt, Name: "max_client", Usage: "最大用户连接", ValInt: 200}, {Typ: cfgInt, Name: "max_client", Usage: "最大用户连接", ValInt: 200},
{Typ: cfgInt, Name: "max_user_client", Usage: "最大单用户连接", ValInt: 3}, {Typ: cfgInt, Name: "max_user_client", Usage: "最大单用户连接", ValInt: 3},
{Typ: cfgInt, Name: "cstp_keepalive", Usage: "keepalive时间(秒)", ValInt: 4}, {Typ: cfgInt, Name: "cstp_keepalive", Usage: "keepalive时间(秒)", ValInt: 4},
@ -56,7 +56,7 @@ var configs = []config{
{Typ: cfgInt, Name: "mobile_keepalive", Usage: "移动端keepalive接检测时间(秒)", ValInt: 7}, {Typ: cfgInt, Name: "mobile_keepalive", Usage: "移动端keepalive接检测时间(秒)", ValInt: 7},
{Typ: cfgInt, Name: "mobile_dpd", Usage: "移动端死链接检测时间(秒)", ValInt: 15}, {Typ: cfgInt, Name: "mobile_dpd", Usage: "移动端死链接检测时间(秒)", ValInt: 15},
{Typ: cfgInt, Name: "mtu", Usage: "最大传输单元MTU", ValInt: 1460}, {Typ: cfgInt, Name: "mtu", Usage: "最大传输单元MTU", ValInt: 1460},
{Typ: cfgInt, Name: "session_timeout", Usage: "session过期时间(秒)", ValInt: 3600}, {Typ: cfgInt, Name: "session_timeout", Usage: "session过期时间(秒)-用于断线重连0永不过期", ValInt: 3600},
// {Typ: cfgInt, Name: "auth_timeout", Usage: "auth_timeout", ValInt: 0}, // {Typ: cfgInt, Name: "auth_timeout", Usage: "auth_timeout", ValInt: 0},
{Typ: cfgInt, Name: "audit_interval", Usage: "审计去重间隔(秒),-1关闭", ValInt: -1}, {Typ: cfgInt, Name: "audit_interval", Usage: "审计去重间隔(秒),-1关闭", ValInt: -1},
@ -64,6 +64,8 @@ var configs = []config{
{Typ: cfgBool, Name: "iptables_nat", Usage: "是否自动添加NAT", ValBool: true}, {Typ: cfgBool, Name: "iptables_nat", Usage: "是否自动添加NAT", ValBool: true},
{Typ: cfgBool, Name: "compression", Usage: "启用压缩", ValBool: false}, {Typ: cfgBool, Name: "compression", Usage: "启用压缩", ValBool: false},
{Typ: cfgInt, Name: "no_compress_limit", Usage: "低于及等于多少字节不压缩", ValInt: 256}, {Typ: cfgInt, Name: "no_compress_limit", Usage: "低于及等于多少字节不压缩", ValInt: 256},
{Typ: cfgBool, Name: "display_error", Usage: "客户端显示详细错误信息(线上环境慎开启)", ValBool: false},
} }
var envs = map[string]string{} var envs = map[string]string{}

View File

@ -50,7 +50,7 @@ max_client = 100
#单个用户同时在线数量 #单个用户同时在线数量
max_user_client = 3 max_user_client = 3
#IP租期(秒) #IP租期(秒)
ip_lease = 1209600 ip_lease = 86400
#默认选择的组 #默认选择的组
default_group = "one" default_group = "one"
@ -81,4 +81,9 @@ iptables_nat = true
#启用压缩 #启用压缩
compression = false compression = false
#低于及等于多少字节不压缩 #低于及等于多少字节不压缩
no_compress_limit = 256 no_compress_limit = 256
#客户端显示详细错误信息(线上环境慎开启)
display_error = false

View File

@ -35,3 +35,9 @@ ipv4_end = "192.168.90.200"
#是否自动添加nat #是否自动添加nat
iptables_nat = true iptables_nat = true
#客户端显示详细错误信息(线上环境慎开启)
display_error = false

View File

@ -5,17 +5,18 @@ import (
"time" "time"
) )
// type IpMap struct { type IpMap struct {
// Id int `json:"id" xorm:"pk autoincr not null"` Id int `json:"id" xorm:"pk autoincr not null"`
// IpAddr string `json:"ip_addr" xorm:"not null unique"` IpAddr string `json:"ip_addr" xorm:"varchar(32) not null unique"`
// MacAddr string `json:"mac_addr" xorm:"not null unique"` MacAddr string `json:"mac_addr" xorm:"varchar(32) not null unique"`
// Username string `json:"username"` UniqueMac bool `json:"unique_mac" xorm:"Bool index"`
// Keep bool `json:"keep"` // 保留 ip-mac 绑定 Username string `json:"username" xorm:"varchar(60)"`
// KeepTime time.Time `json:"keep_time"` Keep bool `json:"keep" xorm:"Bool"` // 保留 ip-mac 绑定
// Note string `json:"note"` // 备注 KeepTime time.Time `json:"keep_time" xorm:"DateTime"`
// LastLogin time.Time `json:"last_login"` Note string `json:"note" xorm:"varchar(255)"` // 备注
// UpdatedAt time.Time `json:"updated_at"` LastLogin time.Time `json:"last_login" xorm:"DateTime"`
// } UpdatedAt time.Time `json:"updated_at" xorm:"DateTime updated"`
}
func SetIpMap(v *IpMap) error { func SetIpMap(v *IpMap) error {
var err error var err error

View File

@ -30,7 +30,7 @@ type User struct {
Email string `json:"email" xorm:"varchar(255)"` Email string `json:"email" xorm:"varchar(255)"`
// Password string `json:"password"` // Password string `json:"password"`
PinCode string `json:"pin_code" xorm:"varchar(32)"` PinCode string `json:"pin_code" xorm:"varchar(32)"`
LimitTime *time.Time `json:"limittime,omitempty" xorm:"Datetime limittime"` //值为null时前端不显示 LimitTime *time.Time `json:"limittime,omitempty" xorm:"Datetime limittime"` // 值为null时前端不显示
OtpSecret string `json:"otp_secret" xorm:"varchar(255)"` OtpSecret string `json:"otp_secret" xorm:"varchar(255)"`
DisableOtp bool `json:"disable_otp" xorm:"Bool"` // 禁用otp DisableOtp bool `json:"disable_otp" xorm:"Bool"` // 禁用otp
Groups []string `json:"groups" xorm:"Text"` Groups []string `json:"groups" xorm:"Text"`
@ -56,19 +56,6 @@ type UserActLog struct {
CreatedAt time.Time `json:"created_at" xorm:"DateTime created"` CreatedAt time.Time `json:"created_at" xorm:"DateTime created"`
} }
type IpMap struct {
Id int `json:"id" xorm:"pk autoincr not null"`
IpAddr string `json:"ip_addr" xorm:"varchar(32) not null unique"`
MacAddr string `json:"mac_addr" xorm:"varchar(32) not null unique"`
UniqueMac bool `json:"unique_mac" xorm:"Bool index"`
Username string `json:"username" xorm:"varchar(60)"`
Keep bool `json:"keep" xorm:"Bool"` // 保留 ip-mac 绑定
KeepTime time.Time `json:"keep_time" xorm:"DateTime"`
Note string `json:"note" xorm:"varchar(255)"` // 备注
LastLogin time.Time `json:"last_login" xorm:"DateTime"`
UpdatedAt time.Time `json:"updated_at" xorm:"DateTime updated"`
}
type Setting struct { type Setting struct {
Id int `json:"id" xorm:"pk autoincr not null"` Id int `json:"id" xorm:"pk autoincr not null"`
Name string `json:"name" xorm:"varchar(60) not null unique"` Name string `json:"name" xorm:"varchar(60) not null unique"`

View File

@ -19,8 +19,8 @@ var profileHash = ""
func LinkAuth(w http.ResponseWriter, r *http.Request) { func LinkAuth(w http.ResponseWriter, r *http.Request) {
// TODO 调试信息输出 // TODO 调试信息输出
//hd, _ := httputil.DumpRequest(r, true) // hd, _ := httputil.DumpRequest(r, true)
//base.Debug("DumpRequest: ", string(hd)) // base.Debug("DumpRequest: ", string(hd))
// 判断anyconnect客户端 // 判断anyconnect客户端
userAgent := strings.ToLower(r.UserAgent()) userAgent := strings.ToLower(r.UserAgent())
@ -88,6 +88,9 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
data := RequestData{Group: cr.GroupSelect, Groups: dbdata.GetGroupNamesNormal(), Error: "用户名或密码错误"} data := RequestData{Group: cr.GroupSelect, Groups: dbdata.GetGroupNamesNormal(), Error: "用户名或密码错误"}
if base.Cfg.DisplayError {
data.Error = err.Error()
}
tplRequest(tpl_request, w, data) tplRequest(tpl_request, w, data)
return return
} }

View File

@ -13,8 +13,10 @@ import (
var ( var (
IpPool = &ipPoolConfig{} IpPool = &ipPoolConfig{}
ipActive = map[string]bool{} ipActive = map[string]bool{}
ipAll = map[string]*dbdata.IpMap{}
ipKeep = map[string]bool{}
// ipKeep and ipLease ipAddr => type // ipKeep and ipLease ipAddr => type
ipLease = map[string]bool{} // ipLease = map[string]bool{}
ipPoolMux sync.Mutex ipPoolMux sync.Mutex
) )
@ -49,33 +51,69 @@ func initIpPool() {
IpPool.IpLongMax = utils.Ip2long(net.ParseIP(base.Cfg.Ipv4End)) IpPool.IpLongMax = utils.Ip2long(net.ParseIP(base.Cfg.Ipv4End))
// 获取IpLease数据 // 获取IpLease数据
//go cronIpLease() // go cronIpLease()
getIpMapAll()
} }
func cronIpLease() { // func cronIpLease() {
getIpLease() // getIpLease()
tick := time.NewTicker(time.Minute * 30) // tick := time.NewTicker(time.Minute * 30)
for range tick.C { // for range tick.C {
getIpLease() // getIpLease()
} // }
} // }
//
// func getIpLease() {
// xdb := dbdata.GetXdb()
// keepIpMaps := []dbdata.IpMap{}
// sNow := time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
// err := xdb.Cols("ip_addr").Where("keep=?", true).
// Or("unique_mac=? and last_login>?", true, sNow).Find(&keepIpMaps)
// if err != nil {
// base.Error(err)
// }
// // fmt.Println(keepIpMaps)
// ipPoolMux.Lock()
// ipLease = map[string]bool{}
// for _, v := range keepIpMaps {
// ipLease[v.IpAddr] = true
// }
// ipPoolMux.Unlock()
// }
func getIpLease() { func IpAllSet(v *dbdata.IpMap) {
xdb := dbdata.GetXdb()
keepIpMaps := []dbdata.IpMap{}
sNow := time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
err := xdb.Cols("ip_addr").Where("keep=?", true).
Or("unique_mac=? and last_login>?", true, sNow).Find(&keepIpMaps)
if err != nil {
base.Error(err)
}
// fmt.Println(keepIpMaps)
ipPoolMux.Lock() ipPoolMux.Lock()
ipLease = map[string]bool{} defer ipPoolMux.Unlock()
for _, v := range keepIpMaps {
ipLease[v.IpAddr] = true ipAll[v.IpAddr] = v
if v.Keep {
ipKeep[v.IpAddr] = true
} else {
delete(ipKeep, v.IpAddr)
}
}
func IpAllDel(v *dbdata.IpMap) {
ipPoolMux.Lock()
defer ipPoolMux.Unlock()
delete(ipAll, v.IpAddr)
delete(ipKeep, v.IpAddr)
}
func getIpMapAll() {
xdb := dbdata.GetXdb()
ipMaps := []dbdata.IpMap{}
err := xdb.Find(&ipMaps)
if err != nil {
base.Fatal(err)
}
for _, v := range ipMaps {
ipAll[v.IpAddr] = &v
if v.Keep {
ipKeep[v.IpAddr] = true
}
} }
ipPoolMux.Unlock()
} }
// AcquireIp 获取动态ip // AcquireIp 获取动态ip
@ -84,21 +122,23 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
defer ipPoolMux.Unlock() defer ipPoolMux.Unlock()
var ( var (
err error err error
tNow = time.Now() tNow = time.Now()
leaseTime = time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
) )
// 唯一mac
if uniqueMac { if uniqueMac {
// 判断是否已经分配过 // 判断是否已经分配过
mi := &dbdata.IpMap{} mi := &dbdata.IpMap{}
err = dbdata.One("mac_addr", macAddr, mi) err = dbdata.One("mac_addr", macAddr, mi)
// 查询报错
if err != nil { if err != nil {
if !dbdata.CheckErrNotFound(err) { // 没有查询到数据
base.Error(err) if dbdata.CheckErrNotFound(err) {
return nil return loopIp(username, macAddr, uniqueMac)
} }
// 查询报错
base.Error(err)
return nil
} }
// 存在ip记录 // 存在ip记录
@ -107,59 +147,73 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
// 跳过活跃连接 // 跳过活跃连接
_, ok := ipActive[ipStr] _, ok := ipActive[ipStr]
// 检测原有ip是否在新的ip池内 // 检测原有ip是否在新的ip池内
if IpPool.Ipv4IPNet.Contains(ip) && !ok && // IpPool.Ipv4IPNet.Contains(ip) &&
utils.Ip2long(ip) >= IpPool.IpLongMin && if utils.Ip2long(ip) >= IpPool.IpLongMin &&
utils.Ip2long(ip) <= IpPool.IpLongMax { utils.Ip2long(ip) <= IpPool.IpLongMax && !ok {
mi.Username = username mi.Username = username
mi.LastLogin = tNow mi.LastLogin = tNow
mi.UniqueMac = uniqueMac mi.UniqueMac = uniqueMac
// 回写db数据 // 回写db数据
_ = dbdata.Set(mi) _ = dbdata.Set(mi)
ipActive[ipStr] = true ipActive[ipStr] = true
ipAll[ipStr] = mi
return ip return ip
} }
// 原有ip不符合
_ = dbdata.Del(mi) _ = dbdata.Del(mi)
delete(ipAll, ipStr)
return loopIp(username, macAddr, uniqueMac)
}
} else { // 没有获取到mac
ipMaps := []dbdata.IpMap{} ipMaps := []dbdata.IpMap{}
err = dbdata.FindWhere(&ipMaps, 50, 1, "username=? and unique_mac=?", username, false) err = dbdata.FindWhere(&ipMaps, 50, 1, "username=? and unique_mac=?", username, false)
// 查询报错 if err != nil {
if err != nil { // 没有查询到数据
if !dbdata.CheckErrNotFound(err) { if dbdata.CheckErrNotFound(err) {
base.Error(err) return loopIp(username, macAddr, uniqueMac)
return nil
}
} }
// 查询报错
//遍历mac记录 base.Error(err)
for _, mi := range ipMaps { return nil
ipStr := mi.IpAddr }
ip := net.ParseIP(ipStr) // 遍历 用户的 mac记录
for _, im := range ipMaps {
// 跳过活跃连接 mi := &im
if _, ok := ipActive[ipStr]; ok { ipStr := mi.IpAddr
continue ip := net.ParseIP(ipStr)
} // 跳过活跃连接
//跳过保留ip if _, ok := ipActive[ipStr]; ok {
if mi.Keep { continue
continue }
} // 跳过保留ip
if mi.Keep {
if IpPool.Ipv4IPNet.Contains(ip) && continue
mi.LastLogin.Before(leaseTime) && // 说明已经超过租期,可以直接使用 }
utils.Ip2long(ip) >= IpPool.IpLongMin && // 没有租期限制
utils.Ip2long(ip) <= IpPool.IpLongMax { if utils.Ip2long(ip) >= IpPool.IpLongMin &&
mi.LastLogin = tNow utils.Ip2long(ip) <= IpPool.IpLongMax {
mi.MacAddr = macAddr mi.LastLogin = tNow
mi.UniqueMac = uniqueMac mi.MacAddr = macAddr
// 回写db数据 mi.UniqueMac = uniqueMac
_ = dbdata.Set(mi) // 回写db数据
ipActive[ipStr] = true _ = dbdata.Set(mi)
return ip ipActive[ipStr] = true
} ipAll[ipStr] = mi
return ip
} }
} }
return loopIp(username, macAddr, uniqueMac)
}
// 遍历ip
func loopIp(username, macAddr string, uniqueMac bool) net.IP {
var (
tNow = time.Now()
leaseTime = time.Now().Add(-1 * time.Duration(base.Cfg.IpLease) * time.Second)
)
// 全局遍历超过租期和未保留的ip // 全局遍历超过租期和未保留的ip
for i := IpPool.IpLongMin; i <= IpPool.IpLongMax; i++ { for i := IpPool.IpLongMin; i <= IpPool.IpLongMax; i++ {
ip := utils.Long2ip(i) ip := utils.Long2ip(i)
@ -169,41 +223,37 @@ func AcquireIp(username, macAddr string, uniqueMac bool) net.IP {
if _, ok := ipActive[ipStr]; ok { if _, ok := ipActive[ipStr]; ok {
continue continue
} }
// 跳过保留ip
mi := &dbdata.IpMap{} if _, ok := ipKeep[ipStr]; ok {
err = dbdata.One("ip_addr", ipStr, mi) continue
if err == nil {
//跳过保留ip
if mi.Keep {
continue
}
if mi.LastLogin.Before(leaseTime) {
// 存在记录,说明已经超过租期,可以直接使用
mi.LastLogin = tNow
mi.MacAddr = macAddr
mi.UniqueMac = uniqueMac
// 回写db数据
_ = dbdata.Set(mi)
ipActive[ipStr] = true
return ip
}
} }
if dbdata.CheckErrNotFound(err) { mi, ok := ipAll[ipStr]
// 该ip没有被使用 if !ok {
mi := &dbdata.IpMap{IpAddr: ipStr, MacAddr: macAddr, UniqueMac: uniqueMac, Username: username, LastLogin: tNow} // 该ip没有被使用 直接返回
mi = &dbdata.IpMap{IpAddr: ipStr, MacAddr: macAddr, UniqueMac: uniqueMac, Username: username, LastLogin: tNow}
_ = dbdata.Add(mi) _ = dbdata.Add(mi)
ipActive[ipStr] = true ipActive[ipStr] = true
ipAll[ipStr] = mi
return ip return ip
} }
// 查询报错 // 存在记录
if err != nil { if mi.LastLogin.Before(leaseTime) {
base.Error(err) // 存在记录,说明已经超过租期,可以直接使用
return nil mi.LastLogin = tNow
mi.MacAddr = macAddr
mi.UniqueMac = uniqueMac
// 回写db数据
_ = dbdata.Set(mi)
ipActive[ipStr] = true
ipAll[ipStr] = mi
return ip
} }
} }
// 最后从未超过租期的记录获取一个
base.Warn("no ip available, please see ip_map table row") base.Warn("no ip available, please see ip_map table row")
return nil return nil
} }
@ -220,5 +270,6 @@ func ReleaseIp(ip net.IP, macAddr string) {
if err == nil { if err == nil {
mi.LastLogin = time.Now() mi.LastLogin = time.Now()
_ = dbdata.Set(mi) _ = dbdata.Set(mi)
ipAll[mi.IpAddr] = mi
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -194,8 +194,7 @@
<el-form-item label="排除本地网络" prop="allow_lan"> <el-form-item label="排除本地网络" prop="allow_lan">
<el-switch <el-switch
v-model="ruleForm.allow_lan" v-model="ruleForm.allow_lan"
active-text="开启后 用户本地所在网段将不通过anylink加密传输" active-text="开启后 用户本地所在网段将不通过anylink加密传输">
inactive-text="开启后 用户本地所在网段将不通过anylink加密传输">
</el-switch> </el-switch>
</el-form-item> </el-form-item>

View File

@ -1,277 +1,283 @@
<template> <template>
<div> <div>
<el-card> <el-card>
<el-form :inline="true"> <el-form :inline="true">
<el-form-item> <el-form-item>
<el-button <el-button
size="small" size="small"
type="primary" type="primary"
icon="el-icon-plus" icon="el-icon-plus"
@click="handleEdit('')">添加 @click="handleEdit('')">添加
</el-button> </el-button>
</el-form-item> </el-form-item>
</el-form> <el-form-item>
<el-alert
title="直接操作数据库增删改数据后请重启anylink服务"
type="warning">
</el-alert>
</el-form-item>
</el-form>
<el-table <el-table
ref="multipleTable" ref="multipleTable"
:data="tableData" :data="tableData"
border> border>
<el-table-column <el-table-column
sortable="true" sortable="true"
prop="id" prop="id"
label="ID" label="ID"
width="60"> width="60">
</el-table-column> </el-table-column>
<el-table-column <el-table-column
prop="ip_addr" prop="ip_addr"
label="IP地址"> label="IP地址">
</el-table-column> </el-table-column>
<el-table-column <el-table-column
prop="mac_addr" prop="mac_addr"
label="MAC地址"> label="MAC地址">
</el-table-column> </el-table-column>
<el-table-column <el-table-column
prop="unique_mac" prop="unique_mac"
label="唯一MAC"> label="唯一MAC">
<template slot-scope="scope"> <template slot-scope="scope">
<el-tag v-if="scope.row.unique_mac" type="success"></el-tag> <el-tag v-if="scope.row.unique_mac" type="success"></el-tag>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column <el-table-column
prop="username" prop="username"
label="用户名"> label="用户名">
</el-table-column> </el-table-column>
<el-table-column <el-table-column
prop="keep" prop="keep"
label="IP保留"> label="IP保留">
<template slot-scope="scope"> <template slot-scope="scope">
<!-- <el-tag v-if="scope.row.keep" type="success">保留</el-tag>--> <!-- <el-tag v-if="scope.row.keep" type="success">保留</el-tag>-->
<el-switch <el-switch
disabled disabled
v-model="scope.row.keep" v-model="scope.row.keep"
active-color="#13ce66"> active-color="#13ce66">
</el-switch> </el-switch>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column <el-table-column
prop="note" prop="note"
label="备注"> label="备注">
</el-table-column> </el-table-column>
<el-table-column <el-table-column
prop="last_login" prop="last_login"
label="最后登陆时间" label="最后登陆时间"
:formatter="tableDateFormat"> :formatter="tableDateFormat">
</el-table-column> </el-table-column>
<el-table-column <el-table-column
label="操作" label="操作"
width="150"> width="150">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button <el-button
size="mini" size="mini"
type="primary" type="primary"
@click="handleEdit(scope.row)">编辑 @click="handleEdit(scope.row)">编辑
</el-button> </el-button>
<el-popconfirm <el-popconfirm
class="m-left-10" class="m-left-10"
@confirm="handleDel(scope.row)" @confirm="handleDel(scope.row)"
title="确定要删除IP映射吗"> title="确定要删除IP映射吗">
<el-button <el-button
slot="reference" slot="reference"
size="mini" size="mini"
type="danger">删除 type="danger">删除
</el-button> </el-button>
</el-popconfirm> </el-popconfirm>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
<div class="sh-20"></div> <div class="sh-20"></div>
<el-pagination <el-pagination
background background
layout="prev, pager, next" layout="prev, pager, next"
:pager-count="11" :pager-count="11"
@current-change="pageChange" @current-change="pageChange"
:total="count"> :total="count">
</el-pagination> </el-pagination>
</el-card> </el-card>
<!--新增修改弹出框--> <!--新增修改弹出框-->
<el-dialog <el-dialog
title="提示" title="提示"
:close-on-click-modal="false" :close-on-click-modal="false"
:visible="user_edit_dialog" :visible="user_edit_dialog"
@close="disVisible" @close="disVisible"
width="600px" width="600px"
center> center>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="ruleForm"> <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="ruleForm">
<el-form-item label="ID" prop="id"> <el-form-item label="ID" prop="id">
<el-input v-model="ruleForm.id" disabled></el-input> <el-input v-model="ruleForm.id" disabled></el-input>
</el-form-item> </el-form-item>
<el-form-item label="IP地址" prop="ip_addr"> <el-form-item label="IP地址" prop="ip_addr">
<el-input v-model="ruleForm.ip_addr"></el-input> <el-input v-model="ruleForm.ip_addr"></el-input>
</el-form-item> </el-form-item>
<el-form-item label="MAC地址" prop="mac_addr"> <el-form-item label="MAC地址" prop="mac_addr">
<el-input v-model="ruleForm.mac_addr"></el-input> <el-input v-model="ruleForm.mac_addr"></el-input>
</el-form-item> </el-form-item>
<el-form-item label="用户名" prop="username"> <el-form-item label="用户名" prop="username">
<el-input v-model="ruleForm.username"></el-input> <el-input v-model="ruleForm.username"></el-input>
</el-form-item> </el-form-item>
<el-form-item label="备注" prop="note"> <el-form-item label="备注" prop="note">
<el-input v-model="ruleForm.note"></el-input> <el-input v-model="ruleForm.note"></el-input>
</el-form-item> </el-form-item>
<el-form-item label="IP保留" prop="keep"> <el-form-item label="IP保留" prop="keep">
<el-switch <el-switch
v-model="ruleForm.keep" v-model="ruleForm.keep"
active-color="#13ce66"> active-color="#13ce66">
</el-switch> </el-switch>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">保存</el-button> <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
<el-button @click="disVisible">取消</el-button> <el-button @click="disVisible">取消</el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
</el-dialog> </el-dialog>
</div> </div>
</template> </template>
<script> <script>
import axios from "axios"; import axios from "axios";
export default { export default {
name: "IpMap", name: "IpMap",
components: {}, components: {},
mixins:[], mixins: [],
created() { created() {
this.$emit('update:route_path', this.$route.path) this.$emit('update:route_path', this.$route.path)
this.$emit('update:route_name', ['用户信息', 'IP映射']) this.$emit('update:route_name', ['用户信息', 'IP映射'])
}, },
mounted() { mounted() {
this.getData(1) this.getData(1)
}, },
data() { data() {
return { return {
tableData: [], tableData: [],
count: 10, count: 10,
nowIndex: 0, nowIndex: 0,
ruleForm: { ruleForm: {
status: 1, status: 1,
groups: [], groups: [],
}, },
rules: { rules: {
username: [ username: [
{required: false, message: '请输入用户名', trigger: 'blur'}, {required: false, message: '请输入用户名', trigger: 'blur'},
{max: 50, message: '长度小于 50 个字符', trigger: 'blur'} {max: 50, message: '长度小于 50 个字符', trigger: 'blur'}
], ],
mac_addr: [ mac_addr: [
{required: true, message: '请输入mac地址', trigger: 'blur'} {required: true, message: '请输入mac地址', trigger: 'blur'}
], ],
ip_addr: [ ip_addr: [
{required: true, message: '请输入ip地址', trigger: 'blur'} {required: true, message: '请输入ip地址', trigger: 'blur'}
], ],
status: [ status: [
{required: true} {required: true}
], ],
}, },
}
},
methods: {
getData(p) {
axios.get('/user/ip_map/list', {
params: {
page: p,
} }
}).then(resp => {
var data = resp.data.data
console.log(data);
this.tableData = data.datas;
this.count = data.count
}).catch(error => {
this.$message.error('哦,请求出错');
console.log(error);
});
}, },
pageChange(p) { methods: {
this.getData(p) getData(p) {
}, axios.get('/user/ip_map/list', {
handleEdit(row) { params: {
!this.$refs['ruleForm'] || this.$refs['ruleForm'].resetFields(); page: p,
console.log(row) }
this.user_edit_dialog = true }).then(resp => {
if (!row) { var data = resp.data.data
return; console.log(data);
} this.tableData = data.datas;
this.count = data.count
}).catch(error => {
this.$message.error('哦,请求出错');
console.log(error);
});
},
pageChange(p) {
this.getData(p)
},
handleEdit(row) {
!this.$refs['ruleForm'] || this.$refs['ruleForm'].resetFields();
console.log(row)
this.user_edit_dialog = true
if (!row) {
return;
}
axios.get('/user/ip_map/detail', { axios.get('/user/ip_map/detail', {
params: { params: {
id: row.id, id: row.id,
} }
}).then(resp => { }).then(resp => {
this.ruleForm = resp.data.data this.ruleForm = resp.data.data
}).catch(error => { }).catch(error => {
this.$message.error('哦,请求出错'); this.$message.error('哦,请求出错');
console.log(error); console.log(error);
}); });
}, },
handleDel(row) { handleDel(row) {
axios.post('/user/ip_map/del?id=' + row.id).then(resp => { axios.post('/user/ip_map/del?id=' + row.id).then(resp => {
var rdata = resp.data var rdata = resp.data
if (rdata.code === 0) { if (rdata.code === 0) {
this.$message.success(rdata.msg); this.$message.success(rdata.msg);
this.getData(1); this.getData(1);
} else { } else {
this.$message.error(rdata.msg); this.$message.error(rdata.msg);
} }
console.log(rdata); console.log(rdata);
}).catch(error => { }).catch(error => {
this.$message.error('哦,请求出错'); this.$message.error('哦,请求出错');
console.log(error); console.log(error);
}); });
}, },
submitForm(formName) { submitForm(formName) {
this.$refs[formName].validate((valid) => { this.$refs[formName].validate((valid) => {
if (!valid) { if (!valid) {
console.log('error submit!!'); console.log('error submit!!');
return false; return false;
} }
// alert('submit!'); // alert('submit!');
axios.post('/user/ip_map/set', this.ruleForm).then(resp => { axios.post('/user/ip_map/set', this.ruleForm).then(resp => {
var rdata = resp.data var rdata = resp.data
if (rdata.code === 0) { if (rdata.code === 0) {
this.$message.success(rdata.msg); this.$message.success(rdata.msg);
this.getData(1); this.getData(1);
} else { } else {
this.$message.error(rdata.msg); this.$message.error(rdata.msg);
} }
console.log(rdata); console.log(rdata);
}).catch(error => { }).catch(error => {
this.$message.error('哦,请求出错'); this.$message.error('哦,请求出错');
console.log(error); console.log(error);
}); });
}); });
},
}, },
},
} }
</script> </script>

View File

@ -6759,6 +6759,13 @@ qs@6.9.7:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.7.tgz#4610846871485e1e048f44ae3b94033f0e675afe" resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.7.tgz#4610846871485e1e048f44ae3b94033f0e675afe"
integrity sha512-IhMFgUmuNpyRfxA90umL7ByLlgRXu6tIfKPpF5TmcfRLlLCckfP/g3IQmju6jjpu+Hh8rA+2p6A27ZSPOOHdKw== integrity sha512-IhMFgUmuNpyRfxA90umL7ByLlgRXu6tIfKPpF5TmcfRLlLCckfP/g3IQmju6jjpu+Hh8rA+2p6A27ZSPOOHdKw==
qs@^6.11.1:
version "6.11.1"
resolved "https://registry.npmmirror.com/qs/-/qs-6.11.1.tgz#6c29dff97f0c0060765911ba65cbc9764186109f"
integrity sha512-0wsrzgTz/kAVIeuxSjnpGC56rzYtr6JT/2BwEvMaPhFIoYa1aGO8LbzuU1R0uUYQkLpWBTOj0l/CLAJB64J6nQ==
dependencies:
side-channel "^1.0.4"
qs@~6.5.2: qs@~6.5.2:
version "6.5.3" version "6.5.3"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.3.tgz#3aeeffc91967ef6e35c0e488ef46fb296ab76aad" resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.3.tgz#3aeeffc91967ef6e35c0e488ef46fb296ab76aad"