diff --git a/README.md b/README.md index 78161b3..b572bc1 100644 --- a/README.md +++ b/README.md @@ -3,43 +3,23 @@ 此实现目前并不是完全并发安全的: - 目前事务等级没有对 cityhash 表的 offset 字段做防丢失,并发操作可能会出问题 -## 模块逻辑 - -**更新网关配置** - -``` -获取所有网关 -获取所有配置 - -构建配置表 `map[网关][配置]` - -循环配置: - 如果配置项需要更新,记录更新信息 - -循环更新信息(加速数据库查询) - -上传配置到云端 -``` - -**同步节点信息(全量更新)** - -``` -获取所有启用城市 -循环城市: - 获取云端节点信息 - 获取本地节点信息 - - 更新列表 = 空 - 删除列表 = 本地节点 - 循环云端节点: - 更新列表 <= 云端节点 - 删除列表 x= 本地节点(同云端 macaddr) - - 更新并(软)删除节点信息 -``` - ### 统一节点调度 节点上下线: -提供一个接口用来为节点加解锁 \ No newline at end of file +提供一个接口用来为节点加解锁 + +## 目录结构 + +整体是一个 go 项目,编译后在服务器执行 + +``` +docs/ 相关文档 + +actions/ 程序功能 +clients/ 外部 api 调用 +model/ 数据库模型 +util/ 工具类 + +scripts/ shell 脚本 +``` \ No newline at end of file diff --git a/actions/configs.go b/actions/configs.go index 0afa5fd..bbb54ec 100644 --- a/actions/configs.go +++ b/actions/configs.go @@ -33,3 +33,16 @@ func UpdateConfigs(tx *gorm.DB, configs []model.ConfigUpdate) error { return nil } + +func CreateConfigs(tx *gorm.DB, configs []model.ConfigCreate) error { + if len(configs) == 0 { + return nil + } + + err := tx.CreateInBatches(&configs, 500).Error + if err != nil { + return err + } + + return nil +} diff --git a/actions/gen.go b/actions/gen.go new file mode 100644 index 0000000..4188f05 --- /dev/null +++ b/actions/gen.go @@ -0,0 +1,55 @@ +package actions + +import ( + "fmt" + "strconv" + "zzman/model" + + u "zzman/util" + + "gorm.io/gorm" +) + +func GenConfig(tx *gorm.DB) error { + + gateways, err := FindGateways(tx) + if err != nil { + return err + } + + cities, err := FindCitiesWithEdgesCount(tx) + if err != nil { + return err + } + + configs := make([]model.ConfigCreate, len(gateways)*30) + for iGateway := range gateways { + for j := range 30 { + n := iGateway*30 + j + iCity := (iGateway*30 + j) % len(cities) + gateway := gateways[iGateway] + city := cities[iCity] + + configs[n] = model.ConfigCreate{ + Table: u.P(strconv.Itoa(j + 221)), + Cityhash: &city.Hash, + CityLabel: &city.Label, + Network: u.P(fmt.Sprintf("172.30.168.%d", j+222)), + GatewayMac: &gateway.Macaddr, + User: u.P(fmt.Sprintf("jdzz%ddt%d", iGateway+1, j+221)), + InnerIp: u.P(fmt.Sprintf("%s%d", gateway.ProxyIp, j+222)), + EdgeMac: u.P(""), + IsChange: u.P(1), + IsOnline: u.P(0), + } + } + } + + println(fmt.Sprintf("生成配置:%d 条", len(configs))) + err = CreateConfigs(tx, configs) + if err != nil { + return err + } + + return nil +} diff --git a/main.go b/main.go index f528f7c..c8b60d8 100644 --- a/main.go +++ b/main.go @@ -91,6 +91,16 @@ func main() { slog.Info("清空节点成功") } return + + // 新增混拨配置(临时) + case "gen-mix": + err := actions.GenConfig(model.DB) + if err != nil { + slog.Error(fmt.Sprintf("生成混拨配置失败:%s", err.Error())) + } else { + slog.Info("生成混拨配置成功") + } + return } println("请输入正确的命令参数") diff --git a/model/config.go b/model/config.go index 10d652a..1db16d3 100644 --- a/model/config.go +++ b/model/config.go @@ -39,3 +39,20 @@ type ConfigUpdate struct { func (ConfigUpdate) TableName() string { return "gateway" } + +type ConfigCreate struct { + GatewayMac *string `gorm:"column:macaddr"` + Table *string `gorm:"column:table"` + EdgeMac *string `gorm:"column:edge"` + Network *string `gorm:"column:network"` + Cityhash *string `gorm:"column:cityhash"` + CityLabel *string `gorm:"column:label"` + User *string `gorm:"column:user"` + InnerIp *string `gorm:"column:inner_ip"` + IsChange *int `gorm:"column:ischange"` + IsOnline *int `gorm:"column:isonline"` +} + +func (ConfigCreate) TableName() string { + return "gateway" +} \ No newline at end of file