mirror of
https://github.com/danbulant/node-x11
synced 2026-06-24 17:21:47 +00:00
reformat code
This commit is contained in:
parent
5645841b80
commit
7a3a1d43a2
1 changed files with 68 additions and 76 deletions
144
lib/auth.js
144
lib/auth.js
|
|
@ -5,87 +5,79 @@ var Buffer = require('buffer').Buffer;
|
||||||
// add 'unpack' method for buffer
|
// add 'unpack' method for buffer
|
||||||
require('./unpackbuffer').addUnpack(Buffer);
|
require('./unpackbuffer').addUnpack(Buffer);
|
||||||
|
|
||||||
function parseXauth( buf )
|
function parseXauth(buf) {
|
||||||
{
|
var offset = 0;
|
||||||
var offset = 0;
|
var auth = [];
|
||||||
var auth = [];
|
var cookieProperties = ['address', 'display', 'authName', 'authData'];
|
||||||
var cookieProperties = ['address', 'display', 'authName', 'authData'];
|
|
||||||
|
|
||||||
function handleCookieProperty(property) {
|
function handleCookieProperty(property) {
|
||||||
var length = buf.unpack('n', offset)[0];
|
var length = buf.unpack('n', offset)[0];
|
||||||
offset += 2;
|
offset += 2;
|
||||||
cookie[property] = buf.unpackString(length, offset);
|
cookie[property] = buf.unpackString(length, offset);
|
||||||
offset += length;
|
offset += length;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (offset < buf.length)
|
while (offset < buf.length) {
|
||||||
{
|
var cookie = {};
|
||||||
var cookie = {};
|
var typeToName = {
|
||||||
var typeToName = {
|
256 : 'Local',
|
||||||
256: 'Local',
|
65535: 'Wild',
|
||||||
65535: 'Wild',
|
254 : 'Netname',
|
||||||
254: 'Netname',
|
253 : 'Krb5Principal',
|
||||||
253: 'Krb5Principal',
|
252 : 'LocalHost',
|
||||||
252: 'LocalHost',
|
0 : 'Internet',
|
||||||
0: 'Internet',
|
1 : 'DECnet',
|
||||||
1: 'DECnet',
|
2 : 'Chaos',
|
||||||
2: 'Chaos',
|
5 : 'ServerInterpreted',
|
||||||
5: 'ServerInterpreted',
|
6 : 'InternetV6'
|
||||||
6: 'InternetV6'
|
};
|
||||||
};
|
cookie.type = buf.unpack('n')[0];
|
||||||
cookie.type = buf.unpack('n')[0];
|
if (!typeToName[cookie.type]) {
|
||||||
if (!typeToName[cookie.type]) {
|
console.warn('Unknown address type');
|
||||||
console.warn('Unknown address type');
|
|
||||||
}
|
|
||||||
offset += 2;
|
|
||||||
//JSHint becomes angry when handleCookieProperty is declared inside loop
|
|
||||||
cookieProperties.forEach(handleCookieProperty);
|
|
||||||
auth.push(cookie);
|
|
||||||
}
|
}
|
||||||
return auth;
|
offset += 2;
|
||||||
|
//JSHint becomes angry when handleCookieProperty is declared inside loop
|
||||||
|
cookieProperties.forEach(handleCookieProperty);
|
||||||
|
auth.push(cookie);
|
||||||
|
}
|
||||||
|
return auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function( display, host, cb )
|
module.exports = function(display, host, cb) {
|
||||||
{
|
var XAuthorityFile = process.env.XAUTHORITY;
|
||||||
var XAuthorityFile = process.env.XAUTHORITY;
|
if (!XAuthorityFile) {
|
||||||
if (!XAuthorityFile)
|
if (process.platform.match(/^win/)) {
|
||||||
{
|
// http://www.straightrunning.com/XmingNotes/trouble.php
|
||||||
if ( process.platform.match(/^win/) ) {
|
//
|
||||||
// http://www.straightrunning.com/XmingNotes/trouble.php
|
// The Xming magic cookie program, xauth (user-based), uses an
|
||||||
//
|
// Xauthority file (not the traditional .Xauthority file) in
|
||||||
// The Xming magic cookie program, xauth (user-based), uses an
|
// the %HOME% directory. To use xauth from Command Processor
|
||||||
// Xauthority file (not the traditional .Xauthority file) in
|
// e.g. on Windows machine 192.168.0.2 with user colin...
|
||||||
// the %HOME% directory. To use xauth from Command Processor
|
XAuthorityFile = process.env.USERPROFILE + '\\Xauthority';
|
||||||
// e.g. on Windows machine 192.168.0.2 with user colin...
|
} else {
|
||||||
XAuthorityFile = process.env.USERPROFILE + '\\Xauthority';
|
XAuthorityFile = process.env.HOME + '/.Xauthority';
|
||||||
} else {
|
}
|
||||||
XAuthorityFile = process.env.HOME + '/.Xauthority';
|
}
|
||||||
}
|
|
||||||
|
fs.readFile(XAuthorityFile, function(err, data) {
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
if (err.code == 'ENOENT') {
|
||||||
|
cb('', '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.readFile(XAuthorityFile, function (err, data) {
|
var auth = parseXauth(data);
|
||||||
|
for (var cookieNum in auth) {
|
||||||
if (err)
|
var cookie = auth[cookieNum];
|
||||||
{
|
if (cookie.display == display && cookie.address == host) {
|
||||||
if (err.code == 'ENOENT')
|
cb(cookie.authName, cookie.authData);
|
||||||
{
|
return;
|
||||||
cb('','');
|
}
|
||||||
return;
|
}
|
||||||
}
|
// throw 'No auth cookie matching display=' + display + ' and host=' + host;
|
||||||
throw err;
|
cb('', '');
|
||||||
}
|
});
|
||||||
|
|
||||||
var auth = parseXauth(data);
|
|
||||||
for (var cookieNum in auth)
|
|
||||||
{
|
|
||||||
var cookie = auth[cookieNum];
|
|
||||||
if (cookie.display == display && cookie.address == host)
|
|
||||||
{
|
|
||||||
cb( cookie.authName, cookie.authData );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// throw 'No auth cookie matching display=' + display + ' and host=' + host;
|
|
||||||
cb( '', '' );
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue