handler.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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) HandleListProxyConfigs(w http.ResponseWriter, r *http.Request) {
  64. }