SDK Routing

Introduction

The SDK boasts a robust routing mechanism underpinned by V2Ray. This documentation delves deep into the intricacies of configuring and understanding the SDK's routing paradigm. By its conclusion, you'll have acquired the tools to manipulate inbound connection routes based on your bespoke requirements.

Starting the VPN Service with SDK

The SDK furnishes users with a start function to initiate the VPN service. One of the pivotal features of this function is its ability to accept custom routing rules through the altRules parameter. By leveraging this parameter, users can provide a JSON-formatted string encompassing their desired routing rules.

V2Ray Routing Mechanism

Every inbound connection is intelligently routed to various outbounds based on user-defined rules. This can be extremely useful in scenarios like diverting traffic by country using Geo IP.

Setting Rules

Rules are defined using the rules array. For every inbound connection, the system sequentially checks each rule. Once a rule is matched, the connection is routed based on the outboundTag specified in that rule.

Structure of a Rule Object:

{
  "type": "field",
  "domain": [...],
  "ip": [...],
  "port": "...",
  "network": "tcp",
  "source": [...],
  "outboundTag": "..."
}

Fields Explained:

  • type: Currently, the only accepted value is "field".

  • domain: Specifies the domains that this rule will be applicable for. Multiple formats are supported including plaintext, regular expression, subdomain, full domain, pre-defined domain list, and domains from external files.

  • ip: Defines the IP ranges. If the targeted IP falls within these ranges, the rule is applicable. Formats supported include standard IPs, CIDR, GeoIP, and IPs from external files.

  • port: Defines the port range for the rule. Can be a range (e.g., "1000-2000") or specific ports separated by commas (e.g., "53,443,1000-2000").

  • outboundTag: This is the tag of the outbound where the connection will be routed if the rule is matched.

Pre-defined Domain Lists

Our system also uses the domain lists from the domain-list-community project. These lists are particularly useful for commonly accessed domains or when blocking ad domains.

Noteworthy lists include:

  • google: All Google domains.
  • facebook: All Facebook domains.
  • speedtest: Domains used by Speedtest.
  • category-ads: Common ad domains. ... and many more.

Outbounds Explained

SDK comes with predefined outbounds:

  • Proxy: Here, the traffic is routed through the connected server.
  • Direct: The traffic bypasses the proxy and connects directly.
  • Reject: The traffic is blocked. It's typically used to block ads.

Examples

  1. Routing YouTube Directly & All Other Traffic Through Proxy

    In this configuration, all YouTube traffic will bypass the proxy and connect directly, while all other traffic will be routed through the proxy.

    
    {
        "domainStrategy": "IPIfNonMatch",
        "rules":
        [
            {
                "domain":
                [
                    "geosite:youtube"
                ],
                "outboundTag": "Direct",
                "type": "field"
            },
            {
                "ip":
                [
                    "0.0.0.0/0"
                ],
                "outboundTag": "Proxy",
                "type": "field"
            }
        ]
    }
    
  2. Routing Only Facebook & Instagram Through Proxy

    In this setup, only traffic to Facebook and Instagram will be routed through the proxy, with all other traffic connecting directly.

    
    {
        "domainStrategy": "IPIfNonMatch",
        "rules":
        [
            {
                "domain":
                [
                    "geosite:facebook",
                    "geosite:instagram"
                ],
                "outboundTag": "Proxy",
                "type": "field"
            },
            {
                "ip":
                [
                    "0.0.0.0/0"
                ],
                "outboundTag": "Direct",
                "type": "field"
            }
        ]
    }
    
  3. Ad Blocking Configuration

    This setup is designed to block ad traffic while allowing other traffic to pass through the proxy. It uses a predefined list of common ad domains (geosite:category-ads) to reject connections, ensuring an ad-free experience.

    
    {
        "domainStrategy": "IPIfNonMatch",
        "rules":
        [
            {
                "ip":
                [
                    "0.0.0.0/0"
                ],
                "outboundTag": "Proxy",
                "type": "field"
            },
            {
                "domain":
                [
                    "geosite:category-ads"
                ],
                "outboundTag": "Reject",
                "type": "field"
            }
        ]
    }
    

Conclusion

By understanding and utilizing the SDK's routing mechanism, users can optimize the performance and security of their connections. Whether it's by efficiently routing traffic through proxies, bypassing them for specific sites, or blocking unwanted ads, the SDK offers a robust solution for advanced networking needs.

Remember, effective routing not only enhances user experience but also ensures optimal utilization of resources. Customize your rules and make the most out of your connections!

  • 3 Users Found This Useful
Was this answer helpful?

Related Articles

Get Connection Log

Function: apivpn_get_connection_log_file This function returns the path to the connection log...