link.go 2.6 KB

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