2019-10-31 09:27:28 +08:00
|
|
|
--
|
|
|
|
-- Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
-- contributor license agreements. See the NOTICE file distributed with
|
|
|
|
-- this work for additional information regarding copyright ownership.
|
|
|
|
-- The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
-- (the "License"); you may not use this file except in compliance with
|
|
|
|
-- the License. You may obtain a copy of the License at
|
|
|
|
--
|
|
|
|
-- http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
--
|
|
|
|
-- Unless required by applicable law or agreed to in writing, software
|
|
|
|
-- distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
-- See the License for the specific language governing permissions and
|
|
|
|
-- limitations under the License.
|
|
|
|
--
|
2020-11-30 10:32:35 +08:00
|
|
|
local json_decode = require("toolkit.json").decode
|
|
|
|
local json_encode = require("toolkit.json").encode
|
2019-07-26 17:16:31 +08:00
|
|
|
|
2019-06-17 23:17:41 +08:00
|
|
|
local _M = {}
|
|
|
|
|
|
|
|
|
2020-09-27 16:43:09 +08:00
|
|
|
local function inject_headers()
|
|
|
|
local hdrs = ngx.req.get_headers()
|
|
|
|
for k, v in pairs(hdrs) do
|
|
|
|
if k:sub(1, 5) == "resp-" then
|
|
|
|
ngx.header[k:sub(6)] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-06-17 23:17:41 +08:00
|
|
|
function _M.hello()
|
2020-10-29 16:48:53 +08:00
|
|
|
local s = "hello world"
|
|
|
|
ngx.header['Content-Length'] = #s + 1
|
|
|
|
ngx.say(s)
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-10-29 16:48:53 +08:00
|
|
|
function _M.hello_chunked()
|
|
|
|
ngx.print("hell")
|
|
|
|
ngx.flush(true)
|
|
|
|
ngx.print("o w")
|
|
|
|
ngx.flush(true)
|
|
|
|
ngx.say("orld")
|
2019-06-17 23:17:41 +08:00
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-07-25 16:06:07 +08:00
|
|
|
function _M.hello1()
|
|
|
|
ngx.say("hello1 world")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-07-29 19:25:30 +08:00
|
|
|
function _M.hello_()
|
|
|
|
ngx.say("hello world")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2021-01-27 21:50:18 +08:00
|
|
|
-- Fake endpoint, needed for testing authz-keycloak plugin.
|
|
|
|
function _M.course_foo()
|
|
|
|
ngx.say("course foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-17 23:17:41 +08:00
|
|
|
function _M.server_port()
|
|
|
|
ngx.print(ngx.var.server_port)
|
|
|
|
end
|
2020-02-06 15:22:49 +08:00
|
|
|
_M.server_port_route2 = _M.server_port
|
|
|
|
_M.server_port_hello = _M.server_port
|
|
|
|
_M.server_port_aa = _M.server_port
|
2019-06-17 23:17:41 +08:00
|
|
|
|
|
|
|
|
2019-06-24 17:02:53 +08:00
|
|
|
function _M.limit_conn()
|
|
|
|
ngx.sleep(0.3)
|
|
|
|
ngx.say("hello world")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-09-27 15:14:49 +08:00
|
|
|
function _M.plugin_proxy_rewrite()
|
|
|
|
ngx.say("uri: ", ngx.var.uri)
|
|
|
|
ngx.say("host: ", ngx.var.host)
|
|
|
|
ngx.say("scheme: ", ngx.var.scheme)
|
|
|
|
end
|
2019-06-24 17:02:53 +08:00
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-10-09 16:02:38 +08:00
|
|
|
function _M.plugin_proxy_rewrite_args()
|
|
|
|
ngx.say("uri: ", ngx.var.uri)
|
|
|
|
local args = ngx.req.get_uri_args()
|
2021-01-29 07:53:41 +08:00
|
|
|
|
|
|
|
local keys = {}
|
|
|
|
for k, _ in pairs(args) do
|
|
|
|
table.insert(keys, k)
|
|
|
|
end
|
|
|
|
table.sort(keys)
|
|
|
|
|
|
|
|
for _, key in ipairs(keys) do
|
|
|
|
ngx.say(key, ": ", args[key])
|
2019-10-09 16:02:38 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-07-17 23:52:15 +08:00
|
|
|
function _M.status()
|
|
|
|
ngx.say("ok")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-07-22 11:27:30 +08:00
|
|
|
function _M.sleep1()
|
|
|
|
ngx.sleep(1)
|
|
|
|
ngx.say("ok")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-08-29 23:14:16 +08:00
|
|
|
function _M.ewma()
|
|
|
|
if ngx.var.server_port == "1981"
|
|
|
|
or ngx.var.server_port == "1982" then
|
|
|
|
ngx.sleep(0.2)
|
|
|
|
else
|
|
|
|
ngx.sleep(0.1)
|
|
|
|
end
|
|
|
|
ngx.print(ngx.var.server_port)
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-08-30 10:15:58 +08:00
|
|
|
function _M.uri()
|
|
|
|
-- ngx.sleep(1)
|
|
|
|
ngx.say("uri: ", ngx.var.uri)
|
|
|
|
local headers = ngx.req.get_headers()
|
2020-11-12 14:37:51 +08:00
|
|
|
|
|
|
|
local keys = {}
|
|
|
|
for k in pairs(headers) do
|
|
|
|
table.insert(keys, k)
|
|
|
|
end
|
|
|
|
table.sort(keys)
|
|
|
|
|
|
|
|
for _, key in ipairs(keys) do
|
|
|
|
ngx.say(key, ": ", headers[key])
|
2019-08-30 10:15:58 +08:00
|
|
|
end
|
|
|
|
end
|
2020-02-06 15:22:49 +08:00
|
|
|
_M.uri_plugin_proxy_rewrite = _M.uri
|
|
|
|
_M.uri_plugin_proxy_rewrite_args = _M.uri
|
2019-08-30 10:15:58 +08:00
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-08-30 10:15:58 +08:00
|
|
|
function _M.old_uri()
|
|
|
|
-- ngx.sleep(1)
|
|
|
|
ngx.say("uri: ", ngx.var.uri)
|
|
|
|
local headers = ngx.req.get_headers()
|
2020-11-12 14:37:51 +08:00
|
|
|
|
|
|
|
local keys = {}
|
|
|
|
for k in pairs(headers) do
|
|
|
|
table.insert(keys, k)
|
|
|
|
end
|
|
|
|
table.sort(keys)
|
|
|
|
|
|
|
|
for _, key in ipairs(keys) do
|
|
|
|
ngx.say(key, ": ", headers[key])
|
2019-08-30 10:15:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-17 23:52:15 +08:00
|
|
|
|
2019-07-26 17:16:31 +08:00
|
|
|
function _M.opentracing()
|
|
|
|
ngx.say("opentracing")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-11-21 21:49:53 +08:00
|
|
|
function _M.with_header()
|
|
|
|
--split into multiple chunk
|
|
|
|
ngx.say("hello")
|
|
|
|
ngx.say("world")
|
|
|
|
ngx.say("!")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-07-26 17:16:31 +08:00
|
|
|
function _M.mock_zipkin()
|
|
|
|
ngx.req.read_body()
|
|
|
|
local data = ngx.req.get_body_data()
|
|
|
|
local spans = json_decode(data)
|
|
|
|
if #spans < 5 then
|
|
|
|
ngx.exit(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, span in pairs(spans) do
|
|
|
|
if string.sub(span.name, 1, 6) ~= 'apisix' then
|
|
|
|
ngx.exit(400)
|
|
|
|
end
|
|
|
|
if not span.traceId then
|
|
|
|
ngx.exit(400)
|
|
|
|
end
|
2020-04-08 08:47:41 +08:00
|
|
|
|
|
|
|
if not span.localEndpoint then
|
|
|
|
ngx.exit(400)
|
|
|
|
end
|
|
|
|
|
2020-09-19 19:07:39 +08:00
|
|
|
if span.localEndpoint.serviceName ~= 'APISIX'
|
|
|
|
and span.localEndpoint.serviceName ~= 'apisix' then
|
2020-04-08 08:47:41 +08:00
|
|
|
ngx.exit(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
if span.localEndpoint.port ~= 1984 then
|
|
|
|
ngx.exit(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
if span.localEndpoint.ipv4 ~= ngx.req.get_uri_args()['server_addr'] then
|
|
|
|
ngx.exit(400)
|
|
|
|
end
|
|
|
|
|
2019-07-26 17:16:31 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-02-06 15:22:49 +08:00
|
|
|
function _M.wolf_rbac_login_rest()
|
|
|
|
ngx.req.read_body()
|
|
|
|
local data = ngx.req.get_body_data()
|
|
|
|
local args = json_decode(data)
|
|
|
|
if not args.username then
|
|
|
|
ngx.say(json_encode({ok=false, reason="ERR_USERNAME_MISSING"}))
|
|
|
|
ngx.exit(0)
|
|
|
|
end
|
|
|
|
if not args.password then
|
|
|
|
ngx.say(json_encode({ok=false, reason="ERR_PASSWORD_MISSING"}))
|
|
|
|
ngx.exit(0)
|
|
|
|
end
|
|
|
|
if args.username ~= "admin" then
|
|
|
|
ngx.say(json_encode({ok=false, reason="ERR_USER_NOT_FOUND"}))
|
|
|
|
ngx.exit(0)
|
|
|
|
end
|
|
|
|
if args.password ~= "123456" then
|
|
|
|
ngx.say(json_encode({ok=false, reason="ERR_PASSWORD_ERROR"}))
|
|
|
|
ngx.exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say(json_encode({ok=true, data={token="wolf-rbac-token",
|
|
|
|
userInfo={nickname="administrator",username="admin", id="100"}}}))
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-02-06 15:22:49 +08:00
|
|
|
function _M.wolf_rbac_access_check()
|
|
|
|
local headers = ngx.req.get_headers()
|
|
|
|
local token = headers['x-rbac-token']
|
|
|
|
if token ~= 'wolf-rbac-token' then
|
|
|
|
ngx.say(json_encode({ok=false, reason="ERR_TOKEN_INVALID"}))
|
|
|
|
ngx.exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
local args = ngx.req.get_uri_args()
|
|
|
|
local resName = args.resName
|
2020-03-31 13:39:49 +08:00
|
|
|
if resName == '/hello' or resName == '/wolf/rbac/custom/headers' then
|
2020-09-19 19:07:39 +08:00
|
|
|
ngx.say(json_encode({ok=true,
|
|
|
|
data={ userInfo={nickname="administrator",
|
|
|
|
username="admin", id="100"} }}))
|
2020-02-06 15:22:49 +08:00
|
|
|
else
|
|
|
|
ngx.status = 401
|
|
|
|
ngx.say(json_encode({ok=false, reason="no permission to access"}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-03-31 13:39:49 +08:00
|
|
|
function _M.wolf_rbac_user_info()
|
|
|
|
local headers = ngx.req.get_headers()
|
|
|
|
local token = headers['x-rbac-token']
|
|
|
|
if token ~= 'wolf-rbac-token' then
|
|
|
|
ngx.say(json_encode({ok=false, reason="ERR_TOKEN_INVALID"}))
|
|
|
|
ngx.exit(0)
|
|
|
|
end
|
|
|
|
|
2020-09-19 19:07:39 +08:00
|
|
|
ngx.say(json_encode({ok=true,
|
|
|
|
data={ userInfo={nickname="administrator", username="admin", id="100"} }}))
|
2020-03-31 13:39:49 +08:00
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-03-31 13:39:49 +08:00
|
|
|
function _M.wolf_rbac_change_pwd()
|
|
|
|
ngx.req.read_body()
|
|
|
|
local data = ngx.req.get_body_data()
|
|
|
|
local args = json_decode(data)
|
|
|
|
if args.oldPassword ~= "123456" then
|
|
|
|
ngx.say(json_encode({ok=false, reason="ERR_OLD_PASSWORD_INCORRECT"}))
|
|
|
|
ngx.exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say(json_encode({ok=true, data={ }}))
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-03-31 13:39:49 +08:00
|
|
|
function _M.wolf_rbac_custom_headers()
|
|
|
|
local headers = ngx.req.get_headers()
|
2020-09-19 19:07:39 +08:00
|
|
|
ngx.say('id:' .. headers['X-UserId'] .. ',username:' .. headers['X-Username']
|
|
|
|
.. ',nickname:' .. headers['X-Nickname'])
|
2020-03-31 13:39:49 +08:00
|
|
|
end
|
2020-02-06 15:22:49 +08:00
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-01-14 14:37:24 +08:00
|
|
|
function _M.websocket_handshake()
|
|
|
|
local websocket = require "resty.websocket.server"
|
|
|
|
local wb, err = websocket:new()
|
|
|
|
if not wb then
|
|
|
|
ngx.log(ngx.ERR, "failed to new websocket: ", err)
|
|
|
|
return ngx.exit(400)
|
|
|
|
end
|
|
|
|
end
|
2020-02-06 15:22:49 +08:00
|
|
|
_M.websocket_handshake_route = _M.websocket_handshake
|
2019-07-26 17:16:31 +08:00
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-10-27 13:40:23 +08:00
|
|
|
function _M.api_breaker()
|
|
|
|
ngx.exit(tonumber(ngx.var.arg_code))
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-10-27 13:40:23 +08:00
|
|
|
function _M.mysleep()
|
|
|
|
ngx.sleep(tonumber(ngx.var.arg_seconds))
|
|
|
|
ngx.say(ngx.var.arg_seconds)
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-09-16 10:19:04 +08:00
|
|
|
local function print_uri()
|
|
|
|
ngx.say(ngx.var.uri)
|
|
|
|
end
|
|
|
|
for i = 1, 100 do
|
|
|
|
_M["print_uri_" .. i] = print_uri
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2019-06-17 23:17:41 +08:00
|
|
|
function _M.go()
|
|
|
|
local action = string.sub(ngx.var.uri, 2)
|
2020-02-06 15:22:49 +08:00
|
|
|
action = string.gsub(action, "[/\\.]", "_")
|
2019-08-12 09:04:41 +08:00
|
|
|
if not action or not _M[action] then
|
2019-06-17 23:17:41 +08:00
|
|
|
return ngx.exit(404)
|
|
|
|
end
|
|
|
|
|
2020-09-27 16:43:09 +08:00
|
|
|
inject_headers()
|
2019-06-17 23:17:41 +08:00
|
|
|
return _M[action]()
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-08-01 08:03:22 +08:00
|
|
|
function _M.headers()
|
|
|
|
local args = ngx.req.get_uri_args()
|
|
|
|
for name, val in pairs(args) do
|
|
|
|
ngx.header[name] = nil
|
|
|
|
ngx.header[name] = val
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("/headers")
|
|
|
|
end
|
2019-06-17 23:17:41 +08:00
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-09-24 10:58:44 +08:00
|
|
|
function _M.log()
|
|
|
|
ngx.req.read_body()
|
|
|
|
local body = ngx.req.get_body_data()
|
2020-11-18 17:51:03 +08:00
|
|
|
local ct = ngx.var.content_type
|
|
|
|
if ct ~= "text/plain" then
|
|
|
|
body = json_decode(body)
|
|
|
|
body = json_encode(body)
|
|
|
|
end
|
2020-09-24 10:58:44 +08:00
|
|
|
ngx.log(ngx.WARN, "request log: ", body or "nil")
|
|
|
|
end
|
|
|
|
|
2020-11-13 22:27:03 +08:00
|
|
|
|
2020-11-27 18:33:02 +08:00
|
|
|
function _M.server_error()
|
|
|
|
error("500 Internal Server Error")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-17 23:17:41 +08:00
|
|
|
return _M
|