Created
June 10, 2019 17:24
-
-
Save prb112/841f0edafc1525348821d0c803fde8b6 to your computer and use it in GitHub Desktop.
Port forwarding for Zookeeper (to 5 remote hosts behind a firewall)
This file contains 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
{ | |
"88" : { | |
"auth-1" : 8088 | |
} | |
} |
This file contains 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
{ | |
"2181": { | |
"type": "socket", | |
"members": [ | |
{ "hostname": "kafka-1", "port": 30991 }, | |
{ "hostname": "kafka-2", "port": 30992 }, | |
{ "hostname": "kafka-3", "port": 30993 }, | |
{ "hostname": "kafka-4", "port": 30994 }, | |
{ "hostname": "kafka-5", "port": 30995 } | |
] | |
} | |
} |
This file contains 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
/** | |
* Kerberos Socket Proxy | |
**/ | |
// Need to use NET (not http) | |
const net = require("net"); | |
const fs = require("fs"); | |
// The total retry count starts at 0 and goes to 5 | |
var retry = 0; | |
// Load the configuration details for the ports (mapping to a localhost port mapping) | |
var jsonConfig = JSON.parse( | |
fs.readFileSync("configuration-map-krb.json", "utf8") | |
); | |
/** | |
* socket server for Zookeeper port forwarding... | |
* must have allowHalfOpen to be true | |
*/ | |
const server = net | |
.createServer({ allowHalfOpen: true}, socket => { | |
socket.on('end', () => { | |
console.log('server client disconnected'); | |
}); | |
// Get the remoteAddress indicators | |
console.log("socket remote address: ", socket.localAddress); | |
console.log("socket remote port: ", socket.localPort); | |
socket.setTimeout(3000); | |
var ipAddr = socket.localAddress; | |
if (ipAddr != null && ipAddr.startsWith("127.0.0.")) { | |
serverId = ipAddr.replace("127.0.0.", ""); | |
socket.on("data", function(data) { | |
// Creates a socket | |
var client = new net.Socket(); | |
client.connect(8088, "localhost", function() { | |
console.log("Connected to backend"); | |
client.write(data); | |
}); | |
//Writes the data from the port back | |
client.on("data", function(chunk) { | |
console.log("Received: ",chunk ); | |
socket.write(chunk); | |
// kill server socket | client socket after server's response | |
client.end(); | |
socket.end(); | |
}); | |
client.on("close", function() { | |
console.log("Connection closed"); | |
}); | |
}); | |
} else { | |
socket.end("Forbidden\n"); | |
} | |
}) | |
.on("error", err => { | |
// Check for the error https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_identifying_paths_for_ipc_connections | |
if (err.code === "EADDRINUSE" && retry < 5) { | |
retry++; // Keep from infinite repeats | |
console.log("Address in use, retrying..."); | |
setTimeout(() => { | |
server.close(); | |
server.listen(88, "localhost"); | |
}, 1000); | |
} else if (retry == 5) { | |
console.error("There is an error in the startup - address in use"); | |
} else { | |
throw err; | |
} | |
}); | |
// We are going to loop over all services and open servers | |
// Repeating this server.listen for a variety of addresses, defaults to only 127.0.0.1 | |
// must map to 0.0.0.0 and limit to only IPs from 127.0.0.0 | |
server.listen(88, "0.0.0.0", () => { | |
console.log("opened server on", server.address()); | |
}); |
This file contains 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
/** | |
* Zookeeper Socket Proxy | |
**/ | |
// Need to use NET (not http) | |
const net = require("net"); | |
const fs = require("fs"); | |
// The total retry count starts at 0 and goes to 5 | |
var retry = 0; | |
// Load the configuration details for the ports (mapping to a localhost port mapping) | |
var jsonConfig = JSON.parse( | |
fs.readFileSync("configuration-map-socket.json", "utf8") | |
); | |
/** | |
* socket server for Zookeeper port forwarding... | |
* must have allowHalfOpen to be true | |
*/ | |
const server = net | |
.createServer({ allowHalfOpen: true}, socket => { | |
socket.on('end', () => { | |
console.log('server client disconnected'); | |
}); | |
// Get the remoteAddress indicators | |
console.log("socket remote address: ", socket.localAddress); | |
console.log("socket remote port: ", socket.localPort); | |
socket.setTimeout(3000); | |
var ipAddr = socket.localAddress; | |
if (ipAddr != null && ipAddr.startsWith("127.0.0.")) { | |
serverId = ipAddr.replace("127.0.0.", ""); | |
socket.on("data", function(data) { | |
console.log("the four letter command that is forwarded: " + data); | |
var mappingPort = jsonConfig[socket.localPort].members[parseInt(serverId)-1]["port"]; | |
console.log("The Mapping Port is : " + mappingPort); | |
// Creates a socket | |
var client = new net.Socket(); | |
client.connect(mappingPort, "localhost", function() { | |
console.log("Connected to backend"); | |
client.write(data); | |
}); | |
//Writes the data from the port back | |
client.on("data", function(chunk) { | |
console.log("Received: ",chunk ); | |
socket.write(chunk); | |
// kill server socket | client socket after server's response | |
client.end(); | |
socket.end(); | |
}); | |
client.on("close", function() { | |
console.log("Connection closed"); | |
}); | |
}); | |
} else { | |
socket.end("Forbidden\n"); | |
} | |
}) | |
.on("error", err => { | |
// Check for the error https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_identifying_paths_for_ipc_connections | |
if (err.code === "EADDRINUSE" && retry < 5) { | |
retry++; // Keep from infinite repeats | |
console.log("Address in use, retrying..."); | |
setTimeout(() => { | |
server.close(); | |
server.listen(2181, "localhost"); | |
}, 1000); | |
} else if (retry == 5) { | |
console.error("There is an error in the startup - address in use"); | |
} else { | |
throw err; | |
} | |
}); | |
// We are going to loop over all services and open servers | |
// Repeating this server.listen for a variety of addresses, defaults to only 127.0.0.1 | |
// must map to 0.0.0.0 and limit to only IPs from 127.0.0.0 | |
server.listen(2181, "0.0.0.0", () => { | |
console.log("opened server on", server.address()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment