networking.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // cause error so the image will not be cached
  66. callback(body || null, response, (error || "TooManyRequests"));
  67. } else {
  68. logging.error(rid + " Unknown reply:");
  69. logging.error(rid + JSON.stringify(response));
  70. callback(body || null, response, error);
  71. }
  72. });
  73. };
  74. // helper method for get_from_options, no options required
  75. exp.get_from = function(rid, url, callback) {
  76. exp.get_from_options(rid, url, {}, function(body, response, err) {
  77. callback(body, response, err);
  78. });
  79. };
  80. // make a request to skins.miencraft.net
  81. // the skin url is taken from the HTTP redirect
  82. // type reference is above
  83. exp.get_username_url = function(rid, name, type, callback) {
  84. exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
  85. if (!err) {
  86. callback(err, response ? (response.statusCode === 404 ? null : response.headers.location) : null);
  87. } else {
  88. callback(err, null);
  89. }
  90. });
  91. };
  92. // gets the URL for a skin/cape from the profile
  93. // +type+ specifies which to retrieve
  94. exp.get_uuid_url = function(profile, type, callback) {
  95. var url = null;
  96. if (type === 0) {
  97. url = exp.extract_skin_url(profile);
  98. } else if (type === 1) {
  99. url = exp.extract_cape_url(profile);
  100. }
  101. callback(url || null);
  102. };
  103. // make a request to sessionserver for +uuid+
  104. // +callback+ contains error, profile
  105. exp.get_profile = function(rid, uuid, callback) {
  106. if (!uuid) {
  107. callback(null, null);
  108. } else {
  109. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  110. callback(err || null, (body !== null ? JSON.parse(body) : null));
  111. });
  112. }
  113. };
  114. // get the skin URL for +userId+
  115. // +profile+ is used if +userId+ is a uuid
  116. exp.get_skin_url = function(rid, userId, profile, callback) {
  117. get_url(rid, userId, profile, 0, function(url) {
  118. callback(url);
  119. });
  120. };
  121. // get the cape URL for +userId+
  122. // +profile+ is used if +userId+ is a uuid
  123. exp.get_cape_url = function(rid, userId, profile, callback) {
  124. get_url(rid, userId, profile, 1, function(url) {
  125. callback(url);
  126. });
  127. };
  128. function get_url(rid, userId, profile, type, callback) {
  129. if (userId.length <= 16) {
  130. //username
  131. exp.get_username_url(rid, userId, type, function(err, url) {
  132. callback(url || null);
  133. });
  134. } else {
  135. exp.get_uuid_url(profile, type, function(url) {
  136. callback(url || null);
  137. });
  138. }
  139. }
  140. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  141. if (tex_hash) {
  142. var textureurl = textures_url + tex_hash;
  143. exp.get_from(rid, textureurl, function(img, response, err) {
  144. if (err) {
  145. logging.error(rid + "error while downloading texture");
  146. callback(err, response, null);
  147. } else {
  148. fs.writeFile(outpath, img, "binary", function(err) {
  149. if (err) {
  150. logging.log(rid + "error: " + err.stack);
  151. }
  152. callback(err, response, img);
  153. });
  154. }
  155. });
  156. } else {
  157. callback(null, null, null);
  158. }
  159. };
  160. module.exports = exp;