kerryzhang 5 gadi atpakaļ
revīzija
88c75e0ec7
6 mainītis faili ar 226 papildinājumiem un 0 dzēšanām
  1. 5 0
      .gitignore
  2. 16 0
      byteutil.go
  3. 57 0
      jrebelprivatekey.go
  4. 18 0
      jrebelsign.go
  5. 125 0
      main.go
  6. 5 0
      uuid.go

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+# Default ignored files
+.idea/
+*.log
+*.exe
+logs/

+ 16 - 0
byteutil.go

@@ -0,0 +1,16 @@
+package main
+
+import (
+	"encoding/base64"
+)
+
+func decodeBase64(str string) (res []byte) {
+	if str == "" {
+		return
+	}
+	res, e := base64.StdEncoding.DecodeString(str)
+	if e != nil {
+		return
+	}
+	return
+}

+ 57 - 0
jrebelprivatekey.go

@@ -0,0 +1,57 @@
+package main
+
+import (
+	"crypto"
+	"crypto/rand"
+	"crypto/rsa"
+	"crypto/x509"
+	"encoding/pem"
+	"fmt"
+	"github.com/pkg/errors"
+)
+
+// pem private key
+const privateKey = `-----BEGIN PRIVATE KEY-----
+MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAND3cI/pKMSd4OLMIXU/8xoEZ/nz
+a+g00Vy7ygyGB1Nn83qpro7tckOvUVILJoN0pKw8J3E8rtjhSyr9849qzaQKBhxFL+J5uu08QVn/
+tMt+Tf0cu5MSPOjT8I2+NWyBZ6H0FjOcVrEUMvHt8sqoJDrDU4pJyex2rCOlpfBeqK6XAgMBAAEC
+gYBM5C+8FIxWxM1CRuCs1yop0aM82vBC0mSTXdo7/3lknGSAJz2/A+o+s50Vtlqmll4drkjJJw4j
+acsR974OcLtXzQrZ0G1ohCM55lC3kehNEbgQdBpagOHbsFa4miKnlYys537Wp+Q61mhGM1weXzos
+gCH/7e/FjJ5uS6DhQc0Y+QJBAP43hlSSEo1BbuanFfp55yK2Y503ti3Rgf1SbE+JbUvIIRsvB24x
+Ha1/IZ+ttkAuIbOUomLN7fyyEYLWphIy9kUCQQDSbqmxZaJNRa1o4ozGRORxR2KBqVn3EVISXqNc
+UH3gAP52U9LcnmA3NMSZs8tzXhUhYkWQ75Q6umXvvDm4XZ0rAkBoymyWGeyJy8oyS/fUW0G63mIr
+oZZ4Rp+F098P3j9ueJ2k/frbImXwabJrhwjUZe/Afel+PxL2ElUDkQW+BMHdAkEAk/U7W4Aanjpf
+s1+Xm9DUztFicciheRa0njXspvvxhY8tXAWUPYseG7L+iRPh+Twtn0t5nm7VynVFN0shSoCIAQJA
+Ljo7A6bzsvfnJpV+lQiOqD/WCw3A2yPwe+1d0X/13fQkgzcbB3K0K81Euo/fkKKiBv0A7yR7wvrN
+jzefE9sKUw==
+-----END PRIVATE KEY-----`
+
+func testSign(data []byte) (signature []byte, err error) {
+	block, _ := pem.Decode([]byte(privateKey))
+	if block == nil {
+		return nil, errors.New("Decode key error, current block is null")
+	}
+	priv, err := x509.ParsePKCS8PrivateKey(block.Bytes)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	h := crypto.Hash.New(crypto.SHA1)
+	h.Write([]byte(data))
+	hashed := h.Sum(nil)
+
+	signature, err = rsa.SignPKCS1v15(rand.Reader, priv.(*rsa.PrivateKey), crypto.SHA1, hashed)
+
+	return
+}
+
+type SHA1withRSA struct {
+	privateKey *rsa.PrivateKey
+}
+
+func sign(privateKey []byte, encryptData []byte) (res []byte, err error) {
+
+	//rsa, err := x509.ParsePKCS8PrivateKey(privateKey)
+
+	return
+}

+ 18 - 0
jrebelsign.go

@@ -0,0 +1,18 @@
+package main
+
+import (
+	"strconv"
+)
+
+//服务端随机数,如果要自己生成,务必将其写到json的serverRandomness中
+const serverRandomness string = "H2ulzLlh7E0="
+
+func toLeaseCreateJson(clientRandomness string, guid string, offline bool, validFrom string, validUntil string) string {
+	var s2 string
+	if offline {
+		s2 = clientRandomness + ";" + serverRandomness + ";" + guid + ";" + strconv.FormatBool(offline) + ";" + validFrom + ";" + validUntil
+	} else {
+		s2 = clientRandomness + ";" + serverRandomness + ";" + guid + ";" + strconv.FormatBool(offline)
+	}
+	return s2
+}

+ 125 - 0
main.go

@@ -0,0 +1,125 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"net/url"
+	"strconv"
+	"time"
+)
+
+func main() {
+	signature, err := testSign([]byte{0, 0, 0, 0})
+	if err != nil {
+		fmt.Println(err)
+	}
+	fmt.Println(signature)
+
+	http.HandleFunc("/", indexHandler)
+	http.HandleFunc("/jrebel/leases", jrebelLeasesHandler)
+	http.HandleFunc("/jrebel/leases/1", jrebelLeases1Handler)
+	http.HandleFunc("/agent/leases", jrebelLeasesHandler)
+	http.HandleFunc("/agent/leases/1", jrebelLeases1Handler)
+	http.HandleFunc("/jrebel/validate-connection", jrebelValidateHandler)
+	http.HandleFunc("/rpc/ping.action", pingHandler)
+	http.HandleFunc("/rpc/obtainTicket.action", obtainTicketHandler)
+	http.HandleFunc("/rpc/releaseTicket.action", releaseTicketHandler)
+
+	_ = http.ListenAndServe(":12345", nil)
+}
+
+func indexHandler(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("content-type", "text/html; charset=utf-8")
+	w.WriteHeader(200)
+	//port := 1000
+	//_, _ = fmt.Fprintf(w, html)
+}
+
+func jrebelLeasesHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Println(len(r.PostForm))
+	fmt.Println(len(r.Form))
+	body, err := ioutil.ReadAll(r.Body)
+	if err != nil {
+		fmt.Println(err)
+	} else {
+		parameter := toHttpBodyParameter(body)
+		randomness := parameter.Get("randomness")
+		username := parameter.Get("username")
+		guid := parameter.Get("guid")
+		if randomness == "" || 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)
+		}
+
+		fmt.Println(validFrom)
+		fmt.Println(validUntil)
+
+	}
+
+	w.Header().Set("content-type", "application/json; charset=utf-8")
+	w.WriteHeader(200)
+
+	fmt.Fprintf(w, "Hello there!\n")
+}
+
+func jrebelLeases1Handler(w http.ResponseWriter, r *http.Request) {
+	fmt.Println("jrebelLeases1Handler")
+
+	randomness := r.Form.Get("randomness")
+	fmt.Println(randomness)
+
+	fmt.Fprintf(w, "Hello there!\n")
+}
+
+func jrebelValidateHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "Hello there!\n")
+}
+func pingHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "Hello there!\n")
+}
+
+func obtainTicketHandler(w http.ResponseWriter, r *http.Request) {
+
+}
+func releaseTicketHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "Hello there!\n")
+}
+
+func toHttpBodyParameter(body []byte) url.Values {
+	s := string(body)
+	ps := url.URL{
+		Scheme:     "",
+		Opaque:     "",
+		User:       nil,
+		Host:       "",
+		Path:       "",
+		RawPath:    "",
+		ForceQuery: false,
+		RawQuery:   s,
+		Fragment:   "",
+	}
+	fmt.Println(s)
+	return ps.Query()
+}

+ 5 - 0
uuid.go

@@ -0,0 +1,5 @@
+package main
+
+func getHex(len int64) {
+
+}