123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- package main
- import (
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- )
- func loggingRequest(tag string, r *http.Request) {
- fmt.Printf("%s --- %s\n", time.Now(), tag)
- }
- // 前面有代理,获取不到正确的域名的情况
- func createIndexHandler(reverseHost string) func(http.ResponseWriter, *http.Request) {
- return func(w http.ResponseWriter, r *http.Request) {
- loggingRequest("indexHandler", r)
- var host string
- if len(reverseHost) > 0 {
- host = reverseHost
- } else {
- host = "http://" + r.Host + r.RequestURI
- }
- host = strings.TrimSuffix(host, "/")
- w.Header().Set("content-type", "text/html; charset=utf-8")
- w.WriteHeader(200)
- html := `<h1>Hello,This is a Jrebel & JetBrains License Server!</h1>
- <p>License Server started at %s
- <p>JRebel 7.1 and earlier version Activation address was: <span style='color:red'>%s/{tokenname}</span>, with any email."
- <p>JRebel 2018.1 and later version Activation address was: %s/{guid}(eg:<span style='color:red'> %s/%s </span>), with any email.`
- _, _ = fmt.Fprintf(w, html, host, host, host, host, newUUIDV4String())
- }
- }
- func jrebelLeasesHandler(w http.ResponseWriter, r *http.Request) {
- loggingRequest("jrebelLeasesHandler", r)
- w.Header().Set("content-type", "application/json; charset=utf-8")
- parameter, err := getHttpBodyParameter(r)
- if err != nil {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- return
- }
- clientRandomness := parameter.Get("randomness")
- username := parameter.Get("username")
- guid := parameter.Get("guid")
- if clientRandomness == "" || username == "" || guid == "" {
- w.WriteHeader(403)
- _, _ = fmt.Fprint(w)
- return
- }
- offline, err := strconv.ParseBool(parameter.Get("offline"))
- if err != nil {
- offline = false
- }
- validFrom := "null"
- validUntil := "null"
- if offline {
- clientTime := parameter.Get("clientTime")
- _ = parameter.Get("offlineDays")
- startTimeInt, err := strconv.ParseInt(clientTime, 10, 64)
- if err != nil {
- startTimeInt = int64(time.Now().Second()) * 1000
- }
- // 过期时间
- expTime := int64(180 * 24 * 60 * 60 * 100)
- validFrom = clientTime
- validUntil = strconv.FormatInt(startTimeInt+expTime, 10)
- }
- serverRandomness := newServerRandomness()
- signature := toLeaseCreateJson(clientRandomness, serverRandomness, guid, offline, validFrom, validUntil)
- var responseBody = jRebelLeases
- responseBody.ServerRandomness = serverRandomness
- responseBody.Signature = signature
- responseBody.Company = username
- response(w, &responseBody)
- }
- func jrebelLeases1Handler(w http.ResponseWriter, r *http.Request) {
- loggingRequest("jrebelLeases1Handler", r)
- w.Header().Set("content-type", "application/json; charset=utf-8")
- parameter, err := getHttpBodyParameter(r)
- if err != nil {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- return
- }
- username := parameter.Get("username")
- var responseBody = jrebelLeases1
- if username != "" {
- responseBody.Company = username
- }
- response(w, &responseBody)
- }
- func jrebelValidateHandler(w http.ResponseWriter, r *http.Request) {
- loggingRequest("jrebelValidateHandler", r)
- w.Header().Add("content-type", "application/json; charset=utf-8")
- w.WriteHeader(200)
- _, _ = fmt.Fprintf(w, "%s\n", jrebelValidateJson)
- }
- func pingHandler(w http.ResponseWriter, r *http.Request) {
- loggingRequest("pingHandler", r)
- w.Header().Add("content-type", "text/html; charset=utf-8")
- parameter, err := getHttpBodyParameter(r)
- if err != nil {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- return
- }
- salt := parameter.Get("salt")
- if salt == "" {
- w.WriteHeader(403)
- _, _ = fmt.Fprint(w)
- } else {
- xmlContent := "<PingResponse><message></message><responseCode>OK</responseCode><salt>" + salt + "</salt></PingResponse>"
- signature, err := signWithMd5([]byte(xmlContent))
- if err != nil {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- } else {
- body := "<!-- " + hex.EncodeToString(signature) + " -->\n" + xmlContent
- w.WriteHeader(200)
- _, _ = fmt.Fprintf(w, "%s\n", body)
- }
- }
- }
- func obtainTicketHandler(w http.ResponseWriter, r *http.Request) {
- w.Header().Add("content-type", "application/json; charset=utf-8")
- parameter, err := getHttpBodyParameter(r)
- if err != nil {
- responseError(w, err, 403)
- return
- }
- salt := parameter.Get("salt")
- username := parameter.Get("userName")
- prolongationPeriod := "607875500"
- if salt == "" || username == "" {
- w.WriteHeader(403)
- _, _ = fmt.Fprintln(w)
- } else {
- w.WriteHeader(200)
- xmlContent := "<ObtainTicketResponse><message></message><prolongationPeriod>" + prolongationPeriod + "</prolongationPeriod><responseCode>OK</responseCode><salt>" + salt + "</salt><ticketId>1</ticketId><ticketProperties>licensee=" + username + "\tlicenseType=0\t</ticketProperties></ObtainTicketResponse>"
- signature, err := signWithMd5([]byte(xmlContent))
- if err != nil {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- } else {
- body := "<!-- " + hex.EncodeToString(signature) + " -->\n" + xmlContent
- w.WriteHeader(200)
- _, _ = fmt.Fprintf(w, "%s\n", body)
- }
- }
- }
- func releaseTicketHandler(w http.ResponseWriter, r *http.Request) {
- w.Header().Add("content-type", "text/html; charset=utf-8")
- parameter, err := getHttpBodyParameter(r)
- if err != nil {
- responseError(w, err, 403)
- return
- }
- salt := parameter.Get("salt")
- if salt == "" {
- w.WriteHeader(403)
- _, _ = fmt.Fprintln(w)
- } else {
- xmlContent := "<ReleaseTicketResponse><message></message><responseCode>OK</responseCode><salt>" + salt + "</salt></ReleaseTicketResponse>"
- signature, err := signWithMd5([]byte(xmlContent))
- if err != nil {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- } else {
- body := "<!-- " + hex.EncodeToString(signature) + " -->\n" + xmlContent
- w.WriteHeader(200)
- _, _ = fmt.Fprintf(w, "%s\n", body)
- }
- }
- }
- func responseError(w http.ResponseWriter, err error, code int) {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- }
- func response(w http.ResponseWriter, resp interface{}) {
- bodyData, err := json.Marshal(&resp)
- if err != nil {
- w.WriteHeader(403)
- _, _ = fmt.Fprintf(w, "%s\n", err)
- return
- }
- w.WriteHeader(200)
- _, _ = fmt.Fprintf(w, "%s\n", string(bodyData))
- }
- func getHttpBodyParameter(r *http.Request) (params url.Values, err error) {
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return
- }
- s := string(body)
- ps := url.URL{
- Scheme: "",
- Opaque: "",
- User: nil,
- Host: "",
- Path: "",
- RawPath: "",
- ForceQuery: false,
- RawQuery: s,
- Fragment: "",
- }
- fmt.Println(s)
- return ps.Query(), err
- }
|