This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.go
107 lines (90 loc) · 2.55 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package akamai
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/golang-jwt/jwt/v5"
jsoniter "github.com/json-iterator/go"
"io"
"net/http"
"time"
)
type SensorInput struct {
Abck string `json:"abck"`
Bmsz string `json:"bmsz"`
Version string `json:"version"`
PageUrl string `json:"pageUrl"`
UserAgent string `json:"userAgent"`
ScriptHash string `json:"scriptHash"`
}
// GenerateSensorData returns the sensor data required to generate valid akamai cookies using the Hyper Solutions API.
func (s *Session) GenerateSensorData(ctx context.Context, input *SensorInput) (string, error) {
const sensorEndpoint = "https://akm.justhyped.dev/sensor"
return s.sendRequest(ctx, sensorEndpoint, input)
}
type PixelInput struct {
UserAgent string `json:"userAgent"`
HTMLVar string `json:"htmlVar"`
ScriptVar string `json:"scriptVar"`
}
// GeneratePixelData returns the pixel data using the Hyper Solutions API.
func (s *Session) GeneratePixelData(ctx context.Context, input *PixelInput) (string, error) {
const pixelEndpoint = "https://akm.justhyped.dev/pixel"
return s.sendRequest(ctx, pixelEndpoint, input)
}
func (s *Session) sendRequest(ctx context.Context, url string, input any) (string, error) {
if s.apiKey == "" {
return "", errors.New("missing api key")
}
payload, err := jsoniter.Marshal(input)
if err != nil {
return "", err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
if err != nil {
return "", err
}
req.Header.Set("content-type", "application/json")
req.Header.Set("accept-encoding", "gzip")
req.Header.Set("x-api-key", s.apiKey)
if s.jwtKey != "" {
signature, err := s.generateSignature()
if err != nil {
return "", err
}
req.Header.Set("x-signature", signature)
}
resp, err := s.client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var response struct {
Payload string `json:"payload"`
Error string `json:"error"`
}
if err := jsoniter.Unmarshal(respBody, &response); err != nil {
return "", err
}
if response.Error != "" {
return "", fmt.Errorf("api returned with: %s", response.Error)
}
return response.Payload, nil
}
func (s *Session) generateSignature() (string, error) {
claims := jwt.MapClaims{
"key": s.apiKey,
"exp": time.Now().Add(time.Second * 15).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, err := token.SignedString([]byte(s.jwtKey))
if err != nil {
return "", err
}
return signedToken, nil
}