From 725167e3d9a8740ac167508aec4e27be53a04f02 Mon Sep 17 00:00:00 2001 From: salaaad2 Date: Mon, 4 Apr 2022 22:38:05 +0200 Subject: cool ui and functioning requests, what can a man ask for (v0.0.1) --- csrc/smith.c | 9 +++++++-- csrc/smith.h | 2 +- gosrc/colors.go | 27 --------------------------- gosrc/main.go | 49 ++++++++++++++++++++++++------------------------- gosrc/requests.go | 36 ++++++++++++++++++++++++++++++++++-- gosrc/structs.go | 26 ++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 57 deletions(-) delete mode 100644 gosrc/colors.go create mode 100644 gosrc/structs.go diff --git a/csrc/smith.c b/csrc/smith.c index 5ac1e73..3643abd 100644 --- a/csrc/smith.c +++ b/csrc/smith.c @@ -8,6 +8,8 @@ #include "smith.h" #include +#include +#include char * mr_smith() @@ -15,13 +17,16 @@ mr_smith() return ("---SMITH_V0.0.0---"); } -long +char* get_timestamp() { struct timeval tv; long ct; + char * buffer; + buffer = (char*)malloc(30); gettimeofday(&tv, NULL); ct = ((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); - return (ct); + sprintf(buffer, "%ld", ct); + return buffer; } diff --git a/csrc/smith.h b/csrc/smith.h index e9bbdbb..3baebf2 100644 --- a/csrc/smith.h +++ b/csrc/smith.h @@ -12,6 +12,6 @@ #include char * mr_smith(); -long get_timestamp(); +char * get_timestamp(); #endif // SMITH_H_ diff --git a/gosrc/colors.go b/gosrc/colors.go deleted file mode 100644 index 6fdf98a..0000000 --- a/gosrc/colors.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -// Theme defines the colors used when primitives are initialized. -type Theme struct { - colorReset string // Main background color for primitives. - colorRed string // Background color for contrasting elements. - colorGreen string // Background color for even more contrasting elements. - colorYellow string // Box borders. - colorBlue string // Box titles. - colorPurple string // Graphics. - colorCyan string // Primary text. - colorWhite string // Secondary text (e.g. labels). -} - -// Styles defines the theme for applications. The default is for a black -// background and some basic colors: black, white, yellow, green, cyan, and -// blue. -var Styles = Theme{ - colorReset: "\033[0m", - colorRed : "\033[31m", - colorGreen : "\033[32m", - colorYellow : "\033[33m", - colorBlue : "\033[34m", - colorPurple : "\033[35m", - colorCyan : "\033[36m", - colorWhite : "\033[37m", -} diff --git a/gosrc/main.go b/gosrc/main.go index 78ced2e..a83189e 100644 --- a/gosrc/main.go +++ b/gosrc/main.go @@ -8,30 +8,17 @@ package main import ( "encoding/json" - "fmt" "io/ioutil" + "fmt" "log" "os" "time" - "github.com/gizak/termui/v3/widgets" ui "github.com/gizak/termui/v3" + "github.com/gizak/termui/v3/widgets" ) -// #include "../csrc/smith.h" -// #cgo LDFLAGS: -lsmith -L../ -import "C" - -type Config struct { - Public_key string - Secret_key string - Mirror string -} - func main() { - fmt.Println("Welcome, traveller, my name is " + Styles.colorGreen + C.GoString(C.mr_smith()) + Styles.colorReset + - "\nI will try to help you get the bag...") - // get user config from json file config_path := "./config.json" for i, a := range os.Args[1:] { @@ -49,15 +36,20 @@ func main() { log.Fatal("error: marshall() ", err) } - // make request body and sign it - body := make_body("qwe", "qwe", "qwe") - signature := sign_request(body, "hello") - ui_loop(config, body, signature) + if err != nil { + log.Fatal("error: request failed %s", "qwe", err) + return + } + // make a cool ui + ui_loop(config) + + fmt.Println("qwe") defer ui.Close() + return } // draw stuff -func ui_loop(config Config, body string, signature string) error { +func ui_loop(config Config) error { if err := ui.Init(); err != nil { log.Fatal("error: failed to initialize termui", err) } @@ -71,9 +63,9 @@ func ui_loop(config Config, body string, signature string) error { p1.Text = config.Mirror p2.Text = "1000.0 BTC" - p3.Text = body - p4.Text = signature - p5.Text = C.GoString(C.mr_smith()) + p3.Text = "hello" + p4.Text = "cool" + p5.Text = "__MR_SMITH_V001__" p5.TextStyle.Fg = ui.ColorGreen p1.Border = true @@ -81,7 +73,7 @@ func ui_loop(config Config, body string, signature string) error { p3.Border = true p4.Border = true - p1.Title = "Mirror" + p1.Title = "Active Mirror" p2.Title = "Balance" p3.Title = "public" p4.Title = "Key" @@ -112,13 +104,15 @@ func ui_loop(config Config, body string, signature string) error { select { case e := <-ui_events: switch e.ID { - case "q", "": + case "": return nil case "": payload := e.Payload.(ui.Resize) main_grid.SetRect(0, 0, payload.Width, payload.Height) ui.Clear() ui.Render(main_grid) + default: + p3.Text = e.ID } case <-ticker: ui.Render(main_grid) @@ -126,3 +120,8 @@ func ui_loop(config Config, body string, signature string) error { } } } + +// make request body and sign it +// body := make_body() +// signature := sign_request(body, config.Secret_key) +// res, err := send_request(body, signature, Endpoints.getall, config) diff --git a/gosrc/requests.go b/gosrc/requests.go index 7be1d49..0a939c0 100644 --- a/gosrc/requests.go +++ b/gosrc/requests.go @@ -9,9 +9,41 @@ package main import ( "fmt" "log" + "net/http" "strings" ) +// #include "../csrc/smith.h" +// #cgo LDFLAGS: -lsmith -L../ +import "C" + +// send request to mirror and return response +// @param body ?qwe=asd&foo=bar +// @param signature duh +// @param endpoint /sapi/v1/capital/config/qwe +// @param config duhh +func send_request(body string, signature string, endpoint string, config Config) (*http.Response, error) { + // new client to allow defering of requests + client := &http.Client{} + url := "https://" + config.Mirror + "/" + endpoint + "?" + body + "&signature=" + signature + + // add some ifs here + req, err := http.NewRequest("GET", url, nil) + if err != nil { + log.Fatal("error: creating http request ", err) + } + req.Header.Add("X-MBX-APIKEY", config.Public_key) + + // finished bakiong request. send it + response, err := client.Do(req) + if err != nil { + log.Fatal("error: making http request ", err) + } + + defer response.Body.Close() + return response, nil +} + // sign request with given private key func sign_request(body string, key string) string { fmt.Println("signing request : ", body) @@ -33,7 +65,7 @@ func sign_request(body string, key string) string { // create body given choice ex : // GET /sapi/v1/capital/config/getall || // POST /sapi/v1/asset/dust-btc -func make_body(order string, ticker string, price string) string { - ret := "GET /sapi/v1/capital/config/getall" +func make_body() string { + ret := "timestamp=" + C.GoString(C.get_timestamp()) + "&recvWindow=50000" return ret } diff --git a/gosrc/structs.go b/gosrc/structs.go new file mode 100644 index 0000000..ac17cf6 --- /dev/null +++ b/gosrc/structs.go @@ -0,0 +1,26 @@ +// SMITH ( // / +// structs ( )/ / +// by salade )(/ / +// ________________ ( /) / +// ()__)____________))))) :^} / + +package main + +// config.json content +type Config struct { + Public_key string + Secret_key string + Mirror string +} + +// urls to hit on mirror +type Targets struct { + getall string + null string +} + +var Endpoints = Targets { + getall: "/sapi/v1/capital/config/getall", + null: "/null", +} + -- cgit v1.2.3