1
0

obj.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package file
  2. import (
  3. "github.com/cnlh/nps/lib/rate"
  4. "github.com/pkg/errors"
  5. "strings"
  6. "sync"
  7. "time"
  8. )
  9. type Flow struct {
  10. ExportFlow int64 //出口流量
  11. InletFlow int64 //入口流量
  12. FlowLimit int64 //流量限制,出口+入口 /M
  13. sync.RWMutex
  14. }
  15. func (s *Flow) Add(in, out int64) {
  16. s.Lock()
  17. defer s.Unlock()
  18. s.InletFlow += int64(in)
  19. s.ExportFlow += int64(out)
  20. }
  21. type Client struct {
  22. Cnf *Config
  23. Id int //id
  24. VerifyKey string //验证密钥
  25. Addr string //客户端ip地址
  26. Remark string //备注
  27. Status bool //是否开启
  28. IsConnect bool //是否连接
  29. RateLimit int //速度限制 /kb
  30. Flow *Flow //流量
  31. Rate *rate.Rate //速度控制
  32. NoStore bool
  33. NoDisplay bool
  34. MaxConn int //客户端最大连接数
  35. NowConn int //当前连接数
  36. id int
  37. ConfigConnAllow bool
  38. sync.RWMutex
  39. }
  40. func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
  41. return &Client{
  42. Cnf: new(Config),
  43. Id: 0,
  44. VerifyKey: vKey,
  45. Addr: "",
  46. Remark: "",
  47. Status: true,
  48. IsConnect: false,
  49. RateLimit: 0,
  50. Flow: new(Flow),
  51. Rate: nil,
  52. NoStore: noStore,
  53. RWMutex: sync.RWMutex{},
  54. NoDisplay: noDisplay,
  55. }
  56. }
  57. func (s *Client) CutConn() {
  58. s.Lock()
  59. defer s.Unlock()
  60. s.NowConn++
  61. }
  62. func (s *Client) AddConn() {
  63. s.Lock()
  64. defer s.Unlock()
  65. s.NowConn--
  66. }
  67. func (s *Client) GetConn() bool {
  68. if s.MaxConn == 0 || s.NowConn < s.MaxConn {
  69. s.CutConn()
  70. return true
  71. }
  72. return false
  73. }
  74. //modify the hosts and the tunnels by health information
  75. func (s *Client) ModifyTarget() {
  76. }
  77. func (s *Client) HasTunnel(t *Tunnel) (exist bool) {
  78. GetCsvDb().Tasks.Range(func(key, value interface{}) bool {
  79. v := value.(*Tunnel)
  80. if v.Client.Id == s.Id && v.Port == t.Port {
  81. exist = true
  82. return false
  83. }
  84. return true
  85. })
  86. return
  87. }
  88. func (s *Client) HasHost(h *Host) bool {
  89. var has bool
  90. GetCsvDb().Hosts.Range(func(key, value interface{}) bool {
  91. v := value.(*Host)
  92. if v.Client.Id == s.Id && v.Host == h.Host && h.Location == v.Location {
  93. has = true
  94. return false
  95. }
  96. return true
  97. })
  98. return has
  99. }
  100. type Tunnel struct {
  101. Id int //Id
  102. Port int //服务端监听端口
  103. ServerIp string
  104. Mode string //启动方式
  105. Target string //目标
  106. TargetArr []string //目标
  107. Status bool //设置是否开启
  108. RunStatus bool //当前运行状态
  109. Client *Client //所属客户端id
  110. Ports string //客户端与服务端传递
  111. Flow *Flow
  112. Password string //私密模式密码,唯一
  113. Remark string //备注
  114. TargetAddr string
  115. NoStore bool
  116. LocalPath string
  117. StripPre string
  118. NowIndex int
  119. Health
  120. sync.RWMutex
  121. }
  122. type Health struct {
  123. HealthCheckTimeout int
  124. HealthMaxFail int
  125. HealthCheckInterval int
  126. HealthNextTime time.Time
  127. HealthMap map[string]int
  128. HttpHealthUrl string
  129. HealthRemoveArr []string
  130. HealthCheckType string
  131. HealthCheckTarget string
  132. sync.RWMutex
  133. }
  134. func (s *Tunnel) GetRandomTarget() (string, error) {
  135. if s.TargetArr == nil {
  136. s.TargetArr = strings.Split(s.Target, "\n")
  137. }
  138. if len(s.TargetArr) == 1 {
  139. return s.TargetArr[0], nil
  140. }
  141. if len(s.TargetArr) == 0 {
  142. return "", errors.New("all inward-bending targets are offline")
  143. }
  144. s.Lock()
  145. defer s.Unlock()
  146. if s.NowIndex >= len(s.TargetArr)-1 {
  147. s.NowIndex = -1
  148. }
  149. s.NowIndex++
  150. return s.TargetArr[s.NowIndex], nil
  151. }
  152. type Config struct {
  153. U string //socks5验证用户名
  154. P string //socks5验证密码
  155. Compress bool //压缩方式
  156. Crypt bool //是否加密
  157. }
  158. type Host struct {
  159. Id int
  160. Host string //启动方式
  161. Target string //目标
  162. HeaderChange string //host修改
  163. HostChange string //host修改
  164. Location string //url 路由
  165. Flow *Flow
  166. Client *Client
  167. Remark string //备注
  168. NowIndex int
  169. TargetArr []string
  170. NoStore bool
  171. Scheme string //http https all
  172. IsClose bool
  173. Health
  174. sync.RWMutex
  175. }
  176. func (s *Host) GetRandomTarget() (string, error) {
  177. if s.TargetArr == nil {
  178. s.TargetArr = strings.Split(s.Target, "\n")
  179. }
  180. if len(s.TargetArr) == 1 {
  181. return s.TargetArr[0], nil
  182. }
  183. if len(s.TargetArr) == 0 {
  184. return "", errors.New("all inward-bending targets are offline")
  185. }
  186. s.Lock()
  187. defer s.Unlock()
  188. if s.NowIndex >= len(s.TargetArr)-1 {
  189. s.NowIndex = -1
  190. }
  191. s.NowIndex++
  192. return s.TargetArr[s.NowIndex], nil
  193. }