tcpprox.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package tcpprox
  2. import (
  3. "errors"
  4. "net"
  5. "sync"
  6. "sync/atomic"
  7. "github.com/google/uuid"
  8. "imuslab.com/zoraxy/mod/database"
  9. )
  10. /*
  11. TCP Proxy
  12. Forward port from one port to another
  13. Also accept active connection and passive
  14. connection
  15. */
  16. const (
  17. ProxyMode_Listen = 0
  18. ProxyMode_Transport = 1
  19. ProxyMode_Starter = 2
  20. ProxyMode_UDP = 3
  21. )
  22. type ProxyRelayOptions struct {
  23. Name string
  24. PortA string
  25. PortB string
  26. Timeout int
  27. Mode int
  28. }
  29. type ProxyRelayConfig struct {
  30. UUID string //A UUIDv4 representing this config
  31. Name string //Name of the config
  32. Running bool //If the service is running
  33. PortA string //Ports A (config depends on mode)
  34. PortB string //Ports B (config depends on mode)
  35. Mode int //Operation Mode
  36. Timeout int //Timeout for connection in sec
  37. stopChan chan bool //Stop channel to stop the listener
  38. aTobAccumulatedByteTransfer atomic.Int64 //Accumulated byte transfer from A to B
  39. bToaAccumulatedByteTransfer atomic.Int64 //Accumulated byte transfer from B to A
  40. parent *Manager `json:"-"`
  41. }
  42. type Options struct {
  43. Database *database.Database
  44. DefaultTimeout int
  45. AccessControlHandler func(net.Conn) bool
  46. }
  47. type Manager struct {
  48. //Config and stores
  49. Options *Options
  50. Configs []*ProxyRelayConfig
  51. //Realtime Statistics
  52. Connections int //currently connected connect counts
  53. UDPClientMap sync.Map //map storing the UDP client-server connections
  54. }
  55. func NewTCProxy(options *Options) *Manager {
  56. options.Database.NewTable("tcprox")
  57. //Load relay configs from db
  58. previousRules := []*ProxyRelayConfig{}
  59. if options.Database.KeyExists("tcprox", "rules") {
  60. options.Database.Read("tcprox", "rules", &previousRules)
  61. }
  62. //Check if the AccessControlHandler is empty. If yes, set it to always allow access
  63. if options.AccessControlHandler == nil {
  64. options.AccessControlHandler = func(conn net.Conn) bool {
  65. //Always allow access
  66. return true
  67. }
  68. }
  69. //Create a new proxy manager for TCP
  70. thisManager := Manager{
  71. Options: options,
  72. Connections: 0,
  73. }
  74. //Inject manager into the rules
  75. for _, rule := range previousRules {
  76. rule.parent = &thisManager
  77. }
  78. thisManager.Configs = previousRules
  79. return &thisManager
  80. }
  81. func (m *Manager) NewConfig(config *ProxyRelayOptions) string {
  82. //Generate two zero value for atomic int64
  83. aAcc := atomic.Int64{}
  84. bAcc := atomic.Int64{}
  85. aAcc.Store(0)
  86. bAcc.Store(0)
  87. //Generate a new config from options
  88. configUUID := uuid.New().String()
  89. thisConfig := ProxyRelayConfig{
  90. UUID: configUUID,
  91. Name: config.Name,
  92. Running: false,
  93. PortA: config.PortA,
  94. PortB: config.PortB,
  95. Mode: config.Mode,
  96. Timeout: config.Timeout,
  97. stopChan: nil,
  98. aTobAccumulatedByteTransfer: aAcc,
  99. bToaAccumulatedByteTransfer: bAcc,
  100. parent: m,
  101. }
  102. m.Configs = append(m.Configs, &thisConfig)
  103. m.SaveConfigToDatabase()
  104. return configUUID
  105. }
  106. func (m *Manager) GetConfigByUUID(configUUID string) (*ProxyRelayConfig, error) {
  107. // Find and return the config with the specified UUID
  108. for _, config := range m.Configs {
  109. if config.UUID == configUUID {
  110. return config, nil
  111. }
  112. }
  113. return nil, errors.New("config not found")
  114. }
  115. // Edit the config based on config UUID, leave empty for unchange fields
  116. func (m *Manager) EditConfig(configUUID string, newName string, newPortA string, newPortB string, newMode int, newTimeout int) error {
  117. // Find the config with the specified UUID
  118. foundConfig, err := m.GetConfigByUUID(configUUID)
  119. if err != nil {
  120. return err
  121. }
  122. // Validate and update the fields
  123. if newName != "" {
  124. foundConfig.Name = newName
  125. }
  126. if newPortA != "" {
  127. foundConfig.PortA = newPortA
  128. }
  129. if newPortB != "" {
  130. foundConfig.PortB = newPortB
  131. }
  132. if newMode != -1 {
  133. if newMode > 2 || newMode < 0 {
  134. return errors.New("invalid mode given")
  135. }
  136. foundConfig.Mode = newMode
  137. }
  138. if newTimeout != -1 {
  139. if newTimeout < 0 {
  140. return errors.New("invalid timeout value given")
  141. }
  142. foundConfig.Timeout = newTimeout
  143. }
  144. /*
  145. err = foundConfig.ValidateConfigs()
  146. if err != nil {
  147. return err
  148. }
  149. */
  150. m.SaveConfigToDatabase()
  151. return nil
  152. }
  153. func (m *Manager) RemoveConfig(configUUID string) error {
  154. // Find and remove the config with the specified UUID
  155. for i, config := range m.Configs {
  156. if config.UUID == configUUID {
  157. m.Configs = append(m.Configs[:i], m.Configs[i+1:]...)
  158. m.SaveConfigToDatabase()
  159. return nil
  160. }
  161. }
  162. return errors.New("config not found")
  163. }
  164. func (m *Manager) SaveConfigToDatabase() {
  165. m.Options.Database.Write("tcprox", "rules", m.Configs)
  166. }