diff --git a/src/daemon.go b/src/daemon.go index e474f88..2de6cba 100644 --- a/src/daemon.go +++ b/src/daemon.go @@ -26,6 +26,8 @@ func Start() { } command := args[1] switch command { + case "block": + doTask() case "start": daemonStart() case "stop": diff --git a/src/http.go b/src/http.go index 8eb4ec1..186c77e 100644 --- a/src/http.go +++ b/src/http.go @@ -7,6 +7,8 @@ import ( "crypto/rand" "crypto/rsa" "crypto/x509" + "encoding/base64" + "encoding/json" "encoding/pem" "github.com/gin-gonic/gin" "io" @@ -23,12 +25,28 @@ func InitHttpServer(host string, port int) { h := gin.Default() h.GET("/api/v1/refresh", refreshCert) h.GET("/api/v1/cert", getCert) + h.GET("/api/v1/domain/list", domainList) err := h.Run(host + ":" + strconv.Itoa(port)) if err != nil { return } } +func domainList(c *gin.Context) { + token := getToken(c) + domains := GetAppConfig().Domains + data, err := json.Marshal(domains) + if err != nil { + log.Fatal(err) + } + + c.JSON(200, gin.H{ + "code": 200, + "msg": "Success", + "data": encryptResult(string(data), token), + }) +} + func getCert(c *gin.Context) { name := c.Param("name") token := getToken(c) @@ -137,7 +155,7 @@ func encryptResult(content string, token string) string { ciphertext := make([]byte, len(plaintext)) mode.CryptBlocks(ciphertext, plaintext) - return string(ciphertext) + return base64.StdEncoding.EncodeToString(ciphertext) } func pad(src []byte, blockSize int) []byte { diff --git a/src/util.go b/src/util.go index 9e62762..c095db5 100644 --- a/src/util.go +++ b/src/util.go @@ -13,10 +13,11 @@ func GenRsa() (priKey string, pubKey string, err error) { return "", "", err } publicKey := &privateKey.PublicKey - publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) - if err != nil { - return "", "", err - } + publicKeyBytes := x509.MarshalPKCS1PublicKey(publicKey) + //publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) + //if err != nil { + // return "", "", err + //} pubKey = base64.StdEncoding.EncodeToString(publicKeyBytes) //pemBlock := &pem.Block{ // Type: "", diff --git a/test/encrypt_test.go b/test/encrypt_test.go new file mode 100644 index 0000000..5a3191b --- /dev/null +++ b/test/encrypt_test.go @@ -0,0 +1,31 @@ +package test + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "fmt" + "log" + "testing" +) + +func TestGenRsa(t *testing.T) { + privateKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + log.Fatal(err) + } + publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey) + if err != nil { + log.Fatal(err) + } + pubKey := base64.StdEncoding.EncodeToString(publicKeyBytes) + //publicKey := &privateKey.PublicKey + //publicKeyBytes := x509.MarshalPKCS1PublicKey(publicKey) + //pubKey := base64.StdEncoding.EncodeToString(publicKeyBytes) + key := x509.MarshalPKCS1PrivateKey(privateKey) + priKey := base64.StdEncoding.EncodeToString(key) + fmt.Println(pubKey) + fmt.Println() + fmt.Println(priKey) +}