jrebelhandler.go 6.5 KB

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