networking.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. var http_code = require("http").STATUS_CODES;
  2. var logging = require("./logging");
  3. var request = require("request");
  4. var config = require("./config");
  5. var fs = require("fs");
  6. var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
  7. var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
  8. var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
  9. var textures_url = "http://textures.minecraft.net/texture/";
  10. var mojang_urls = [skins_url, capes_url];
  11. var exp = {};
  12. function extract_url(profile, property) {
  13. var url = null;
  14. if (profile && profile.properties) {
  15. profile.properties.forEach(function(prop) {
  16. if (prop.name === "textures") {
  17. var json = new Buffer(prop.value, "base64").toString();
  18. var props = JSON.parse(json);
  19. url = props && props.textures && props.textures[property] && props.textures[property].url || null;
  20. }
  21. });
  22. }
  23. return url;
  24. }
  25. // exracts the skin url of a +profile+ object
  26. // returns null when no url found (user has no skin)
  27. exp.extract_skin_url = function(profile) {
  28. return extract_url(profile, 'SKIN');
  29. };
  30. // exracts the cape url of a +profile+ object
  31. // returns null when no url found (user has no cape)
  32. exp.extract_cape_url = function(profile) {
  33. return extract_url(profile, 'CAPE');
  34. };
  35. // makes a GET request to the +url+
  36. // +options+ hash includes these options:
  37. // encoding (string), default is to return a buffer
  38. // +callback+ contains the body, response,
  39. // and error buffer. get_from helper method is available
  40. exp.get_from_options = function(rid, url, options, callback) {
  41. request.get({
  42. url: url,
  43. headers: {
  44. "User-Agent": "https://crafatar.com"
  45. },
  46. timeout: config.http_timeout,
  47. followRedirect: false,
  48. encoding: (options.encoding || null),
  49. }, function(error, response, body) {
  50. // log url + code + description
  51. var code = response.statusCode;
  52. logfunc = code && code < 405 ? logging.log : logging.warn;
  53. logfunc(rid + url + " " + code + " " + http_code[code]);
  54. // 200 or 301 depending on content type
  55. if (!error && (code === 200 || code === 301)) {
  56. // response received successfully
  57. callback(body, response, null);
  58. } else if (error) {
  59. callback(body || null, response, error);
  60. } else if (code === 404 || code === 204) {
  61. // page does not exist
  62. callback(null, response, null);
  63. } else if (code === 429) {
  64. // Too Many Requests exception - code 429
  65. callback(body || null, response, error);
  66. } else {
  67. logging.error(rid + " Unknown reply:");
  68. logging.error(rid + JSON.stringify(response));
  69. callback(body || null, response, error);
  70. }
  71. });
  72. };
  73. // helper method for get_from_options, no options required
  74. exp.get_from = function(rid, url, callback) {
  75. exp.get_from_options(rid, url, {}, function(body, response, err) {
  76. callback(body, response, err);
  77. });
  78. };
  79. // make a request to skins.miencraft.net
  80. // the skin url is taken from the HTTP redirect
  81. // type reference is above
  82. exp.get_username_url = function(rid, name, type, callback) {
  83. exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
  84. if (!err) {
  85. callback(err, response ? (response.statusCode === 404 ? null : response.headers.location) : null);
  86. } else {
  87. callback(err, null);
  88. }
  89. });
  90. };
  91. // gets the URL for a skin/cape from the profile
  92. // +type+ specifies which to retrieve
  93. exp.get_uuid_url = function(profile, type, callback) {
  94. var url = null;
  95. if (type === 0) {
  96. url = exp.extract_skin_url(profile);
  97. } else if (type === 1) {
  98. url = exp.extract_cape_url(profile);
  99. }
  100. callback(url || null);
  101. };
  102. // make a request to sessionserver for +uuid+
  103. // +callback+ contains error, profile
  104. exp.get_profile = function(rid, uuid, callback) {
  105. if (!uuid) {
  106. callback(null, null);
  107. } else {
  108. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  109. callback(err || null, (body !== null ? JSON.parse(body) : null));
  110. });
  111. }
  112. };
  113. // get the skin URL for +userId+
  114. // +profile+ is used if +userId+ is a uuid
  115. exp.get_skin_url = function(rid, userId, profile, callback) {
  116. get_url(rid, userId, profile, 0, function(url) {
  117. callback(url);
  118. });
  119. };
  120. // get the cape URL for +userId+
  121. // +profile+ is used if +userId+ is a uuid
  122. exp.get_cape_url = function(rid, userId, profile, callback) {
  123. get_url(rid, userId, profile, 1, function(url) {
  124. callback(url);
  125. });
  126. };
  127. function get_url(rid, userId, profile, type, callback) {
  128. if (userId.length <= 16) {
  129. //username
  130. exp.get_username_url(rid, userId, type, function(err, url) {
  131. callback(url || null);
  132. });
  133. } else {
  134. exp.get_uuid_url(profile, type, function(url) {
  135. callback(url || null);
  136. });
  137. }
  138. }
  139. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  140. if (tex_hash) {
  141. var textureurl = textures_url + tex_hash;
  142. exp.get_from(rid, textureurl, function(img, response, err) {
  143. if (err) {
  144. logging.error(rid + "error while downloading texture");
  145. callback(err, response, null);
  146. } else {
  147. fs.writeFile(outpath, img, "binary", function(err) {
  148. if (err) {
  149. logging.log(rid + "error: " + err.stack);
  150. }
  151. callback(err, response, img);
  152. });
  153. }
  154. });
  155. } else {
  156. callback(null, null, null);
  157. }
  158. };
  159. module.exports = exp;