Skip to content

Commit

Permalink
style: improve readability by using urlencode
Browse files Browse the repository at this point in the history
  • Loading branch information
portasynthinca3 committed May 24, 2024
1 parent 6e6960a commit 5b19d98
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 44 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
main/include/secrets.h
/build
/keys
/.vscode
/sdkconfig.old
/managed_components
/bracket.stl
/bracket.gcode
/flash_key.bin
/signing_key.pem
/bot_logo.png
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ Hex-значения прописываются в нижнем регистре
```
- получите ключи шифрования и подписи:
- если собираете новое устройство, сгенерируйте ключи:
`espsecure.py generate_flash_encryption_key flash_key.bin`,
`espsecure.py generate_signing_key --version 1 signing_key.pem`
`espsecure.py generate_flash_encryption_key keys/flash.bin`,
`espsecure.py generate_signing_key --version 1 keys/sign.pem`
- если работаете с уже собранным устройством, получите их от админов и
поместите в корень проекта (файлы `flash_key.bin` и `signing_key.pem`)
поместите в папку `keys` (файлы `flash.bin` и `sign.pem`)
- соберите проект и загрузите прошивку:
- в самый первый раз: `idf.py build flash monitor`
- в последующие разы, в т.ч. для уже собранного устройства: `./flash.sh`
66 changes: 36 additions & 30 deletions main/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,37 @@

#define TAG "http"

// urlencoded string constants
#define ENTRY_ATTEMPT "%D0%9F%D0%BE%D0%BF%D1%8B%D1%82%D0%BA%D0%B0%20%D0%B2%D1%85%D0%BE%D0%B4%D0%B0%3A%20" // "Попытка входа: "
#define TO_SPACE "%20%D0%B2%20%D1%81%D0%BF%D0%B5%D0%B9%D1%81" // " в спейс"

extern const char tg_api_root[] asm("_binary_tg_api_root_pem_start");
static const char* gendered_verb_table[] = {
"%D0%B2%D0%BE%D1%88%D1%91%D0%BB", // вошёл
"%D0%B2%D0%BE%D1%88%D0%BB%D0%BE", // вошло
"%D0%B2%D0%BE%D1%88%D0%BB%D0%B0", // вошла
"%D0%B2%D0%BE%D1%88%D0%BB%D0%B8", // вошли
"вошёл", "вошло", "вошла", "вошли",
};
static const char* entry_emoji[] = {
"%F0%9F%91%8B", // 👋
"%F0%9F%91%80", // 👀
"%F0%9F%98%B3", // 😳
"%F0%9F%A4%AD", // 🤭
"%F0%9F%98%B0", // 😰
"%F0%9F%99%82", // 🙂
"%E2%9D%A4%EF%B8%8F", // ❤️
"👋", "👀", "😳", "🤭", "😰", "🙂", "❤️",
};
#define ENTRY_EMOJIS (sizeof(entry_emoji) / sizeof(char*))
static const char* refusal_emoji[] = {
"%F0%9F%A4%AC", // 🤬
"%F0%9F%98%A4", // 😤
"%F0%9F%A7%90", // 🧐
"%F0%9F%A4%A8", // 🤨
"%F0%9F%A4%9B", // 🤛
"%F0%9F%96%95", // 🖕
"🤬", "😤", "🧐", "🤨", "🤛", "🖕",
};
#define REFUSAL_EMOJIS (sizeof(refusal_emoji) / sizeof(char*))

QueueHandle_t http_queue;

static uint32_t _http_urlencode(char* output, const char* input) {
uint32_t pos = 0;
char byte = 0;

while((byte = *(input++))) {
bool is_allowed = (byte >= 'A' && byte <= 'Z')
|| (byte >= 'a' && byte <= 'z')
|| (byte >= '0' && byte <= '9')
|| byte == '_' || byte == '.' || byte == '-';
pos += sprintf(output + pos, is_allowed ? "%c" : "%%%02X", byte);
}

*(output + pos) = 0;
return pos;
}

static esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
return ESP_OK;
}
Expand Down Expand Up @@ -76,16 +74,24 @@ static esp_err_t _http_hass_log_entry(http_message_t* msg) {
}

static esp_err_t _http_tg_log_entry(http_message_t* msg) {
// format URL
char path[100], query[300];
// format path
char path[128];
sprintf(path, "/bot%s/sendMessage", TG_KEY);
if(msg->type == http_message_type_entry)
sprintf(query, "chat_id=%s&disable_web_page_preview=true&parse_mode=HTML&text=%%3Ca%%20href%%3D%%22t.me%%2F%s%%22%%3E%s%%3C%%2Fa%%3E%%20%s%s%%20%s",
TG_CHAT_ID, msg->username, msg->username, gendered_verb_table[msg->gender], TO_SPACE,
entry_emoji[esp_random() % ENTRY_EMOJIS]);
else
sprintf(query, "chat_id=%s&disable_web_page_preview=true&parse_mode=MarkdownV2&text=%s%s%%20%s",
TG_CHAT_ID, ENTRY_ATTEMPT, msg->username, refusal_emoji[esp_random() % REFUSAL_EMOJIS]); // msg->username contains the unauthorized credential

// format text that will then be urlencoded into the query
char text[128];
if(msg->type == http_message_type_entry) {
const char* emoji = entry_emoji[esp_random() % ENTRY_EMOJIS];
sprintf(text, "<a href=\"t.me/%s\">@%s</a> %s в спейс %s", msg->username, msg->username, gendered_verb_table[msg->gender], emoji);
} else {
const char* emoji = refusal_emoji[esp_random() % ENTRY_EMOJIS];
sprintf(text, "Попытка входа: %s %s", msg->username, emoji); // "username" contains the refused credential in this case
}

// format query
char query[512];
uint32_t offs = sprintf(query, "chat_id=%s&disable_web_page_preview=true&parse_mode=HTML&text=", TG_CHAT_ID);
_http_urlencode(query + offs, text);

// configure HTTP client
esp_http_client_config_t config = {
Expand Down
6 changes: 3 additions & 3 deletions main/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#define PN532_RST 9 // note: RSTPD_N, not RSTO
#define DOOR_RELAY 10

#define SCAN_TIMEOUT (5 * 1000 * 1000) // uS
#define REPL_LOGON_TIMEOUT (5 * 60 * 1000 * 1000) // uS
#define NFC_REINIT_PERIOD (3600 * 1000 * 1000) // uS
#define SCAN_TIMEOUT (5LL * 1000 * 1000) // uS
#define REPL_LOGON_TIMEOUT (5LL * 60 * 1000 * 1000) // uS
#define NFC_REINIT_PERIOD (3600LL * 1000 * 1000) // uS
#define OPEN_DOOR_FOR (5000 / portTICK_PERIOD_MS)

#define HTTP_TRIES 3
3 changes: 2 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ void nfc_task(void* _arg){
}

// scan card
uint8_t success = pn532_readPassiveTargetID(&nfc, PN532_MIFARE_ISO14443A, data_buf, &uid_len, 100);
uint8_t success = pn532_readPassiveTargetID(&nfc, PN532_MIFARE_ISO14443A, data_buf, &uid_len, 1000);
ESP_LOGD(TAG, "%d", success);
if(!success)
continue;

Expand Down
8 changes: 4 additions & 4 deletions partitions.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
ota_0, app, ota_0, , 1M,
factory, app, factory, 0x10000, 2M,
# ota_0, app, ota_0, , 1M,
nvs_key, data, nvs_keys, , 0x1000, encrypted
ota_1, app, ota_1, , 1M,
otadata, data, ota, , 0x2000, encrypted
# ota_1, app, ota_1, , 1M,
# otadata, data, ota, , 0x2000, encrypted
2 changes: 1 addition & 1 deletion sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT=y
CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME=y
CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT=y
CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES=y
CONFIG_SECURE_BOOT_SIGNING_KEY="signing_key.pem"
CONFIG_SECURE_BOOT_SIGNING_KEY="keys/sign.pem"
CONFIG_SECURE_FLASH_ENC_ENABLED=y
# CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT is not set
CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE=y
Expand Down

0 comments on commit 5b19d98

Please sign in to comment.