package main

import (
	"fmt"
	http "github.com/bogdanfinn/fhttp"
	"github.com/bogdanfinn/fhttp/http2"
	tls_client "github.com/bogdanfinn/tls-client"
	"github.com/bogdanfinn/tls-client/profiles"
	tls "github.com/bogdanfinn/utls"
	"io"
)

//	var Chrome_117 = profiles.ClientProfile{
//		clientHelloId: ,
//		settings: ,
//		settingsOrder: ,
//		pseudoHeaderOrder: ,
//		connectionFlow: 15663105,
//	}
func main() {
	clientHello := tls.ClientHelloID{
		Client:               "Chrome",
		RandomExtensionOrder: false,
		Version:              "117",
		Seed:                 nil,
		SpecFactory: func() (tls.ClientHelloSpec, error) {
			return tls.ClientHelloSpec{
				CipherSuites: []uint16{
					tls.GREASE_PLACEHOLDER,
					tls.TLS_AES_128_GCM_SHA256,
					tls.TLS_AES_256_GCM_SHA384,
					tls.TLS_CHACHA20_POLY1305_SHA256,
					tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
					tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
					tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
					tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
					tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
					tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
					tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
					tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
					tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
					tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
					tls.TLS_RSA_WITH_AES_128_CBC_SHA,
					tls.TLS_RSA_WITH_AES_256_CBC_SHA,
				},
				CompressionMethods: []uint8{
					tls.CompressionNone,
				},
				Extensions: []tls.TLSExtension{
					&tls.UtlsGREASEExtension{},

					&tls.SNIExtension{},
					&tls.StatusRequestExtension{}, // 5
					&tls.SupportedCurvesExtension{[]tls.CurveID{ // 10
						tls.CurveID(tls.GREASE_PLACEHOLDER),
						tls.X25519,
						tls.CurveP256,
						tls.CurveP384,
					}},
					&tls.SupportedPointsExtension{SupportedPoints: []byte{ // 11
						tls.PointFormatUncompressed,
					}},
					&tls.ALPNExtension{AlpnProtocols: []string{"h2", "http/1.1"}}, // 16
					&tls.StatusRequestV2Extension{},                               //17

					&tls.UtlsExtendedMasterSecretExtension{}, //23
					&tls.SessionTicketExtension{},            //35

					&tls.SignatureAlgorithmsExtension{SupportedSignatureAlgorithms: []tls.SignatureScheme{ //13
						tls.ECDSAWithP256AndSHA256,
						tls.PSSWithSHA256,
						tls.PKCS1WithSHA256,
						tls.ECDSAWithP384AndSHA384,
						tls.PSSWithSHA384,
						tls.PKCS1WithSHA384,
						tls.PSSWithSHA512,
						tls.PKCS1WithSHA512,
					}},
					&tls.SupportedVersionsExtension{[]uint16{ //43
						tls.GREASE_PLACEHOLDER,
						tls.VersionTLS13,
						tls.VersionTLS12,
					}},
					&tls.PSKKeyExchangeModesExtension{[]uint8{ // 45
						tls.PskModeDHE,
					}},
					&tls.SignatureAlgorithmsCertExtension{SupportedSignatureAlgorithms: []tls.SignatureScheme{tls.PKCS1WithSHA256}}, // 50
					&tls.KeyShareExtension{[]tls.KeyShare{ //51
						{Group: tls.CurveID(tls.GREASE_PLACEHOLDER), Data: []byte{0}},
						{Group: tls.X25519},
					}},
					&tls.RenegotiationInfoExtension{Renegotiation: tls.RenegotiateOnceAsClient}, // 65281
					//&tls.PreSharedKeyExtension{},
					//
					//&tls.ALPSExtension{SupportedProtocols: []string{"h2"}}, // 17513
					//
					//&tls.UtlsCompressCertExtension{[]tls.CertCompressionAlgo{ //27
					//	tls.CertCompressionBrotli,
					//}},
					//&tls.SCTExtension{}, // 18
					//
					//&tls.UtlsGREASEExtension{},
					//&tls.UtlsPaddingExtension{GetPaddingLen: tls.BoringPaddingStyle}, // 21
				},
			}, nil
		},
	}
	h2Settings := map[http2.SettingID]uint32{
		http2.SettingHeaderTableSize:   65536,
		http2.SettingEnablePush:        0,
		http2.SettingInitialWindowSize: 6291456,
		http2.SettingMaxHeaderListSize: 262144,
	}
	h2SettingIds := []http2.SettingID{
		http2.SettingHeaderTableSize,
		http2.SettingEnablePush,
		http2.SettingInitialWindowSize,
		http2.SettingMaxHeaderListSize,
	}
	h2Order := []string{
		":method",
		":authority",
		":scheme",
		":path",
	}

	Chrome := profiles.NewClientProfile(clientHello, h2Settings, h2SettingIds, h2Order, 15663105, nil, nil)
	fmt.Println(Chrome)
	url := "https://tls.peet.ws/api/all"
	options := []tls_client.HttpClientOption{
		//tls_client.WithClientProfile(Chrome),
		tls_client.WithClientProfile(profiles.Chrome_117),
		//tls_client.WithForceHttp1(),
		//tls_client.WithCookieJar(jar),
		//tls_client.WithRandomTLSExtensionOrder(),
		//tls_client.WithProxyUrl("http://user:pass@host:port"),
		tls_client.WithCharlesProxy("127.0.0.1", "8890"),
		tls_client.WithInsecureSkipVerify(),
	}

	logger := tls_client.NewNoopLogger()
	client, err := tls_client.NewHttpClient(logger, options...)
	if err != nil {
		panic(client)
	}
	get, err := client.Get(url)
	if err != nil {
		return
	}
	defer get.Body.Close()
	all, err := io.ReadAll(get.Body)
	if err != nil {
		return
	}
	fmt.Print(string(all))
	req, _ := http.NewRequest("GET", "https://new.hongkongairlines.com/hxair/ibe/deeplink/ancillary.do?PT=F&MO=T&SC=Y&ORI=HKG&DES=BJS&DD1=2023-12-30&TA=1&TC=0&TI=0&ICS=T&CUR=HKD&language=en&market=HK&DD2=2024-01-02&FLC=2", nil)
	req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
	req.Header.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
	req.Header.Set("host", "new.hongkongairlines.com")
	req.Header.Set("accept-encoding", "gzip")
	do, err := client.Do(req)
	if err != nil {
		panic(err)
		return
	}
	defer do.Body.Close()
	all, err = io.ReadAll(do.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(all))

}
