2014-02-06 74 views
2

好吧,我在這裏掙扎了一下。這是我想要實現的: 創建並運行一個smpp服務器,用於偵聽來自我們的短信服務平臺的smpp發送請求。只有 我們的平臺應該能夠連接到這臺服務器。理解auth上的smpp node.js

在收到綁定,授權它我們的服務,並接收發送請求

只接受來自發送授權用戶/密碼的請求。

在收到發送請求的,將其發送給我們的消息提供者,與registered_delivery = 1

等待DELIVER_SM響應,讓我們知道它已經到達

更新我們的本地數據庫與消息送達狀態。

我的問題 - 我的服務似乎讓任何未經授權的做submit_sm。

這裏有一些粗糙,醜陋的測試代碼,我已經放在一起。我構造的東西錯了嗎?請記住,我對smpp和node.js都很陌生,所以我可能會瘋狂地偏離軌道。

var smpp = require('smpp'); 
// create listener for incoming connections from our server 
var server = smpp.createServer(function(session) { 
    // create outbound connection to provider to pass messages on 
    var outsession = smpp.connect('smpp.provider.net', 8101); 
    outsession.bind_transceiver({ 
     system_id: 'myaccount', 
     password: 'abcdef' 
    }, function (pdu){ 
     console.log("outsession bind completed"); 
     if (pdu.command_status == 0){ 
      console.log("outsession bind completed ok, status 0"); 
     } 
    }); 
    console.log("Srv: starting session.on event handlers"); 
    session.on('bind_transceiver', function(pdu) { 
     // we pause the session to prevent further incoming pdu events, 
     // untill we authorize the session with some async operation. 
     // auth incoming - can ONLY be our local system so far. 
     //if (checkAsyncUserPass(id,pw)){ 
      // credentials ok 
      // how do I remember this for future send request ? 
     //} else { 
      // report fail 
     //} 
      console.log("Srv: received bind_transceiver"); 
      session.send(pdu.response()); 
      outsession.submit_sm({ 
       destination_addr: '447957123456', 
       registered_delivery: 1, 
       short_message: 'Hello from provider, received a bind to server' 
      }, function(pdu) { 
       if (pdu.command_status == 0) { 
        console.log('srv: sent ok to provider'); 
       } 
      }); 
      session.send(pdu.response()); 
     // listen for incoming delivery response messages for previous messages 
     outsession.on('deliver_sm_resp', function(pdu){ 
      console.log('srv: outsession received a deliver_sm_resp'); 
     }); 
     outsession.on('deliver_sm', function(pdu){ 
      console.log('srv: outsession received a deliver_sm pdu'); 
     }); 
    }); 
    session.on('unbind', function(pdu){ 
     console.log("Srv: received unbind"); 
     session.send(pdu.response()); 
    }); 
    session.on('submit_sm', function(pdu){ 
     console.log("Srv: received submit_sm, pdu:"); 
     session.send(pdu.response()); 
    }); 
    session.on('submit_sm_resp', function(pdu){ 
     console.log("Srv: received submit_sm_resp"); 
     session.send(pdu.response()); 
    }); 
    session.on('deliver_sm', function(pdu){ 
     console.log("Srv: received deliver_sm pdu:"); 
     session.send(pdu.response()); 
    }); 
    session.on('deliver_sm_resp', function(pdu){ 
     console.log("Srv: session received deliver_sm_resp"); 
     session.send(pdu.response()); 
    }); 
    session.on('enquire_link', function(pdu){ 
     console.log("Srv: received enquire_link"); 
     session.send(pdu.response()); 
    }); 
    session.on('enquire_link_sm', function(pdu){ 
     console.log("Srv: received enquire_link_sm"); 
     session.send(pdu.response()); 
    }); 
}); 
function checkAsyncUserPass(id, pw, fn){ 
    return true; 
} 

server.listen(8101); 

回答

0

必須調用授權與證書用戶的asynchronic功能:

session.on('bind_transceiver', function(pdu) { 
    // pause the session to prevent further incoming pdu events 
    // until we authorize the session with an async operation 
    session.pause(); 
    checkAsyncUserPass(pdu.system_id, pdu.password, function(client){ 
     if(!client){ 
      session.send(pdu.response({ 
       command_status: smpp.ESME_RBINDFAIL //Here reject WRONG clients 
      })); 
      session.close(); 
      return; 
     } 
     // receive SMS... 
     session.resume(); 
     session.on('submit_sm', function(pdu) { 
      // do whatever you want to do with received SMS 
      // e.g. respond with an uuid response or something: 
      session.send(pdu.response({ 
       message_id: msgid //uuid (you have to generate this) 
      }); 
     }); 
    }); //End checkAsyncUserPass 
}); //End bind_transceiver