yamlconfig.go 9.6 KB

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