Advertisement
Guest User

node.js用 2ch新API簡易串

a guest
Feb 26th, 2015
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2.  
  3. var createHmac = require('crypto').createHmac;
  4. var https = require('https');
  5. var http = require('http');
  6. var stringify = require('querystring').stringify;
  7. var url = require('url');
  8.  
  9. var APP_KEY = 'xxfvFQcOzpTBvwuwPMwwzLZxiCSaGb';
  10. var HM_KEY = 'DgQ3aNpoluV1cl3GFJAqitBg5xKiXZ';
  11. var PORT = 8088;
  12.  
  13. var headerList = [
  14.   'accept-encoding',
  15.   'if-modified-since',
  16.   'range'
  17. ];
  18.  
  19. var _sid;
  20.  
  21. function authenticate() {
  22.   var ct = '1234567890';
  23.   var message = APP_KEY + ct;
  24.   var hb = createHmac('sha256', HM_KEY).update(message).digest('hex');
  25.   var data = { ID: '', PW: '', KY: APP_KEY, CT: ct, HB: hb };
  26.   var headers = {
  27.     'User-Agent': '',
  28.     'X-2ch-UA': 'JaneStyle/3.80',
  29.     'Content-Type': 'application/x-www-form-urlencoded'
  30.   };
  31.   var options = {
  32.     'method': 'POST',
  33.     'hostname': 'api.2ch.net',
  34.     'path': '/v1/auth/',
  35.     'headers': headers
  36.   };
  37.  
  38.   https.request(options, function(response) {
  39.     var buf = '';
  40.     response.on('data', function(d) { buf += d; });
  41.     response.on('end', function() { _sid = buf.split(':')[1]; });
  42.   }).end(stringify(data));
  43. }
  44.  
  45. authenticate();
  46.  
  47. var server = http.createServer(function(req, res) {
  48.   var o = url.parse(req.url);
  49.   if (o.href.match(/http:\/\/(\w+)\.2ch\.net\/(\w+)\/dat\/(\d+)\.dat/)) {
  50.     var server = RegExp.$1;
  51.     var board = RegExp.$2;
  52.     var threadID = RegExp.$3;
  53.  
  54.     var message = '/v1/' + server + '/' + board + '/' + threadID;
  55.     message += _sid + APP_KEY;
  56.     var hobo = createHmac('sha256', HM_KEY).update(message).digest('hex');
  57.     var data = { sid: _sid, hobo: hobo, appkey: APP_KEY };
  58.     var headers = {
  59.       'user-agent': 'Mozilla/3.0 (compatible; JaneStyle/3.80..)',
  60.       'content-type': 'application/x-www-form-urlencoded'
  61.     };
  62.     headerList.forEach(function(key) {
  63.       if (key in req.headers) headers[key] = req.headers[key];
  64.     });
  65.     var options = {
  66.       'method': 'POST',
  67.       'hostname': 'api.2ch.net',
  68.       'path': '/v1/' + server + '/' + board + '/' + threadID,
  69.       'headers': headers
  70.     };
  71.  
  72.     return https.request(options, function(response) {
  73.       res.writeHead(response.statusCode, response.headers);
  74.       response.pipe(res);
  75.     }).end(stringify(data));
  76.   }
  77.  
  78.   o.method = req.method;
  79.   o.headers = req.headers;
  80.   req.pipe(http.request(o, function(response) {
  81.     res.writeHead(response.statusCode, response.headers);
  82.     response.pipe(res);
  83.   }));
  84. });
  85.  
  86. server.on('clientError', function(err, sock) {
  87.   console.log(err);
  88.   sock.end();
  89. });
  90.  
  91. server.listen(PORT, function() {
  92.   console.log('proxy server is listening on port %d', server.address().port);
  93. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement