-
Notifications
You must be signed in to change notification settings - Fork 6
Add a π€ Pit toggle: resolve Moshpit names for the session, like the Tor toggle #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Moshpit ("the pit") session routing β what the π€ Pit toggle installs. | ||
| // | ||
| // Moshpit endings (.eggs, .moshpit, .yeah, β¦) live outside the ICANN root, so | ||
| // the system resolver has no answer for them. `moshcode dns enable` teaches the | ||
| // whole machine, but needs root. This is the no-root, this-browser-only route, | ||
| // built like the Tor toggle: the launcher's helper runs a loopback SOCKS5 | ||
| // resolver (tron-tor-helper, /pit/*) that answers through the Moshpit | ||
| // DNS-over-HTTPS resolver, and the extension points a PAC script at it. | ||
| // | ||
| // The PAC decides per host with `dnsResolve`: whatever the system resolver CAN | ||
| // answer goes DIRECT, untouched; only a host it has no answer for goes to the | ||
| // pit. That is the house policy β clearnet wins, the pit is the fallback β so no | ||
| // ending list is needed and a real domain is never redirected. Pure functions, | ||
| // tested in pit-proxy.test.js. | ||
|
|
||
| /** Loopback port the helper's pit resolver listens on (tron-tor-helper PIT_SOCKS_PORT). */ | ||
| export const PIT_SOCKS_PORT = 9081; | ||
|
|
||
| function checkPort(port) { | ||
| const p = Number(port); | ||
| if (!Number.isInteger(p) || p <= 0 || p > 65535) throw new Error(`bad pit port: ${port}`); | ||
| return p; | ||
| } | ||
|
|
||
| /** | ||
| * The PAC script. Kept to plain ES3-style JavaScript: Chromium evaluates it in | ||
| * its own PAC sandbox, where only the PAC helpers (dnsResolve, β¦) exist. | ||
| */ | ||
| export function buildPitPac(port = PIT_SOCKS_PORT) { | ||
| const p = checkPort(port); | ||
| return [ | ||
| 'function FindProxyForURL(url, host) {', | ||
| " var h = String(host || '').toLowerCase();", | ||
| " if (h.charAt(h.length - 1) === '.') h = h.slice(0, -1);", | ||
| // Loopback, single-label intranet names and IP literals never touch the pit. | ||
| " if (!h || h === 'localhost' || h.indexOf('.') === -1) return 'DIRECT';", | ||
| " if (h.slice(-10) === '.localhost') return 'DIRECT';", | ||
| " if (/^[0-9.]+$/.test(h) || h.indexOf(':') !== -1) return 'DIRECT';", | ||
| // Clearnet wins: anything the system resolver knows stays exactly as it was. | ||
| " if (dnsResolve(h)) return 'DIRECT';", | ||
| ` return 'SOCKS5 127.0.0.1:${p}';`, | ||
| '}', | ||
| ].join('\n'); | ||
| } | ||
|
|
||
| /** chrome.proxy.settings value for the pit route. */ | ||
| export function pitProxyConfig(port = PIT_SOCKS_PORT) { | ||
| // Not `mandatory`: if the PAC ever fails to evaluate, Chromium falls back to | ||
| // DIRECT and ordinary browsing keeps working β a pit name failing is the | ||
| // acceptable failure, every site failing is not. | ||
| return { mode: 'pac_script', pacScript: { data: buildPitPac(port) } }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { PIT_SOCKS_PORT, buildPitPac, pitProxyConfig } from './pit-proxy.js'; | ||
|
|
||
| // Run the PAC the way Chromium would: only the PAC helpers exist, and | ||
| // dnsResolve is the system resolver β here a stub that knows two clearnet hosts. | ||
| function findProxy(host, resolvable = ['example.com', 'www.github.io']) { | ||
| const dnsResolve = (h) => (resolvable.includes(h) ? '93.184.216.34' : null); | ||
| const find = new Function('dnsResolve', `${buildPitPac()}; return FindProxyForURL;`)(dnsResolve); | ||
| return find(`http://${host}/`, host); | ||
| } | ||
|
|
||
| describe('buildPitPac', () => { | ||
| it('sends a host the system resolver has no answer for to the pit resolver', () => { | ||
| expect(findProxy('scrambled.eggs')).toBe(`SOCKS5 127.0.0.1:${PIT_SOCKS_PORT}`); | ||
| expect(findProxy('anything.moshpit')).toBe(`SOCKS5 127.0.0.1:${PIT_SOCKS_PORT}`); | ||
| }); | ||
|
|
||
| it('leaves a host the system resolver knows untouched (clearnet wins)', () => { | ||
| expect(findProxy('example.com')).toBe('DIRECT'); | ||
| // .io is claimed as a Moshpit ending too; a name that resolves still goes direct. | ||
| expect(findProxy('www.github.io')).toBe('DIRECT'); | ||
| }); | ||
|
|
||
| it('never routes loopback, intranet names or IP literals through the pit', () => { | ||
| expect(findProxy('localhost')).toBe('DIRECT'); | ||
| expect(findProxy('app.localhost')).toBe('DIRECT'); | ||
| expect(findProxy('intranet')).toBe('DIRECT'); | ||
| expect(findProxy('127.0.0.1')).toBe('DIRECT'); | ||
| expect(findProxy('10.0.0.7')).toBe('DIRECT'); | ||
| expect(findProxy('::1')).toBe('DIRECT'); | ||
| expect(findProxy('')).toBe('DIRECT'); | ||
| }); | ||
|
|
||
| it('normalises case and a trailing dot before deciding', () => { | ||
| expect(findProxy('Example.COM.')).toBe('DIRECT'); | ||
| expect(findProxy('Mosh.EGGS.')).toBe(`SOCKS5 127.0.0.1:${PIT_SOCKS_PORT}`); | ||
| }); | ||
|
|
||
| it('uses the port it is given and refuses a bad one', () => { | ||
| expect(buildPitPac(1234)).toContain('SOCKS5 127.0.0.1:1234'); | ||
| expect(() => buildPitPac(0)).toThrow(/bad pit port/); | ||
| expect(() => buildPitPac(70000)).toThrow(/bad pit port/); | ||
| expect(() => buildPitPac('nope')).toThrow(/bad pit port/); | ||
| }); | ||
| }); | ||
|
|
||
| describe('pitProxyConfig', () => { | ||
| it('is a non-mandatory inline PAC, so a broken script falls back to DIRECT', () => { | ||
| const cfg = pitProxyConfig(); | ||
| expect(cfg.mode).toBe('pac_script'); | ||
| expect(cfg.pacScript.data).toContain('function FindProxyForURL'); | ||
| expect(cfg.pacScript.mandatory).toBeUndefined(); | ||
| expect(cfg.pacScript.url).toBeUndefined(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.