[go: nahoru, domu]

Skip to content

Commit

Permalink
Working snac 01,03 for boss server. Also shuffed some things around
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewML committed Dec 25, 2020
1 parent f37daf4 commit ea87e8f
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/clientSnacs.ts → src/AIMAuthServer/clientSnacs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseTLVs } from './parseTLVs';
import { TLVS } from './constants';
import { parseTLVs } from '../parseTLVs';
import { TLVS } from '../constants';

/**
* @see http://iserverd1.khstu.ru/oscar/snac_17_06.html
Expand Down
File renamed without changes.
22 changes: 13 additions & 9 deletions src/AIMAuthServer.ts → src/AIMAuthServer/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { OscarServer, OscarSocket } from './OscarServer';
import { OscarServer, OscarSocket } from '../OscarServer';
import assert from 'assert';
import crypto from 'crypto';
import { hashClientPassword } from './hashClientLogin';
import { matchSnac, parseSnac } from './snacUtils';
import { matchSnac, parseSnac } from '../snacUtils';
import {
authKeyResponseSnac,
loginErrorSnac,
loginSuccessSnac,
} from './serverSentSnacs';
} from './serverSnacs';
import { parseAuthRequest, parseMD5LoginRequest } from './clientSnacs';
import { LOGIN_ERRORS } from './constants';
import { LOGIN_ERRORS } from '../constants';

/**
* @summary The first server an Oscar Protocol client
Expand Down Expand Up @@ -74,7 +74,10 @@ export class AIMAuthServer extends OscarServer {
salt: state.getSalt(),
});
const isValidUsername = true; // TODO: Real lookup
const isValidPass = payload.passwordHash.equals(hashToMatch);
const isValidPass = crypto.timingSafeEqual(
payload.passwordHash,
hashToMatch,
);

// TODO: handle various diff error types,
// rather than mapping all to INCORRECT_NICK_OR_PASS
Expand Down Expand Up @@ -114,19 +117,20 @@ export class AIMAuthServer extends OscarServer {
oscarSocket.write(responseFlap);
return;
}
console.log('Unhandled Channel 2 Flap: ', flap);
console.log('AIMAuthServer Unhandled Channel 2 Flap: ', flap);
});

oscarSocket.onChannel(0x3, (flap) => {
console.log('Unimplemented channel 3 flap: ', flap);
console.log('AIMAuthServer unimplemented channel 3 flap: ', flap);
});

oscarSocket.onChannel(0x4, (flap) => {
console.log('Unimplemented channel 4 flap: ', flap);
// TODO: handle disconnect negotiation
console.log('AIMAuthServer unimplemented channel 4 flap: ', flap);
});

oscarSocket.onChannel(0x5, (flap) => {
console.log('Unimplemented channel 5 flap: ', flap);
console.log('AIMAuthServer unimplemented channel 5 flap: ', flap);
});

// Initialize state needed for auth requests
Expand Down
6 changes: 3 additions & 3 deletions src/serverSentSnacs.ts → src/AIMAuthServer/serverSnacs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import { stringTLV, uint16TLV } from './buildTLV';
import { buildSnac } from './snacUtils';
import { LOGIN_ERRORS, SNACS, TLVS } from './constants';
import { stringTLV, uint16TLV } from '../buildTLV';
import { buildSnac } from '../snacUtils';
import { SNACS, TLVS } from '../constants';

/**
* @see http://iserverd1.khstu.ru/oscar/snac_17_07.html
Expand Down
8 changes: 0 additions & 8 deletions src/BossServer.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/BossServer/clientSnacs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import assert from 'assert';
import { parseTLVs } from '../parseTLVs';
import { TLVS } from '../constants';

/**
* @see http://iserverd.khstu.ru/oscar/cli_cookie.html
*/
export function parseCookieRequest(data: Buffer) {
const flapVersion = data.readUInt32BE(0);
assert(flapVersion === 0x1, 'Incorrect client FLAP version');

const tlvs = parseTLVs(data.subarray(4));
const authCookie = tlvs.first(TLVS.AUTH_COOKIE).value;

return { authCookie };
}
44 changes: 44 additions & 0 deletions src/BossServer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { timingSafeEqual } from 'crypto';
import { parseCookieRequest } from './clientSnacs';
import { OscarServer, OscarSocket } from '../OscarServer';
import { supportedFamiliesSnac } from './serverSnacs';
import { assert } from 'console';

export class BossServer extends OscarServer {
onConnection(oscarSocket: OscarSocket) {
const { host, port } = oscarSocket.remoteAddress;
console.log(`BossServer: New connection from ${host}:${port}`);

oscarSocket.sendStartFlap();

oscarSocket.onChannel(0x1, (flap) => {
const { authCookie } = parseCookieRequest(flap.data);
// TODO: Grab cookie from shared storage with auth service
const expectedCookie = Buffer.from('111111111', 'ascii');
const validCookie = timingSafeEqual(authCookie, expectedCookie);

// TODO: Unsure of what client expects for invalid cookie,
// maybe close via channel 4?
assert(validCookie, 'BossServer: Invalid auth cookie');

const snac = supportedFamiliesSnac({ reqID: 1 });
oscarSocket.write({ channel: 2, data: snac });
});

oscarSocket.onChannel(0x2, (flap) => {
console.log('boss channel 2 flap: ', flap);
});

oscarSocket.onChannel(0x3, (flap) => {
console.log('boss channel 3 flap: ', flap);
});

oscarSocket.onChannel(0x4, (flap) => {
console.log('boss channel 4 flap: ', flap);
});

oscarSocket.onChannel(0x5, (flap) => {
console.log('boss channel 5 flap: ', flap);
});
}
}
38 changes: 38 additions & 0 deletions src/BossServer/serverSnacs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { buildSnac } from '../snacUtils';
import { SNACS } from '../constants';
import { endianness } from 'os';

/**
* @see http://iserverd.khstu.ru/oscar/snac_01_03.html
*/
export function supportedFamiliesSnac(opts: { reqID: number }) {
/**
* @see http://iserverd.khstu.ru/oscar/families.html
*/
const families = Buffer.from(
Uint16Array.from([
SNACS.GENERAL.family,
SNACS.LOCATION.family,
SNACS.BUDDYLIST.family,
SNACS.ICBM.family,
SNACS.INVITATION.family,
SNACS.ADMINISTRATIVE.family,
SNACS.POPUP_NOTICE.family,
SNACS.PRIVACY_MGMT.family,
SNACS.USER_LOOKUP.family,
SNACS.USAGE_STATS.family,
SNACS.SSI.family,
SNACS.OFFLINE.family,
]).buffer,
);
// There has to be less noisy sugar for generating a BE
// list of 16 bit ints...
if (endianness() === 'LE') families.swap16();

return buildSnac({
family: SNACS.GENERAL.family,
subtype: SNACS.GENERAL.subtypes.SUPPORTED_FAMILIES,
reqID: opts.reqID,
data: Buffer.from(Uint16Array.from(families).buffer).swap16(),
});
}
5 changes: 3 additions & 2 deletions src/OscarServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class OscarSocket {

constructor(private socket: Socket) {
socket.on('data', this.onData.bind(this));
socket.on('error', (e) => console.error(e));
}

get remoteAddress() {
Expand All @@ -68,12 +69,12 @@ export class OscarSocket {
/**
* @summary Send the FLAP version number (always 0x1).
* OSCAR clients will not start sending flaps
* until the start FLAP is sent
* until the start FLAP is sent from the server
*/
sendStartFlap() {
this.write({
channel: 1,
data: Buffer.from([0x1]),
data: Buffer.from([0x0, 0x0, 0x0, 0x1]),
});
}

Expand Down
39 changes: 39 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,45 @@ export const TLVS = {
* @see http://iserverd1.khstu.ru/oscar/families.html
*/
export const SNACS = {
GENERAL: {
family: 0x1,
subtypes: {
SUPPORTED_FAMILIES: 0x3,
},
},
LOCATION: {
family: 0x2,
},
BUDDYLIST: {
family: 0x3,
},
ICBM: {
family: 0x4,
},
INVITATION: {
family: 0x6,
},
ADMINISTRATIVE: {
family: 0x7,
},
POPUP_NOTICE: {
family: 0x8,
},
PRIVACY_MGMT: {
family: 0x9,
},
USER_LOOKUP: {
family: 0x0a,
},
USAGE_STATS: {
family: 0x0b,
},
SSI: {
family: 0x13,
},
OFFLINE: {
family: 0x15,
},
AUTH: {
family: 0x17,
subtypes: {
Expand Down
2 changes: 1 addition & 1 deletion src/parseTLVs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function parseTLVs(data: Buffer) {
tlvs.set(type, { type, length, value });

cursor = valueEnd;
assert(cursor <= data.byteLength, 'Overflow that should never happen');
assert(cursor <= data.byteLength, 'parseTLVs: Malformed data');
}

return tlvs;
Expand Down

0 comments on commit ea87e8f

Please sign in to comment.