aboutsummaryrefslogtreecommitdiffstats
path: root/gosrc/requests.go
blob: 9110c8948c888563b7e31cec1c174cfdd1751466 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
//   SMITH            (  //       /
//   requests          ( )/       /
//   by salade         )(/        /
//  ________________  ( /)        /
// ()__)____________)))))   :^}   /

package main

import (
	"encoding/json"
	"io"
	"log"
	"net/http"
	"strings"
)

// #include "../csrc/smith.h"
// #cgo LDFLAGS: -lsmith -L../
import "C"

func sendRequest(node_name string, config Config) (*http.Response, error) {
	endpoint := getRequestType(node_name)
	if len(endpoint) == 0 {
		return nil, nil
	}

	// create payload
	body := makeBody(endpoint)
	signature := signRequest(body, config.Secret_key)
	url := "https://" + config.Mirror + "/" + endpoint + "?" + body + "&signature=" + signature
	// make a request out of it
	req, err := http.NewRequest("GET", url, nil)
	req.Header.Add("X-MBX-APIKEY", config.Public_key)
	if err != nil {
		log.Fatal("error: creating http request ", err)
	}

	// send it
	client := &http.Client{}
	response, err := client.Do(req)
	if err != nil {
		log.Fatal("error: making http request ", err)
		return response, err
	}
	return response, nil
}

// return request path from tree node name
func getRequestType(name string) string {
	switch (name) {
		case "Account Status":
			return GET_Targets.status
		case "Available Coins":
			return GET_Targets.getall
		case "Deposit Address":
			return GET_Targets.address
		case "Daily Snapshot":
			return GET_Targets.snapshot
		default :
			return ""
	}
}

func displayResponse(node_name string, response *http.Response, output *string) {
	requestType := getRequestType(node_name)
	body, _ := io.ReadAll(response.Body)
	var outputFormatted string

	switch (requestType) {
		case GET_Targets.status:
			var structuredRep AccountStatusResponse
			json.Unmarshal(body, &structuredRep)
			outputFormatted =
				"Status     [" + structuredRep.Data + "]\n"
		case GET_Targets.address:
			var structuredRep DepositAddressResponse
			json.Unmarshal(body, &structuredRep)
			outputFormatted =
				"Coin     [" + structuredRep.Coin + "]\n" +
				"Address  [" + structuredRep.Address + "]\n" +
				"Url      [" + structuredRep.Url + "]\n" +
				"Tag      [" + structuredRep.Tag + "]\n"
		default:
			outputFormatted = string(body)
	}
	*output = outputFormatted
}

// Sign payload using openssl
func signRequest(body string, key string) string {
	// run pipeline
	out2, err := RunStrings("/usr/bin/echo", "-n", body, "|", "/usr/bin/openssl", "dgst", "-sha256", "-hmac", key)
	if err != nil {
		log.Fatal("error: failed to sign request", err)
	}

	// remove unwanted characters
	tok := strings.Index(out2, "(")
	last := len(out2) - 1
	first := tok + len("(stdin)= ")
	out2 = out2[first:last]
	return out2
}

// return body value from endpoint
func makeBody(endpoint string) string {
	ret := "timestamp=" + C.GoString(C.get_timestamp()) + "&recvWindow=50000"

	switch (endpoint) {
		case GET_Targets.getall , GET_Targets.status:
			break
		case GET_Targets.snapshot:
			ret += "&type=SPOT"
		case GET_Targets.address:
			ret += "&coin=BNB"
	}
	return ret
}