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 := `

Hello,This is a Jrebel & JetBrains License Server!

License Server started at %s

JRebel 7.1 and earlier version Activation address was: %s/{tokenname}, with any email."

JRebel 2018.1 and later version Activation address was: %s/{guid}(eg: %s/%s ), 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 := "OK" + salt + "" signature, err := signWithMd5([]byte(xmlContent)) if err != nil { w.WriteHeader(403) _, _ = fmt.Fprintf(w, "%s\n", err) } else { body := "\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 := "" + prolongationPeriod + "OK" + salt + "1licensee=" + username + "\tlicenseType=0\t" signature, err := signWithMd5([]byte(xmlContent)) if err != nil { w.WriteHeader(403) _, _ = fmt.Fprintf(w, "%s\n", err) } else { body := "\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 := "OK" + salt + "" signature, err := signWithMd5([]byte(xmlContent)) if err != nil { w.WriteHeader(403) _, _ = fmt.Fprintf(w, "%s\n", err) } else { body := "\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 }