PBH-BTN / PeerBanHelper

自动封禁不受欢迎、吸血和异常的 BT 客户端,并支持自定义规则。PeerId黑名单/UserAgent黑名单/IP CIDR/假进度/超量下载/进度回退/多播追猎/连锁封禁/伪装检测 支持 qBittorrent/Transmission/Deluge/BiglyBT
GNU General Public License v3.0
539 stars 15 forks source link

新增基于状态机模型的规则模块 #160

Closed PluieM closed 1 week ago

PluieM commented 2 weeks ago

主要有两点更新 1、按照 #156 中 @Gaojianli 提供的思路 新增基于状态机模型的规则模块,并基于这个模块重构了订阅规则模块和PeerID黑名单模块 2、#155 拓展规则订阅支持的规则类型,现在支持如下几种类型IP/PEER_ID_CONTAINS/PEER_ID_STARTS_WITH/CLIENT_NAME_CONTAINS/CLIENT_NAME_STARTS_WITH

Summary by CodeRabbit

coderabbitai[bot] commented 2 weeks ago

Walkthrough

The changes introduced a new state machine-based banning mechanism by adding multiple classes and modifying existing ones to manage peer connections and bans more efficiently. Key additions include constructing state machines, updating peer conditions, and enhanced rule matching. The updated logic significantly improves how peer bans are managed, focusing on modular rule handling and better configuration.

Changes

File Path Change Summary
pom.xml Added a new dependency for state machine functionality.
src/main/java/com/ghostchu/.../PeerBanHelperServer.java Added state machine logic, new fields, and refactored existing methods for better peer management.
src/main/java/com/ghostchu/.../config/ProfileUpdateScript.java Introduced and modified methods for updating module configuration settings.
src/main/java/com/ghostchu/.../database/DatabaseHelper.java Renamed IPBanRuleUpdateType to RuleUpdateType affecting method signatures and usage.
src/main/java/com/ghostchu/.../database/RuleSubLog.java Updated to reflect the renaming of rule update types and affected fields.
src/main/java/com/ghostchu/.../module/AbstractRuleBlocker.java Introduced an abstract class with methods for rule blocking and state machine management.
src/main/java/com/ghostchu/.../module/MatchEvents.java Added enum to define various match events for state transitions.
src/main/java/com/ghostchu/.../module/MatchResultDetail.java Added new class for capturing match result details with context and reasons.
src/main/java/com/ghostchu/.../module/PeerMatchContext.java Added new class encapsulating peer match operations with state machine.
src/main/java/com/ghostchu/.../module/PeerState.java Added enum defining various states of a peer in the state machine.
src/main/java/com/ghostchu/.../module/RuleBlocker.java Added interface defining methods for rule-based blocking and state machine interactions.
src/main/java/com/ghostchu/.../impl/rule/PeerIdBlocker.java Implemented class for blocking peers based on IDs with configuration parsing and rule evaluations.
src/main/java/com/ghostchu/.../impl/rule/RuleSubBlocker.java Renamed and refactored old class to improve rule handling and added new matcher types.
src/main/java/com/ghostchu/.../impl/webapi/RuleSubController.java Refactored API module methods and updated to reflect renamed classes and types.
src/main/java/com/ghostchu/.../text/Lang.java Updated multiple strings related to subscription rules and added new entries.
src/main/java/com/ghostchu/.../util/rule/RuleMatcher.java Introduced new utility for rule matching.

Sequence Diagrams

New State Machine-Based Peer Ban Workflow

sequenceDiagram
    actor Admin
    participant PeerBanHelperServer
    participant RuleBlocker
    participant StateMachine
    participant PeerMatchRecord

    Admin ->> PeerBanHelperServer: Initialize Server
    PeerBanHelperServer ->> RuleBlocker: registerModules()
    RuleBlocker ->> StateMachine: Initialize StateMachine
    RuleBlocker ->> PeerMatchRecord: Create matchRecords map

    PeerMatchRecord ->> StateMachine: Evaluate match with rules
    StateMachine ->> PeerMatchRecord: Update state based on events

    RuleBlocker ->> PeerBanHelperServer: Update peer state
    PeerBanHelperServer ->> Admin: Inform about ban wave

Poem

In the land of code and peers, Where modules and state machines steer, A rabbit hops through lines of might, Banning those who flee in fright. Rules are strict, yet fair in kind, Connection safe, with peace of mind.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
Gaojianli commented 2 weeks ago

这可真是个大的变更,能不能多描述一下状态机的实现


该邮件从移动设备发送

--------------原始邮件-------------- 发件人:"PluieM @.>; 发送时间:2024年6月19日(星期三) 晚上9:04 收件人:"PBH-BTN/PeerBanHelper" @.>; 抄送:"高渐离 @.>;"Mention @.>; 主题:[PBH-BTN/PeerBanHelper] 新增基于状态机模型的规则模块 (PR #160)

主要有两点更新 1、按照 #156 中 @Gaojianli 提供的思路 新增基于状态机模型的规则模块,并基于这个模块重构了订阅规则模块和PeerID黑名单模块 2、#155 拓展规则订阅支持的规则类型,现在支持如下几种类型IP/PEER_ID_CONTAINS/PEER_ID_STARTS_WITH/CLIENT_NAME_CONTAINS/CLIENT_NAME_STARTS_WITH

You can view, comment on, or merge this pull request online at:

  https://github.com/PBH-BTN/PeerBanHelper/pull/160

Commit Summary

2f7f213 1、新增一个基于状态机模型的规则模块

b0cce39 规则订阅支持订阅以下匹配类型 IP/PEER_ID_CONTAINS/PEER_ID_STARTS_WITH/CLIENT_NAME_CONTAINS/CLIENT_NAME_STARTS_WITH

File Changes

(22 files)

 M     pom.xml     (5)   

 M     src/main/java/com/ghostchu/peerbanhelper/PeerBanHelperServer.java     (109)   

 M     src/main/java/com/ghostchu/peerbanhelper/config/ProfileUpdateScript.java     (11)   

 M     src/main/java/com/ghostchu/peerbanhelper/database/DatabaseHelper.java     (6)   

 M     src/main/java/com/ghostchu/peerbanhelper/database/RuleSubLog.java     (4)   

 A     src/main/java/com/ghostchu/peerbanhelper/module/AbstractRuleBlocker.java     (43)   

 A     src/main/java/com/ghostchu/peerbanhelper/module/MatchEvents.java     (27)   

 A     src/main/java/com/ghostchu/peerbanhelper/module/MatchResultDetail.java     (5)   

 A     src/main/java/com/ghostchu/peerbanhelper/module/PeerMatchRecord.java     (21)   

 A     src/main/java/com/ghostchu/peerbanhelper/module/PeerState.java     (27)   

 A     src/main/java/com/ghostchu/peerbanhelper/module/RuleBlocker.java     (115)   

 R     src/main/java/com/ghostchu/peerbanhelper/module/RuleUpdateType.java     (2)   

 A     src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/PeerIdBlocker.java     (65)   

 R     src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/RuleSubBlocker.java     (248)   

 M     src/main/java/com/ghostchu/peerbanhelper/module/impl/webapi/RuleSubController.java     (90)   

 M     src/main/java/com/ghostchu/peerbanhelper/text/Lang.java     (59)   

 A     src/main/java/com/ghostchu/peerbanhelper/util/rule/RuleMatcher.java     (35)   

 A     src/main/java/com/ghostchu/peerbanhelper/util/rule/RuleType.java     (27)   

 R     src/main/java/com/ghostchu/peerbanhelper/util/rule/matcher/IPMatcher.java     (31)   

 A     src/main/java/com/ghostchu/peerbanhelper/util/rule/matcher/PrefixMatcher.java     (48)   

 A     src/main/java/com/ghostchu/peerbanhelper/util/rule/matcher/SubStrMatcher.java     (48)   

 M     src/main/resources/profile.yml     (6)   

Patch Links:

https://github.com/PBH-BTN/PeerBanHelper/pull/160.patch

https://github.com/PBH-BTN/PeerBanHelper/pull/160.diff

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

Ghost-chu commented 2 weeks ago

PBH 对于 ClientName、PeerID 和 IP 匹配(本地)都有缓存机制,检查只会执行一次,我在考虑这个状态机是否放在其它地方会好一点。

Gaojianli commented 2 weeks ago

状态机的优点是近似检测,比如如果不命中XL规则,那么XL0019就不用看了


该邮件从移动设备发送

--------------原始邮件-------------- 发件人:"Ghost_chu @.>; 发送时间:2024年6月20日(星期四) 中午1:19 收件人:"PBH-BTN/PeerBanHelper" @.>; 抄送:"高渐离 @.>;"Mention @.>; 主题:Re: [PBH-BTN/PeerBanHelper] 新增基于状态机模型的规则模块 (PR #160)

PBH 对于 ClientName、PeerID 和 IP 匹配(本地)都有缓存机制,检查只会执行一次,我在考虑这个状态机是否放在其它地方会好一点。

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

Gaojianli commented 1 week ago

@PluieM 简单看了下代码,前缀树的构造在哪个部分呀,没找到呀

PluieM commented 1 week ago

还没调整完毕,容我稍后画图解释

Ghost-chu commented 1 week ago

@coderabbitai 详细解释此PR,如果可以的话,请再画一个图

coderabbitai[bot] commented 1 week ago

[!TIP] For best results, initiate chat on the files or code changes.

@Ghost-chu, here's a detailed explanation of the changes introduced in PR #160, along with a diagram to visually represent the new architecture.

Detailed Explanation

This PR introduces a significant update to the PeerBanHelper project, focusing on enhancing the rule-based peer banning system through a state machine model and expanding the rule types supported by the system.

  1. State Machine-Based Rule Module:

    • A new state machine model (AbstractRuleBlocker) has been introduced. This model serves as the foundation for handling different states of peer connections and deciding the actions based on various match events.
    • The state machine transitions between states like INIT, MATCH, BAN, ACTIVE, and END based on events such as MATCH, HIT, PASS, DISCONNECT, and TIMEOUT.
  2. Refactoring of Modules:

    • The subscription rule module and the PeerID blacklist module have been refactored to utilize the new state machine model. This allows for more dynamic and efficient handling of peer states and rule matching.
  3. Expansion of Rule Types:

    • The system now supports additional rule types such as IP, PEER_ID_CONTAINS, PEER_ID_STARTS_WITH, CLIENT_NAME_CONTAINS, and CLIENT_NAME_STARTS_WITH. This enhancement broadens the scope of the rules that can be applied, making the system more versatile.
  4. Integration with Existing Components:

    • Changes have been made across various components like PeerBanHelperServer, ProfileUpdateScript, and DatabaseHelper to integrate the new rule module and support the expanded rule types.

Architectural Diagram

Below is a diagram illustrating the integration of the state machine model with the rule modules:

flowchart TD
    A[PeerBanHelperServer] -->|Manages| B[AbstractRuleBlocker]
    B -->|Utilizes| C[StateMachine]
    C --> D[PeerState]
    D --> E[MatchEvents]
    B -->|Controls| F[RuleBlocker]
    F -->|Implements| G[PeerIdBlocker]
    F -->|Implements| H[RuleSubBlocker]
    C -->|Triggers| I[MatchResultDetail]
    I --> J[PeerMatchRecord]

This diagram shows how PeerBanHelperServer manages instances of AbstractRuleBlocker, which utilize a StateMachine to handle different PeerStates based on MatchEvents. The RuleBlocker interface is implemented by specific blockers like PeerIdBlocker and RuleSubBlocker, which are responsible for the actual rule enforcement based on the state transitions.

I hope this provides a clear understanding of the changes and the architecture introduced in this PR. If you have any more questions or need further clarification, feel free to ask!

Ghost-chu commented 1 week ago

@coderabbitai 详细解释此PR,如果可以的话,请再画一个图

Ghost-chu commented 1 week ago

@coderabbitai 请详细描述一下 Peer 在此 PR 是如何匹配的。简述各种情况下状态机发生的变化。

coderabbitai[bot] commented 1 week ago

[!TIP] For best results, initiate chat on the files or code changes.

@Ghost-chu, as requested, here's a more detailed explanation of the changes introduced in PR #160, along with a diagram to visually represent the new architecture.

Detailed Explanation

This PR introduces a significant update to the PeerBanHelper project, focusing on enhancing the rule-based peer banning system through a state machine model and expanding the rule types supported by the system.

  1. State Machine-Based Rule Module:

    • A new state machine model (AbstractRuleBlocker) has been introduced. This model serves as the foundation for handling different states of peer connections and deciding the actions based on various match events.
    • The state machine transitions between states like INIT, MATCH, BAN, ACTIVE, and END based on events such as MATCH, HIT, PASS, DISCONNECT, and TIMEOUT.
  2. Refactoring of Modules:

    • The subscription rule module and the PeerID blacklist module have been refactored to utilize the new state machine model. This allows for more dynamic and efficient handling of peer states and rule matching.
  3. Expansion of Rule Types:

    • The system now supports additional rule types such as IP, PEER_ID_CONTAINS, PEER_ID_STARTS_WITH, CLIENT_NAME_CONTAINS, and CLIENT_NAME_STARTS_WITH. This enhancement broadens the scope of the rules that can be applied, making the system more versatile.
  4. Integration with Existing Components:

    • Changes have been made across various components like PeerBanHelperServer, ProfileUpdateScript, and DatabaseHelper to integrate the new rule module and support the expanded rule types.

Architectural Diagram

Below is a diagram illustrating the integration of the state machine model with the rule modules:

flowchart TD
    A[PeerBanHelperServer] -->|Manages| B[AbstractRuleBlocker]
    B -->|Utilizes| C[StateMachine]
    C --> D[PeerState]
    D --> E[MatchEvents]
    B -->|Controls| F[RuleBlocker]
    F -->|Implements| G[PeerIdBlocker]
    F -->|Implements| H[RuleSubBlocker]
    C -->|Triggers| I[MatchResultDetail]
    I --> J[PeerMatchRecord]

This diagram shows how PeerBanHelperServer manages instances of AbstractRuleBlocker, which utilize a StateMachine to handle different PeerStates based on MatchEvents. The RuleBlocker interface is implemented by specific blockers like PeerIdBlocker and RuleSubBlocker, which are responsible for the actual rule enforcement based on the state transitions.

I hope this provides a clear understanding of the changes and the architecture introduced in this PR. If you have any more questions or need further clarification, feel free to ask!

coderabbitai[bot] commented 1 week ago

[!TIP] For best results, initiate chat on the files or code changes.

@Ghost-chu, 在此 PR 中,Peer 的匹配和状态机的变化是通过新引入的基于状态机模型的规则模块来管理的。这里是详细的解释:

Peer 匹配过程

  1. 初始化 (INIT): 当 Peer 连接时,它首先进入 INIT 状态。
  2. 规则匹配: 根据配置的规则(如 IP、PEER_ID_CONTAINS、PEER_ID_STARTS_WITH、CLIENT_NAME_CONTAINS 和 CLIENT_NAME_STARTS_WITH),系统会检查每个 Peer 是否符合任何规则。
  3. 状态转换:
    • 如果 Peer 匹配某个规则,状态机将触发 MATCH 事件,并可能根据规则的具体要求转移到 BAN 状态。
    • 如果 Peer 不匹配任何规则,状态机可能触发 PASS 事件,保持在 ACTIVE 状态或转移到其他状态,这取决于系统的配置和规则。

状态机的变化

这个状态机模型允许系统灵活地处理各种 Peer 状态和事件,确保根据实时数据动态调整 Peer 的处理。如果您需要更多信息或有其他问题,请随时提问!

Ghost-chu commented 1 week ago

@coderabbitai summary

coderabbitai[bot] commented 1 week ago
Actions performed Summary regeneration triggered.
Gaojianli commented 1 week ago

感觉没对齐,能看下qq群吗? @PluieM 我说的是类似NFA的文法状态机 9c571254a47f3b770452a728b7707404