Files
bananarchy/overlay/rootfs/home/user/Projects/neonet_demo.yab
T

630 lines
20 KiB
Plaintext
Executable File

#!/usr/bin/yabasic
// ============================================================================
// neonet_demo.yab - Bananarchy NeoNet + NeoUI Showcase for Yabasic
// ============================================================================
// Demonstrates combining NeoNet (libcurl + socket FFI) with NeoUI (240x320 UI):
// - Tab 1 (1:HTTP/S): HTTP/HTTPS GET, POST, and HEAD requests with custom
// headers, status code badge, server/IP inspection, and scrollable output
// - Tab 2 (2:Sockets): Raw TLS (port 443), TCP (port 43 WHOIS), and binary
// UDP (port 53 DNS A-record) socket data exchange
// - Tab 3 (3:Tools): URL encode/decode, file download to /tmp, TLS verify
// toggle, and libcurl version/capability inspection
//
// Controls:
// - 1 / 2 / 3 (when not typing in an input box): Switch tabs
// - Up / Down: Navigate controls or scroll response viewer
// - Left / Right: Cycle HTTP method / socket protocol or switch tabs
// - OK (Enter): Execute request / toggle option / edit input
// - Left Softkey (F1): Open Presets & Actions menu
// - Right Softkey (F2) / Backspace: Delete char (in input) or Quit
// - Both Softkeys together: Open Bananarchy XDrawer overlay
// ============================================================================
import neoui
import neonet
if neoui_init("neonet_demo", 0) = 0 then
print "Failed to initialize NeoUI X11 window"
exit 1
fi
r = neonet_init()
r = neonet_set_timeout(10000)
r = neonet_set_connect_timeout(6000)
// Active tab: 0 = HTTP/S, 1 = Sockets, 2 = Tools & Info
active_tab = 0
// Tab 0 (HTTP/S) state
http_focus = 2
http_url$ = "https://example.com/"
http_method_idx = 0
dim http_methods$(3)
http_methods$(0) = "GET"
http_methods$(1) = "POST"
http_methods$(2) = "HEAD"
custom_hdr_on = 1
custom_hdr$ = "X-Bananarchy: NeoNet-1.0|Accept: */*"
show_raw_headers = 0
http_status_str$ = "Ready (press OK on Send)"
http_meta_str$ = "Custom Header: X-Bananarchy"
http_output$ = "Press OK on [Send] or open Left Softkey Menu for presets."
http_scroll = 0
// Tab 1 (Sockets) state
sock_focus = 2
sock_proto_idx = 0
dim sock_protos$(3)
sock_protos$(0) = "TLS :443 (HTTPS Raw)"
sock_protos$(1) = "TCP :43 (IANA WHOIS)"
sock_protos$(2) = "UDP :53 (DNS Query)"
sock_host$ = "example.com"
sock_status_str$ = "Ready (select protocol & Run)"
sock_output$ = "Supports raw TLS, TCP, and UDP socket data exchange."
sock_scroll = 0
// Tab 2 (Tools) state
tool_focus = 0
tls_insecure = 0
sample_raw$ = "Bananarchy 240x320 & NeoNet!"
sample_enc$ = neonet_url_encode$(sample_raw$)
dl_status$ = "Not downloaded yet"
// Modal state: 0 = None, 1 = Presets Menu
modal_mode = 0
menu_sel = 0
// Track which input box owns multi-tap buffer (0 = http_url$, 1 = sock_host$)
active_input_owner = -1
sub sync_input_box(owner_id)
if active_input_owner = owner_id then
return
fi
active_input_owner = owner_id
if owner_id = 0 then
neoui_input_reset(http_url$)
elsif owner_id = 1 then
neoui_input_reset(sock_host$)
fi
end sub
sub save_input_box()
if active_input_owner = 0 then
neoui_input_commit_tap()
http_url$ = neoui_input_get_text$()
elsif active_input_owner = 1 then
neoui_input_commit_tap()
sock_host$ = neoui_input_get_text$()
fi
end sub
sub sanitize_display_text$(s$, max_chars)
local out$, n, i, ch$, code
n = len(s$)
if n > max_chars then
n = max_chars
fi
out$ = ""
for i = 1 to n
ch$ = mid$(s$, i, 1)
code = asc(ch$)
if code = 10 or (code >= 32 and code <= 126) then
out$ = out$ + ch$
elsif code = 9 then
out$ = out$ + " "
fi
next i
return out$
end sub
sub run_http_request()
local m$, hdrs$, body$, st, blen, srv$, ip$
save_input_box()
m$ = http_methods$(http_method_idx)
if custom_hdr_on then
hdrs$ = custom_hdr$
else
hdrs$ = ""
fi
neoui_show_toast(m$ + " " + http_url$ + " ...", 500)
render_all()
neoui_flush()
if m$ = "GET" then
body$ = neonet_get_ex$(http_url$, hdrs$)
elsif m$ = "POST" then
body$ = neonet_post$(http_url$, "{\"device\":\"nokia-argon\",\"sdk\":\"NeoNet\"}", "application/json", hdrs$)
else
st = neonet_head(http_url$, hdrs$)
body$ = neonet_response_headers$()
fi
st = neonet_status()
blen = neonet_body_len()
http_scroll = 0
if neonet_last_code() <> 0 then
http_status_str$ = "ERR (" + str$(neonet_last_code()) + "): " + neonet_error$()
http_meta_str$ = "URL: " + http_url$
http_output$ = "Request failed: " + neonet_error$()
neoui_show_toast("HTTP error!", 1600)
else
srv$ = neonet_header$("server")
if srv$ = "" then
srv$ = "n/a"
fi
ip$ = neonet_primary_ip$()
http_status_str$ = m$ + " -> HTTP " + str$(st) + " (" + str$(blen) + " B)"
http_meta_str$ = "IP: " + ip$ + " Srv: " + srv$
if show_raw_headers or m$ = "HEAD" then
http_output$ = sanitize_display_text$(neonet_response_headers$(), 1800)
else
http_output$ = sanitize_display_text$(body$, 1800)
fi
neoui_show_toast("HTTP " + str$(st) + " (" + str$(blen) + " B)", 1500)
fi
end sub
sub run_socket_exchange()
local crlf$, req$, rx$, ip$, dns_srv$
save_input_box()
crlf$ = chr$(13) + chr$(10)
sock_scroll = 0
if sock_proto_idx = 0 then
neoui_show_toast("TLS connect " + sock_host$ + ":443 ...", 500)
render_all()
neoui_flush()
req$ = "HEAD / HTTP/1.1" + crlf$ + "Host: " + sock_host$ + crlf$ + "User-Agent: NeoNet-TLS/1.0" + crlf$ + "Connection: close" + crlf$ + crlf$
rx$ = neonet_tls_exchange$(sock_host$, 443, req$, 4000)
if rx$ = "" and neonet_error$() <> "" then
sock_status_str$ = "TLS Error: " + neonet_error$()
sock_output$ = "Could not complete TLS exchange with " + sock_host$ + ":443"
neoui_show_toast("TLS failed", 1500)
else
sock_status_str$ = "TLS :443 OK (" + str$(neonet_last_len()) + " B recv)"
sock_output$ = sanitize_display_text$(rx$, 1800)
neoui_show_toast("TLS recv " + str$(neonet_last_len()) + " B", 1500)
fi
elsif sock_proto_idx = 1 then
neoui_show_toast("TCP connect whois.iana.org:43 ...", 500)
render_all()
neoui_flush()
rx$ = neonet_tcp_exchange$("whois.iana.org", 43, sock_host$ + crlf$, 4000)
if rx$ = "" and neonet_error$() <> "" then
sock_status_str$ = "TCP Error: " + neonet_error$()
sock_output$ = "Could not query whois.iana.org:43"
neoui_show_toast("TCP failed", 1500)
else
sock_status_str$ = "TCP :43 OK (" + str$(neonet_last_len()) + " B recv)"
sock_output$ = sanitize_display_text$(rx$, 1800)
neoui_show_toast("TCP recv " + str$(neonet_last_len()) + " B", 1500)
fi
else
dns_srv$ = neonet_system_dns$()
neoui_show_toast("UDP DNS @" + dns_srv$ + ":53 ...", 500)
render_all()
neoui_flush()
ip$ = neonet_udp_dns_a$(dns_srv$, sock_host$, 3000)
if ip$ = "" then
sock_status_str$ = "UDP DNS Error: " + neonet_error$()
sock_output$ = "Resolver: " + dns_srv$ + ":53\nQuery: " + sock_host$ + "\nError: " + neonet_error$()
neoui_show_toast("UDP DNS failed", 1500)
else
sock_status_str$ = "UDP :53 -> " + ip$
sock_output$ = "Protocol: RFC 1035 Binary UDP Datagram\nServer: " + dns_srv$ + ":53\nQuestion: " + sock_host$ + " (IN A)\nResolved IPv4: " + ip$ + "\nPacket Size: " + str$(neonet_last_len()) + " bytes"
neoui_show_toast(sock_host$ + " -> " + ip$, 1800)
fi
fi
end sub
sub render_http_tab()
local url_disp$, hdr_state$, view_lbl$
if http_focus = 0 then
sync_input_box(0)
url_disp$ = neoui_input_get_text$()
else
url_disp$ = http_url$
fi
neoui_draw_input_box(6, 60, 228, 36, http_focus = 0, "Target URL [" + http_methods$(http_method_idx) + "]", url_disp$, "https://...", 0)
if custom_hdr_on then
hdr_state$ = "ON"
else
hdr_state$ = "OFF"
fi
neoui_draw_toggle_row(0, 98, 28, http_focus = 1, neoui_icon$("edit"), neoui.TEXT_BLUE, "Hdr: X-Bananarchy", custom_hdr_on, hdr_state$)
if show_raw_headers then
view_lbl$ = "Show:Hdrs"
else
view_lbl$ = "Show:Body"
fi
neoui_draw_button(6, 128, 72, 22, http_focus = 2, 0, neoui_icon$("globe"), "Send")
neoui_draw_button(84, 128, 72, 22, http_focus = 3, 0, neoui_icon$("sort"), "Method")
neoui_draw_button(162, 128, 72, 22, http_focus = 4, 0, neoui_icon$("eye"), view_lbl$)
neoui_fill_rect(6, 153, 228, 114, neoui_color(neoui.COLOR_BG_ALT_ODD))
if http_focus = 5 then
neoui_draw_rect(6, 153, 228, 114, neoui_color(neoui.COLOR_ACCENT))
else
neoui_draw_rect(6, 153, 228, 114, neoui_color(neoui.COLOR_BORDER))
fi
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_GREEN, 10, 155, 14, http_status_str$, 220)
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_YELLOW, 10, 169, 14, http_meta_str$, 220)
neoui_draw_line(8, 184, 232, 184, neoui_color(neoui.COLOR_DIVIDER))
neoui_draw_text_wrapped(neoui.FONT_SMALL, neoui.TEXT_LIGHT, 10, 186, 220, 78, http_output$, http_scroll)
if http_focus = 0 then
neoui_draw_multitap_bar(0, 248, 240, 22, "0-9:Type 1:Sym #:Mode OK:Send")
fi
end sub
sub render_sockets_tab()
local host_disp$
if sock_focus = 1 then
sync_input_box(1)
host_disp$ = neoui_input_get_text$()
else
host_disp$ = sock_host$
fi
neoui_draw_list_row(0, 60, 30, sock_focus = 0, neoui_icon$("wifi"), neoui.TEXT_YELLOW, sock_protos$(sock_proto_idx), "", "< >")
neoui_draw_input_box(6, 92, 228, 36, sock_focus = 1, "Host / Domain", host_disp$, "example.com", 0)
neoui_draw_button(6, 130, 140, 22, sock_focus = 2, 0, neoui_icon$("check"), "Run Exchange")
neoui_draw_button(152, 130, 82, 22, sock_focus = 3, 0, neoui_icon$("refresh"), "Clear")
neoui_fill_rect(6, 155, 228, 112, neoui_color(neoui.COLOR_BG_ALT_ODD))
if sock_focus = 4 then
neoui_draw_rect(6, 155, 228, 112, neoui_color(neoui.COLOR_ACCENT))
else
neoui_draw_rect(6, 155, 228, 112, neoui_color(neoui.COLOR_BORDER))
fi
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_GREEN, 10, 157, 15, sock_status_str$, 220)
neoui_draw_line(8, 173, 232, 173, neoui_color(neoui.COLOR_DIVIDER))
neoui_draw_text_wrapped(neoui.FONT_SMALL, neoui.TEXT_LIGHT, 10, 175, 220, 90, sock_output$, sock_scroll)
if sock_focus = 1 then
neoui_draw_multitap_bar(0, 248, 240, 22, "0-9:Type 1:Sym #:Mode OK:Run")
fi
end sub
sub render_tools_tab()
local tls_lbl$
if tls_insecure then
tls_lbl$ = "Insecure"
else
tls_lbl$ = "Strict"
fi
neoui_draw_toggle_row(0, 62, 32, tool_focus = 0, neoui_icon$("lock"), neoui.TEXT_GREEN, "TLS Cert Check", 1 - tls_insecure, tls_lbl$)
neoui_draw_list_row(1, 96, 32, tool_focus = 1, neoui_icon$("folder"), neoui.TEXT_BLUE, "Download example.com", dl_status$, "Run")
neoui_fill_rect(6, 132, 228, 134, neoui_color(neoui.COLOR_BG_ALT_ODD))
neoui_draw_rect(6, 132, 228, 134, neoui_color(neoui.COLOR_BORDER))
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_YELLOW, 10, 135, 15, "URL Encode / Decode FFI:", 220)
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_LIGHT, 10, 150, 14, "In : " + sample_raw$, 220)
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_MUTED, 10, 164, 14, "Enc: " + sample_enc$, 220)
neoui_draw_line(8, 181, 232, 181, neoui_color(neoui.COLOR_DIVIDER))
neoui_draw_text(neoui.FONT_SMALL, neoui.TEXT_YELLOW, 10, 184, 15, "System DNS: " + neonet_system_dns$(), 220)
neoui_draw_text_wrapped(neoui.FONT_SMALL, neoui.TEXT_MUTED, 10, 200, 220, 62, "Engine: " + neonet_version$(), 0)
end sub
sub render_all()
local badge$, mid_lbl$, right_lbl$
neoui_begin_frame()
if (active_tab = 0 and http_focus = 0) or (active_tab = 1 and sock_focus = 1) then
badge$ = neoui_input_mode_badge$()
else
badge$ = "libcurl"
fi
neoui_draw_header(neoui_icon$("globe"), neoui.TEXT_YELLOW, "NeoNet + NeoUI", badge$)
neoui_draw_tabs("1:HTTP/S|2:Sockets|3:Tools", active_tab, 34, 24)
mid_lbl$ = "Select"
right_lbl$ = "Quit"
if active_tab = 0 then
render_http_tab()
if http_focus = 0 and len(neoui_input_get_text$()) > 0 then
right_lbl$ = "Del"
fi
if http_focus = 0 or http_focus = 2 then
mid_lbl$ = "Send"
elsif http_focus = 5 then
mid_lbl$ = "Hdr/Body"
fi
elsif active_tab = 1 then
render_sockets_tab()
if sock_focus = 1 and len(neoui_input_get_text$()) > 0 then
right_lbl$ = "Del"
fi
if sock_focus = 1 or sock_focus = 2 then
mid_lbl$ = "Run"
fi
else
render_tools_tab()
fi
if modal_mode = 1 then
neoui_draw_menu_popup("HTTPS GET example.com|HTTPS HEAD example.com|TLS :443 Raw Exchange|TCP :43 WHOIS Lookup|UDP :53 DNS Resolve|Reload Theme|Quit Demo", neoui_icon$("globe") + "|" + neoui_icon$("info") + "|" + neoui_icon$("lock") + "|" + neoui_icon$("term") + "|" + neoui_icon$("wifi") + "|" + neoui_icon$("theme") + "|" + neoui_icon$("close"), menu_sel, 7)
neoui_draw_softkeys("Close", "Run", "Back")
else
neoui_draw_softkeys("Presets", mid_lbl$, right_lbl$)
fi
neoui_end_frame()
end sub
render_all()
running = 1
while running
ev = neoui_poll_event(250)
if ev = neoui.EV_CLOSE_REQ then
running = 0
elsif ev = neoui.EV_EXPOSE or ev = neoui.EV_THEME_RELOAD then
render_all()
elsif ev = neoui.EV_SOFTKEY_LEFT then
save_input_box()
if modal_mode = 0 then
modal_mode = 1
menu_sel = 0
else
modal_mode = 0
fi
render_all()
elsif ev = neoui.EV_SOFTKEY_RIGHT then
if modal_mode <> 0 then
modal_mode = 0
render_all()
elsif active_tab = 0 and http_focus = 0 and len(neoui_input_get_text$()) > 0 then
neoui_input_backspace()
http_url$ = neoui_input_get_text$()
render_all()
elsif active_tab = 1 and sock_focus = 1 and len(neoui_input_get_text$()) > 0 then
neoui_input_backspace()
sock_host$ = neoui_input_get_text$()
render_all()
else
running = 0
fi
elsif ev = neoui.EV_KEY_PRESS then
k = neoui_event_key()
if modal_mode = 1 then
if k = neoui.KEY_UP then
menu_sel = mod(menu_sel + 6, 7)
elsif k = neoui.KEY_DOWN then
menu_sel = mod(menu_sel + 1, 7)
elsif k = neoui.KEY_BACK then
modal_mode = 0
elsif k = neoui.KEY_OK then
modal_mode = 0
if menu_sel = 0 then
active_tab = 0
http_focus = 2
http_url$ = "https://example.com/"
http_method_idx = 0
show_raw_headers = 0
run_http_request()
elsif menu_sel = 1 then
active_tab = 0
http_focus = 2
http_url$ = "https://example.com/"
http_method_idx = 2
show_raw_headers = 1
run_http_request()
elsif menu_sel = 2 then
active_tab = 1
sock_focus = 2
sock_proto_idx = 0
sock_host$ = "example.com"
run_socket_exchange()
elsif menu_sel = 3 then
active_tab = 1
sock_focus = 2
sock_proto_idx = 1
sock_host$ = "example.com"
run_socket_exchange()
elsif menu_sel = 4 then
active_tab = 1
sock_focus = 2
sock_proto_idx = 2
sock_host$ = "example.com"
run_socket_exchange()
elsif menu_sel = 5 then
neoui_reload_theme()
neoui_show_toast("Theme reloaded!", 1400)
elsif menu_sel = 6 then
running = 0
fi
fi
render_all()
elsif active_tab = 0 and http_focus = 0 then
sync_input_box(0)
if k = neoui.KEY_UP then
save_input_box()
http_focus = 5
elsif k = neoui.KEY_DOWN then
save_input_box()
http_focus = 1
elsif k = neoui.KEY_OK then
run_http_request()
else
neoui_input_feed_key(k)
http_url$ = neoui_input_get_text$()
fi
render_all()
elsif active_tab = 1 and sock_focus = 1 then
sync_input_box(1)
if k = neoui.KEY_UP then
save_input_box()
sock_focus = 0
elsif k = neoui.KEY_DOWN then
save_input_box()
sock_focus = 2
elsif k = neoui.KEY_OK then
run_socket_exchange()
else
neoui_input_feed_key(k)
sock_host$ = neoui_input_get_text$()
fi
render_all()
else
if k = neoui.KEY_1 then
save_input_box()
active_tab = 0
elsif k = neoui.KEY_2 then
save_input_box()
active_tab = 1
elsif k = neoui.KEY_3 then
save_input_box()
active_tab = 2
elsif k = neoui.KEY_BACK then
running = 0
elsif active_tab = 0 then
if k = neoui.KEY_UP then
if http_focus = 5 and http_scroll > 0 then
http_scroll = http_scroll - 1
else
http_focus = mod(http_focus + 5, 6)
fi
elsif k = neoui.KEY_DOWN then
if http_focus = 5 then
http_scroll = http_scroll + 1
else
http_focus = mod(http_focus + 1, 6)
fi
elsif k = neoui.KEY_LEFT then
if http_focus = 3 then
http_method_idx = mod(http_method_idx + 2, 3)
elsif http_focus = 4 then
http_focus = 3
elsif http_focus = 5 and http_scroll > 0 then
http_scroll = 0
http_focus = 2
else
active_tab = 2
fi
elsif k = neoui.KEY_RIGHT then
if http_focus = 2 then
http_focus = 3
elsif http_focus = 3 then
http_focus = 4
else
active_tab = 1
fi
elsif k = neoui.KEY_OK then
if http_focus = 1 then
custom_hdr_on = 1 - custom_hdr_on
elsif http_focus = 2 then
run_http_request()
elsif http_focus = 3 then
http_method_idx = mod(http_method_idx + 1, 3)
elsif http_focus = 4 or http_focus = 5 then
show_raw_headers = 1 - show_raw_headers
http_scroll = 0
if show_raw_headers then
http_output$ = sanitize_display_text$(neonet_response_headers$(), 1800)
else
http_output$ = sanitize_display_text$(neonet_body$(), 1800)
fi
fi
fi
elsif active_tab = 1 then
if k = neoui.KEY_UP then
if sock_focus = 4 and sock_scroll > 0 then
sock_scroll = sock_scroll - 1
else
sock_focus = mod(sock_focus + 4, 5)
fi
elsif k = neoui.KEY_DOWN then
if sock_focus = 4 then
sock_scroll = sock_scroll + 1
else
sock_focus = mod(sock_focus + 1, 5)
fi
elsif k = neoui.KEY_LEFT then
if sock_focus = 0 then
sock_proto_idx = mod(sock_proto_idx + 2, 3)
elsif sock_focus = 3 then
sock_focus = 2
else
active_tab = 0
fi
elsif k = neoui.KEY_RIGHT then
if sock_focus = 0 then
sock_proto_idx = mod(sock_proto_idx + 1, 3)
elsif sock_focus = 2 then
sock_focus = 3
else
active_tab = 2
fi
elsif k = neoui.KEY_OK then
if sock_focus = 0 then
sock_proto_idx = mod(sock_proto_idx + 1, 3)
elsif sock_focus = 2 then
run_socket_exchange()
elsif sock_focus = 3 then
sock_output$ = ""
sock_status_str$ = "Cleared"
sock_scroll = 0
fi
fi
elsif active_tab = 2 then
if k = neoui.KEY_UP or k = neoui.KEY_DOWN then
tool_focus = 1 - tool_focus
elsif k = neoui.KEY_LEFT then
active_tab = 1
elsif k = neoui.KEY_RIGHT then
active_tab = 0
elsif k = neoui.KEY_OK then
if tool_focus = 0 then
tls_insecure = 1 - tls_insecure
r = neonet_set_insecure(tls_insecure)
if tls_insecure then
neoui_show_toast("TLS cert check disabled", 1400)
else
neoui_show_toast("TLS cert check enabled", 1400)
fi
elsif tool_focus = 1 then
neoui_show_toast("Downloading to /tmp/neonet_dl.html ...", 500)
render_all()
neoui_flush()
if neonet_download("https://example.com/", "/tmp/neonet_dl.html", "") then
dl_status$ = "Saved /tmp/neonet_dl.html (HTTP " + str$(neonet_status()) + ")"
neoui_show_toast("Saved /tmp/neonet_dl.html!", 1600)
else
dl_status$ = "Error: " + neonet_error$()
neoui_show_toast("Download failed", 1600)
fi
fi
fi
fi
render_all()
fi
fi
wend
r = neonet_cleanup()
neoui_shutdown()