• Go语言中实现应用IP防火墙


    简介

    在公司里面经常会听到某应用有安全漏洞问题,没有做安全加固,IP防火墙就是一个典型的安全加固解决方案,只允许指定的ip段访问应用,一般是内网ip

    本文主要讲解go语言如何实现ip防火墙

    标准库实现

    其实go的net包以及有相应的实现,我们只需要简单应用即可

    源码如下

    // ParseCIDR parses s as a CIDR notation IP address and prefix length,
    // like "192.0.2.0/24" or "2001:db8::/32", as defined in
    // RFC 4632 and RFC 4291.
    //
    // It returns the IP address and the network implied by the IP and
    // prefix length.
    // For example, ParseCIDR("192.0.2.1/24") returns the IP address
    // 192.0.2.1 and the network 192.0.2.0/24.
    func ParseCIDR(s string) (IP, *IPNet, error)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    意思就是我们可以设置一个IP段例如 192.0.2.1/24 那么192.0.2.1就会在这个范围内,而 192.0.3.1 不在这个范围内应该做相应的处理

    代码实现

    package main
    
    import (
    	"fmt"
    	"net"
    	"strings"
    )
    
    func main() {
    	ipWall := NewFireWall()
    	// 设置可以访问应用的ip段
    	ipWall.ParseNode("127.0.0.1")
    	ipWall.ParseNode("192.0.2.1/24")
    	ipWall.ParseNode("2001:db8::/32") // 可以支持ipv6
    
    	// 测试
    	fmt.Println("127.0.0.1", ipWall.Check("127.0.0.1"))
    	fmt.Println("192.0.2.10", ipWall.Check("192.0.2.10"))
    	fmt.Println("192.0.3.10", ipWall.Check("192.0.3.10"))
    	fmt.Println("2001:db8::1", ipWall.Check("2001:db8::1"))
    	fmt.Println("2001:db9::1", ipWall.Check("2001:db9::1"))
    
    }
    
    // 定义防火墙,保存规则nodes
    
    type FireWall struct {
    	nodes []net.IPNet
    }
    
    func NewFireWall() *FireWall {
    	return &FireWall{
    		nodes: make([]net.IPNet, 0),
    	}
    }
    
    // 添加规则
    
    func (b *FireWall) ParseNode(line string) {
    	if !strings.Contains(line, "/") {
    		parsedIP := net.ParseIP(line)
    
    		if ipv4 := parsedIP.To4(); ipv4 != nil {
    			// return ip in a 4-byte representation
    			parsedIP = ipv4
    		}
    		if parsedIP != nil {
    			switch len(parsedIP) {
    			case net.IPv4len:
    				line += "/32"
    			case net.IPv6len:
    				line += "/128"
    			}
    		}
    	}
    	_, cidrNet, err := net.ParseCIDR(line)
    	if err == nil {
    		b.nodes = append(b.nodes, *cidrNet)
    	}
    }
    
    // 检查某个ip在不在设置的规则里
    
    func (b *FireWall) Check(ip string) bool {
    	for _, cidr := range b.nodes {
    		remoteIP := net.ParseIP(ip)
    		if cidr.Contains(remoteIP) {
    			return true
    		}
    	}
    	return false
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    执行以上代码,输出

    127.0.0.1 true
    192.0.2.10 true
    192.0.3.10 false
    2001:db8::1 true
    2001:db9::1 false
    
    • 1
    • 2
    • 3
    • 4
    • 5

    以上就是ip防火墙的实现了,在gin框架里也是这么实现的


    欢迎关注,学习不迷路!

  • 相关阅读:
    R统计绘图-线性混合效应模型详解(理论、模型构建、检验、选择、方差分解及结果可视化)
    MySQL基础
    UnrealEngine源码下载
    django学习入门系列之第二点《浏览器能识别的标签3》
    什么是SystemUI
    Java中的深拷贝与浅拷贝
    计算机毕业设计ssm社区基层党建管理信息系统s5738系统+程序+源码+lw+远程部署
    [代码随想录]二叉树篇
    算法沉淀——穷举、暴搜、深搜、回溯、剪枝综合练习二(leetcode真题剖析)
    网页url完整请求流程介绍
  • 原文地址:https://blog.csdn.net/weixin_44412085/article/details/134517230