yamlconfig.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package model
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gopkg.in/yaml.v2"
  5. "io/ioutil"
  6. "os"
  7. "rocommon/util"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. )
  13. ///0 serverlist
  14. ///1 inner server
  15. ///2 outer server
  16. ///https://blog.51cto.com/xingej/2115258 配置文件内容
  17. var serviceConfig *ServerListNode = nil
  18. type ServerListNode struct {
  19. configLock sync.RWMutex
  20. //#白名单列表
  21. ServerList []*ServerNode `yaml:"serverlist,flow"`
  22. WhiteList WhiteListNode `yaml:"whitelist"`
  23. BlackList WhiteListNode `yaml:"blacklist"`
  24. BlackPlatformList []string `yaml:"blackplatformlist"`
  25. serverNormalList []*ServerNode //正常外网服务器
  26. serverMapList map[int]*ServerNode
  27. serverSpeList []*ServerNode //type特殊类型服务器1-20
  28. configPath string
  29. fileInfo os.FileInfo
  30. }
  31. //State 0正常使用 1维护中
  32. type ServerNode struct {
  33. ServerId int `yaml:serverid`
  34. ServerName string `yaml:servername`
  35. Ip string `yaml:ip`
  36. Port []int `yaml:port,flow`
  37. Type int `yaml:type`
  38. STime string `yaml:stime`
  39. State int `yaml:state`
  40. Invisible int `yaml:invisible`
  41. STimeStamp uint64
  42. }
  43. type WhiteListNode struct {
  44. Ip []string `yaml:"ip"`
  45. Openid []string `yaml:"openid"`
  46. ServerList *ServerNode `yaml:"serverlist"`
  47. ShenheVersion []string `yaml:"shenheversion"`
  48. }
  49. type AllServerSt struct {
  50. ServerList []ServerNode
  51. ServerNum int
  52. Page int
  53. MaxServerId int
  54. }
  55. func newServerListNode(path string) *ServerListNode {
  56. serviceConfig := &ServerListNode{
  57. configPath: path,
  58. serverMapList: map[int]*ServerNode{},
  59. }
  60. return serviceConfig
  61. }
  62. var sessionIdGen int64 = 0
  63. func (this *ServerListNode) GetServerById(c *gin.Context, nodeId string) *ServerNode {
  64. serverId, err := strconv.Atoi(nodeId)
  65. if err != nil {
  66. return nil
  67. }
  68. tmpConfig := GetServiceConfig()
  69. ip := c.ClientIP()
  70. resver := c.DefaultQuery("resver", "") ///default ""
  71. bBlack := tmpConfig.IsBlackList("", ip, resver)
  72. //黑名单只看到特殊的IP列表
  73. if bBlack {
  74. if tmpConfig.BlackList.ServerList != nil {
  75. return tmpConfig.BlackList.ServerList
  76. }
  77. return nil
  78. }
  79. subplatform := c.DefaultQuery("sub_platform", "") ///default 0
  80. bBlockPlatform := tmpConfig.IsPlatformBlackList(subplatform)
  81. bWhite := tmpConfig.IsWhiteList("", ip, resver)
  82. var retServerNode ServerNode
  83. for idx := 0; idx < len(this.serverSpeList); idx++ {
  84. if this.serverSpeList[idx].ServerId == serverId {
  85. if bWhite {
  86. retServerNode = *this.serverSpeList[idx]
  87. //白名单玩家状态修改
  88. //5维护 6待测试
  89. if retServerNode.State != 6 {
  90. retServerNode.State = 0
  91. }
  92. } else {
  93. retServerNode = *this.serverSpeList[idx]
  94. }
  95. break
  96. }
  97. }
  98. if serverNode, ok := this.serverMapList[serverId]; ok {
  99. if bWhite {
  100. retServerNode = *serverNode
  101. //白名单玩家状态修改
  102. //5维护 6待测试
  103. if retServerNode.State != 6 {
  104. retServerNode.State = 0
  105. }
  106. } else {
  107. retServerNode = *serverNode
  108. }
  109. }
  110. if bBlockPlatform {
  111. retServerNode.State = 5
  112. }
  113. if retServerNode.ServerId > 0 {
  114. return &retServerNode
  115. }
  116. return nil
  117. }
  118. func (this *ServerListNode) GetLatestServer(bForce bool) *ServerNode {
  119. if len(this.serverNormalList) > 0 {
  120. if bForce {
  121. return this.serverNormalList[len(this.serverNormalList)-1]
  122. } else {
  123. normalListLen := len(this.serverNormalList)
  124. for idx := normalListLen - 1; idx >= 0; idx-- {
  125. if this.serverNormalList[idx].Invisible > 0 {
  126. continue
  127. }
  128. return this.serverNormalList[idx]
  129. }
  130. }
  131. }
  132. return nil
  133. }
  134. func (this *ServerListNode) IsWhiteList(openId, ip, shenheversion string) bool {
  135. for idx := 0; idx < len(this.WhiteList.Ip); idx++ {
  136. if this.WhiteList.Ip[idx] == ip {
  137. return true
  138. }
  139. }
  140. for idx := 0; idx < len(this.WhiteList.Openid); idx++ {
  141. if this.WhiteList.Openid[idx] == openId {
  142. return true
  143. }
  144. }
  145. return false
  146. }
  147. func (this *ServerListNode) IsPlatformBlackList(subplatform string) bool {
  148. if subplatform == "" {
  149. return false
  150. }
  151. for idx := 0; idx < len(GetServiceConfig().BlackPlatformList); idx++ {
  152. if subplatform == GetServiceConfig().BlackPlatformList[idx] {
  153. return true
  154. }
  155. }
  156. return false
  157. }
  158. func (this *ServerListNode) IsBlackList(openId, ip, shenheversion string) bool {
  159. for idx := 0; idx < len(this.BlackList.Ip); idx++ {
  160. if strings.Contains(ip, this.BlackList.Ip[idx]) {
  161. return true
  162. }
  163. }
  164. for idx := 0; idx < len(this.BlackList.Openid); idx++ {
  165. if this.WhiteList.Openid[idx] == openId {
  166. return true
  167. }
  168. }
  169. for idx := 0; idx < len(this.BlackList.ShenheVersion); idx++ {
  170. if this.BlackList.ShenheVersion[idx] == shenheversion {
  171. return true
  172. }
  173. }
  174. return false
  175. }
  176. func (this *ServerListNode) GetServerList(c *gin.Context, pageIdStr string) interface{} {
  177. pageId, err := strconv.Atoi(pageIdStr)
  178. if err != nil {
  179. return nil
  180. }
  181. var allServerSt AllServerSt
  182. allServerSt.Page = 10
  183. ip := c.ClientIP()
  184. resver := c.DefaultQuery("resver", "") ///default ""
  185. openId := c.DefaultQuery("openid", "") ///default ""
  186. platform := c.DefaultQuery("platform", "") ///default ""
  187. openId = ConvertPlatform(openId, platform)
  188. tmpConfig := GetServiceConfig()
  189. bBlack := tmpConfig.IsBlackList(openId, ip, resver)
  190. //黑名单只看到特殊的IP列表
  191. if bBlack {
  192. allServerSt.ServerNum = 1
  193. if tmpConfig.BlackList.ServerList != nil {
  194. tmpNode := *tmpConfig.BlackList.ServerList
  195. if tmpNode.ServerId > allServerSt.MaxServerId {
  196. allServerSt.MaxServerId = tmpNode.ServerId
  197. }
  198. allServerSt.ServerList = append(allServerSt.ServerList, tmpNode)
  199. }
  200. return allServerSt
  201. }
  202. subplatform := c.DefaultQuery("sub_platform", "") ///default 0
  203. bBlockPlatform := tmpConfig.IsPlatformBlackList(subplatform)
  204. bWhite := tmpConfig.IsWhiteList(openId, ip, resver)
  205. serverNum := this.GetServerListNum(bWhite)
  206. if pageId <= 0 {
  207. pageId = serverNum / 10
  208. if serverNum%10 != 0 {
  209. pageId += 1
  210. }
  211. if pageId <= 0 {
  212. pageId = 1
  213. }
  214. }
  215. eIdx := pageId * 10
  216. sIdx := eIdx - 10
  217. if eIdx > serverNum {
  218. eIdx = serverNum
  219. }
  220. allServerSt.ServerNum = serverNum
  221. for idx := sIdx; idx < eIdx; idx++ {
  222. tmpNode := *this.serverNormalList[idx]
  223. if bWhite {
  224. //白名单玩家状态修改
  225. //5维护 6待测试
  226. if tmpNode.State != 6 {
  227. tmpNode.State = 0
  228. }
  229. } else if bBlockPlatform {
  230. tmpNode.State = 5
  231. }
  232. if !bWhite && tmpNode.Invisible > 0 {
  233. continue
  234. }
  235. allServerSt.ServerList = append(allServerSt.ServerList, tmpNode)
  236. if tmpNode.ServerId > allServerSt.MaxServerId {
  237. allServerSt.MaxServerId = tmpNode.ServerId
  238. }
  239. }
  240. allServerSt.MaxServerId = serverNum
  241. return allServerSt
  242. }
  243. func (this *ServerListNode) GetServerListNum(bWhite bool) int {
  244. if bWhite {
  245. return len(this.serverNormalList)
  246. } else {
  247. retNum := 0
  248. for idx := 0; idx < len(this.serverNormalList); idx++ {
  249. if this.serverNormalList[idx].Invisible <= 0 {
  250. retNum++
  251. }
  252. }
  253. return retNum
  254. }
  255. }
  256. func (this *ServerListNode) loadConfig() {
  257. yamlOpenFile, err := os.OpenFile(this.configPath, os.O_RDWR, 0660)
  258. if err != nil {
  259. util.FatalF("config not exist!!! %v:%v", this.configPath, err)
  260. return
  261. }
  262. defer yamlOpenFile.Close()
  263. tmpFileInfo, err1 := yamlOpenFile.Stat()
  264. if err1 != nil {
  265. util.FatalF("file info invalid!!! %v:%v", this.configPath, err1)
  266. return
  267. }
  268. if this.fileInfo != nil && this.fileInfo.ModTime().Equal(tmpFileInfo.ModTime()) {
  269. //util.InfoF("file same do not load again")
  270. return
  271. }
  272. //reset and clear
  273. this.fileInfo = tmpFileInfo
  274. this.serverNormalList = []*ServerNode{}
  275. this.serverNormalList = []*ServerNode{}
  276. this.serverSpeList = []*ServerNode{}
  277. this.serverMapList = map[int]*ServerNode{}
  278. //whitelist
  279. this.WhiteList.Ip = this.WhiteList.Ip[:0]
  280. this.WhiteList.Openid = this.WhiteList.Openid[:0]
  281. yamlFile, err2 := ioutil.ReadAll(yamlOpenFile)
  282. if err2 != nil {
  283. util.FatalF("load config [%v] err:%v\n", this.configPath, err2)
  284. return
  285. }
  286. err = yaml.Unmarshal(yamlFile, serviceConfig)
  287. if err != nil {
  288. util.FatalF("unmarshal [%v] err:%v\n", this.configPath, err)
  289. return
  290. }
  291. //serverlist
  292. for idx := 0; idx < len(this.ServerList); idx++ {
  293. if this.ServerList[idx].STime != "" {
  294. tmpSTime := util.GetTimeByStr(this.ServerList[idx].STime)
  295. this.ServerList[idx].STimeStamp = uint64(tmpSTime.Unix())
  296. }
  297. if this.ServerList[idx].Type > 0 {
  298. this.serverSpeList = append(this.serverSpeList, this.ServerList[idx])
  299. } else {
  300. this.serverNormalList = append(this.serverNormalList, this.ServerList[idx])
  301. this.serverMapList[this.ServerList[idx].ServerId] = this.ServerList[idx]
  302. }
  303. }
  304. sort.Slice(this.serverNormalList, func(i, j int) bool {
  305. return this.serverNormalList[i].ServerId < this.serverNormalList[j].ServerId
  306. })
  307. util.InfoF("Server yaml config load success:%v", this.configPath)
  308. }
  309. func (this *ServerListNode) Save() {
  310. yamlOpenFile, err := os.OpenFile(this.configPath, os.O_RDWR, 0660)
  311. if err != nil {
  312. util.FatalF("config not exist!!! %v:%v", this.configPath, err)
  313. return
  314. }
  315. defer yamlOpenFile.Close()
  316. datas, err := yaml.Marshal(serviceConfig)
  317. if err != nil {
  318. return
  319. }
  320. err = ioutil.WriteFile(this.configPath, datas, 0660)
  321. if err != nil {
  322. util.FatalF("save yaml config path=%v err=%v", this.configPath, err)
  323. return
  324. }
  325. }
  326. func InitConfig(configPath string) {
  327. if configPath == "" && serviceConfig == nil {
  328. return
  329. }
  330. if serviceConfig == nil {
  331. serviceConfig = newServerListNode(configPath)
  332. }
  333. serviceConfig.loadConfig()
  334. }
  335. func GetServiceConfig() *ServerListNode {
  336. InitConfig("")
  337. return serviceConfig
  338. }