main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. )
  10. func main() {
  11. signature, err := testSign([]byte{0, 0, 0, 0})
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. fmt.Println(signature)
  16. http.HandleFunc("/", indexHandler)
  17. http.HandleFunc("/jrebel/leases", jrebelLeasesHandler)
  18. http.HandleFunc("/jrebel/leases/1", jrebelLeases1Handler)
  19. http.HandleFunc("/agent/leases", jrebelLeasesHandler)
  20. http.HandleFunc("/agent/leases/1", jrebelLeases1Handler)
  21. http.HandleFunc("/jrebel/validate-connection", jrebelValidateHandler)
  22. http.HandleFunc("/rpc/ping.action", pingHandler)
  23. http.HandleFunc("/rpc/obtainTicket.action", obtainTicketHandler)
  24. http.HandleFunc("/rpc/releaseTicket.action", releaseTicketHandler)
  25. _ = http.ListenAndServe(":12345", nil)
  26. }
  27. func indexHandler(w http.ResponseWriter, r *http.Request) {
  28. w.Header().Set("content-type", "text/html; charset=utf-8")
  29. w.WriteHeader(200)
  30. //port := 1000
  31. //_, _ = fmt.Fprintf(w, html)
  32. }
  33. func jrebelLeasesHandler(w http.ResponseWriter, r *http.Request) {
  34. fmt.Println(len(r.PostForm))
  35. fmt.Println(len(r.Form))
  36. body, err := ioutil.ReadAll(r.Body)
  37. if err != nil {
  38. fmt.Println(err)
  39. } else {
  40. parameter := toHttpBodyParameter(body)
  41. randomness := parameter.Get("randomness")
  42. username := parameter.Get("username")
  43. guid := parameter.Get("guid")
  44. if randomness == "" || username == "" || guid == "" {
  45. w.WriteHeader(403)
  46. _, _ = fmt.Fprint(w)
  47. return
  48. }
  49. offline, err := strconv.ParseBool(parameter.Get("offline"))
  50. if err != nil {
  51. offline = false
  52. }
  53. validFrom := "null"
  54. validUntil := "null"
  55. if offline {
  56. clientTime := parameter.Get("clientTime")
  57. _ = parameter.Get("offlineDays")
  58. startTimeInt, err := strconv.ParseInt(clientTime, 10, 64)
  59. if err != nil {
  60. startTimeInt = int64(time.Now().Second()) * 1000
  61. }
  62. // 过期时间
  63. expTime := int64(180 * 24 * 60 * 60 * 100)
  64. validFrom = clientTime
  65. validUntil = strconv.FormatInt(startTimeInt+expTime, 10)
  66. }
  67. fmt.Println(validFrom)
  68. fmt.Println(validUntil)
  69. }
  70. w.Header().Set("content-type", "application/json; charset=utf-8")
  71. w.WriteHeader(200)
  72. fmt.Fprintf(w, "Hello there!\n")
  73. }
  74. func jrebelLeases1Handler(w http.ResponseWriter, r *http.Request) {
  75. fmt.Println("jrebelLeases1Handler")
  76. randomness := r.Form.Get("randomness")
  77. fmt.Println(randomness)
  78. fmt.Fprintf(w, "Hello there!\n")
  79. }
  80. func jrebelValidateHandler(w http.ResponseWriter, r *http.Request) {
  81. fmt.Fprintf(w, "Hello there!\n")
  82. }
  83. func pingHandler(w http.ResponseWriter, r *http.Request) {
  84. fmt.Fprintf(w, "Hello there!\n")
  85. }
  86. func obtainTicketHandler(w http.ResponseWriter, r *http.Request) {
  87. }
  88. func releaseTicketHandler(w http.ResponseWriter, r *http.Request) {
  89. fmt.Fprintf(w, "Hello there!\n")
  90. }
  91. func toHttpBodyParameter(body []byte) url.Values {
  92. s := string(body)
  93. ps := url.URL{
  94. Scheme: "",
  95. Opaque: "",
  96. User: nil,
  97. Host: "",
  98. Path: "",
  99. RawPath: "",
  100. ForceQuery: false,
  101. RawQuery: s,
  102. Fragment: "",
  103. }
  104. fmt.Println(s)
  105. return ps.Query()
  106. }