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.

104 lines
1.9 KiB

  1. package main
  2. import (
  3. "os"
  4. "strconv"
  5. "time"
  6. "fmt"
  7. "math/rand"
  8. "encoding/json"
  9. "encoding/base64"
  10. "net/http"
  11. "bytes"
  12. "io/ioutil"
  13. "github.com/vocdoni/dvote-relay/types"
  14. )
  15. func makeBallot() string {
  16. var bal types.Ballot
  17. bal.Type = "ballot0"
  18. pidBytes := make([]byte, 32)
  19. rand.Read(pidBytes)
  20. bal.PID = base64.StdEncoding.EncodeToString(pidBytes)
  21. nullifier := make([]byte, 32)
  22. rand.Read(nullifier)
  23. bal.Nullifier = nullifier
  24. vote := make([]byte, 32)
  25. rand.Read(vote)
  26. bal.Vote = vote
  27. franchise := make([]byte, 32)
  28. rand.Read(franchise)
  29. bal.Franchise = franchise
  30. b, err := json.Marshal(bal)
  31. if err != nil {
  32. fmt.Println(err)
  33. return "error"
  34. }
  35. //todo: add encryption, pow
  36. return string(b)
  37. }
  38. func makeEnvelope(ballot string) string {
  39. var env types.Envelope
  40. env.Type = "envelope0"
  41. env.Nonce = rand.Uint64()
  42. kp := make([]byte, 4)
  43. rand.Read(kp)
  44. env.KeyProof = kp
  45. env.Ballot = []byte(ballot)
  46. env.Timestamp = time.Now()
  47. e, err := json.Marshal(env)
  48. if err != nil {
  49. fmt.Println(err)
  50. return "error"
  51. }
  52. //todo: add encryption, pow
  53. return string(e)
  54. }
  55. func main() {
  56. interval := os.Args[1]
  57. i, _ := strconv.Atoi(interval)
  58. timer := time.NewTicker(time.Millisecond * time.Duration(i))
  59. rand.Seed(time.Now().UnixNano())
  60. url := "http://localhost:8080/submit"
  61. fmt.Println("URL:>", url)
  62. for {
  63. select {
  64. case <- timer.C:
  65. fmt.Println(makeEnvelope(makeBallot()))
  66. var jsonStr = []byte(makeEnvelope(makeBallot()))
  67. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  68. req.Header.Set("X-Custom-Header", "myvalue")
  69. req.Header.Set("Content-Type", "application/json")
  70. client := &http.Client{}
  71. resp, err := client.Do(req)
  72. if err != nil {
  73. panic(err)
  74. }
  75. defer resp.Body.Close()
  76. fmt.Println("response Status:", resp.Status)
  77. fmt.Println("response Headers:", resp.Header)
  78. body, _ := ioutil.ReadAll(resp.Body)
  79. fmt.Println("response Body:", string(body))
  80. default:
  81. continue
  82. }
  83. }
  84. }