server.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package server
  2. import (
  3. "be-vpn/internal/dto"
  4. "be-vpn/internal/model"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "log"
  8. "net/http"
  9. "sort"
  10. "sync"
  11. "time"
  12. )
  13. var nodes = make([]*model.Node, 0)
  14. var locker = sync.RWMutex{}
  15. func Register(c *gin.Context) {
  16. locker.Lock()
  17. defer locker.Unlock()
  18. var request dto.RegisterRequest
  19. if err := c.ShouldBindJSON(&request); err != nil {
  20. dto.BadRequest(c, err)
  21. return
  22. }
  23. for _, node := range nodes {
  24. if node.Ip == request.Ip {
  25. node.Ip = request.Ip
  26. node.Secret = request.Secret
  27. node.CountryCode = request.CountryCode
  28. node.CountryName = request.CountryName
  29. node.City = request.City
  30. node.Vip = request.Vip
  31. node.LastUpdateTime = time.Now()
  32. c.JSON(http.StatusOK, dto.RegisterResponse{
  33. Response: dto.NewOkResponse(),
  34. Data: dto.RegisterResult{
  35. Success: true,
  36. },
  37. })
  38. return
  39. }
  40. }
  41. node := &model.Node{
  42. Ip: request.Ip,
  43. Secret: request.Secret,
  44. LastUpdateTime: time.Now(),
  45. }
  46. nodes = append(nodes, node)
  47. log.Printf("update nodes: %+v", nodes)
  48. }
  49. func Group(c *gin.Context) {
  50. group(c, "v.starttransfernow.com", func(node *model.Node) bool {
  51. return true
  52. })
  53. }
  54. func V2Group(c *gin.Context) {
  55. vip := c.Query("vip")
  56. group(c, "v.sweeterlife.net", func(node *model.Node) bool {
  57. if vip == "" {
  58. return true
  59. }
  60. if vip == "true" && node.Vip {
  61. return true
  62. }
  63. if vip == "false" && !node.Vip {
  64. return true
  65. }
  66. return false
  67. })
  68. }
  69. func group(c *gin.Context, host string, predicate func(*model.Node) bool) {
  70. locker.RLock()
  71. defer locker.RUnlock()
  72. nodes := healthNodes()
  73. sort.SliceStable(nodes, func(i, j int) bool {
  74. return nodes[i].CountryCode > nodes[j].CountryCode
  75. })
  76. countryLabelSeqs := make(map[string]int)
  77. dtoNodes := make([]*dto.Node, 0)
  78. for _, node := range nodes {
  79. if predicate(node) {
  80. seq, ok := countryLabelSeqs[node.CountryCode]
  81. if !ok {
  82. countryLabelSeqs[node.CountryCode] = 0
  83. } else {
  84. countryLabelSeqs[node.CountryCode] = seq + 1
  85. }
  86. dtoNodes = append(dtoNodes, convert2DtoNode(node, host))
  87. }
  88. }
  89. continentMaps := make(map[string]bool)
  90. for _, node := range dtoNodes {
  91. continentMaps[node.Continent] = true
  92. }
  93. continents := make([]string, 0)
  94. for continent := range continentMaps {
  95. continents = append(continents, continent)
  96. }
  97. sort.SliceStable(continents, func(i, j int) bool {
  98. return continents[i] <= continents[j]
  99. })
  100. groupDtos := make([]*dto.Group, 0)
  101. for _, continent := range continents {
  102. groupDto := &dto.Group{
  103. Continent: continent,
  104. Nodes: make([]*dto.Node, 0),
  105. }
  106. for _, dtoNode := range dtoNodes {
  107. if dtoNode.Continent == continent {
  108. groupDto.Nodes = append(groupDto.Nodes, dtoNode)
  109. }
  110. }
  111. groupDtos = append(groupDtos, groupDto)
  112. }
  113. c.JSON(http.StatusOK, dto.GroupResponse{
  114. Response: dto.NewOkResponse(),
  115. Data: groupDtos,
  116. })
  117. }
  118. func Secret(c *gin.Context) {
  119. locker.RLock()
  120. defer locker.RUnlock()
  121. var request dto.DetailRequest
  122. if err := c.ShouldBindQuery(&request); err != nil {
  123. dto.BadRequest(c, err)
  124. return
  125. }
  126. for _, node := range nodes {
  127. if node.Ip == request.Ip {
  128. c.Header("Content-Disposition", "attachment; filename=client.ovpn")
  129. c.Data(http.StatusOK, "plain/text", []byte(node.Secret))
  130. return
  131. }
  132. }
  133. c.JSON(http.StatusNotFound, gin.H{
  134. "message": "not found ip",
  135. })
  136. }
  137. func Health(c *gin.Context) {
  138. c.JSON(http.StatusOK, gin.H{"status": "up"})
  139. }
  140. func healthNodes() []*model.Node {
  141. healthNodes := make([]*model.Node, 0)
  142. for _, node := range nodes {
  143. if node.LastUpdateTime.Add(10 * time.Second).After(time.Now()) {
  144. healthNodes = append(healthNodes, node)
  145. }
  146. }
  147. return healthNodes
  148. }
  149. func convert2DtoNode(node *model.Node, host string) *dto.Node {
  150. if node == nil {
  151. return nil
  152. }
  153. icons := map[string]string{
  154. "AE": "http://v.starttransfernow.com/static/AE.png",
  155. "AU": "http://v.starttransfernow.com/static/AU.png",
  156. "BH": "http://v.starttransfernow.com/static/BH.png",
  157. "BR": "http://v.starttransfernow.com/static/BR.png",
  158. "CA": "http://v.starttransfernow.com/static/CA.png",
  159. "CH": "http://v.starttransfernow.com/static/CH.png",
  160. "DE": "http://v.starttransfernow.com/static/DE.png",
  161. "ES": "http://v.starttransfernow.com/static/ES.png",
  162. "FR": "http://v.starttransfernow.com/static/FR.png",
  163. "GB": "http://v.starttransfernow.com/static/GB.png",
  164. "HK": "http://v.starttransfernow.com/static/HK.png",
  165. "ID": "http://v.starttransfernow.com/static/ID.png",
  166. "IL": "http://v.starttransfernow.com/static/IL.png",
  167. "IN": "http://v.starttransfernow.com/static/IN.png",
  168. "IT": "http://v.starttransfernow.com/static/IT.png",
  169. "JP": "http://v.starttransfernow.com/static/JP.png",
  170. "KR": "http://v.starttransfernow.com/static/KR.png",
  171. "SE": "http://v.starttransfernow.com/static/SE.png",
  172. "SG": "http://v.starttransfernow.com/static/SG.png",
  173. "US": "http://v.starttransfernow.com/static/US.png",
  174. "ZA": "http://v.starttransfernow.com/static/ZA.png",
  175. }
  176. countryLabels := map[string]string{
  177. "AE": "United Arab Emirates",
  178. "AU": "Australia",
  179. "BH": "Bahrain",
  180. "BR": "Brazil",
  181. "CA": "Canada",
  182. "CH": "Switzerland",
  183. "DE": "Germany",
  184. "ES": "Spain",
  185. "FR": "France",
  186. "GB": "United Kingdom",
  187. "HK": "Hong Kong",
  188. "ID": "Indonesia",
  189. "IL": "Israel",
  190. "IN": "India",
  191. "IT": "Italy",
  192. "JP": "Japan",
  193. "KR": "South Korea",
  194. "SE": "Sweden",
  195. "SG": "Singapore",
  196. "US": "United States",
  197. "ZA": "South Africa",
  198. }
  199. countryContinents := map[string]string{
  200. "AE": "asia",
  201. "AU": "oce",
  202. "BH": "asia",
  203. "BR": "southam",
  204. "CA": "northam",
  205. "CH": "eu",
  206. "DE": "eu",
  207. "ES": "eu",
  208. "FR": "eu",
  209. "GB": "eu",
  210. "HK": "asia",
  211. "ID": "asia",
  212. "IL": "asia",
  213. "IN": "asia",
  214. "IT": "eu",
  215. "JP": "asia",
  216. "KR": "asia",
  217. "SE": "eu",
  218. "SG": "asia",
  219. "US": "northam",
  220. "ZA": "southam",
  221. }
  222. countryLabel := fmt.Sprintf("%s", countryLabels[node.CountryCode])
  223. return &dto.Node{
  224. Ip: node.Ip,
  225. CountryCode: node.CountryCode,
  226. CountryName: node.CountryName,
  227. City: node.City,
  228. CountryLabel: countryLabel,
  229. Icon: icons[node.CountryCode],
  230. SecretUrl: fmt.Sprintf("http://%s/secret?ip=%s", host, node.Ip),
  231. Continent: countryContinents[node.CountryCode],
  232. Vip: node.Vip,
  233. }
  234. }