liminix-fork/pkgs/tufted/tufted.fnl

46 lines
1.1 KiB
Text
Raw Normal View History

2022-10-05 00:08:43 +02:00
(local tftp (require :tftp))
(local { : realpath} (require :posix.stdlib))
(local { : view } (require :fennel))
(local options
(match arg
["-a" ip-address dir]
{ :allow ip-address :base-directory (realpath dir)}
[dir]
{ :allow nil :base-directory (realpath dir)}
[]
(error "missing command line parameters")
))
(print (.. "TFTP serving from " options.base-directory))
(fn merge-pathname [directory filename]
(if (directory:match "/$")
(.. directory filename)
(.. directory "/" filename)))
(->
(tftp:listen
(fn [file host port]
(if (or (not options.allow) (= host options.allow))
(let [pathname (merge-pathname options.base-directory file)
f (io.open pathname "rb")
size (f:seek "end")]
(f:seek "set" 0)
(var eof? false)
(values
(fn handler [reqlen]
(let [bytes (f:read reqlen)]
2023-02-15 20:50:00 +01:00
(if bytes
2022-10-05 00:08:43 +02:00
(values true bytes)
2023-02-15 20:50:00 +01:00
(values false nil))))
2022-10-05 00:08:43 +02:00
size))
(error "host not allowed")))
nil
["*"]
69)
(os.exit))