Skip to content

Commit 19e914c

Browse files
authored
Merge pull request #72 from Madriix/master
Update the bouncer.js
2 parents 0a13ae5 + 3547e10 commit 19e914c

File tree

1 file changed

+128
-104
lines changed

1 file changed

+128
-104
lines changed

bouncer.js

Lines changed: 128 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,16 @@ function iphash(data) {
6868
return crypto.createHash('md5').update(data).digest('hex').substr(0,6);
6969
}
7070

71-
String.prototype.hexEncode = function(){
72-
var hex, i;
73-
74-
var result = "";
75-
for (i=0; i<this.length; i++) {
76-
hex = this.charCodeAt(i).toString(16);
77-
result += ("000"+hex).slice(-4);
78-
}
79-
80-
return result
81-
}
71+
function generateMD5AndGetSubstring(data) {
72+
const md5Hash = crypto.createHash('md5').update(data).digest('hex');
73+
// Retrieve the first 4 and the last 4 characters of the hash
74+
const first4Characters = md5Hash.slice(0, 4);
75+
const last4Characters = md5Hash.slice(-4);
76+
return {
77+
first4: first4Characters,
78+
last4: last4Characters
79+
};
80+
}
8281
// Bouncer Server
8382
let server;
8483
let doServer;
@@ -249,7 +248,7 @@ server = doServer(tlsOptions,function(socket) {
249248
}
250249
else if(commands[1]) {
251250
this.irc.nick=commands[1].trim();
252-
if(false&&this.irc.user) {
251+
if(this.irc.user) {
253252
this.hash=hash(this.irc.nick+this.irc.user+this.irc.password+this.irc.server+this.irc.port.toString());
254253
if(connections[socket.hash]) {
255254
clientReconnect(this);
@@ -638,6 +637,10 @@ server = doServer(tlsOptions,function(socket) {
638637
console.log(err);
639638
this.end();
640639
});
640+
socket.on('end', function() {
641+
console.log('Connection finished: socket');
642+
return;
643+
});
641644
});
642645

643646
// IRC Client
@@ -846,9 +849,16 @@ function clientConnect(socket) {
846849
} catch(e) {
847850
_reverse_ip = this.host;
848851
}
849-
if(false) { // My server irc
850-
_vhost = iphash(this.nick);
851-
this.write('WEBIRC '+SERVER_WEBIRC+' '+this.user+' galaxy-'+this.host+'.cloud-'+(_vhost?_vhost:'0')+'.jbnc '+this.host+' :secure\n');
852+
if (false) { // My server irc
853+
let isIPv6 = this.host.includes(':');
854+
let ip = isIPv6 ? this.host.split(':').slice(0, 4).join(':') : this.host;
855+
let cleanIp = ip.replace(/:/g, '');
856+
let md5Hash = generateMD5AndGetSubstring(cleanIp);
857+
let vhost = iphash(this.accountsasl) || '0';
858+
859+
let webircMessage = `WEBIRC ${SERVER_WEBIRC} ${this.user} galaxy-${cleanIp}.ip${md5Hash.first4}${md5Hash.last4}.cloud-${vhost}.jbnc ${this.host} :secure\n`;
860+
861+
this.write(webircMessage);
852862
}
853863
else if(SERVER_WEBIRCHASHIP && !SERVER_WEBIRCPROXY) {
854864
this.write('WEBIRC '+SERVER_WEBIRC+' '+this.user+' jbnc.'+iphash(this.hostonce)+" "+this.host+"\n");
@@ -1072,60 +1082,64 @@ function clientConnect(socket) {
10721082
_mode[i]=='e' || _mode[i]=='b' || _mode[i]=='I' || _mode[i]=='q' || _mode[i]=='f' ||
10731083
_mode[i]=='j')) {
10741084
if(_mode[i]=='o' || _mode[i]=='v' || _mode[i]=='h') {
1075-
for(c=0;c<curchan.names.length;c++) {
1076-
if(curchan.names[c].replace(/(&|~|@|%|\+)/,"")==_mode_target[_mode_count]) {
1077-
switch(_mode[i]) {
1078-
case 'o':
1079-
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1080-
if(curchan.names[c].indexOf("@")==-1) {
1081-
curchan.names[c]="@"+curchan.names[c];
1082-
if(_mode_target[_mode_count]!=this.nick && curchan.aop && curchan.aop.indexOf(_this_target)<0 && this.opmode) {
1083-
curchan.aop.push(_this_target);
1085+
if (curchan && curchan.names) {
1086+
for(c=0;c<curchan.names.length;c++) {
1087+
if(curchan.names[c].replace(/(&|~|@|%|\+)/,"")==_mode_target[_mode_count]) {
1088+
switch(_mode[i]) {
1089+
case 'o':
1090+
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1091+
if(curchan.names[c].indexOf("@")==-1) {
1092+
curchan.names[c]="@"+curchan.names[c];
1093+
if(_mode_target[_mode_count]!=this.nick && curchan.aop && curchan.aop.indexOf(_this_target)<0 && this.opmode) {
1094+
curchan.aop.push(_this_target);
1095+
}
10841096
}
1085-
}
1086-
if(_mode_target[_mode_count]==this.nick) curchan.isop=true;
1087-
break;
1088-
case 'v':
1089-
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1090-
if(curchan.names[c].indexOf("+")==-1) {
1091-
if(curchan.names[c].indexOf("&")==-1 || curchan.names[c].indexOf("~")==-1 || curchan.names[c].indexOf("@")==-1) {
1092-
if(curchan.names[c].indexOf("%")==-1) {
1093-
curchan.names[c]="+"+curchan.names[c];
1097+
if(_mode_target[_mode_count]==this.nick) curchan.isop=true;
1098+
break;
1099+
case 'v':
1100+
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1101+
if(curchan.names[c].indexOf("+")==-1) {
1102+
if(curchan.names[c].indexOf("&")==-1 || curchan.names[c].indexOf("~")==-1 || curchan.names[c].indexOf("@")==-1) {
1103+
if(curchan.names[c].indexOf("%")==-1) {
1104+
curchan.names[c]="+"+curchan.names[c];
1105+
}
1106+
else {
1107+
curchan.names[c]=curchan.names[c].substr(0,1)+"+"+curchan.names[c].substr(1);
1108+
}
10941109
}
10951110
else {
1096-
curchan.names[c]=curchan.names[c].substr(0,1)+"+"+curchan.names[c].substr(1);
1111+
if(curchan.names[c].indexOf("%")==-1) {
1112+
curchan.names[c]=curchan.names[c].substr(0,1)+"+"+curchan.names[c].substr(1);
1113+
}
1114+
else {
1115+
curchan.names[c]=curchan.names[c].substr(0,2)+"+"+curchan.names[c].substr(2);
1116+
}
1117+
}
1118+
if(_mode_target[_mode_count]!=this.nick && curchan.aov && curchan.aov.indexOf(_this_target)<0 && this.opmode) {
1119+
curchan.aov.push(_mode_target[_this_target]);
10971120
}
10981121
}
1099-
else {
1100-
if(curchan.names[c].indexOf("%")==-1) {
1101-
curchan.names[c]=curchan.names[c].substr(0,1)+"+"+curchan.names[c].substr(1);
1122+
if(_mode_target[_mode_count]==this.nick) curchan.isvoice=true;
1123+
break;
1124+
case 'h':
1125+
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1126+
if(curchan.names[c].indexOf("%")==-1) {
1127+
if(curchan.names[c].indexOf("&")==-1 || curchan.names[c].indexOf("~")==-1 || curchan.names[c].indexOf("@")==-1) {
1128+
curchan.names[c]="%"+curchan.names[c];
11021129
}
1103-
else {
1104-
curchan.names[c]=curchan.names[c].substr(0,2)+"+"+curchan.names[c].substr(2);
1130+
else
1131+
curchan.names[c]=curchan.names[c].substr(0,1)+"%"+curchan.names[c].substr(1);
1132+
if(_mode_target[_mode_count]!=this.nick && curchan.aoh && curchan.aoh.indexOf(_this_target)<0 && this.opmode) {
1133+
curchan.aoh.push(_mode_target[_this_target]);
11051134
}
11061135
}
1107-
if(_mode_target[_mode_count]!=this.nick && curchan.aov && curchan.aov.indexOf(_this_target)<0 && this.opmode) {
1108-
curchan.aov.push(_mode_target[_this_target]);
1109-
}
1110-
}
1111-
if(_mode_target[_mode_count]==this.nick) curchan.isvoice=true;
1112-
break;
1113-
case 'h':
1114-
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1115-
if(curchan.names[c].indexOf("%")==-1) {
1116-
if(curchan.names[c].indexOf("&")==-1 || curchan.names[c].indexOf("~")==-1 || curchan.names[c].indexOf("@")==-1) {
1117-
curchan.names[c]="%"+curchan.names[c];
1118-
}
1119-
else
1120-
curchan.names[c]=curchan.names[c].substr(0,1)+"%"+curchan.names[c].substr(1);
1121-
if(_mode_target[_mode_count]!=this.nick && curchan.aoh && curchan.aoh.indexOf(_this_target)<0 && this.opmode) {
1122-
curchan.aoh.push(_mode_target[_this_target]);
1123-
}
1124-
}
1125-
if(_mode_target[_mode_count]==this.nick) curchan.ishop=true;
1126-
break;
1136+
if(_mode_target[_mode_count]==this.nick) curchan.ishop=true;
1137+
break;
1138+
}
11271139
}
11281140
}
1141+
} else {
1142+
console.error("curchan or curchan.names is undefined.");
11291143
}
11301144
_mode_count++;
11311145
continue;
@@ -1156,35 +1170,39 @@ function clientConnect(socket) {
11561170
_mode[i]=='e' || _mode[i]=='b' || _mode[i]=='I' || _mode[i]=='q' || _mode[i]=='f' ||
11571171
_mode[i]=='j')) {
11581172
if(_mode[i]=='o' || _mode[i]=='v' || _mode[i]=='h') {
1159-
for(c=0;c<curchan.names.length;c++) {
1160-
if(curchan.names[c].replace(/(&|~|@|%|\+)/,"")==_mode_target[_mode_count]) {
1161-
switch(_mode[i]) {
1162-
case 'o':
1163-
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1164-
curchan.names[c]=curchan.names[c].replace(/(&|~|@)/,"");
1165-
if(_mode_target[_mode_count]!=this.nick && curchan.aop && curchan.aop.indexOf(_this_target)>=0 && this.opmode) {
1166-
curchan.aop.splice(curchan.aop.indexOf(_this_target),1);
1167-
}
1168-
if(_mode_target[_mode_count]==this.nick) this.isop=false;
1169-
break;
1170-
case 'v':
1171-
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1172-
curchan.names[c]=curchan.names[c].replace("+","");
1173-
if(_mode_target[_mode_count]!=this.nick && curchan.aov && curchan.aov.indexOf(_this_target)>=0 && this.opmode) {
1174-
curchan.aov.splice(curchan.aov.indexOf(_this_target),1);
1175-
}
1176-
if(_mode_target[_mode_count]==this.nick) this.isvoice=false;
1177-
break;
1178-
case 'h':
1179-
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1180-
curchan.names[c]=curchan.names[c].replace("%","");
1181-
if(_mode_target[_mode_count]!=this.nick && curchan.aoh && curchan.aoh.indexOf(_this_target)>=0 && this.opmode) {
1182-
curchan.aoh.splice(curchan.aoh.indexOf(_this_target),1);
1183-
}
1184-
if(_mode_target[_mode_count]==this.nick) this.ishop=false;
1185-
break;
1173+
if (curchan && curchan.names) {
1174+
for(c=0;c<curchan.names.length;c++) {
1175+
if(curchan.names[c].replace(/(&|~|@|%|\+)/,"")==_mode_target[_mode_count]) {
1176+
switch(_mode[i]) {
1177+
case 'o':
1178+
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1179+
curchan.names[c]=curchan.names[c].replace(/(&|~|@)/,"");
1180+
if(_mode_target[_mode_count]!=this.nick && curchan.aop && curchan.aop.indexOf(_this_target)>=0 && this.opmode) {
1181+
curchan.aop.splice(curchan.aop.indexOf(_this_target),1);
1182+
}
1183+
if(_mode_target[_mode_count]==this.nick) this.isop=false;
1184+
break;
1185+
case 'v':
1186+
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1187+
curchan.names[c]=curchan.names[c].replace("+","");
1188+
if(_mode_target[_mode_count]!=this.nick && curchan.aov && curchan.aov.indexOf(_this_target)>=0 && this.opmode) {
1189+
curchan.aov.splice(curchan.aov.indexOf(_this_target),1);
1190+
}
1191+
if(_mode_target[_mode_count]==this.nick) this.isvoice=false;
1192+
break;
1193+
case 'h':
1194+
_this_target = _mode_target[_mode_count] + "!" + (curchan.userhosts[c]?curchan.userhosts[c]:"*@*");
1195+
curchan.names[c]=curchan.names[c].replace("%","");
1196+
if(_mode_target[_mode_count]!=this.nick && curchan.aoh && curchan.aoh.indexOf(_this_target)>=0 && this.opmode) {
1197+
curchan.aoh.splice(curchan.aoh.indexOf(_this_target),1);
1198+
}
1199+
if(_mode_target[_mode_count]==this.nick) this.ishop=false;
1200+
break;
1201+
}
11861202
}
11871203
}
1204+
} else {
1205+
console.error("curchan or curchan.names is undefined.");
11881206
}
11891207
_mode_count++;
11901208
continue;
@@ -1230,28 +1248,30 @@ function clientConnect(socket) {
12301248
__channel=_channel.toLowerCase();
12311249
if(_nick==this.nick) {
12321250
if(!this.channels[__channel]) {
1233-
this.channels[__channel]={};
1234-
this.channels[__channel].modes='';
1235-
this.channels[__channel].topic='';
1236-
this.channels[__channel].topic_set='';
1237-
this.channels[__channel].topic_time=0;
1238-
this.channels[__channel].key=null;
1239-
this.channels[__channel].limit=null;
1240-
this.channels[__channel].forward=null;
1241-
this.channels[__channel].throttle=null;
1242-
this.channels[__channel].names=[];
1243-
this.channels[__channel].userhosts=[];
1244-
this.channels[__channel].name=_channel;
1245-
this.channels[__channel].aop=[];
1246-
this.channels[__channel].aoh=[];
1247-
this.channels[__channel].aov=[];
1248-
this.channels[__channel].isop=false;
1249-
this.channels[__channel].ishop=false;
1250-
this.channels[__channel].isvoice=false;
1251+
this.channels[__channel]={
1252+
modes:'',
1253+
topic:'',
1254+
topic_set:'',
1255+
topic_time:0,
1256+
key:null,
1257+
limit:null,
1258+
forward:null,
1259+
throttle:null,
1260+
names:[],
1261+
userhosts:[],
1262+
name:_channel,
1263+
aop:[],
1264+
aoh:[],
1265+
aov:[],
1266+
isop:false,
1267+
ishop:false,
1268+
isvoice:false
1269+
};
12511270
}
12521271
if(this.channels[__channel]) {
12531272
this.channels[__channel].name=_channel;
12541273
}
1274+
this.write(`MODE ${_channel}\n`);
12551275
}
12561276
else {
12571277
if(this.channels[__channel]) {
@@ -1447,6 +1467,10 @@ function clientConnect(socket) {
14471467
connection.on('error', function(err) {
14481468
this.end();
14491469
});
1470+
connection.on('end', function() {
1471+
console.log('Connection finished : connection');
1472+
return;
1473+
});
14501474
}
14511475
else {
14521476
socket.end();

0 commit comments

Comments
 (0)