yamlconfig.go 11 KB

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