apisix网关ip-restriction插件使用说明

ops/2025/2/9 6:16:36/

ip-restriction插件可以在网关层进行客户端请求ip拦截。

当然了,一般不推荐使用该方法,专业的事专业工具做。建议有条件,还是上防火墙或者waf来做。

官方文档:ip-restriction | Apache APISIX® -- Cloud-Native API Gateway

whitelist:白名单,配置允许访问的ip。

blacklist:黑名单,配置禁止访问的ip。

估计有朋友要问了,我上面那个external_auth的配置节干什么用的。

这个由于业务需要,我们做了一个动态校验请求。就是拦截后会请求这个链接,软如参数,然后如果返回200,就自动放通,如果返回其他状态,就拦截。

这个是自己写脚本实现的。代码如下,其实就是改了ip-restriction的脚本,增加了一个参数。

---
apiVersion: v1
data:init.lua: |---- 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.--local ipairs    = ipairslocal core      = require("apisix.core")local http   = require("resty.http")local lrucache  = core.lrucache.new({ttl = 300, count = 512})local schema = {type = "object",properties = {message = {type = "string",minLength = 1,maxLength = 1024,default = "Your IP address is not allowed"},whitelist = {type = "array",items = {anyOf = core.schema.ip_def},minItems = 1},blacklist = {type = "array",items = {anyOf = core.schema.ip_def},minItems = 1},external_auth = {type = "object",properties = {uri = {type = "string"},timeout = {type = "integer",minimum = 1,maximum = 60000,default = 3000,description = "timeout in milliseconds",},method = {type = "string",default = "GET",enum = {"GET", "POST"},description = "the method for client to request the auth service"},headers = {type = "object"},ip_param_key = {type = "string", default = "ip"},allow_degradation = {type = "boolean", default = true},rejected_code = {type = "integer", minimum = 200, maximum = 599, default = 403},rejected_msg = {type = "string", minLength = 1}}}},anyOf = {{required = {"whitelist"}},{required = {"blacklist"}},{required = {"external_auth"}},}}local plugin_name = "ip-restriction"local _M = {version = 0.1,priority = 3000,name = plugin_name,schema = schema,}function _M.check_schema(conf)local ok, err = core.schema.check(schema, conf)if not ok thenreturn false, errend-- we still need this as it is too complex to filter out all invalid IPv6 via regexif conf.whitelist thenfor _, cidr in ipairs(conf.whitelist) doif not core.ip.validate_cidr_or_ip(cidr) thenreturn false, "invalid ip address: " .. cidrendendendif conf.blacklist thenfor _, cidr in ipairs(conf.blacklist) doif not core.ip.validate_cidr_or_ip(cidr) thenreturn false, "invalid ip address: " .. cidrendendendreturn trueendfunction _M.restrict(conf, ctx)local remote_addr = ctx.var.remote_addrlocal block = falseif conf.blacklist thenlocal matcher = lrucache(conf.blacklist, nil,core.ip.create_ip_matcher, conf.blacklist)if matcher thenblock = matcher:match(remote_addr)endendif block then-- 黑名单中的 ip 直接拒绝return 403, { message = conf.message }endlocal in_white = falseif conf.whitelist thenlocal matcher = lrucache(conf.whitelist, nil,core.ip.create_ip_matcher, conf.whitelist)if matcher thenin_white = matcher:match(remote_addr)endendif in_white then-- 白名单中的 ip 直接放行returnendif conf.external_auth thenlocal external_auth = conf.external_authlocal params = {method = external_auth.request_method}local httpc = http.new()httpc:set_timeout(external_auth.timeout)local uri = external_auth.ip_param_key .. '=' .. remote_addrif string.find(external_auth.uri, "?") thenuri = external_auth.uri .. "&" .. urielseuri = external_auth.uri .. "?" .. uriendlocal res, err = httpc:request_uri(uri, params)-- 校验 ip 的服务不可用的时候if not res thencore.log.error("failed to auth ip, err: ", err)if conf.external_auth.allow_degradation then-- 允许放行returnelsereturn external_auth.rejected_code, { message = conf.message }endend-- 返回值为 2xx 的时候表示校验通过if res.status >= 300 thenreturn external_auth.rejected_code, { message = conf.message }endendendreturn _M
kind: ConfigMap
metadata:name: ip-restrictionnamespace: apisix-szxc-qxz2v397g6resourceVersion: '224926381'

核心部分:

然后请求的接口,就可以自己编写了,相对更灵活。

对了,上面贴的是k8s的yaml,这是一个comfigmap,注入到apisix容器中,替换了原文件。

其实apisix的组件都是lua脚本实现的,很灵活,都可以根据需要自行重写。


http://www.ppmy.cn/ops/156908.html

相关文章

【机器学习】训练数据集和测试数据集的划分及KNN准确率测试

训练数据集和测试数据集的划分 一、摘要二、机器学习算法性能评估三、train test split的具体实现四、调用KNN算法进行准确率测试五、提升模型性能的策略思考 一、摘要 本博文围绕机器学习算法性能评估方法展开,重点介绍了训练数据集与测试数据集的分离&#xff08…

适用于 Windows 的 Zed 编辑器的非官方稳定版。通过 scoop 或 pwsh 脚本轻松安装。不隶属于 Zed Industries

一、软件介绍(文末提供下载) Zed,这是一款由 Atom 和 Tree-sitter 的创建者提供的高性能多人 Atom and Tree-sitter.。 二、macOS 和 Linux安装 在 macOS 和 Linux 上,您可以直接下载 Zed 或通过本地包管理器安装 Zed。 本地包…

003 Linux驱动开发——第一个简单开发实验

01 开发板的第 1 个 APP 实验 gcc -o hello hello.c ./hello Hello, world! ./hello weidongshan Hello, weidongshan!要想给 ARM 板编译出 hello 程序,需要使用交叉编译工具链 arm-buildroot-linux-gnueabihf-gcc -o hello hello.c 这样编译出来的 hello 程序才可以…

如何在Windows上使用Docker

引言 WSL2(Windows Subsystem for Linux2)是微软开发的一种技术,允许在 Windows 操作系统上运行 Linux 环境。它提供了一个兼容层,使得用户可以在 Windows 系统中直接运行 Linux 命令行工具、应用程序和开发工具,而无需…

【学术投稿-第五届消费电子与计算机工程国际学术会议】HTML核心元素详解:超链接、列表、表格与实用技巧

基本信息 大会官网:www.iccece.org 线下召开时间:2025年2月28-3月2日 目录 前言 一、超链接:连接万物的桥梁 1. 基础语法 2. 高级应用 3.代码案例​编辑 4. 注意事项 二、列表:结构化内容的利器 1. 有序列表(O…

边缘计算网关驱动智慧煤矿智能升级——实时预警、低延时决策与数字孪生护航矿山安全高效运营

迈向智能化煤矿管理新时代 工业物联网和边缘计算技术的迅猛发展,煤矿安全生产与高效运营正迎来全新变革。传统煤矿监控模式由于现场环境复杂、数据采集和传输延时较高,已难以满足当下高标准的安全管理要求。为此,借助边缘计算网关的实时数据…

ubuntu中 使用C++ FFmpeg拉取RTSP视频流

在C中使用FFmpeg拉取RTSP视频流涉及多个步骤,包括初始化FFmpeg库、打开RTSP流、读取帧数据等。以下是一个简单的示例代码,展示如何使用FFmpeg库拉取RTSP视频流并解码视频帧。 1. 安装FFmpeg库 首先,确保你已经安装了FFmpeg库。你可以通过以…

全面的生成式语言模型学习路线

设计了一套全面的生成式语言模型学习路线,包含基础储备、核心知识学习、实践应用和进阶提升四个阶段,你可以根据自身情况进行调整。 第一阶段:基础储备(1 - 2个月) 数学基础 线性代数 学习向量、矩阵的基本运算&…