//go:build windows

package yu_hpsocket

import (
	yu_base64 "gogs.qqck.cn/s/tools/base64"
	yu_fast "gogs.qqck.cn/s/tools/fast"
	yu_proxy "gogs.qqck.cn/s/tools/proxy"
	yu_strconv "gogs.qqck.cn/s/tools/strconv"
	yu_sys "gogs.qqck.cn/s/tools/sys"
	"net"
	"net/netip"
)

// SetProxy
//
//	@Description: 设置代理
func (t *Client[T]) SetProxy(info *yu_proxy.Info) {
	t.proxy_info = info
	return
}

// DelProxy
//
//	@Description: 清除代理
func (t *Client[T]) DelProxy() {
	t.proxy_info.Host = ""
	t.proxy_info.User = ""
	t.proxy_info.Pass = ""
	t.proxy_info.Type = 0
	t.proxy_info.Timeout = 0
	return
}

func (t *Client[T]) connect_proxy() bool {
	var j_addr netip.Addr
	var j_err error
	var j_doamin bool
	if j_addr, j_err = netip.ParseAddr(t._RemoteAddress); j_err != nil {
		j_addrs, j_err := net.LookupHost(t._RemoteAddress)
		if j_err != nil || len(j_addrs) == 0 {
			t.Stop()
			return false
		}
		if j_addr, j_err = netip.ParseAddr(j_addrs[0]); j_err != nil {
			t.Stop()
			return false
		}
		j_doamin = true
	}
	if t.proxy_info.IsHttp() {
		j_buf := make([]byte, 0, 256)
		j_buf = append(j_buf, "CONNECT "...)
		if t.proxy_info.Domain {
			j_buf = append(j_buf, t._RemoteAddress...)
		} else if j_addr.IsValid() {
			if j_addr.Is4() {
				j_buf = append(j_buf, j_addr.String()...)
			} else if j_addr.Is6() {
				j_buf = append(j_buf, '[')
				j_buf = append(j_buf, j_addr.String()...)
				j_buf = append(j_buf, ']')
			} else {
				t.Stop()
				return false
			}
		} else {
			t.Stop()
			return false
		}
		j_buf = append(j_buf, ':')
		j_buf = append(j_buf, yu_strconv.FormatInt(t._Port)...)
		j_buf = append(j_buf, " HTTP/1.1\r\n"...)
		j_buf = append(j_buf, "Proxy-Connection: Keep-Alive"...)
		j_buf = append(j_buf, 13, 10)
		if t.proxy_info.IsAuthorization() {
			j_buf = append(j_buf, "Proxy-Authorization: Basic "...)
			j_buf = append(j_buf, yu_base64.Std.EncodeS2S(t.proxy_info.User+":"+t.proxy_info.Pass)...)
			j_buf = append(j_buf, 13, 10)
		}
		j_buf = append(j_buf, 13, 10)
		if !yu_sys.Socket_write_tcp(t.socket, j_buf, 30, 0) {
			t.Stop()
			return false
		}

		if j_buf = yu_sys.Socket_read_tcp(t.socket, 1024, 30, 0); len(j_buf) == 0 {
			t.Stop()
			return false
		}
		// "HTTP/1.1 200 Connection Established"、"HTTP/1.1 200 Connection established"
		// HTTP/1.0 200 OK
		if len(j_buf) < 36 || yu_fast.B2S(j_buf[9:12]) != "200" {
			t.Stop()
			return false
		}
		switch yu_fast.B2S(j_buf[13:35]) {
		case "Connection Established", "Connection established", "connection Established", "connection established":
		default:
			t.Stop()
			return false
		}
		return true
	}
	if t.proxy_info.IsSocks5() {
		// https://zhuanlan.zhihu.com/p/458173597?utm_id=0
		// https://www.jianshu.com/p/97873541510f
		j_buf := make([]byte, 0, 256)
		j_buf = append(j_buf, 5) // VER 协议版本号
		j_buf = append(j_buf, 1) // NMETHODS 客户端支持的认证方法数量
		// X'00' 不需要身份验证(NO AUTHENTICATION REQUIRED)
		// X'01' GSSAPI
		// X'02' 用户密码认证(USERNAME/PASSWORD)
		// X'03' to X'7F' IANA ASSIGNED
		// X'80' to X'FE' RESERVED FOR PRIVATE METHODS
		if t.proxy_info.IsAuthorization() {
			j_buf = append(j_buf, 0x02) // METHODS：认证方法
		} else {
			j_buf = append(j_buf, 0x00) // METHODS：认证方法
		}
		if !yu_sys.Socket_write_tcp(t.socket, j_buf, 30, 0) {
			t.Stop()
			return false
		}
		if j_buf = yu_sys.Socket_read_tcp(t.socket, 1024, 30, 0); len(j_buf) == 0 {
			t.Stop()
			return false
		}
		if len(j_buf) != 2 || j_buf[0] != 5 {
			t.Stop()
			return false
		}
		if j_buf[1] == 0x02 {
			j_buf = append(j_buf[0:0], 1) // VERSION 认证子协商版本（与SOCKS协议版本的0x05无关系）
			j_buf = append(j_buf, byte(len(t.proxy_info.User)))
			j_buf = append(j_buf, t.proxy_info.User...)
			j_buf = append(j_buf, byte(len(t.proxy_info.Pass)))
			j_buf = append(j_buf, t.proxy_info.Pass...)
			if !yu_sys.Socket_write_tcp(t.socket, j_buf, 30, 0) {
				t.Stop()
				return false
			}
			if j_buf = yu_sys.Socket_read_tcp(t.socket, 1024, 30, 0); len(j_buf) == 0 {
				t.Stop()
				return false
			}
			if len(j_buf) != 2 || (j_buf[0] != 1 && j_buf[0] != 5) || j_buf[1] != 0 {
				t.Stop()
				return false
			}
		} else if j_buf[1] != 0x00 {
			t.Stop()
			return false
		}
		j_buf = append(j_buf[0:0], 5) // VER 协议版本号
		j_buf = append(j_buf, 0x01)   // 0x01 CONNECT 连接目标服务器
		j_buf = append(j_buf, 0)      // RSV 保留字段
		if t.proxy_info.Domain && j_doamin {
			j_buf = append(j_buf, 0x03) // 0x03 域名地址(没有打错，就是没有0x02)，域名地址的第1个字节为域名长度，剩下字节为域名名称字节数组
			j_buf = append(j_buf, byte(len(t._RemoteAddress)))
			j_buf = append(j_buf, t._RemoteAddress...)
		} else if j_addr.IsValid() {
			if j_addr.Is4() {
				j_buf = append(j_buf, 0x01) // 0x01 IP V4地址
			} else if j_addr.Is6() {
				j_buf = append(j_buf, 0x04) // 0x04 IP V6地址
			} else {
				t.Stop()
				return false
			}
			j_buf = append(j_buf, j_addr.AsSlice()...)
		} else {
			t.Stop()
			return false
		}
		j_buf = append(j_buf, byte(t._Port>>8), byte(t._Port))
		if !yu_sys.Socket_write_tcp(t.socket, j_buf, 30, 0) {
			t.Stop()
			return false
		}
		if j_buf = yu_sys.Socket_read_tcp(t.socket, 1024, 30, 0); len(j_buf) == 0 {
			t.Stop()
			return false
		}
		if len(j_buf) < 10 || j_buf[0] != 5 || j_buf[1] != 0 || j_buf[2] != 0 {
			t.Stop()
			return false
		}
		return true
	}
	t.Stop()
	return false
}
