obj.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package file
  2. import (
  3. "github.com/cnlh/nps/lib/rate"
  4. "strings"
  5. "sync"
  6. )
  7. type Flow struct {
  8. ExportFlow int64 //出口流量
  9. InletFlow int64 //入口流量
  10. FlowLimit int64 //流量限制,出口+入口 /M
  11. sync.RWMutex
  12. }
  13. func (s *Flow) Add(in, out int64) {
  14. s.Lock()
  15. defer s.Unlock()
  16. s.InletFlow += int64(in)
  17. s.ExportFlow += int64(out)
  18. }
  19. type Client struct {
  20. Cnf *Config
  21. Id int //id
  22. VerifyKey string //验证密钥
  23. Addr string //客户端ip地址
  24. Remark string //备注
  25. Status bool //是否开启
  26. IsConnect bool //是否连接
  27. RateLimit int //速度限制 /kb
  28. Flow *Flow //流量
  29. Rate *rate.Rate //速度控制
  30. NoStore bool
  31. NoDisplay bool
  32. MaxConn int //客户端最大连接数
  33. NowConn int //当前连接数
  34. id int
  35. sync.RWMutex
  36. }
  37. func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
  38. return &Client{
  39. Cnf: new(Config),
  40. Id: 0,
  41. VerifyKey: vKey,
  42. Addr: "",
  43. Remark: "",
  44. Status: true,
  45. IsConnect: false,
  46. RateLimit: 0,
  47. Flow: new(Flow),
  48. Rate: nil,
  49. NoStore: noStore,
  50. RWMutex: sync.RWMutex{},
  51. NoDisplay: noDisplay,
  52. }
  53. }
  54. func (s *Client) CutConn() {
  55. s.Lock()
  56. defer s.Unlock()
  57. s.NowConn++
  58. }
  59. func (s *Client) AddConn() {
  60. s.Lock()
  61. defer s.Unlock()
  62. s.NowConn--
  63. }
  64. func (s *Client) GetConn() bool {
  65. if s.MaxConn == 0 || s.NowConn < s.MaxConn {
  66. s.CutConn()
  67. return true
  68. }
  69. return false
  70. }
  71. func (s *Client) HasTunnel(t *Tunnel) bool {
  72. for _, v := range GetCsvDb().Tasks {
  73. if v.Client.Id == s.Id && v.Port == t.Port {
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. func (s *Client) HasHost(h *Host) bool {
  80. for _, v := range GetCsvDb().Hosts {
  81. if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. type Tunnel struct {
  88. Id int //Id
  89. Port int //服务端监听端口
  90. Mode string //启动方式
  91. Target string //目标
  92. Status bool //设置是否开启
  93. RunStatus bool //当前运行状态
  94. Client *Client //所属客户端id
  95. Ports string //客户端与服务端传递
  96. Flow *Flow
  97. Password string //私密模式密码,唯一
  98. Remark string //备注
  99. TargetAddr string
  100. NoStore bool
  101. LocalPath string
  102. StripPre string
  103. }
  104. type Config struct {
  105. U string //socks5验证用户名
  106. P string //socks5验证密码
  107. Compress bool //压缩方式
  108. Crypt bool //是否加密
  109. }
  110. type Host struct {
  111. Id int
  112. Host string //启动方式
  113. Target string //目标
  114. HeaderChange string //host修改
  115. HostChange string //host修改
  116. Location string //url 路由
  117. Flow *Flow
  118. Client *Client
  119. Remark string //备注
  120. NowIndex int
  121. TargetArr []string
  122. NoStore bool
  123. sync.RWMutex
  124. }
  125. func (s *Host) GetRandomTarget() string {
  126. if s.TargetArr == nil {
  127. s.TargetArr = strings.Split(s.Target, "\n")
  128. }
  129. s.Lock()
  130. defer s.Unlock()
  131. if s.NowIndex >= len(s.TargetArr)-1 {
  132. s.NowIndex = 0
  133. } else {
  134. s.NowIndex++
  135. }
  136. return s.TargetArr[s.NowIndex]
  137. }