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")
	flag.StringVar(&publicKeyPath, "publicKeyPath", "", "publicKeyPath")
	flag.StringVar(&ip, "ip", "", "ip")
	flag.StringVar(&bussinessCode, "bussiness_code", "", "bussiness_code")
	flag.StringVar(&ptCode, "ptCode", "", "ptCode")
	flag.StringVar(&reqBody, "reqBody", "", "reqBody")
	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
}