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

Consider making rewrite domain-aware #13

Closed
WORMSS opened this issue Feb 7, 2023 · 10 comments
Closed

Consider making rewrite domain-aware #13

WORMSS opened this issue Feb 7, 2023 · 10 comments

Comments

@WORMSS
Copy link

WORMSS commented Feb 7, 2023

Hello, I think I already know the answer to this after a brief play with cli, and a quick look through the code, but it never hurts to ask.

npx lws-rewrite "https://a.mydomain.com/(.+)" "http://localhost:8081/a/$1" "https://a.mydomain.com/test"

I was hoping to see

http://localhost:8081/a/test

but I was greeted with the lovely error message of

C:\WINDOWS\system32>npx lws-rewrite "https://a.mydomain.com/(.+)" "http://localhost:8081/a/$1" "https://a.mydomain.com/test"
C:\Users\CRichardson\AppData\Roaming\nvm\v19.6.0\node_modules\lws-rewrite\node_modules\path-to-regexp\dist\index.js:48
                throw new TypeError("Missing parameter name at ".concat(i));
                ^

TypeError: Missing parameter name at 5
    at lexer (C:\Users\CRichardson\AppData\Roaming\nvm\v19.6.0\node_modules\lws-rewrite\node_modules\path-to-regexp\dist\index.js:48:23)
    at parse (C:\Users\CRichardson\AppData\Roaming\nvm\v19.6.0\node_modules\lws-rewrite\node_modules\path-to-regexp\dist\index.js:98:18)
    at stringToRegexp (C:\Users\CRichardson\AppData\Roaming\nvm\v19.6.0\node_modules\lws-rewrite\node_modules\path-to-regexp\dist\index.js:329:27)
    at pathToRegexp (C:\Users\CRichardson\AppData\Roaming\nvm\v19.6.0\node_modules\lws-rewrite\node_modules\path-to-regexp\dist\index.js:407:12)
    at Object.getTargetUrl (C:\Users\CRichardson\AppData\Roaming\nvm\v19.6.0\node_modules\lws-rewrite\lib\util.js:21:14)
    at Object.<anonymous> (C:\Users\CRichardson\AppData\Roaming\nvm\v19.6.0\node_modules\lws-rewrite\bin\cli.js:9:20)
    at Module._compile (node:internal/modules/cjs/loader:1246:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1300:10)
    at Module.load (node:internal/modules/cjs/loader:1103:32)
    at Module._load (node:internal/modules/cjs/loader:942:12)

Node.js v19.6.0

So, yes, I can see by the examples, you guys are expecting first and third params to be the path from server, and not a path including server..

But You never know, there could be some secret option I was not able to find, so thought best to ask.

@75lb
Copy link
Member

75lb commented Feb 7, 2023

Are you on mac/linux? If so, use single quotes as bash/shell will perform parameter expansion otherwise.. try

npx lws-rewrite 'https://a.mydomain.com/(.+)' 'http://localhost:8081/a/$1' 'https://a.mydomain.com/test'

@WORMSS
Copy link
Author

WORMSS commented Feb 7, 2023

Screenshot_20230207-203048

Windows.

@75lb
Copy link
Member

75lb commented Feb 7, 2023

the three input params should be paths, not full URLs.. except param 2 ("to" address) - that can be a full URL if you explicitly redirecting to a new host (e.g. localhost in your case)

$ npx lws-rewrite '/(.+)' 'http://localhost:8081/a/$1' '/test/hello'
http://localhost:8081/a/test/hello

@WORMSS
Copy link
Author

WORMSS commented Feb 7, 2023

So it can't be done then?

My ultimate goal is to have
https://a.mydomain.com/(.*) -> http://localhost:8081/$1
https://b.mydomain.com/(.*) -> http://localhost:8082/$1
https://c.mydomain.com/(.*) -> http://localhost:8083/$1
https://d.mydomain.com/(.*) -> http://localhost:8084/$1
https://e.mydomain.com/(.*) -> http://localhost:8085/$1

I knew lws-redirect wouldn't work for this so I wrote myself a reverse proxy from scratch,
but it is cumbersome and when I just happened to come across the lws-rewrite plugin and thought.. OOH... MAYBE...

This is why I was asking if there was a option that I was unaware of to make it domain aware, and not limited to just paths.

@75lb
Copy link
Member

75lb commented Feb 7, 2023

so requests to a, b, c, d and e.mydomain.com are all forwarded to one ws instance? So you have a DNS Alias record something like this?

A *.mydomain.com 192.168.1.1

lws-rewrite uses path-to-regexp internally (from the express project), i don't think it's domain-aware - it operates on the path portion only.. I will need to take a deeper look into this..

@WORMSS
Copy link
Author

WORMSS commented Feb 7, 2023

Yes, I have all subdomains directed towards a single IP.

I have very many internal servers all running different SPAs or other type of testing servers.
All these servers run on various ports, usually around the 8080 and increasing.

And I have 2 servers that are run on port 80

ws --port 80 --stack lws-redirect --redirect "http -> https"

And port 443.. This is where I would LOVE to be able to use

ws --port 443 --https --key **** --cert **** --stack lws-rewrite --rewrite <all the stuffs>

But instead I got a server that loops through a list of domains and ports and basically does

const config = [
  ['a.mydomain.com', 8080],
  ['b.mydomain.com', 8081],
  ['c.mydomain.com', 8082],
  ['d.mydomain.com', 8083],
  ['e.mydomain.com', 8084],
];
return (req, res) => {
  for (const [domain, port] of config) {
    const originalURL = new URL(req.url ?? '', `https://${req.headers.host ?? 'localhost'}`);

    if (originalURL.host !== domain) {
      continue;
    }

    const requestURL = new URL(originalURL);
    requestURL.host = 'localhost';
    requestURL.port = String(port);
    requestURL.protocol = 'http';

    const options = {
      ...req,
      headers: Object.fromEntries(Object.entries(req.headers)), // I don't remember why, but I had to
    };

    const requestConnection = require('http')
      .request(requestURL, options, (response) => {
        res.writeHead(response.statusCode ?? 500, response.headers);
        response.pipe(res);
      })
      .on('error', (err) => {
        res.writeHead(500);
        res.write(String(err));
        res.end();
      });
    req.pipe(requestConnection);
  }
}

Not the greatest in the world.. but it's keeping me afloat for now..

@75lb 75lb changed the title Rewrite as reverse-proxy Consider making rewrite domain-aware Mar 5, 2023
@WORMSS
Copy link
Author

WORMSS commented Jun 26, 2023

@75lb
I was just wondering how much "considering" has happened since last we talked

@75lb
Copy link
Member

75lb commented Jun 26, 2023

I didn't assign this as high priority since you're the only one to mention the issue so far plus you had a temporary workaound..

@75lb
Copy link
Member

75lb commented Jul 25, 2024

Closing.. I'm not saying it is not useful but it is beyond the scope of lws-rewrite.

@75lb 75lb closed this as completed Jul 25, 2024
@75lb
Copy link
Member

75lb commented Jul 27, 2024

const config = [
['a.mydomain.com', 8080],
['b.mydomain.com', 8081],
['c.mydomain.com', 8082],
['d.mydomain.com', 8083],
['e.mydomain.com', 8084],
];

Just noticed this example, reminded me of what you were trying to achieve..

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