MetaCubeX / mihomo

A simple Python Pydantic model for Honkai: Star Rail parsed data from the Mihomo API.
https://wiki.metacubex.one
MIT License
16.84k stars 2.67k forks source link

[Feature] Allow the rules field to accept an optional list for switching rules at runtime, e.g. [rules, sub-rule] #1566

Closed MNDIA closed 1 month ago

MNDIA commented 1 month ago

Verify steps

Description

"什么功能?" Allow the rules field to accept an optional list for switching rules at runtime, e.g. [rules, sub-rule], rather than just a fixed value rules / my-sub-rule. Like proxy-groups switches the proxy inside, runtime switches a validated rule in the rule list of the listener

"目前 Mihomo Core 的行为是什麽?"

listeners: # It's the right thing to do for now.
- name: tproxy-in
  type: tproxy
  port: *TroxyPort
  listen: 127.0.0.1
  udp: true
  rule: rules
listeners:
- name: tproxy-in
  type: tproxy
  port: *TroxyPort
  listen: 127.0.0.1
  udp: true
  rule: [rules,rule-reqable] # This won't work, the value of the rule key will be recognized as a string instead one of obj.rule objects

- name: socks-in
  type: socks
  port: *SockPort
  listen: 127.0.0.1
  udp: true
  rule: rules # default

proxies: 
- name: Reqable
  type: socks5
  server: 127.0.0.1
  port: *ReqablePort
  udp: true

sub-rules:
  rule-reqable:
    - MATCH,Reqable

"这个问题如何影响到你?" Originally the traffic Path was routed to

[device kernel]<tproxy port>[mihomo-rules]<>[outgoing server]

On top of that, we need to capture the web packets. (Or insert other treatments into mihomo) The Path :

[device kernel]<tproxy port>[mihomo-rule-reqable <proxies:socks>                    <listeners:socks-in> mihomo-rules]<>[outgoing server]
                                                      < socks port> [reqable-catch] <stocks port>

image

The process of switching between the two paths, requires runtime changes: [tproxy port-mihomo-rules]->[tproxy port-mihomo-rule-reqable]

Global/rules mode can do runtime switching, very similar but different. The functionality is that an inbound traffic needs to be diverted from complex rules to a node (like global mode), but the problem is that all the inbound stations of mihomo are in a shared (rules/global) mode, and it is not possible to keep a tproxy all to one node (like switching the global), and at the same time keep a stocks still using complex rules.

Possible Solution

Like proxy-groups switches the proxy inside. In runtime switches to a validated rule in the rule list of the listener

List

reqable

xishang0128 commented 1 month ago

Using PASS in sub-rules

MNDIA commented 1 month ago

Thank you very much for your help. I'll leave a PASS example for those who follow up looking for related issues. The only current mihomo that enables runtime switching is proxy-groups

TroxyInPort: &TroxyInPort 65535
ReqableOutPort: &ReqableOutPort 9000
ReqableInPort: &ReqableInPort 65529

listeners:
- name: tproxy-in      # This entry point enters all the traffic from the device kernel (no loopback) 
  type: tproxy
  port: *TroxyInPort
  listen: 127.0.0.1
  udp: true
  rule: rules #default

- name: http-in        # Accepting post-capture proxies from reqable
  type: http
  port: *ReqableInPort
  listen: 127.0.0.1
  rule: rules #default

proxies: 
  - name: Reqable     # Send traffic to reqable via socks
    type: socks5
    server: 127.0.0.1
    port: *ReqableOutPort
    udp: true

proxy-groups:         # proxy-groups, the only kind of thing that can be switched without affecting other proxies at runtime
  - {name: InboundBridge , type: select, proxies: [PASS,Reqable]}

rules:
  - IP-CIDR,100.64.0.0/10,Tailscale,no-resolve
  - DOMAIN-SUFFIX,.ts.net,Tailscale
  - GEOIP,lan,DIRECT,no-resolve
  - RULE-SET,loopback_classical,LoopOutlet # An insurance policy against loopbacks, that should never go into effect
  - IN-NAME,tproxy-in,InboundBridge        # If required, Send remaining traffic to reqable via socks
  - ......Many complex diversions