yamlconfig.go 9.4 KB

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