mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-28 13:45:37 -04:00
Merge pull request #503 from PrismarineJS/proxys
add 2 examples : client_http_proxy and client_socks_proxy. fix #185 fix #436
This commit is contained in:
commit
6246e0e644
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
node_modules
|
node_modules
|
||||||
test/npm-debug.log
|
test/npm-debug.log
|
||||||
test/server
|
test/server
|
||||||
|
package-lock.json
|
||||||
|
@ -21,6 +21,9 @@ automatically logged in and validated against mojang's auth.
|
|||||||
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
|
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
|
||||||
* errorHandler : A way to override the default error handler for client errors. A function that takes a Client and an error.
|
* errorHandler : A way to override the default error handler for client errors. A function that takes a Client and an error.
|
||||||
The default kicks the client.
|
The default kicks the client.
|
||||||
|
* stream : a stream to use as connection
|
||||||
|
* connect : a function taking the client as parameter and that should client.setSocket(socket)
|
||||||
|
and client.emit('connect') when appropriate (see the proxy examples for an example of use)
|
||||||
|
|
||||||
## mc.Server(version,[customPackets])
|
## mc.Server(version,[customPackets])
|
||||||
|
|
||||||
|
49
examples/client_http_proxy/client_http_proxy.js
Normal file
49
examples/client_http_proxy/client_http_proxy.js
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
const mc = require('minecraft-protocol');
|
||||||
|
const Http = require("http");
|
||||||
|
|
||||||
|
if(process.argv.length < 6 || process.argv.length > 8) {
|
||||||
|
console.log("Usage : node client_http_proxy.js <host> <port> <proxyHost> <proxyPort> [<name>] [<password>]");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const proxyHost=process.argv[4];
|
||||||
|
const proxyPort=process.argv[5];
|
||||||
|
|
||||||
|
const client = mc.createClient({
|
||||||
|
connect:(client) => {
|
||||||
|
const req = Http.request({
|
||||||
|
host: proxyHost,
|
||||||
|
port: proxyPort,
|
||||||
|
method: 'CONNECT',
|
||||||
|
path: process.argv[2] + ":" + parseInt(process.argv[3])
|
||||||
|
});
|
||||||
|
req.end();
|
||||||
|
|
||||||
|
req.on("connect", function(res, stream) {
|
||||||
|
client.setSocket(stream);
|
||||||
|
client.emit('connect');
|
||||||
|
});
|
||||||
|
},
|
||||||
|
username: process.argv[6] ? process.argv[6] : "echo",
|
||||||
|
password: process.argv[7]
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('connect', function() {
|
||||||
|
console.info('connected');
|
||||||
|
});
|
||||||
|
client.on('disconnect', function(packet) {
|
||||||
|
console.log('disconnected: '+ packet.reason);
|
||||||
|
});
|
||||||
|
client.on('end', function(err) {
|
||||||
|
console.log('Connection lost');
|
||||||
|
});
|
||||||
|
client.on('chat', function(packet) {
|
||||||
|
const jsonMsg = JSON.parse(packet.message);
|
||||||
|
if(jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
||||||
|
const username = jsonMsg.with[0].text;
|
||||||
|
const msg = jsonMsg.with[1];
|
||||||
|
if(username === client.username) return;
|
||||||
|
client.write('chat', {message: msg});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
7
examples/client_http_proxy/package.json
Normal file
7
examples/client_http_proxy/package.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"name": "node-minecraft-protocol-example",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {},
|
||||||
|
"description": "A node-minecraft-protocol example"
|
||||||
|
}
|
55
examples/client_socks_proxy/client_socks_proxy.js
Normal file
55
examples/client_socks_proxy/client_socks_proxy.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
const mc = require('minecraft-protocol');
|
||||||
|
const socks = require("socks");
|
||||||
|
|
||||||
|
if(process.argv.length < 6 || process.argv.length > 8) {
|
||||||
|
console.log("Usage : node client_socks_proxy.js <host> <port> <proxyHost> <proxyPort> [<name>] [<password>]");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const proxyHost=process.argv[4];
|
||||||
|
const proxyPort=process.argv[5];
|
||||||
|
|
||||||
|
const client = mc.createClient({
|
||||||
|
connect: client => {
|
||||||
|
socks.createConnection({
|
||||||
|
proxy: {
|
||||||
|
ipaddress: proxyHost,
|
||||||
|
port: proxyPort,
|
||||||
|
type: 5
|
||||||
|
},
|
||||||
|
target: {
|
||||||
|
host: process.argv[2],
|
||||||
|
port: parseInt(process.argv[3])
|
||||||
|
},
|
||||||
|
}, function(err, socket) {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.setSocket(socket);
|
||||||
|
client.emit('connect');
|
||||||
|
});
|
||||||
|
},
|
||||||
|
username: process.argv[6] ? process.argv[6] : "echo",
|
||||||
|
password: process.argv[7]
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('connect', function() {
|
||||||
|
console.info('connected');
|
||||||
|
});
|
||||||
|
client.on('disconnect', function(packet) {
|
||||||
|
console.log('disconnected: '+ packet.reason);
|
||||||
|
});
|
||||||
|
client.on('end', function(err) {
|
||||||
|
console.log('Connection lost');
|
||||||
|
});
|
||||||
|
client.on('chat', function(packet) {
|
||||||
|
const jsonMsg = JSON.parse(packet.message);
|
||||||
|
if(jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
||||||
|
const username = jsonMsg.with[0].text;
|
||||||
|
const msg = jsonMsg.with[1];
|
||||||
|
if(username === client.username) return;
|
||||||
|
client.write('chat', {message: msg});
|
||||||
|
}
|
||||||
|
});
|
9
examples/client_socks_proxy/package.json
Normal file
9
examples/client_socks_proxy/package.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "node-minecraft-protocol-example",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"socks": "^1.1.10"
|
||||||
|
},
|
||||||
|
"description": "A node-minecraft-protocol example"
|
||||||
|
}
|
@ -9,9 +9,8 @@ const minecraft_data = require('minecraft-data');
|
|||||||
module.exports = function(client, options) {
|
module.exports = function(client, options) {
|
||||||
client.wait_connect = true; // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
|
client.wait_connect = true; // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
|
||||||
debug('pinging',options.host);
|
debug('pinging',options.host);
|
||||||
const pingOptions = {host: options.host, port: options.port};
|
|
||||||
// TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping
|
// TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping
|
||||||
ping(pingOptions, function(err, response) {
|
ping(options, function(err, response) {
|
||||||
if (err) throw err; // hmm
|
if (err) throw err; // hmm
|
||||||
debug('ping response',response);
|
debug('ping response',response);
|
||||||
// TODO: could also use ping pre-connect to save description, type, max players, etc.
|
// TODO: could also use ping pre-connect to save description, type, max players, etc.
|
||||||
|
@ -5,9 +5,11 @@ module.exports = function(client, options) {
|
|||||||
options.port = options.port || 25565;
|
options.port = options.port || 25565;
|
||||||
options.host = options.host || 'localhost';
|
options.host = options.host || 'localhost';
|
||||||
|
|
||||||
options.connect = (client) => {
|
if(!options.connect)
|
||||||
|
options.connect = (client) => {
|
||||||
if (options.stream) {
|
if (options.stream) {
|
||||||
client.setSocket(options.stream);
|
client.setSocket(options.stream);
|
||||||
|
client.emit('connect');
|
||||||
} else if (options.port == 25565 && net.isIP(options.host) === 0) {
|
} else if (options.port == 25565 && net.isIP(options.host) === 0) {
|
||||||
dns.resolveSrv("_minecraft._tcp." + options.host, function(err, addresses) {
|
dns.resolveSrv("_minecraft._tcp." + options.host, function(err, addresses) {
|
||||||
if(addresses && addresses.length > 0) {
|
if(addresses && addresses.length > 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user