obj.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package file
  2. import (
  3. "strings"
  4. "sync"
  5. "sync/atomic"
  6. "time"
  7. "github.com/cnlh/nps/lib/rate"
  8. "github.com/pkg/errors"
  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 Config struct {
  23. U string
  24. P string
  25. Compress bool
  26. Crypt bool
  27. }
  28. type Client struct {
  29. Cnf *Config
  30. Id int //id
  31. VerifyKey string //verify key
  32. Addr string //the ip of client
  33. Remark string //remark
  34. Status bool //is allow connect
  35. IsConnect bool //is the client connect
  36. RateLimit int //rate /kb
  37. Flow *Flow //flow setting
  38. Rate *rate.Rate //rate limit
  39. NoStore bool //no store to file
  40. NoDisplay bool //no display on web
  41. MaxConn int //the max connection num of client allow
  42. NowConn int32 //the connection num of now
  43. WebUserName string //the username of web login
  44. WebPassword string //the password of web login
  45. ConfigConnAllow bool //is allow connected by config file
  46. MaxTunnelNum int
  47. sync.RWMutex
  48. }
  49. func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
  50. return &Client{
  51. Cnf: new(Config),
  52. Id: 0,
  53. VerifyKey: vKey,
  54. Addr: "",
  55. Remark: "",
  56. Status: true,
  57. IsConnect: false,
  58. RateLimit: 0,
  59. Flow: new(Flow),
  60. Rate: nil,
  61. NoStore: noStore,
  62. RWMutex: sync.RWMutex{},
  63. NoDisplay: noDisplay,
  64. }
  65. }
  66. func (s *Client) CutConn() {
  67. atomic.AddInt32(&s.NowConn, 1)
  68. }
  69. func (s *Client) AddConn() {
  70. atomic.AddInt32(&s.NowConn, -1)
  71. }
  72. func (s *Client) GetConn() bool {
  73. if s.MaxConn == 0 || int(s.NowConn) < s.MaxConn {
  74. s.CutConn()
  75. return true
  76. }
  77. return false
  78. }
  79. func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
  80. GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
  81. v := value.(*Tunnel)
  82. if v.Client.Id == s.Id && v.Port == t.Port && t.Port != 0 {
  83. exist = true
  84. return false
  85. }
  86. return true
  87. })
  88. return
  89. }
  90. func (s *Client) GetTunnelNum() (num int) {
  91. GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
  92. v := value.(*Tunnel)
  93. if v.Client.Id == s.Id {
  94. num++
  95. }
  96. return true
  97. })
  98. return
  99. }
  100. func (s *Client) HasHost(h *Host) bool {
  101. var has bool
  102. GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
  103. v := value.(*Host)
  104. if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
  105. has = true
  106. return false
  107. }
  108. return true
  109. })
  110. return has
  111. }
  112. type Tunnel struct {
  113. Id int
  114. Port int
  115. ServerIp string
  116. Mode string
  117. Status bool
  118. RunStatus bool
  119. Client *Client
  120. Ports string
  121. Flow *Flow
  122. Password string
  123. Remark string
  124. TargetAddr string
  125. NoStore bool
  126. LocalPath string
  127. StripPre string
  128. Target *Target
  129. Health
  130. sync.RWMutex
  131. }
  132. type Health struct {
  133. HealthCheckTimeout int
  134. HealthMaxFail int
  135. HealthCheckInterval int
  136. HealthNextTime time.Time
  137. HealthMap map[string]int
  138. HttpHealthUrl string
  139. HealthRemoveArr []string
  140. HealthCheckType string
  141. HealthCheckTarget string
  142. sync.RWMutex
  143. }
  144. type Host struct {
  145. Id int
  146. Host string //host
  147. HeaderChange string //header change
  148. HostChange string //host change
  149. Location string //url router
  150. Remark string //remark
  151. Scheme string //http https all
  152. CertFilePath string
  153. KeyFilePath string
  154. NoStore bool
  155. IsClose bool
  156. Flow *Flow
  157. Client *Client
  158. Target *Target //目标
  159. Health `json:"-"`
  160. sync.RWMutex
  161. }
  162. type Target struct {
  163. nowIndex int
  164. TargetStr string
  165. TargetArr []string
  166. LocalProxy bool
  167. sync.RWMutex
  168. }
  169. func (s *Target) GetRandomTarget() (string, error) {
  170. if s.TargetArr == nil {
  171. s.TargetArr = strings.Split(s.TargetStr, "\n")
  172. }
  173. if len(s.TargetArr) == 1 {
  174. return s.TargetArr[0], nil
  175. }
  176. if len(s.TargetArr) == 0 {
  177. return "", errors.New("all inward-bending targets are offline")
  178. }
  179. s.Lock()
  180. defer s.Unlock()
  181. if s.nowIndex >= len(s.TargetArr)-1 {
  182. s.nowIndex = -1
  183. }
  184. s.nowIndex++
  185. return s.TargetArr[s.nowIndex], nil
  186. }