yamlconfig.go 9.4 KB

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