以下是一个简单的基于C++的防火墙代码示例:
 

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Firewall {
private:
    vector<string> allowed_ips;
public:
    Firewall(vector<string> allowed_ips) {
        this->allowed_ips = allowed_ips;
    }

    bool is_allowed(string ip_address) {
        for (string allowed_ip : allowed_ips) {
            if (ip_address == allowed_ip) {
                return true;
            }
        }
        return false;
    }
};

int main() {
    vector<string> allowed_ips = {"192.168.0.1", "10.0.0.1"};
    Firewall firewall(allowed_ips);

    string ip_address;
    cout << "Enter IP address: ";
    cin >> ip_address;

    if (firewall.is_allowed(ip_address)) {
        cout << "Access granted" << endl;
    } else {
        cout << "Access denied" << endl;
    }

    return 0;
}

        这个防火墙类有一个构造函数,它需要一个允许的IP地址向量,并将其存储。它还有一个is_allowed()`方法,它接受一个IP地址,并将该地址与允许的地址进行比较。如果提供的IP是一个允许的IP,则返回true,否则返回false。

        在主函数中,我们实例化了一个防火墙对象并指定允许的IP地址。然后,我们要求用户提供一个IP地址,并使用`is_allowed()方法检查该地址是否允许访问。最后,我们输出一个访问授权或拒绝的消息。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐