cosmopolitan/tool/net/demo/.init.lua
Justine Tunney f7ff77d865
Make fixes and improvements
- Invent iso8601us() for faster timestamps
- Improve --strace descriptions of sigset_t
- Rebuild the Landlock Make bootstrap binary
- Introduce MODE=sysv for non-Windows builds
- Permit OFD fcntl() locks under pledge(flock)
- redbean can now protect your kernel from ddos
- Have vfork() fallback to sys_fork() not fork()
- Change kmalloc() to not die when out of memory
- Improve documentation for some termios functions
- Rewrite putenv() and friends to conform to POSIX
- Fix linenoise + strace verbosity issue on Windows
- Fix regressions in our ability to show backtraces
- Change redbean SetHeader() to no-op if value is nil
- Improve fcntl() so SQLite locks work in non-WAL mode
- Remove some unnecessary work during fork() on Windows
- Create redbean-based SSL reverse proxy for IPv4 TurfWar
- Fix ape/apeinstall.sh warning when using non-bash shells
- Add ProgramTrustedIp(), and IsTrustedIp() APIs to redbean
- Support $PWD, $UID, $GID, and $EUID in command interpreter
- Introduce experimental JTqFpD APE prefix for non-Windows builds
- Invent blackhole daemon for firewalling IP addresses via UNIX named socket
- Add ProgramTokenBucket(), AcquireToken(), and CountTokens() APIs to redbean
2022-10-19 07:19:19 -07:00

95 lines
2.5 KiB
Lua

mymodule = require "mymodule"
sqlite3 = require "lsqlite3"
-- ddos protection
ProgramTokenBucket()
-- /.init.lua is loaded at startup in redbean's main process
HidePath('/usr/share/zoneinfo/')
HidePath('/usr/share/ssl/')
-- open a browser tab using explorer/open/xdg-open
LaunchBrowser('/tool/net/demo/index.html')
-- sql database (see sql.lua)
db = sqlite3.open_memory()
db:exec[[
CREATE TABLE test (
id INTEGER PRIMARY KEY,
content TEXT
);
INSERT INTO test (content) VALUES ('Hello World');
INSERT INTO test (content) VALUES ('Hello Lua');
INSERT INTO test (content) VALUES ('Hello Sqlite3');
]]
-- shared memory hit counter
SHM_LOCK = 0 -- index zero (first eight bytes) will hold mutex
SHM_JSON = 8 -- store json payload starting at the index eight
shm = unix.mapshared(65520)
function Lock()
local ok, old = shm:cmpxchg(SHM_LOCK, 0, 1)
if not ok then
if old == 1 then
old = shm:xchg(SHM_LOCK, 2)
end
while old > 0 do
shm:wait(SHM_LOCK, 2)
old = shm:xchg(SHM_LOCK, 2)
end
end
end
function Unlock()
old = shm:fetch_add(SHM_LOCK, -1)
if old == 2 then
shm:store(SHM_LOCK, 0)
shm:wake(SHM_LOCK, 1)
end
end
function OnServerListen(fd, ip, port)
unix.setsockopt(fd, unix.SOL_TCP, unix.TCP_SAVE_SYN, true)
return false
end
function OnClientConnection(ip, port, serverip, serverport)
syn, synerr = unix.getsockopt(GetClientFd(), unix.SOL_TCP, unix.TCP_SAVED_SYN)
end
function UpdateHitCounter()
local s, t, k
Lock()
s = shm:read(SHM_JSON)
if s == '' then s = '{}' end
t = DecodeJson(s)
k = GetPath()
if not t[k] then t[k] = 0 end
t[k] = t[k] + 1
shm:write(SHM_JSON, EncodeJson(t))
Unlock()
end
-- this intercepts all requests if it's defined
function OnHttpRequest()
UpdateHitCounter()
if GetHeader('User-Agent') then
Log(kLogInfo, "client is running %s and reports %s" % {
finger.GetSynFingerOs(finger.FingerSyn(syn)),
VisualizeControlCodes(GetHeader('User-Agent'))})
end
if HasParam('magic') then
Write('<p>\r\n')
Write('OnHttpRequest() has intercepted your request<br>\r\n')
Write('because you specified the magic parameter\r\n')
Write('<pre>\r\n')
Write(EscapeHtml(LoadAsset('/.init.lua')))
Write('</pre>\r\n')
else
Route() -- this asks redbean to do the default thing
end
SetHeader('Server', 'redbean!')
end
function Adder(x, y)
return x + y
end