obj.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package file
  2. import (
  3. "github.com/cnlh/nps/lib/rate"
  4. "strings"
  5. "sync"
  6. )
  7. type Flow struct {
  8. ExportFlow int64 //出口流量
  9. InletFlow int64 //入口流量
  10. FlowLimit int64 //流量限制,出口+入口 /M
  11. sync.RWMutex
  12. }
  13. func (s *Flow) Add(in, out int) {
  14. s.Lock()
  15. defer s.Unlock()
  16. s.InletFlow += int64(in)
  17. s.ExportFlow += int64(out)
  18. }
  19. type Client struct {
  20. Cnf *Config
  21. Id int //id
  22. VerifyKey string //验证密钥
  23. Addr string //客户端ip地址
  24. Remark string //备注
  25. Status bool //是否开启
  26. IsConnect bool //是否连接
  27. RateLimit int //速度限制 /kb
  28. Flow *Flow //流量
  29. Rate *rate.Rate //速度控制
  30. NoStore bool
  31. NoDisplay bool
  32. id int
  33. sync.RWMutex
  34. }
  35. func NewClient(vKey string, noStore bool, noDisplay bool) *Client {
  36. return &Client{
  37. Cnf: new(Config),
  38. Id: 0,
  39. VerifyKey: vKey,
  40. Addr: "",
  41. Remark: "",
  42. Status: true,
  43. IsConnect: false,
  44. RateLimit: 0,
  45. Flow: new(Flow),
  46. Rate: nil,
  47. NoStore: noStore,
  48. id: GetCsvDb().GetClientId(),
  49. RWMutex: sync.RWMutex{},
  50. NoDisplay: noDisplay,
  51. }
  52. }
  53. func (s *Client) GetId() int {
  54. s.Lock()
  55. defer s.Unlock()
  56. s.id++
  57. return s.id
  58. }
  59. type Tunnel struct {
  60. Id int //Id
  61. Port int //服务端监听端口
  62. Mode string //启动方式
  63. Target string //目标
  64. Status bool //设置是否开启
  65. RunStatus bool //当前运行状态
  66. Client *Client //所属客户端id
  67. Ports string //客户端与服务端传递
  68. Flow *Flow
  69. Remark string //备注
  70. NoStore bool
  71. }
  72. type Config struct {
  73. U string //socks5验证用户名
  74. P string //socks5验证密码
  75. Compress string //压缩方式
  76. Crypt bool //是否加密
  77. CompressEncode int //加密方式
  78. CompressDecode int //解密方式
  79. }
  80. type Host struct {
  81. Id int
  82. Host string //启动方式
  83. Target string //目标
  84. HeaderChange string //host修改
  85. HostChange string //host修改
  86. Location string //url 路由
  87. Flow *Flow
  88. Client *Client
  89. Remark string //备注
  90. NowIndex int
  91. TargetArr []string
  92. NoStore bool
  93. sync.RWMutex
  94. }
  95. func (s *Host) GetRandomTarget() string {
  96. if s.TargetArr == nil {
  97. s.TargetArr = strings.Split(s.Target, "\n")
  98. }
  99. s.Lock()
  100. defer s.Unlock()
  101. if s.NowIndex >= len(s.TargetArr)-1 {
  102. s.NowIndex = 0
  103. } else {
  104. s.NowIndex++
  105. }
  106. return s.TargetArr[s.NowIndex]
  107. }
  108. //深拷贝Config
  109. func DeepCopyConfig(c *Config) *Config {
  110. return &Config{
  111. U: c.U,
  112. P: c.P,
  113. Compress: c.Compress,
  114. Crypt: c.Crypt,
  115. CompressEncode: c.CompressEncode,
  116. CompressDecode: c.CompressDecode,
  117. }
  118. }