obj.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package file
  2. import (
  3. "strings"
  4. "sync"
  5. "sync/atomic"
  6. "time"
  7. "ehang.io/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. Version string
  48. sync.RWMutex
  49. }
  50. func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
  51. return &Client{
  52. Cnf: new(Config),
  53. Id: 0,
  54. VerifyKey: vKey,
  55. Addr: "",
  56. Remark: "",
  57. Status: true,
  58. IsConnect: false,
  59. RateLimit: 0,
  60. Flow: new(Flow),
  61. Rate: nil,
  62. NoStore: noStore,
  63. RWMutex: sync.RWMutex{},
  64. NoDisplay: noDisplay,
  65. }
  66. }
  67. func (s *Client) CutConn() {
  68. atomic.AddInt32(&s.NowConn, 1)
  69. }
  70. func (s *Client) AddConn() {
  71. atomic.AddInt32(&s.NowConn, -1)
  72. }
  73. func (s *Client) GetConn() bool {
  74. if s.MaxConn == 0 || int(s.NowConn) < s.MaxConn {
  75. s.CutConn()
  76. return true
  77. }
  78. return false
  79. }
  80. func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
  81. GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
  82. v := value.(*Tunnel)
  83. if v.Client.Id == s.Id && v.Port == t.Port && t.Port != 0 {
  84. exist = true
  85. return false
  86. }
  87. return true
  88. })
  89. return
  90. }
  91. func (s *Client) GetTunnelNum() (num int) {
  92. GetDb().JsonDb.Tasks.Range(func(key, value interface{}) bool {
  93. v := value.(*Tunnel)
  94. if v.Client.Id == s.Id {
  95. num++
  96. }
  97. return true
  98. })
  99. return
  100. }
  101. func (s *Client) HasHost(h *Host) bool {
  102. var has bool
  103. GetDb().JsonDb.Hosts.Range(func(key, value interface{}) bool {
  104. v := value.(*Host)
  105. if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
  106. has = true
  107. return false
  108. }
  109. return true
  110. })
  111. return has
  112. }
  113. type Tunnel struct {
  114. Id int
  115. Port int
  116. ServerIp string
  117. Mode string
  118. Status bool
  119. RunStatus bool
  120. Client *Client
  121. Ports string
  122. Flow *Flow
  123. Password string
  124. Remark string
  125. TargetAddr string
  126. NoStore bool
  127. LocalPath string
  128. StripPre string
  129. Target *Target
  130. MultiAccount *MultiAccount
  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. type MultiAccount struct {
  172. AccountMap map[string]string // multi account and pwd
  173. }
  174. func (s *Target) GetRandomTarget() (string, error) {
  175. if s.TargetArr == nil {
  176. s.TargetArr = strings.Split(s.TargetStr, "\n")
  177. }
  178. if len(s.TargetArr) == 1 {
  179. return s.TargetArr[0], nil
  180. }
  181. if len(s.TargetArr) == 0 {
  182. return "", errors.New("all inward-bending targets are offline")
  183. }
  184. s.Lock()
  185. defer s.Unlock()
  186. if s.nowIndex >= len(s.TargetArr)-1 {
  187. s.nowIndex = -1
  188. }
  189. s.nowIndex++
  190. return s.TargetArr[s.nowIndex], nil
  191. }