handler.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package tcpprox
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "imuslab.com/zoraxy/mod/utils"
  7. )
  8. /*
  9. Handler.go
  10. Handlers for the tcprox. Remove this file
  11. if your application do not need any http
  12. handler.
  13. */
  14. func (m *Manager) HandleAddProxyConfig(w http.ResponseWriter, r *http.Request) {
  15. name, err := utils.PostPara(r, "name")
  16. if err != nil {
  17. utils.SendErrorResponse(w, "name cannot be empty")
  18. return
  19. }
  20. portA, err := utils.PostPara(r, "porta")
  21. if err != nil {
  22. utils.SendErrorResponse(w, "first address cannot be empty")
  23. return
  24. }
  25. portB, err := utils.PostPara(r, "portb")
  26. if err != nil {
  27. utils.SendErrorResponse(w, "second address cannot be empty")
  28. return
  29. }
  30. timeoutStr, _ := utils.PostPara(r, "timeout")
  31. timeout := m.Options.DefaultTimeout
  32. if timeoutStr != "" {
  33. timeout, err = strconv.Atoi(timeoutStr)
  34. if err != nil {
  35. utils.SendErrorResponse(w, "invalid timeout value: "+timeoutStr)
  36. return
  37. }
  38. }
  39. modeValue := ProxyMode_Transport
  40. mode, err := utils.PostPara(r, "mode")
  41. if err != nil || mode == "" {
  42. utils.SendErrorResponse(w, "no mode given")
  43. } else if mode == "listen" {
  44. modeValue = ProxyMode_Listen
  45. } else if mode == "transport" {
  46. modeValue = ProxyMode_Transport
  47. } else if mode == "starter" {
  48. modeValue = ProxyMode_Starter
  49. } else {
  50. utils.SendErrorResponse(w, "invalid mode given. Only support listen / transport / starter")
  51. }
  52. //Create the target config
  53. newConfigUUID := m.NewConfig(&ProxyRelayOptions{
  54. Name: name,
  55. PortA: portA,
  56. PortB: portB,
  57. Timeout: timeout,
  58. Mode: modeValue,
  59. })
  60. js, _ := json.Marshal(newConfigUUID)
  61. utils.SendJSONResponse(w, string(js))
  62. }
  63. func (m *Manager) HandleEditProxyConfigs(w http.ResponseWriter, r *http.Request) {
  64. // Extract POST parameters using utils.PostPara
  65. configUUID, err := utils.PostPara(r, "uuid")
  66. if err != nil {
  67. utils.SendErrorResponse(w, "config UUID cannot be empty")
  68. return
  69. }
  70. newName, _ := utils.PostPara(r, "name")
  71. newPortA, _ := utils.PostPara(r, "porta")
  72. newPortB, _ := utils.PostPara(r, "portb")
  73. newModeStr, _ := utils.PostPara(r, "mode")
  74. newMode := -1
  75. if newModeStr != "" {
  76. if newModeStr == "listen" {
  77. newMode = 0
  78. } else if newModeStr == "transport" {
  79. newMode = 1
  80. } else if newModeStr == "starter" {
  81. newMode = 2
  82. } else {
  83. utils.SendErrorResponse(w, "invalid new mode value")
  84. return
  85. }
  86. }
  87. newTimeoutStr, _ := utils.PostPara(r, "timeout")
  88. newTimeout := -1
  89. if newTimeoutStr != "" {
  90. newTimeout, err = strconv.Atoi(newTimeoutStr)
  91. if err != nil {
  92. utils.SendErrorResponse(w, "invalid newTimeout value: "+newTimeoutStr)
  93. return
  94. }
  95. }
  96. // Call the EditConfig method to modify the configuration
  97. err = m.EditConfig(configUUID, newName, newPortA, newPortB, newMode, newTimeout)
  98. if err != nil {
  99. utils.SendErrorResponse(w, err.Error())
  100. return
  101. }
  102. utils.SendOK(w)
  103. }
  104. func (m *Manager) HandleListConfigs(w http.ResponseWriter, r *http.Request) {
  105. js, _ := json.Marshal(m.Configs)
  106. utils.SendJSONResponse(w, string(js))
  107. }
  108. func (m *Manager) HandleGetProxyStatus(w http.ResponseWriter, r *http.Request) {
  109. uuid, err := utils.GetPara(r, "uuid")
  110. if err != nil {
  111. utils.SendErrorResponse(w, "invalid uuid given")
  112. return
  113. }
  114. targetConfig, err := m.GetConfigByUUID(uuid)
  115. if err != nil {
  116. utils.SendErrorResponse(w, err.Error())
  117. return
  118. }
  119. js, _ := json.Marshal(targetConfig)
  120. utils.SendJSONResponse(w, string(js))
  121. }
  122. func (m *Manager) HandleConfigValidate(w http.ResponseWriter, r *http.Request) {
  123. uuid, err := utils.GetPara(r, "uuid")
  124. if err != nil {
  125. utils.SendErrorResponse(w, "invalid uuid given")
  126. return
  127. }
  128. targetConfig, err := m.GetConfigByUUID(uuid)
  129. if err != nil {
  130. utils.SendErrorResponse(w, err.Error())
  131. return
  132. }
  133. err = targetConfig.ValidateConfigs()
  134. if err != nil {
  135. utils.SendErrorResponse(w, err.Error())
  136. return
  137. }
  138. utils.SendOK(w)
  139. }