networking.js 5.6 KB

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