Created
June 18, 2018 17:40
-
-
Save robertlabrie/fe987d220eeee469edcbe8ec8192c338 to your computer and use it in GitHub Desktop.
Openresty conf for generating TCP / UDP traffic from gets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
worker_processes 8; | |
# daemon off; | |
# master_process off; | |
error_log /dev/stdout info; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
lua_shared_dict req 1m; | |
init_by_lua_block { | |
local file = io.open("/etc/somefile.json","r") | |
local contents = file:read() | |
file:close() | |
local req = ngx.shared.req; | |
req:set("req",contents); | |
} | |
include mime.types; | |
default_type application/octet-stream; | |
sendfile on; | |
keepalive_timeout 65; | |
server { | |
listen 10082 default_server; | |
server_name _; | |
location /tcp { | |
root /var/www; | |
index index.html; | |
content_by_lua_block { | |
local req = ngx.shared.req; | |
local socket = ngx.socket | |
local tcp = assert(socket.tcp()) | |
local ok, err = tcp:connect("127.0.0.1",20001) | |
if not ok then | |
ngx.status = 503 | |
ngx.say("TCP failed to connect") | |
return | |
end | |
local ok, err = tcp:send(req:get("req")) | |
if not ok then | |
ngx.status = 503 | |
ngx.say("TCP failed to send") | |
return | |
end | |
tcp:close() | |
ngx.say("tcp") | |
} | |
} | |
location /udp { | |
root /var/www; | |
index index.html; | |
content_by_lua_block { | |
local req = ngx.shared.req; | |
local socket = ngx.socket | |
local udp = assert(socket.udp()) | |
local ok, err = udp:setpeername("127.0.0.1", 20001) | |
local ok, err = udp:send(req:get("req")) | |
if not ok then | |
ngx.status = 503 | |
ngx.say "UDP failed to send" | |
return | |
end | |
ngx.say("udp") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment