Skip to content
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

Allow use_local_engine to use local network addresses like 192.168.*.* #925

Open
yaustar opened this issue Nov 14, 2022 · 1 comment
Open

Comments

@yaustar
Copy link
Contributor

yaustar commented Nov 14, 2022

Private network address ranges are standardised to certain ranges: https://whatismyipaddress.com/private-ip

Having this will make engine development with the Editor with devices like XR headsets and phones a lot easier

@kungfooman
Copy link

kungfooman commented Nov 14, 2022

Quick snippet to test if an URL is in a private network that can be quickly iterated in devtools:

console.clear();
// Alternatively, but regex seems more complex:
// https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp
function isIP(str) {
    const parts = str.split(".");
    if (parts.length != 4) {
        return false;
    }
    const nums = parts.map(part => parseInt(part));
    return nums.every(num => num >= 0 && num <= 255);
}
function isPrivate(url_) {
    const { host } = new URL(url_);
    if (host === 'localhost' || host === '127.0.0.1') {
        return true;
    }
    if (isIP(host)) {
        if (host.startsWith("192.168.") || host.startsWith("10.")) {
            return true;
        }
        if (host.startsWith("172.")) {
            const second = parseInt(host.split('.')[1]);
            return second >= 16 && second <= 31;
        }
    }
    return false;
}
const tests = [
    // private:
    "http://10.0.1.2/playcanvas-engine/src/index.js",
    "http://127.0.0.1/playcanvas-engine/src/index.js",
    "http://172.16.1.2/playcanvas-engine/src/index.js",
    "http://172.31.1.2/playcanvas-engine/src/index.js",
    "http://localhost/playcanvas-engine/src/index.js",
    // NOT private:
    "http://localhostfake.com/playcanvas-engine/src/index.js",
    "http://9.0.1.2/playcanvas-engine/src/index.js",
    "http://172.15.1.2/playcanvas-engine/src/index.js",
    "http://172.32.1.2/playcanvas-engine/src/index.js",
    "http://192.168.0.1.xss.com/playcanvas-engine/src/index.js"
];
tests.forEach(_ => console.log(isPrivate(_) ? "private" : "NOT private", '=', _));

Output:

private = http://10.0.1.2/playcanvas-engine/src/index.js
private = http://127.0.0.1/playcanvas-engine/src/index.js
private = http://172.16.1.2/playcanvas-engine/src/index.js
private = http://172.31.1.2/playcanvas-engine/src/index.js
private = http://localhost/playcanvas-engine/src/index.js
NOT private = http://localhostfake.com/playcanvas-engine/src/index.js
NOT private = http://9.0.1.2/playcanvas-engine/src/index.js
NOT private = http://172.15.1.2/playcanvas-engine/src/index.js
NOT private = http://172.32.1.2/playcanvas-engine/src/index.js
NOT private = http://192.168.0.1.xss.com/playcanvas-engine/src/index.js

Edit: fix "IP" hiding as subdomain of xss.com by testing if it's actually an IP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants