obj.go 4.3 KB

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