package main

import (
	"bufio"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"flag"
	socialwork_sdk "gitlab.workai.com.cn/chenang/socialwork-sdk"
	"io/ioutil"
	"os"
)

/*func init(){
	var bits int
	flag.IntVar(&bits, "b", 1024, "密钥长度,默认为1024位")
	flag.Parse()

	if err := GenRsaKey(bits); err != nil {
		log.Fatal("密钥文件生成失败!")
	}
	log.Println("密钥文件生成成功!")
}
*/
func main() {
	var key string
	var iv string
	var privateKeyPath string
	var publicKeyPath string
	var ip string
	var bussinessCode string
	var ptCode string
	var reqBody string
	var errFilePath string
	var resultFilePath string
	flag.StringVar(&key, "key", "", "key,用于")
	flag.StringVar(&iv, "iv", "", "iv,加密使用的偏移量")
	flag.StringVar(&privateKeyPath, "privateKeyPath", "", "privateKeyPath,私钥文件所在位置(.pem文件)")
	flag.StringVar(&publicKeyPath, "publicKeyPath", "", "publicKeyPath,平台公钥文件所在位置(.pem文件)")
	flag.StringVar(&ip, "ip", "", "ip,访问地址,如:http://47.110.250.177:20000/")
	flag.StringVar(&bussinessCode, "bussiness_code", "", "bussiness_code,访问接口的bussiness_code参数")
	flag.StringVar(&ptCode, "ptCode", "", "ptCode,访问接口的ptcode参数")
	flag.StringVar(&reqBody, "reqBody", "", "reqBody,需要加密的数据(json格式)")
	flag.StringVar(&errFilePath, "errFilePath", "", "errFilePath,错误信息写入的文件地址")
	flag.StringVar(&resultFilePath, "resultFilePath", "", "resultFilePath,返回资源写入的文件地址")
	flag.Parse()
	if key == "" || iv == "" || privateKeyPath == "" || publicKeyPath == "" || ip == "" || bussinessCode == "" || ptCode == "" || reqBody == "" || resultFilePath == "" || errFilePath == "" {
		os.Exit(1)
	}

	fd, _ := os.OpenFile(errFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
	defer fd.Close()

	publicKey, err := ioutil.ReadFile(publicKeyPath)
	if err != nil {
		write := bufio.NewWriter(fd)
		write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
		write.Flush()
		return
	}
	privateKey, err := ioutil.ReadFile(privateKeyPath)
	if err != nil {
		write := bufio.NewWriter(fd)
		write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
		write.Flush()
		return
	}

	client := socialwork_sdk.NewOrderClient(key, iv, string(privateKey), string(publicKey), ip, ptCode)
	req, err := client.SendRequest(bussinessCode, reqBody)
	if err != nil {
		write := bufio.NewWriter(fd)
		write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
		write.Flush()
		return
	} else {
		file, err := os.Create(resultFilePath)
		defer file.Close()

		if err != nil {
			write := bufio.NewWriter(fd)
			write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
			write.Flush()
			return
		}
		file.WriteString(req)
		return
	}
}

func GenRsaKey(bits int) error {
	// 生成私钥文件
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		return err
	}
	derStream := x509.MarshalPKCS1PrivateKey(privateKey)
	block := &pem.Block{
		Type:  "私钥",
		Bytes: derStream,
	}
	file, err := os.Create("private.pem")
	if err != nil {
		return err
	}
	err = pem.Encode(file, block)
	if err != nil {
		return err
	}
	// 生成公钥文件
	publicKey := &privateKey.PublicKey
	derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
	if err != nil {
		return err
	}
	block = &pem.Block{
		Type:  "公钥",
		Bytes: derPkix,
	}
	file, err = os.Create("public.pem")
	if err != nil {
		return err
	}
	err = pem.Encode(file, block)
	if err != nil {
		return err
	}
	return nil
}