-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
148 lines (117 loc) · 6.17 KB
/
main.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include "logger.h"
#include "libopensubsonic/crypto.h"
#include "libopensubsonic/httpclient.h"
#include "libopensubsonic/endpoint_ping.h"
#include "libopensubsonic/endpoint_getArtists.h"
#include "libopensubsonic/endpoint_getArtist.h"
// 149 bytes in total (without padding)
const char* openSubSonicUsername = "admin"; // 32 byte limit
const char* openSubSonicPassword = "password"; // 32 byte limit
const char* openSubSonicServer = "192.168.5.250:4533"; // 38 byte limit (32 for the domain, 1 for colon, 5 for the port)
const char* openSubSonicProtocol = "http"; // 5 byte limit (http / https)
const char* openSubSonicVersion = "1.8.0"; // 10 byte limit
const char* openSubSonicClientName = "MalextyClient"; // 32 byte limit
char* openSubSonicLoginSalt = NULL;
char* openSubSonicLoginToken = NULL;
int main() {
logger_log_title(__func__, "OpenSubsonic Client");
int rc = 0;
// Check lengths of user defined settings to avoid buffer overflows
// TODO
// Allocate space on the heap for the login token and salt
openSubSonicLoginSalt = malloc(16 + 1);
openSubSonicLoginToken = malloc(32 + 1);
// Generate the login token and salt
crypto_secure_generate_salt(openSubSonicLoginSalt);
crypto_secure_generate_token(openSubSonicPassword, openSubSonicLoginSalt, openSubSonicLoginToken);
logger_log_important(__func__, "Login Salt: %s", openSubSonicLoginSalt);
logger_log_important(__func__, "Login Token: %s", openSubSonicLoginToken);
// Contact the /ping endpoint to test login
logger_log_general(__func__, "Attempting login to OpenSubsonic server at %s://%s...", openSubSonicProtocol, openSubSonicServer);
char* opensubsonic_ping_res;
opensubsonic_ping_struct pingStruct;
for (size_t retry = 0; retry < 3; retry++) {
rc = opensubsonic_ping(openSubSonicProtocol, openSubSonicServer, openSubSonicUsername, openSubSonicLoginToken, openSubSonicLoginSalt, openSubSonicVersion, openSubSonicClientName, &opensubsonic_ping_res);
if (rc == 600) {
if (retry < 2) {
logger_log_error(__func__, "Retrying...");
} else {
logger_log_error(__func__, "Critical error has occured while attempting to download, exiting.");
exit(EXIT_FAILURE);
}
} else {
break;
}
}
rc = opensubsonic_ping_parse(opensubsonic_ping_res, &pingStruct);
if (rc != 0) {
logger_log_error(__func__, "Critical error has occured while attempting to parse JSON.");
exit(EXIT_FAILURE);
}
free(opensubsonic_ping_res);
logger_log_general(__func__, "Server: %s %s (%s)", pingStruct.serverType, pingStruct.version, pingStruct.serverVersion);
if (pingStruct.error) {
logger_log_error(__func__, "There was an error logging in (Code %d): %s\nExiting...", pingStruct.errorCode, pingStruct.errorMessage);
exit(EXIT_FAILURE);
}
// Fetch all artists from the /getArtists endpoint
logger_log_general(__func__, "Fetching a list of artists from the /getArtists endpoint...");
char* opensubsonic_getArtists_res;
opensubsonic_getArtists_struct getArtistsStruct;
for (size_t retry = 0; retry < 3; retry++) {
rc = opensubsonic_getArtists(openSubSonicProtocol, openSubSonicServer, openSubSonicUsername, openSubSonicLoginToken, openSubSonicLoginSalt, openSubSonicVersion, openSubSonicClientName, &opensubsonic_getArtists_res);
if (rc == 600) {
if (retry < 2) {
logger_log_error(__func__, "Retrying...");
} else {
logger_log_error(__func__, "Critical error has occured while attempting to download, exiting.");
exit(EXIT_FAILURE);
}
} else {
break;
}
}
rc = opensubsonic_getArtists_parse(opensubsonic_getArtists_res, &getArtistsStruct);
if (rc != 0) {
logger_log_error(__func__, "Critical error has occured while attempting to parse JSON.");
exit(EXIT_FAILURE);
}
free(opensubsonic_getArtists_res);
printf("Status: %s\nArtists: %d\nLast modified: %ld\n", getArtistsStruct.status, getArtistsStruct.artistCount, getArtistsStruct.lastModified);
for (int i = 0; i < getArtistsStruct.artistCount; i++){
printf("%s %s %s %d\n",
getArtistsStruct.artists[i].name, getArtistsStruct.artists[i].id,
getArtistsStruct.artists[i].coverArt, getArtistsStruct.artists[i].albumCount);
}
char* lmao = NULL;
char* artist_id = "95a5c13aa2ebd2fa5f8434880215bdfc";
opensubsonic_getArtist(openSubSonicProtocol, openSubSonicServer, openSubSonicUsername, openSubSonicLoginToken, openSubSonicLoginSalt, openSubSonicVersion, openSubSonicClientName, artist_id, &lmao);
opensubsonic_getArtist_struct getArtistStruct;
opensubsonic_getArtist_parse(lmao, &getArtistStruct);
for (size_t i = 0; i < 3; i++) {
printf("%s\n", getArtistStruct.albums[i].id);
}
/*
binary_response_struct binary_res;
char* id = "1923f47530cf919848414e79dbbe7f84";
char* orig = NULL;
char* scale_a = NULL;
char* scale_b = NULL;
opensubsonic_getCoverArt(openSubSonicProtocol, openSubSonicServer, openSubSonicUsername, openSubSonicLoginToken, openSubSonicLoginSalt, openSubSonicVersion, openSubSonicClientName, id, &binary_res);
*/
//opensubsonic_getCoverArtScaled(openSubSonicProtocol, openSubSonicServer, openSubSonicUsername, openSubSonicLoginToken, openSubSonicLoginSalt, openSubSonicVersion, openSubSonicClientName, id, 600, &scale_a);
//opensubsonic_getCoverArtScaled(openSubSonicProtocol, openSubSonicServer, openSubSonicUsername, openSubSonicLoginToken, openSubSonicLoginSalt, openSubSonicVersion, openSubSonicClientName, id, 250, &scale_b);
//printf("%d %d %d\n", sizeof(orig), sizeof(scale_a), sizeof(scale_b));
//printf("Final image size: %ld\n", binary_res.size);
// fifo test
logger_log_title(__func__, "Performing exit sequence.");
// Free structs
opensubsonic_ping_struct_free(&pingStruct);
opensubsonic_getArtists_struct_free(&getArtistsStruct);
// Free login salt and token
free(openSubSonicLoginSalt);
free(openSubSonicLoginToken);
}
//https://www.subsonic.org/pages/api.jsp