networking.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 skins = require("./skins");
  6. require("./object-patch");
  7. var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
  8. var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
  9. var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
  10. var textures_url = "http://textures.minecraft.net/texture/";
  11. var mojang_urls = [skins_url, capes_url];
  12. var exp = {};
  13. // helper method that calls `get_username_url` or `get_uuid_info` based on the +usedId+
  14. // +userId+ is used for usernames, while +profile+ is used for UUIDs
  15. // callback: error, url, slim
  16. function get_info(rid, userId, profile, type, callback) {
  17. if (userId.length <= 16) {
  18. // username
  19. exp.get_username_url(rid, userId, type, function(err, url) {
  20. callback(err, url || null, false);
  21. });
  22. } else {
  23. exp.get_uuid_info(profile, type, function(url, slim) {
  24. callback(null, url || null, slim);
  25. });
  26. }
  27. }
  28. // performs a GET request to the +url+
  29. // +options+ object includes these options:
  30. // encoding (string), default is to return a buffer
  31. // callback: the body, response,
  32. // and error buffer. get_from helper method is available
  33. exp.get_from_options = function(rid, url, options, callback) {
  34. request.get({
  35. url: url,
  36. headers: {
  37. "User-Agent": "https://crafatar.com"
  38. },
  39. timeout: config.server.http_timeout,
  40. followRedirect: false,
  41. encoding: options.encoding || null,
  42. }, function(error, response, body) {
  43. // log url + code + description
  44. var code = response && response.statusCode;
  45. var logfunc = code && code < 405 ? logging.debug : logging.warn;
  46. logfunc(rid, url, code || error && error.code, http_code[code]);
  47. // not necessarily used
  48. var e = new Error(code);
  49. e.name = "HTTP";
  50. e.code = "HTTPERROR";
  51. switch (code) {
  52. case 200:
  53. case 301:
  54. case 302: // never seen, but mojang might use it in future
  55. case 307: // never seen, but mojang might use it in future
  56. case 308: // never seen, but mojang might use it in future
  57. // these are okay
  58. break;
  59. case 204: // no content, used like 404 by mojang. making sure it really has no content
  60. case 404:
  61. // can be cached as null
  62. body = null;
  63. break;
  64. case 429: // this shouldn't usually happen, but occasionally does
  65. case 500:
  66. case 503:
  67. case 504:
  68. // we don't want to cache this
  69. error = error || e;
  70. body = null;
  71. break;
  72. default:
  73. if (!error) {
  74. // Probably 500 or the likes
  75. logging.error(rid, "Unexpected response:", code, body);
  76. }
  77. error = error || e;
  78. body = null;
  79. break;
  80. }
  81. if (body && !body.length) {
  82. // empty response
  83. body = null;
  84. }
  85. callback(body, response, error);
  86. });
  87. };
  88. // helper method for get_from_options, no options required
  89. exp.get_from = function(rid, url, callback) {
  90. exp.get_from_options(rid, url, {}, function(body, response, err) {
  91. callback(body, response, err);
  92. });
  93. };
  94. // make a request to skins.miencraft.net
  95. // the skin url is taken from the HTTP redirect
  96. // type reference is above
  97. exp.get_username_url = function(rid, name, type, callback) {
  98. type = Number(type === "CAPE");
  99. exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
  100. if (!err) {
  101. if (response) {
  102. callback(err, response.statusCode === 404 ? null : response.headers.location);
  103. } else {
  104. callback(err, null);
  105. }
  106. } else {
  107. callback(err, null);
  108. }
  109. });
  110. };
  111. // gets the URL for a skin/cape from the profile
  112. // +type+ "SKIN" or "CAPE", specifies which to retrieve
  113. // callback: url, slim
  114. exp.get_uuid_info = function(profile, type, callback) {
  115. var properties = Object.get(profile, "properties") || [];
  116. properties.forEach(function(prop) {
  117. if (prop.name === "textures") {
  118. var json = new Buffer(prop.value, "base64").toString();
  119. profile = JSON.parse(json);
  120. }
  121. });
  122. var url = Object.get(profile, "textures." + type + ".url");
  123. var slim;
  124. if (type === "SKIN") {
  125. slim = Object.get(profile, "textures.SKIN.metadata.model");
  126. }
  127. callback(url || null, !!slim);
  128. };
  129. // make a request to sessionserver for +uuid+
  130. // callback: error, profile
  131. exp.get_profile = function(rid, uuid, callback) {
  132. if (!uuid) {
  133. callback(null, null);
  134. } else {
  135. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  136. try {
  137. body = body ? JSON.parse(body) : null;
  138. callback(err || null, body);
  139. } catch(e) {
  140. if (e instanceof SyntaxError) {
  141. logging.warn(rid, "Failed to parse JSON", e);
  142. logging.debug(rid, body);
  143. callback(err || null, null);
  144. } else {
  145. throw e;
  146. }
  147. }
  148. });
  149. }
  150. };
  151. // get the skin URL and type for +userId+
  152. // +profile+ is used if +userId+ is a uuid
  153. // callback: error, url, slim
  154. exp.get_skin_info = function(rid, userId, profile, callback) {
  155. get_info(rid, userId, profile, "SKIN", callback);
  156. };
  157. // get the cape URL for +userId+
  158. // +profile+ is used if +userId+ is a uuid
  159. exp.get_cape_url = function(rid, userId, profile, callback) {
  160. get_info(rid, userId, profile, "CAPE", callback);
  161. };
  162. // download the +tex_hash+ image from the texture server
  163. // and save it in the +outpath+ file
  164. // callback: error, response, image buffer
  165. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  166. if (tex_hash) {
  167. var textureurl = textures_url + tex_hash;
  168. exp.get_from(rid, textureurl, function(img, response, err) {
  169. if (err) {
  170. callback(err, response, null);
  171. } else {
  172. skins.save_image(img, outpath, function(img_err, saved_img) {
  173. callback(img_err, response, saved_img);
  174. });
  175. }
  176. });
  177. } else {
  178. callback(null, null, null);
  179. }
  180. };
  181. module.exports = exp;