base.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package server
  2. import (
  3. "github.com/cnlh/easyProxy/bridge"
  4. "github.com/cnlh/easyProxy/utils"
  5. "sync"
  6. )
  7. //server base struct
  8. type server struct {
  9. bridge *bridge.Bridge
  10. task *utils.Tunnel
  11. config *utils.Config
  12. sync.Mutex
  13. }
  14. func (s *server) GetTunnelAndWriteHost(connType string, clientId int, cnf *utils.Config, addr string) (link *utils.Conn, err error) {
  15. if link, err = s.bridge.GetTunnel(clientId, cnf.CompressEncode, cnf.CompressDecode, cnf.Crypt, cnf.Mux); err != nil {
  16. return
  17. }
  18. if _, err = link.WriteHost(connType, addr); err != nil {
  19. link.Close()
  20. }
  21. return
  22. }
  23. func (s *server) FlowAdd(in, out int64) {
  24. s.Lock()
  25. defer s.Unlock()
  26. s.task.Flow.ExportFlow += out
  27. s.task.Flow.InletFlow += in
  28. }
  29. func (s *server) FlowAddHost(host *utils.Host, in, out int64) {
  30. s.Lock()
  31. defer s.Unlock()
  32. host.Flow.ExportFlow += out
  33. host.Flow.InletFlow += in
  34. }
  35. //热更新配置
  36. func (s *server) ResetConfig() {
  37. //获取最新数据
  38. task, err := CsvDb.GetTask(s.task.Id)
  39. if err != nil {
  40. return
  41. }
  42. s.task.UseClientCnf = task.UseClientCnf
  43. //使用客户端配置
  44. if s.task.UseClientCnf {
  45. client, err := CsvDb.GetClient(s.task.Client.Id)
  46. if err == nil {
  47. s.config.U = client.Cnf.U
  48. s.config.P = client.Cnf.P
  49. s.config.Compress = client.Cnf.Compress
  50. s.config.Mux = client.Cnf.Mux
  51. s.config.Crypt = client.Cnf.Crypt
  52. }
  53. } else {
  54. if err == nil {
  55. s.config.U = task.Config.U
  56. s.config.P = task.Config.P
  57. s.config.Compress = task.Config.Compress
  58. s.config.Mux = task.Config.Mux
  59. s.config.Crypt = task.Config.Crypt
  60. }
  61. }
  62. s.config.CompressDecode, s.config.CompressEncode = utils.GetCompressType(s.config.Compress)
  63. }