handler.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. newMode, err = strconv.Atoi(newModeStr)
  77. if err != nil {
  78. utils.SendErrorResponse(w, "invalid newMode value: "+newModeStr)
  79. return
  80. }
  81. }
  82. newTimeoutStr, _ := utils.PostPara(r, "timeout")
  83. newTimeout := -1
  84. if newTimeoutStr != "" {
  85. newTimeout, err = strconv.Atoi(newTimeoutStr)
  86. if err != nil {
  87. utils.SendErrorResponse(w, "invalid newTimeout value: "+newTimeoutStr)
  88. return
  89. }
  90. }
  91. // Call the EditConfig method to modify the configuration
  92. err = m.EditConfig(configUUID, newName, newPortA, newPortB, newMode, newTimeout)
  93. if err != nil {
  94. utils.SendErrorResponse(w, err.Error())
  95. return
  96. }
  97. utils.SendOK(w)
  98. }
  99. func (m *Manager) HandleListConfigs(w http.ResponseWriter, r *http.Request) {
  100. js, _ := json.Marshal(m.Configs)
  101. utils.SendJSONResponse(w, string(js))
  102. }
  103. func (m *Manager) HandleGetProxyStatus(w http.ResponseWriter, r *http.Request) {
  104. uuid, err := utils.GetPara(r, "uuid")
  105. if err != nil {
  106. utils.SendErrorResponse(w, "invalid uuid given")
  107. return
  108. }
  109. targetConfig, err := m.GetConfigByUUID(uuid)
  110. if err != nil {
  111. utils.SendErrorResponse(w, err.Error())
  112. return
  113. }
  114. js, _ := json.Marshal(targetConfig)
  115. utils.SendJSONResponse(w, string(js))
  116. }