package tls

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"time"
)

type ForwardItem struct {
	Proxy          string            `json:"proxy,omitempty"`
	Method         string            `json:"method,omitempty"`
	Headers        map[string]string `json:"headers,omitempty"`
	Url            string            `json:"url,omitempty"`
	Data           string            `json:"data,omitempty"`
	Timeout        int               `json:"timeout,omitempty"`
	AllowRedirects bool              `json:"allow_redirects,omitempty"`
	Verify         bool              `json:"verify,omitempty"`
	Debug          bool              `json:"debug"`
	RandomTls      bool              `json:"random_tls"`
}

func ForwardIOS(c *gin.Context) {
	var forWard ForwardItem
	if err := c.ShouldBindJSON(&forWard); err != nil {
		logrus.Error(err.Error())
		c.JSON(400, gin.H{
			"msg": err.Error(),
		})
		return
	}
	fmt.Print(forWard.Url)
	logrus.Infof("开始转发 url: %s  proxy: %s  %v", forWard.Url, forWard.Proxy, forWard.AllowRedirects)
	t1 := time.Now()
	// 开始转发
	client, err := NewClient(forWard, true)
	if err != nil {
		logrus.Error("client初始化失败 ", err.Error())
		c.JSON(400, gin.H{
			"msg": "client初始化失败 " + err.Error(),
		})
		return
	}
	if err := client.Start(); err != nil {
		logrus.Error("请求失败 ", err.Error())
		c.JSON(400, gin.H{
			"msg": "请求失败 " + err.Error(),
		})
		return
	}

	res := gin.H{}
	res["msg"] = "请求成功"
	res["status_code"] = client.GetStatusCode()
	res["proxy"] = forWard.Proxy
	res["headers"] = client.GetResponseHeaders()
	res["cookies"] = client.GetSessionCookies()
	res["cost_time"] = time.Now().Sub(t1).Milliseconds()
	res["url"] = client.GetRespUrl()
	res["text"] = client.GetText()
	defer func() {
		err := client.Response.Body.Close()
		if err != nil {
			fmt.Errorf("text %s", err.Error())
			return
		}
	}()
	client.Client.CloseIdleConnections()
	logrus.Infof("%s 转发 %s %s %d 耗时 %s", client.TlsVersion, forWard.Url, forWard.Proxy, client.GetStatusCode(), time.Now().Sub(t1).String())
	c.JSON(200, res)
}
func Forward(c *gin.Context) {
	var forWard ForwardItem
	if err := c.ShouldBindJSON(&forWard); err != nil {
		logrus.Error(err.Error())
		c.JSON(400, gin.H{
			"msg": err.Error(),
		})
		return
	}
	logrus.Infof("开始转发 url: %s  proxy: %s", forWard.Url, forWard.Proxy)

	t1 := time.Now()
	// 开始转发
	client, err := NewClient(forWard, false)
	if err != nil {
		logrus.Error("client初始化失败 ", err.Error())
		c.JSON(400, gin.H{
			"msg": "client初始化失败 " + err.Error(),
		})
		return
	}
	if err := client.Start(); err != nil {
		logrus.Error("请求失败 ", err.Error())
		c.JSON(400, gin.H{
			"msg": "请求失败 " + err.Error(),
		})
		return
	}

	res := gin.H{}
	res["msg"] = "请求成功"
	res["status_code"] = client.GetStatusCode()
	res["proxy"] = forWard.Proxy
	res["headers"] = client.GetResponseHeaders()
	res["cookies"] = client.GetSessionCookies()
	res["cost_time"] = time.Now().Sub(t1).Milliseconds()
	res["url"] = client.GetRespUrl()
	res["text"] = client.GetText()
	defer func() {
		err := client.Response.Body.Close()
		if err != nil {
			fmt.Errorf("text %s", err.Error())
			return
		}
	}()
	client.Client.CloseIdleConnections()
	logrus.Infof("%s 转发 %s %s %d 耗时 %s", client.TlsVersion, forWard.Url, forWard.Proxy, client.GetStatusCode(), time.Now().Sub(t1).String())
	c.JSON(200, res)
}
