119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io"
|
|
"math/rand"
|
|
"net/http"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
// PopulateKnessetCookies fetches the seed from the given URL, performs the required requests,
|
|
// and populates the provided http.Client's CookieJar with the necessary cookies.
|
|
// Returns the waap_id value if successful, or an error.
|
|
func PopulateKnessetCookies(client *http.Client, url string) (string, error) {
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error fetching URL: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
|
|
re := `seed":"([^"]+)"` // Extract seed from HTML
|
|
matches := regexp.MustCompile(re).FindStringSubmatch(string(body))
|
|
if len(matches) < 2 {
|
|
return "", fmt.Errorf("seed value not found")
|
|
}
|
|
seed := matches[1]
|
|
final := SeedToFinalValue(seed)
|
|
|
|
// Prepare cookie request
|
|
random32 := generateRandomString(32)
|
|
cookiePath := "/7060ac19f50208cbb6b45328ef94140a612ee92387e015594234077b4d1e64f1/" + random32
|
|
zebraKey := "x-zebra-" + generateRandomString(8)
|
|
req, err := http.NewRequest("GET", "https://main.knesset.gov.il"+cookiePath, nil)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error creating cookie request: %w", err)
|
|
}
|
|
req.Header.Set(zebraKey, final)
|
|
resp2, err := client.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("error sending cookie request: %w", err)
|
|
}
|
|
defer resp2.Body.Close()
|
|
|
|
var waapID string
|
|
for k, v := range resp2.Header {
|
|
if k == "Set-Cookie" {
|
|
for _, cookieStr := range v {
|
|
re := regexp.MustCompile(`waap_id=([^;]+)`)
|
|
match := re.FindStringSubmatch(cookieStr)
|
|
if len(match) > 1 {
|
|
waapID = match[1]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Optionally, fetch the original page again to ensure cookies are set
|
|
/*
|
|
req2, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return waapID, fmt.Errorf("error creating second request: %w", err)
|
|
}
|
|
if waapID != "" {
|
|
req2.Header.Set("Cookie", "waap_id="+waapID)
|
|
}
|
|
_, err = client.Do(req2)
|
|
if err != nil {
|
|
return waapID, fmt.Errorf("error fetching page with cookie: %w", err)
|
|
}
|
|
*/
|
|
return waapID, nil
|
|
}
|
|
|
|
func SeedToFinalValue(seed string) string {
|
|
seedWithZero := seed + "0"
|
|
sha1sum := sha1.Sum([]byte(seedWithZero))
|
|
sha1hex := fmt.Sprintf("%x", sha1sum)
|
|
concatData := []string{
|
|
sha1hex,
|
|
"0",
|
|
"-22222222020",
|
|
"disabled",
|
|
seed,
|
|
}
|
|
joined := ""
|
|
for i, v := range concatData {
|
|
if i > 0 {
|
|
joined += ";$(hash);_xcalc(arguments.calle);"
|
|
}
|
|
joined += v
|
|
}
|
|
b64 := base64.StdEncoding.EncodeToString([]byte(joined))
|
|
b64 = regexp.MustCompile("=").ReplaceAllString(b64, "-")
|
|
return b64
|
|
}
|
|
|
|
// generateRandomString returns a random string of the given length (default 4)
|
|
func generateRandomString(length ...int) string {
|
|
l := 4
|
|
if len(length) > 0 && length[0] > 0 {
|
|
l = length[0]
|
|
}
|
|
chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
result := make([]byte, l)
|
|
rand.Seed(time.Now().UnixNano())
|
|
for i := 0; i < l; i++ {
|
|
result[i] = chars[rand.Intn(len(chars))]
|
|
}
|
|
return string(result)
|
|
}
|