prismarine-proxy icon indicating copy to clipboard operation
prismarine-proxy copied to clipboard

Multi version proxy

Open Beaness opened this issue 4 years ago • 3 comments

Interesting proxy would be for example a 1.12.2 proxy that can join 1.13 servers and convert packets to 1.12.2 so you have a multi version proxy layer

Beaness avatar Jul 06 '20 08:07 Beaness

Much need to be done, even adding something to a bottom base, minecraft-data. Otherwise, such code don't even let me load successful if I use client 1.16.1:

const mc = require('minecraft-protocol')
const verList = require('minecraft-data').postNettyVersionsByProtocolVersion

const states = mc.states

let printAllNames = false
const printNameWhitelist = {};
const printNameBlacklist = {};
const version = '1.12.2';


const srv = mc.createServer({
  'online-mode': false,
  port: 25565,
  keepAlive: false,
})
srv.on('login', function (client) { 
  const addr = client.socket.remoteAddress
  const clientVersion = verList.pc[client.version][0].minecraftVersion;
  console.log('Incoming connection', '(' + addr + ')')
  let endedClient = false
  let endedTargetClient = false
  client.on('end', function () {
    endedClient = true
    console.log('Connection closed by client', '(' + addr + ')')
    if (!endedTargetClient) { targetClient.end('End') }
  })
  client.on('error', function (err) {
    endedClient = true
    console.log('Connection error by client', '(' + addr + ')')
    console.log(err.stack)
    if (!endedTargetClient) { targetClient.end('Error') }
  })
  const targetClient = mc.createClient({
    host: '99.88.77.66',
    port: 25565,
    username: client.username,
    keepAlive: false,
    version: version
  })
  client.on('packet', function (data, meta) {
    if (targetClient.state === states.PLAY && meta.state === states.PLAY) {
      if (shouldDump(meta.name, 'o')) {
        console.log('client->server:',
          client.state + ' ' + meta.name + ' :',
          JSON.stringify(data))
      }
      if (!endedTargetClient) { upFix.call(targetClient, meta.name, data, clientVersion, version) }
    }
  })
  targetClient.on('packet', function (data, meta) {
    if (meta.state === states.PLAY && client.state === states.PLAY) {
      if (shouldDump(meta.name, 'i')) {
        console.log('client<-server:',
          targetClient.state + '.' + meta.name + ' :' +
          JSON.stringify(data))
      }
      if (!endedClient) {
        downFix.call(client, meta.name, data, version, clientVersion)
        if (meta.name === 'set_compression') {
          client.compressionThreshold = data.threshold
        } // Set compression
      }
    }
  })
  
  targetClient.on('end', function () {
    endedTargetClient = true
    console.log('Connection closed by server', '(' + addr + ')')
    if (!endedClient) { client.end('End') }
  })
  targetClient.on('error', function (err) {
    endedTargetClient = true
    console.log('Connection error by server', '(' + addr + ') ', err)
    console.log(err.stack)
    if (!endedClient) { client.end('Error') }
  })
})

function shouldDump (name, direction) {
  if (matches(printNameBlacklist[name])) return false
  if (printAllNames) return true
  return matches(printNameWhitelist[name])

  function matches (result) {
    return result !== undefined && result !== null && result.indexOf(direction) !== -1
  }
}

function upFix(meta, data, vClient, vServer) { 	try {this.write (meta, data); } catch (e) {} }
function downFix(meta, data, vServer, vClient) { 	try {this.write (meta, data); } catch (e) {} }

837951602 avatar Jul 25 '20 17:07 837951602

So reimplementing ViaVersion in nodejs?

Heath123 avatar Dec 31 '20 17:12 Heath123

Yes.

Beaness avatar Dec 31 '20 18:12 Beaness