link.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package lib
  2. import (
  3. "net"
  4. "strings"
  5. "sync"
  6. )
  7. type Link struct {
  8. Id int //id
  9. ConnType string //连接类型
  10. Host string //目标
  11. En int //加密
  12. De int //解密
  13. Crypt bool //加密
  14. Conn *Conn
  15. Flow *Flow
  16. UdpListener *net.UDPConn
  17. Rate *Rate
  18. UdpRemoteAddr *net.UDPAddr
  19. }
  20. func NewLink(id int, connType string, host string, en, de int, crypt bool, conn *Conn, flow *Flow, udpListener *net.UDPConn, rate *Rate, UdpRemoteAddr *net.UDPAddr) *Link {
  21. return &Link{
  22. Id: id,
  23. ConnType: connType,
  24. Host: host,
  25. En: en,
  26. De: de,
  27. Crypt: crypt,
  28. Conn: conn,
  29. Flow: flow,
  30. UdpListener: udpListener,
  31. Rate: rate,
  32. UdpRemoteAddr: UdpRemoteAddr,
  33. }
  34. }
  35. type Flow struct {
  36. ExportFlow int64 //出口流量
  37. InletFlow int64 //入口流量
  38. FlowLimit int64 //流量限制,出口+入口 /M
  39. sync.RWMutex
  40. }
  41. func (s *Flow) Add(in, out int) {
  42. s.Lock()
  43. defer s.Unlock()
  44. s.InletFlow += int64(in)
  45. s.ExportFlow += int64(out)
  46. }
  47. type Client struct {
  48. Cnf *Config
  49. Id int //id
  50. VerifyKey string //验证密钥
  51. Addr string //客户端ip地址
  52. Remark string //备注
  53. Status bool //是否开启
  54. IsConnect bool //是否连接
  55. RateLimit int //速度限制 /kb
  56. Flow *Flow //流量
  57. Rate *Rate //速度控制
  58. id int
  59. sync.RWMutex
  60. }
  61. func (s *Client) GetId() int {
  62. s.Lock()
  63. defer s.Unlock()
  64. s.id++
  65. return s.id
  66. }
  67. type Tunnel struct {
  68. Id int //Id
  69. TcpPort int //服务端与客户端通信端口
  70. Mode string //启动方式
  71. Target string //目标
  72. Status bool //是否开启
  73. Client *Client //所属客户端id
  74. Flow *Flow
  75. Config *Config
  76. UseClientCnf bool //是否继承客户端配置
  77. Remark string //备注
  78. }
  79. type Config struct {
  80. U string //socks5验证用户名
  81. P string //socks5验证密码
  82. Compress string //压缩方式
  83. Crypt bool //是否加密
  84. CompressEncode int //加密方式
  85. CompressDecode int //解密方式
  86. }
  87. type Host struct {
  88. Host string //启动方式
  89. Target string //目标
  90. HeaderChange string //host修改
  91. HostChange string //host修改
  92. Flow *Flow
  93. Client *Client
  94. Remark string //备注
  95. NowIndex int
  96. TargetArr []string
  97. sync.RWMutex
  98. }
  99. func (s *Host) GetRandomTarget() string {
  100. if s.TargetArr == nil {
  101. s.TargetArr = strings.Split(s.Target, "\n")
  102. }
  103. s.Lock()
  104. defer s.Unlock()
  105. if s.NowIndex >= len(s.TargetArr)-1 {
  106. s.NowIndex = 0
  107. } else {
  108. s.NowIndex++
  109. }
  110. return s.TargetArr[s.NowIndex]
  111. }
  112. //深拷贝Config
  113. func DeepCopyConfig(c *Config) *Config {
  114. return &Config{
  115. U: c.U,
  116. P: c.P,
  117. Compress: c.Compress,
  118. Crypt: c.Crypt,
  119. CompressEncode: c.CompressEncode,
  120. CompressDecode: c.CompressDecode,
  121. }
  122. }