jrebelhandler.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package main
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "time"
  11. )
  12. func loggingRequest(tag string, r *http.Request) {
  13. fmt.Printf("%s --- %s\n", time.Now(), tag)
  14. }
  15. func indexHandler(w http.ResponseWriter, r *http.Request) {
  16. loggingRequest("indexHandler", r)
  17. host := "http://" + r.Host
  18. w.Header().Set("content-type", "text/html; charset=utf-8")
  19. w.WriteHeader(200)
  20. html := `<h1>Hello,This is a Jrebel & JetBrains License Server!</h1>
  21. <p>License Server started at %s
  22. <p>JRebel 7.1 and earlier version Activation address was: <span style='color:red'>%s/{tokenname}</span>, with any email."
  23. <p>JRebel 2018.1 and later version Activation address was: %s/{guid}(eg:<span style='color:red'> %s/%s </span>), with any email.`
  24. _, _ = fmt.Fprintf(w, html, host, host, host, host, newUUIDV4String())
  25. }
  26. func jrebelLeasesHandler(w http.ResponseWriter, r *http.Request) {
  27. loggingRequest("jrebelLeasesHandler", r)
  28. w.Header().Set("content-type", "application/json; charset=utf-8")
  29. parameter, err := getHttpBodyParameter(r)
  30. if err != nil {
  31. w.WriteHeader(403)
  32. _, _ = fmt.Fprintf(w, "%s\n", err)
  33. return
  34. }
  35. clientRandomness := parameter.Get("randomness")
  36. username := parameter.Get("username")
  37. guid := parameter.Get("guid")
  38. if clientRandomness == "" || username == "" || guid == "" {
  39. w.WriteHeader(403)
  40. _, _ = fmt.Fprint(w)
  41. return
  42. }
  43. offline, err := strconv.ParseBool(parameter.Get("offline"))
  44. if err != nil {
  45. offline = false
  46. }
  47. validFrom := "null"
  48. validUntil := "null"
  49. if offline {
  50. clientTime := parameter.Get("clientTime")
  51. _ = parameter.Get("offlineDays")
  52. startTimeInt, err := strconv.ParseInt(clientTime, 10, 64)
  53. if err != nil {
  54. startTimeInt = int64(time.Now().Second()) * 1000
  55. }
  56. // 过期时间
  57. expTime := int64(180 * 24 * 60 * 60 * 100)
  58. validFrom = clientTime
  59. validUntil = strconv.FormatInt(startTimeInt+expTime, 10)
  60. }
  61. serverRandomness := newServerRandomness()
  62. signature := toLeaseCreateJson(clientRandomness, serverRandomness, guid, offline, validFrom, validUntil)
  63. var responseBody = jRebelLeases
  64. responseBody.ServerRandomness = serverRandomness
  65. responseBody.Signature = signature
  66. responseBody.Company = username
  67. response(w, &responseBody)
  68. }
  69. func jrebelLeases1Handler(w http.ResponseWriter, r *http.Request) {
  70. loggingRequest("jrebelLeases1Handler", r)
  71. w.Header().Set("content-type", "application/json; charset=utf-8")
  72. parameter, err := getHttpBodyParameter(r)
  73. if err != nil {
  74. w.WriteHeader(403)
  75. _, _ = fmt.Fprintf(w, "%s\n", err)
  76. return
  77. }
  78. username := parameter.Get("username")
  79. var responseBody = jrebelLeases1
  80. if username != "" {
  81. responseBody.Company = username
  82. }
  83. response(w, &responseBody)
  84. }
  85. func jrebelValidateHandler(w http.ResponseWriter, r *http.Request) {
  86. loggingRequest("jrebelValidateHandler", r)
  87. w.Header().Add("content-type", "application/json; charset=utf-8")
  88. w.WriteHeader(200)
  89. _, _ = fmt.Fprintf(w, "%s\n", jrebelValidateJson)
  90. }
  91. func pingHandler(w http.ResponseWriter, r *http.Request) {
  92. loggingRequest("pingHandler", r)
  93. w.Header().Add("content-type", "text/html; charset=utf-8")
  94. parameter, err := getHttpBodyParameter(r)
  95. if err != nil {
  96. w.WriteHeader(403)
  97. _, _ = fmt.Fprintf(w, "%s\n", err)
  98. return
  99. }
  100. salt := parameter.Get("salt")
  101. if salt == "" {
  102. w.WriteHeader(403)
  103. _, _ = fmt.Fprint(w)
  104. } else {
  105. xmlContent := "<PingResponse><message></message><responseCode>OK</responseCode><salt>" + salt + "</salt></PingResponse>"
  106. signature, err := signWithMd5([]byte(xmlContent))
  107. if err != nil {
  108. w.WriteHeader(403)
  109. _, _ = fmt.Fprintf(w, "%s\n", err)
  110. } else {
  111. body := "<!-- " + hex.EncodeToString(signature) + " -->\n" + xmlContent
  112. w.WriteHeader(200)
  113. _, _ = fmt.Fprintf(w, "%s\n", body)
  114. }
  115. }
  116. }
  117. func obtainTicketHandler(w http.ResponseWriter, r *http.Request) {
  118. w.Header().Add("content-type", "application/json; charset=utf-8")
  119. parameter, err := getHttpBodyParameter(r)
  120. if err != nil {
  121. responseError(w, err, 403)
  122. return
  123. }
  124. salt := parameter.Get("salt")
  125. username := parameter.Get("userName")
  126. prolongationPeriod := "607875500"
  127. if salt == "" || username == "" {
  128. w.WriteHeader(403)
  129. _, _ = fmt.Fprintln(w)
  130. } else {
  131. w.WriteHeader(200)
  132. xmlContent := "<ObtainTicketResponse><message></message><prolongationPeriod>" + prolongationPeriod + "</prolongationPeriod><responseCode>OK</responseCode><salt>" + salt + "</salt><ticketId>1</ticketId><ticketProperties>licensee=" + username + "\tlicenseType=0\t</ticketProperties></ObtainTicketResponse>"
  133. signature, err := signWithMd5([]byte(xmlContent))
  134. if err != nil {
  135. w.WriteHeader(403)
  136. _, _ = fmt.Fprintf(w, "%s\n", err)
  137. } else {
  138. body := "<!-- " + hex.EncodeToString(signature) + " -->\n" + xmlContent
  139. w.WriteHeader(200)
  140. _, _ = fmt.Fprintf(w, "%s\n", body)
  141. }
  142. }
  143. }
  144. func releaseTicketHandler(w http.ResponseWriter, r *http.Request) {
  145. w.Header().Add("content-type", "text/html; charset=utf-8")
  146. parameter, err := getHttpBodyParameter(r)
  147. if err != nil {
  148. responseError(w, err, 403)
  149. return
  150. }
  151. salt := parameter.Get("salt")
  152. if salt == "" {
  153. w.WriteHeader(403)
  154. _, _ = fmt.Fprintln(w)
  155. } else {
  156. xmlContent := "<ReleaseTicketResponse><message></message><responseCode>OK</responseCode><salt>" + salt + "</salt></ReleaseTicketResponse>"
  157. signature, err := signWithMd5([]byte(xmlContent))
  158. if err != nil {
  159. w.WriteHeader(403)
  160. _, _ = fmt.Fprintf(w, "%s\n", err)
  161. } else {
  162. body := "<!-- " + hex.EncodeToString(signature) + " -->\n" + xmlContent
  163. w.WriteHeader(200)
  164. _, _ = fmt.Fprintf(w, "%s\n", body)
  165. }
  166. }
  167. }
  168. func responseError(w http.ResponseWriter, err error, code int) {
  169. w.WriteHeader(403)
  170. _, _ = fmt.Fprintf(w, "%s\n", err)
  171. }
  172. func response(w http.ResponseWriter, resp interface{}) {
  173. bodyData, err := json.Marshal(&resp)
  174. if err != nil {
  175. w.WriteHeader(403)
  176. _, _ = fmt.Fprintf(w, "%s\n", err)
  177. return
  178. }
  179. w.WriteHeader(200)
  180. _, _ = fmt.Fprintf(w, "%s\n", string(bodyData))
  181. }
  182. func getHttpBodyParameter(r *http.Request) (params url.Values, err error) {
  183. body, err := ioutil.ReadAll(r.Body)
  184. if err != nil {
  185. return
  186. }
  187. s := string(body)
  188. ps := url.URL{
  189. Scheme: "",
  190. Opaque: "",
  191. User: nil,
  192. Host: "",
  193. Path: "",
  194. RawPath: "",
  195. ForceQuery: false,
  196. RawQuery: s,
  197. Fragment: "",
  198. }
  199. fmt.Println(s)
  200. return ps.Query(), err
  201. }