Giter Club home page Giter Club logo

basic-firewall's Introduction

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

PHP Basic Firewall

Thư viện PHP Basic Firewall được xây dựng bằng PHP cung cấp 1 phương thức đơn giản để hạn chế quyền truy cập website / api / webservice dựa vào địa chỉ IP truy cập của người dùng.

Thông thường Firewall tốt nhất nên được thiết đặt từ tầng hệ thống: hệ điều hành có cài tường lửa như iptables , firewalld hay CSF.

Tuy nhiên, trong 1 số trường hợp DEV không có quyền can thiệp hệ thống hoặc không nắm chắc. Hoặc giả như muốn chủ động trong các tình huống thì có thể tích hợp gói PHP Basic Firewall này vào để thực hiện từ tầng Application.

Version Support

  • V1.x support all PHP version >=5.4
  • V2.x support all PHP version >=7.0

Hướng dẫn sử dụng

Cài đặt gói

Cài đặt gói Basic Firewall thông qua composer với lệnh như sau

composer require nguyenanhung/basic-firewall

Hướng dẫn tích hợp Firewall

Tham khảo cách tích hợp thông qua hướng dẫn tại đoạn code ví dụ dưới đây

<?php
/**
 * Project basic-firewall
 * Created by PhpStorm
 * User: 713uk13m <[email protected]>
 * Copyright: 713uk13m <[email protected]>
 * Date: 09/01/2021
 * Time: 00:50
 */
require_once __DIR__ . '/vendor/autoload.php';

use nguyenanhung\PhpBasicFirewall\FirewallIP;

// ==================================== Setup List IP Whitelist
// Setup constants HUNGNG_IP_WHITELIST
defined('HUNGNG_IP_WHITELIST') or define('HUNGNG_IP_WHITELIST', array(
    '127.0.0.1',
    '192.168.0.*',
));
// Or Whitelist Array
$whiteList = array(
    '127.0.0.1',
    '192.168.0.*',
);

// ==================================== Setup List IP Blacklist
// Setup constants HUNGNG_IP_BLACKLIST
defined('HUNGNG_IP_BLACKLIST') or define('HUNGNG_IP_BLACKLIST', array(
    '127.0.0.1',
    '192.168.0.*',
));
// Or Blacklist Array
$blackList = array(
    '192.168.0.50',
);

// ==================================== Start Firewall

$firewall = new FirewallIP();
$firewall->setLogDestination(__DIR__ . '/logs/FirewallLog.log')
         ->setIpWhiteList($whiteList)
         ->setIpBlackList($blackList)
         ->checkUserConnect(false);

if (true !== $firewall->isAccess()) {
    $firewall->writeErrorLog($firewall->errorLogMessage()); // Write log to /tmp/FirewallLog.log
    $firewall->accessDeniedResponse(); // Response 403 http code, Access Denied message
}

// ==================================== End Firewall

// Pass qua firewall sẽ là các đoạn code thực hiện nghiệp vụ của bạn

Trong ví dụ trên, chỉ những IP bắt đầu bằng 192.168.0 (loại trừ 192.168.0.50) và 127.0.0.1 sẽ được cho phép truy cập bởi Firewall. Tất cả các IP khác, firewall sẽ return false

  • checkUserConnect(false) khai báo true hoặc false để xác định mặc định firewall cho phép hay từ chối truy cập ( Optional - Default false). true nếu mặc định cho phép truy cập, false nếu mặc định từ chối
  • setIpWhiteList($whiteList) khai báo $whiteList IP list cho phép truy cập
  • setIpBlackList($blackList) khai báo $blackList IP list từ chối truy cập

IP List Formats

Firewall hỗ trợ input các IP whitelist và blacklist như sau

Type Syntax Details
IPV6 ::1 Hỗ trợ các viết tắt
IPV4 192.168.0.1
Range 192.168.0.0-192.168.1.60 Bao gồm tất cả các IP từ 192.168.0.0 đến 192.168.0.255
và từ 192.168.1.0 đến 198.168.1.60
Wild card 192.168.0.* Tất cả IP bắt đầu bằng 192.168.0
Nó tương tự với cách khai báo 192.168.0.0-192.168.0.255
Subnet mask 192.168.0.0/255.255.255.0 Tất cả IP bắt đầu bằng 192.168.0
Nó tương tự với cách khai báo 192.168.0.0-192.168.0.255
192.168.0.*
CIDR Mask 192.168.0.0/24 Tất cả IP bắt đầu bằng 192.168.0
Nó tương tự với cách khai báo 192.168.0.0-192.168.0.255
192.168.0.* cũng như 192.168.0.0/255.255.255.0

Hướng dẫn tích hợp hàm tiện ích Check System

Gói này cung cấp thêm 1 lớp tiện ích, dùng kiểm tra mạng, extension hoặc kiểm tra kết nối tới MySQL Database. Tham khảo cách sử dụng dưới đây

<?php
/**
 * Project basic-firewall
 * Created by PhpStorm
 * User: 713uk13m <[email protected]>
 * Copyright: 713uk13m <[email protected]>
 * Date: 09/01/2021
 * Time: 00:55
 */
require_once __DIR__ . '/../vendor/autoload.php';

use nguyenanhung\PhpBasicFirewall\CheckSystem;

$system = new CheckSystem();

// Kiểm tra phiên bản PHP
$system->phpVersion();

// Kiểm tra các extension cần thiết
$system->checkExtension('curl');
$system->checkExtension('pdo');
$system->checkExtension('mysqli');
$system->checkExtension('gd');
$system->checkExtension('mbstring');
$system->checkExtension('json');
$system->checkExtension('session');
$system->checkExtension('sockets');
$system->checkExtension('bcmath');

// Kiểm tra kết nối tới 1 server nào đó
$system->phpTelnet('127.0.0.1', 3306);
$system->phpTelnet('127.0.0.1', 2842);

// Kiểm tra kết nối CSDL
$system->checkConnectDatabase('127.0.0.1', '3306', 'my_data', 'root', 'hungna');

LICENSE

Gói được phân phối bởi giấy phép License, tham khảo chi tiết giấy phép tại đây

Gói có sử dụng packages m6web/firewall được cung cấp bởi M6Web, bạn cũng có thể sử dụng riêng gói này theo đường link tại đây

Hỗ trợ

Nếu có bất kì câu hỏi hoặc cần hỗ trợ nào, liên hệ theo thông tin sau

Name Email Skype Facebook
Hung Nguyen [email protected] nguyenanhung5891 @nguyenanhung

From Vietnam with Love ❤️

basic-firewall's People

Contributors

hungnguyenhp avatar nguyenanhung avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.