# CFnew Proxy Region Priority: How the Smart Selection Algorithm Works

> Discover how CFnew's smart selection algorithm prioritizes proxy IPs. Learn about its intelligent region selection process for optimal performance.

- Repository: [byJoey/cfnew](https://github.com/byJoey/cfnew)
- Tags: internals
- Published: 2026-05-23

---

**CFnew prioritizes proxy IPs by first selecting the worker's own region, then checking geographically nearby regions, and finally falling back to a fixed global sequence of US → SG → JP → KR → DE → SE → NL → FI → GB.**

The open-source repository `byJoey/cfnew` implements a latency-optimized routing system through its **smart selection algorithm**. Understanding the **proxy region priority** logic helps developers predict which egress IP the worker will select and troubleshoot geographic routing configurations.

## How the Algorithm Constructs Region Priority

CFnew builds a hierarchical priority list for each worker request using a three-tier fallback system defined in the main source file `明文源吗`.

### Tier 1: Worker Region Identity

The algorithm begins by placing the worker's current region at the highest priority. If the worker is deployed in `DE` (Germany), German proxy IPs are checked first.

### Tier 2: Nearby Region Mapping

After the worker region, the system consults `getNearbyRegions(region)` (lines 45-58 in `明文源吗`) to determine geographic neighbors. The proximity map is hardcoded as follows:

| Region | Nearby Regions |
|--------|----------------|
| US | SG, JP, KR |
| SG | JP, KR, US |
| JP | SG, KR, US |
| KR | JP, SG, US |
| DE | NL, GB, SE, FI |
| SE | DE, NL, FI, GB |
| NL | DE, GB, SE, FI |
| FI | SE, DE, NL, GB |
| GB | DE, NL, SE, FI |

These mappings reflect latency-optimized regional clusters rather than strict geographic distance.

### Tier 3: Fixed Global Order

Remaining regions are appended in a static order defined at lines 361-365 inside `getAllRegionsByPriority`:

```js
function getAllRegionsByPriority(region) {
    const nearbyRegions = getNearbyRegions(region);
    const allRegions = ['US', 'SG', 'JP', 'KR', 'DE', 'SE', 'NL', 'FI', 'GB'];
    // worker region first, then its nearby regions, then the rest
    return [region, ...nearbyRegions,
            ...allRegions.filter(r => r !== region && !nearbyRegions.includes(r))];
}

```

This produces a final array where the worker region leads, followed by its nearby set, followed by all other regions in the sequence above.

## IP Sorting Implementation

The actual sorting occurs in `getSmartRegionSelection` at lines 68-83 of `明文源吗`. This function iterates over the priority list generated by `getAllRegionsByPriority` and filters the available proxy IPs to match that exact sequence:

```js
function getSmartRegionSelection(workerRegion, availableIPs, useRegionMatching = enableRegionMatching) {
    if (!useRegionMatching || !workerRegion) return availableIPs;
    const priorityRegions = getAllRegionsByPriority(workerRegion);
    const sortedIPs = [];
    for (const region of priorityRegions) {
        const regionIPs = availableIPs.filter(ip => ip.regionCode === region);
        sortedIPs.push(...regionIPs);
    }
    return sortedIPs;
}

```

IPs are returned in the order their `regionCode` first appears in the priority array, ensuring the lowest-latency geographic match is attempted first.

## Practical Code Examples

To retrieve the priority list for a specific worker region:

```js
import cfnew from './明文源吗';

const workerRegion = 'US';
const priority = cfnew.getAllRegionsByPriority(workerRegion);
console.log(priority);
// Output: ["US", "SG", "JP", "KR", "DE", "SE", "NL", "FI", "GB"]

```

To sort available proxies according to the smart selection algorithm:

```js
const availableIPs = [
  { domain: 'ProxyIP.SG.CMLiussss.net', regionCode: 'SG' },
  { domain: 'ProxyIP.US.CMLiussss.net', regionCode: 'US' },
  { domain: 'ProxyIP.DE.CMLiussss.net', regionCode: 'DE' },
];

const sorted = cfnew.getSmartRegionSelection('US', availableIPs);
console.log(sorted.map(i => i.regionCode));
// Output: [ 'US', 'SG', 'DE' ]

```

For a European worker in Germany, the priority shifts accordingly:

```js
console.log(cfnew.getAllRegionsByPriority('DE'));
// Output: ["DE", "NL", "GB", "SE", "FI", "US", "SG", "JP", "KR"]

```

## Summary

- CFnew's **proxy region priority** follows a three-tier hierarchy: worker region → nearby regions → fixed global order.
- The fixed global sequence is `US → SG → JP → KR → DE → SE → NL → FI → GB`.
- **Nearby regions** are defined in `getNearbyRegions` inside `明文源吗` (lines 45-58) and group regions by expected low-latency connectivity.
- The **priority array** is constructed by `getAllRegionsByPriority` (lines 361-365) and applied by `getSmartRegionSelection` (lines 68-83).
- The algorithm filters available IPs by matching their `regionCode` against this priority list, preserving the calculated order.

## Frequently Asked Questions

### What is the exact priority order if my worker is in Singapore?

If the worker region is `SG`, the priority order is SG → JP → KR → US → DE → SE → NL → FI → GB. The worker's own region comes first, followed by its nearby regions (JP, KR, US), and finally the remaining regions in the fixed global sequence.

### How does CFnew define "nearby" regions?

Nearby regions are hardcoded proximity mappings in the `getNearbyRegions` function at lines 45-58 of `明文源吗`. These mappings represent regional latency clusters; for example, DE (Germany) prioritizes NL, GB, SE, and FI before considering intercontinental regions like US or SG.

### Can I modify the region priority order?

The priority order is determined by the source code in `明文源吗`. To change it, you must modify the `getNearbyRegions` function (for proximity mappings) or the `allRegions` array inside `getAllRegionsByPriority` (for the fixed global sequence), then redeploy the worker.

### Where is the smart selection logic implemented?

The core logic resides in the file **`明文源吗`** within the `byJoey/cfnew` repository. Specifically, `getNearbyRegions` defines geographic relationships (lines 45-58), `getAllRegionsByPriority` builds the ranked list (lines 361-365), and `getSmartRegionSelection` performs the IP sorting (lines 68-83).