You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.9 KiB

  1. package processHttp
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "log"
  8. "github.com/vocdoni/dvote-process/tree"
  9. )
  10. var T tree.Tree
  11. type Claim struct {
  12. ProcessID string `json:"processID"`
  13. ClaimData string `json:"claimData"`
  14. ProofData string `json:"proofData"`
  15. }
  16. type Result struct {
  17. Error bool `json:"error"`
  18. Response string `json:"response"`
  19. }
  20. func reply(resp *Result, w http.ResponseWriter) {
  21. err := json.NewEncoder(w).Encode(resp)
  22. if err != nil {
  23. http.Error(w, err.Error(), 500)
  24. } else {
  25. w.Header().Set("content-type", "application/json")
  26. }
  27. }
  28. func checkRequest(w http.ResponseWriter,req *http.Request) bool {
  29. if req.Body == nil {
  30. http.Error(w, "Please send a request body", 400)
  31. return false
  32. }
  33. return true
  34. }
  35. func claimHandler(w http.ResponseWriter, req *http.Request, op string) {
  36. var c Claim
  37. var resp Result
  38. if ok := checkRequest(w, req); !ok { return }
  39. // Decode JSON
  40. err := json.NewDecoder(req.Body).Decode(&c)
  41. if err != nil {
  42. http.Error(w, err.Error(), 400)
  43. return
  44. }
  45. // Process data
  46. log.Printf("Received: %s,%s,%s ", c.ProcessID, c.ClaimData, c.ProofData)
  47. resp.Error = false
  48. resp.Response = ""
  49. if len(c.ProcessID) > 0 {
  50. T.Namespace = c.ProcessID
  51. } else {
  52. resp.Error = true
  53. resp.Response = "processID is not valid"
  54. reply(&resp, w)
  55. return
  56. }
  57. if len(c.ClaimData) < 0 {
  58. resp.Error = true
  59. resp.Response = "data not valid"
  60. reply(&resp, w)
  61. return
  62. }
  63. if op == "add" {
  64. err = T.AddClaim([]byte(c.ClaimData))
  65. }
  66. if op == "gen" {
  67. resp.Response, err = T.GenProof([]byte(c.ClaimData))
  68. }
  69. if op == "check" {
  70. if len(c.ProofData) < 1 {
  71. resp.Error = true
  72. resp.Response = "proofData not provided"
  73. reply(&resp, w)
  74. return
  75. }
  76. var validProof bool
  77. validProof, err = T.CheckProof([]byte(c.ClaimData), c.ProofData)
  78. if validProof {
  79. resp.Response = "valid"
  80. } else {
  81. resp.Response = "invalid"
  82. }
  83. }
  84. if err != nil {
  85. resp.Error = true
  86. resp.Response = fmt.Sprint(err)
  87. log.Print(err)
  88. reply(&resp, w)
  89. return
  90. }
  91. reply(&resp, w)
  92. }
  93. func Listen(port int, proto string) {
  94. srv := &http.Server{
  95. Addr: fmt.Sprintf(":%d", port),
  96. ReadHeaderTimeout: 4 * time.Second,
  97. ReadTimeout: 4 * time.Second,
  98. WriteTimeout: 4 * time.Second,
  99. IdleTimeout: 3 * time.Second,
  100. }
  101. http.HandleFunc("/addClaim", func(w http.ResponseWriter, r *http.Request) {
  102. claimHandler(w, r, "add")})
  103. http.HandleFunc("/genProof", func(w http.ResponseWriter, r *http.Request) {
  104. claimHandler(w, r, "gen")})
  105. http.HandleFunc("/checkProof", func(w http.ResponseWriter, r *http.Request) {
  106. claimHandler(w, r, "check")})
  107. if proto == "https" {
  108. log.Print("Starting server in https mode")
  109. if err := srv.ListenAndServeTLS("server.crt", "server.key"); err != nil {
  110. panic(err)
  111. }
  112. }
  113. if proto == "http" {
  114. log.Print("Starting server in http mode")
  115. srv.SetKeepAlivesEnabled(false)
  116. if err := srv.ListenAndServe(); err != nil {
  117. panic(err)
  118. }
  119. }
  120. }