Commit eeb559ec authored by nanahira's avatar nanahira

manual lookup

parent 16665cdc
Pipeline #21236 failed with stages
in 93 minutes and 56 seconds
......@@ -3,6 +3,8 @@ import { Server, createConnection } from 'net';
import { hashAddress } from './src/hash-address';
import { createLogger } from 'bunyan';
import { Socket } from 'net';
import Address from 'ipaddr.js';
import dns from 'dns';
declare module 'net' {
interface Server {
......@@ -24,20 +26,34 @@ const log = createLogger({ name: 'switcher' });
const server: Server = createProxyServer({
auth: true,
createProxyConnection: async (info) => {
let socket: Socket;
const src: Address.IPv4 | Address.IPv6 = info.socket[targetIpSym];
log.info(
`Connecting to ${info.dstHost}:${info.dstPort} with ${info.socket[targetIpSym]}`,
`Connecting to ${info.dstHost}:${info.dstPort} with ${src?.toString()}`,
);
try {
socket = createConnection({
host: info.dstHost,
port: info.dstPort,
localAddress: info.socket[targetIpSym] || undefined,
});
} catch (e) {
log.warn(
`Failed to connect to ${info.dstHost}:${info.dstPort}, retrying direct connection: ${e}`,
);
let socket: Socket;
if (src) {
try {
const lookup = await dns.promises.lookup(
info.dstHost,
src.kind() === 'ipv4' ? 4 : 6,
);
socket = createConnection({
host: lookup.address,
port: info.dstPort,
localAddress: src.toString(),
});
} catch (e) {
log.warn(
`Remote ${
info.dstHost
} does not support ${src.kind()}, fallback to direct connect.`,
);
socket = createConnection({
host: info.dstHost,
port: info.dstPort,
});
}
} else {
socket = createConnection({
host: info.dstHost,
port: info.dstPort,
......@@ -56,8 +72,9 @@ server.on('proxy-auth', (socket, username, password, callback) => {
return;
}
const token = `${username}:${password}`;
socket[targetIpSym] = hashAddress(process.env.CIDR, token).toString();
log.info(`Mapped ${token} to ${socket[targetIpSym]}`);
const address = hashAddress(process.env.CIDR, token);
socket[targetIpSym] = address;
log.info(`Mapped ${token} to ${address.toString()}`);
callback(true);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment