obj.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package file
  2. import (
  3. "github.com/cnlh/nps/lib/rate"
  4. "github.com/pkg/errors"
  5. "strings"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. )
  10. type Flow struct {
  11. ExportFlow int64
  12. InletFlow int64
  13. FlowLimit int64
  14. sync.RWMutex
  15. }
  16. func (s *Flow) Add(in, out int64) {
  17. s.Lock()
  18. defer s.Unlock()
  19. s.InletFlow += int64(in)
  20. s.ExportFlow += int64(out)
  21. }
  22. type Client struct {
  23. Cnf *Config
  24. Id int //id
  25. VerifyKey string //verify key
  26. Addr string //the ip of client
  27. Remark string //remark
  28. Status bool //is allow connect
  29. IsConnect bool //is the client connect
  30. RateLimit int //rate /kb
  31. Flow *Flow //flow setting
  32. Rate *rate.Rate //rate limit
  33. NoStore bool //no store to file
  34. NoDisplay bool //no display on web
  35. MaxConn int //the max connection num of client allow
  36. NowConn int32 //the connection num of now
  37. WebUserName string //the username of web login
  38. WebPassword string //the password of web login
  39. ConfigConnAllow bool //is allow connected by config file
  40. sync.RWMutex
  41. }
  42. func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
  43. return &Client{
  44. Cnf: new(Config),
  45. Id: 0,
  46. VerifyKey: vKey,
  47. Addr: "",
  48. Remark: "",
  49. Status: true,
  50. IsConnect: false,
  51. RateLimit: 0,
  52. Flow: new(Flow),
  53. Rate: nil,
  54. NoStore: noStore,
  55. RWMutex: sync.RWMutex{},
  56. NoDisplay: noDisplay,
  57. }
  58. }
  59. func (s *Client) CutConn() {
  60. atomic.AddInt32(&s.NowConn, 1)
  61. }
  62. func (s *Client) AddConn() {
  63. atomic.AddInt32(&s.NowConn, -1)
  64. }
  65. func (s *Client) GetConn() bool {
  66. if s.MaxConn == 0 || int(s.NowConn) < s.MaxConn {
  67. s.CutConn()
  68. return true
  69. }
  70. return false
  71. }
  72. func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
  73. GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
  74. v := value.(*Tunnel)
  75. if v.Client.Id == s.Id && v.Port == t.Port {
  76. exist = true
  77. return false
  78. }
  79. return true
  80. })
  81. return
  82. }
  83. func (s *Client) HasHost(h *Host) bool {
  84. var has bool
  85. GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
  86. v := value.(*Host)
  87. if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
  88. has = true
  89. return false
  90. }
  91. return true
  92. })
  93. return has
  94. }
  95. type Tunnel struct {
  96. Id int //Id
  97. Port int //服务端监听端口
  98. ServerIp string
  99. Mode string //启动方式
  100. Status bool //设置是否开启
  101. RunStatus bool //当前运行状态
  102. Client *Client //所属客户端id
  103. Ports string //客户端与服务端传递
  104. Flow *Flow
  105. Password string //私密模式密码,唯一
  106. Remark string //备注
  107. TargetAddr string
  108. NoStore bool
  109. LocalPath string
  110. StripPre string
  111. Target *Target
  112. Health
  113. sync.RWMutex
  114. }
  115. type Health struct {
  116. HealthCheckTimeout int
  117. HealthMaxFail int
  118. HealthCheckInterval int
  119. HealthNextTime time.Time
  120. HealthMap map[string]int
  121. HttpHealthUrl string
  122. HealthRemoveArr []string
  123. HealthCheckType string
  124. HealthCheckTarget string
  125. sync.RWMutex
  126. }
  127. type Target struct {
  128. nowIndex int
  129. TargetStr string
  130. TargetArr []string //目标
  131. sync.RWMutex
  132. }
  133. func (s *Target) GetRandomTarget() (string, error) {
  134. if s.TargetArr == nil {
  135. s.TargetArr = strings.Split(s.TargetStr, "\n")
  136. }
  137. if len(s.TargetArr) == 1 {
  138. return s.TargetArr[0], nil
  139. }
  140. if len(s.TargetArr) == 0 {
  141. return "", errors.New("all inward-bending targets are offline")
  142. }
  143. s.Lock()
  144. defer s.Unlock()
  145. if s.nowIndex >= len(s.TargetArr)-1 {
  146. s.nowIndex = -1
  147. }
  148. s.nowIndex++
  149. return s.TargetArr[s.nowIndex], nil
  150. }
  151. type Config struct {
  152. U string
  153. P string
  154. Compress bool
  155. Crypt bool
  156. }
  157. type Host struct {
  158. Id int
  159. Host string //host
  160. HeaderChange string //header change
  161. HostChange string //host change
  162. Location string //url router
  163. Remark string //remark
  164. Scheme string //http https all
  165. NoStore bool
  166. IsClose bool
  167. Flow *Flow
  168. Client *Client
  169. Target *Target //目标
  170. Health
  171. sync.RWMutex
  172. }