db.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package file
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "sort"
  7. "strings"
  8. "sync"
  9. "ehang.io/nps/lib/common"
  10. "ehang.io/nps/lib/crypt"
  11. "ehang.io/nps/lib/rate"
  12. )
  13. type DbUtils struct {
  14. JsonDb *JsonDb
  15. }
  16. var (
  17. Db *DbUtils
  18. once sync.Once
  19. )
  20. //init csv from file
  21. func GetDb() *DbUtils {
  22. once.Do(func() {
  23. jsonDb := NewJsonDb(common.GetRunPath())
  24. jsonDb.LoadClientFromJsonFile()
  25. jsonDb.LoadTaskFromJsonFile()
  26. jsonDb.LoadHostFromJsonFile()
  27. Db = &DbUtils{JsonDb: jsonDb}
  28. })
  29. return Db
  30. }
  31. func GetMapKeys(m sync.Map, isSort bool, sortKey, order string) (keys []int) {
  32. if sortKey != "" && isSort {
  33. return sortClientByKey(m, sortKey, order)
  34. }
  35. m.Range(func(key, value interface{}) bool {
  36. keys = append(keys, key.(int))
  37. return true
  38. })
  39. sort.Ints(keys)
  40. return
  41. }
  42. func (s *DbUtils) GetClientList(start, length int, search, sort, order string, clientId int) ([]*Client, int) {
  43. list := make([]*Client, 0)
  44. var cnt int
  45. keys := GetMapKeys(s.JsonDb.Clients, true, sort, order)
  46. for _, key := range keys {
  47. if value, ok := s.JsonDb.Clients.Load(key); ok {
  48. v := value.(*Client)
  49. if v.NoDisplay {
  50. continue
  51. }
  52. if clientId != 0 && clientId != v.Id {
  53. continue
  54. }
  55. if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.VerifyKey, search) || strings.Contains(v.Remark, search)) {
  56. continue
  57. }
  58. cnt++
  59. if start--; start < 0 {
  60. if length--; length >= 0 {
  61. list = append(list, v)
  62. }
  63. }
  64. }
  65. }
  66. return list, cnt
  67. }
  68. func (s *DbUtils) GetIdByVerifyKey(vKey string, addr string) (id int, err error) {
  69. var exist bool
  70. s.JsonDb.Clients.Range(func(key, value interface{}) bool {
  71. v := value.(*Client)
  72. if common.Getverifyval(v.VerifyKey) == vKey && v.Status {
  73. v.Addr = common.GetIpByAddr(addr)
  74. id = v.Id
  75. exist = true
  76. return false
  77. }
  78. return true
  79. })
  80. if exist {
  81. return
  82. }
  83. return 0, errors.New("not found")
  84. }
  85. func (s *DbUtils) NewTask(t *Tunnel) (err error) {
  86. s.JsonDb.Tasks.Range(func(key, value interface{}) bool {
  87. v := value.(*Tunnel)
  88. if (v.Mode == "secret" || v.Mode == "p2p") && v.Password == t.Password {
  89. err = errors.New(fmt.Sprintf("secret mode keys %s must be unique", t.Password))
  90. return false
  91. }
  92. return true
  93. })
  94. if err != nil {
  95. return
  96. }
  97. t.Flow = new(Flow)
  98. s.JsonDb.Tasks.Store(t.Id, t)
  99. s.JsonDb.StoreTasksToJsonFile()
  100. return
  101. }
  102. func (s *DbUtils) UpdateTask(t *Tunnel) error {
  103. s.JsonDb.Tasks.Store(t.Id, t)
  104. s.JsonDb.StoreTasksToJsonFile()
  105. return nil
  106. }
  107. func (s *DbUtils) DelTask(id int) error {
  108. s.JsonDb.Tasks.Delete(id)
  109. s.JsonDb.StoreTasksToJsonFile()
  110. return nil
  111. }
  112. //md5 password
  113. func (s *DbUtils) GetTaskByMd5Password(p string) (t *Tunnel) {
  114. s.JsonDb.Tasks.Range(func(key, value interface{}) bool {
  115. if crypt.Md5(value.(*Tunnel).Password) == p {
  116. t = value.(*Tunnel)
  117. return false
  118. }
  119. return true
  120. })
  121. return
  122. }
  123. func (s *DbUtils) GetTask(id int) (t *Tunnel, err error) {
  124. if v, ok := s.JsonDb.Tasks.Load(id); ok {
  125. t = v.(*Tunnel)
  126. return
  127. }
  128. err = errors.New("not found")
  129. return
  130. }
  131. func (s *DbUtils) DelHost(id int) error {
  132. s.JsonDb.Hosts.Delete(id)
  133. s.JsonDb.StoreHostToJsonFile()
  134. return nil
  135. }
  136. func (s *DbUtils) IsHostExist(h *Host) bool {
  137. var exist bool
  138. s.JsonDb.Hosts.Range(func(key, value interface{}) bool {
  139. v := value.(*Host)
  140. if v.Id != h.Id && v.Host == h.Host && h.Location == v.Location && (v.Scheme == "all" || v.Scheme == h.Scheme) {
  141. exist = true
  142. return false
  143. }
  144. return true
  145. })
  146. return exist
  147. }
  148. func (s *DbUtils) NewHost(t *Host) error {
  149. if t.Location == "" {
  150. t.Location = "/"
  151. }
  152. if s.IsHostExist(t) {
  153. return errors.New("host has exist")
  154. }
  155. t.Flow = new(Flow)
  156. s.JsonDb.Hosts.Store(t.Id, t)
  157. s.JsonDb.StoreHostToJsonFile()
  158. return nil
  159. }
  160. func (s *DbUtils) GetHost(start, length int, id int, search string) ([]*Host, int) {
  161. list := make([]*Host, 0)
  162. var cnt int
  163. keys := GetMapKeys(s.JsonDb.Hosts, false, "", "")
  164. for _, key := range keys {
  165. if value, ok := s.JsonDb.Hosts.Load(key); ok {
  166. v := value.(*Host)
  167. if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.Host, search) || strings.Contains(v.Remark, search)) {
  168. continue
  169. }
  170. if id == 0 || v.Client.Id == id {
  171. cnt++
  172. if start--; start < 0 {
  173. if length--; length >= 0 {
  174. list = append(list, v)
  175. }
  176. }
  177. }
  178. }
  179. }
  180. return list, cnt
  181. }
  182. func (s *DbUtils) DelClient(id int) error {
  183. s.JsonDb.Clients.Delete(id)
  184. s.JsonDb.StoreClientsToJsonFile()
  185. return nil
  186. }
  187. func (s *DbUtils) NewClient(c *Client) error {
  188. var isNotSet bool
  189. if c.WebUserName != "" && !s.VerifyUserName(c.WebUserName, c.Id) {
  190. return errors.New("web login username duplicate, please reset")
  191. }
  192. reset:
  193. if c.VerifyKey == "" || isNotSet {
  194. isNotSet = true
  195. c.VerifyKey = crypt.GetRandomString(16)
  196. }
  197. if c.RateLimit == 0 {
  198. c.Rate = rate.NewRate(int64(2 << 23))
  199. } else if c.Rate == nil {
  200. c.Rate = rate.NewRate(int64(c.RateLimit * 1024))
  201. }
  202. c.Rate.Start()
  203. if !s.VerifyVkey(c.VerifyKey, c.Id) {
  204. if isNotSet {
  205. goto reset
  206. }
  207. return errors.New("Vkey duplicate, please reset")
  208. }
  209. if c.Id == 0 {
  210. c.Id = int(s.JsonDb.GetClientId())
  211. }
  212. if c.Flow == nil {
  213. c.Flow = new(Flow)
  214. }
  215. s.JsonDb.Clients.Store(c.Id, c)
  216. s.JsonDb.StoreClientsToJsonFile()
  217. return nil
  218. }
  219. func (s *DbUtils) VerifyVkey(vkey string, id int) (res bool) {
  220. res = true
  221. s.JsonDb.Clients.Range(func(key, value interface{}) bool {
  222. v := value.(*Client)
  223. if v.VerifyKey == vkey && v.Id != id {
  224. res = false
  225. return false
  226. }
  227. return true
  228. })
  229. return res
  230. }
  231. func (s *DbUtils) VerifyUserName(username string, id int) (res bool) {
  232. res = true
  233. s.JsonDb.Clients.Range(func(key, value interface{}) bool {
  234. v := value.(*Client)
  235. if v.WebUserName == username && v.Id != id {
  236. res = false
  237. return false
  238. }
  239. return true
  240. })
  241. return res
  242. }
  243. func (s *DbUtils) UpdateClient(t *Client) error {
  244. s.JsonDb.Clients.Store(t.Id, t)
  245. if t.RateLimit == 0 {
  246. t.Rate = rate.NewRate(int64(2 << 23))
  247. t.Rate.Start()
  248. }
  249. return nil
  250. }
  251. func (s *DbUtils) IsPubClient(id int) bool {
  252. client, err := s.GetClient(id)
  253. if err == nil {
  254. return client.NoDisplay
  255. }
  256. return false
  257. }
  258. func (s *DbUtils) GetClient(id int) (c *Client, err error) {
  259. if v, ok := s.JsonDb.Clients.Load(id); ok {
  260. c = v.(*Client)
  261. return
  262. }
  263. err = errors.New("未找到客户端")
  264. return
  265. }
  266. func (s *DbUtils) GetClientIdByVkey(vkey string) (id int, err error) {
  267. var exist bool
  268. s.JsonDb.Clients.Range(func(key, value interface{}) bool {
  269. v := value.(*Client)
  270. if crypt.Md5(v.VerifyKey) == vkey {
  271. exist = true
  272. id = v.Id
  273. return false
  274. }
  275. return true
  276. })
  277. if exist {
  278. return
  279. }
  280. err = errors.New("未找到客户端")
  281. return
  282. }
  283. func (s *DbUtils) GetHostById(id int) (h *Host, err error) {
  284. if v, ok := s.JsonDb.Hosts.Load(id); ok {
  285. h = v.(*Host)
  286. return
  287. }
  288. err = errors.New("The host could not be parsed")
  289. return
  290. }
  291. //get key by host from x
  292. func (s *DbUtils) GetInfoByHost(host string, r *http.Request) (h *Host, err error) {
  293. var hosts []*Host
  294. //Handling Ported Access
  295. host = common.GetIpByAddr(host)
  296. s.JsonDb.Hosts.Range(func(key, value interface{}) bool {
  297. v := value.(*Host)
  298. if v.IsClose {
  299. return true
  300. }
  301. //Remove http(s) http(s)://a.proxy.com
  302. //*.proxy.com *.a.proxy.com Do some pan-parsing
  303. if v.Scheme != "all" && v.Scheme != r.URL.Scheme {
  304. return true
  305. }
  306. tmpHost := v.Host
  307. if strings.Contains(tmpHost, "*") {
  308. tmpHost = strings.Replace(tmpHost, "*", "", -1)
  309. if strings.Contains(host, tmpHost) {
  310. hosts = append(hosts, v)
  311. }
  312. } else if v.Host == host {
  313. hosts = append(hosts, v)
  314. }
  315. return true
  316. })
  317. for _, v := range hosts {
  318. //If not set, default matches all
  319. if v.Location == "" {
  320. v.Location = "/"
  321. }
  322. if strings.Index(r.RequestURI, v.Location) == 0 {
  323. if h == nil || (len(v.Location) > len(h.Location)) {
  324. h = v
  325. }
  326. }
  327. }
  328. if h != nil {
  329. return
  330. }
  331. err = errors.New("The host could not be parsed")
  332. return
  333. }