2
0

networking.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, undefined);
  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": "Crafatar (+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 502: // CloudFront can't reach mojang origin
  67. case 503:
  68. case 504:
  69. // we don't want to cache this
  70. error = error || e;
  71. body = null;
  72. break;
  73. default:
  74. if (!error) {
  75. // Probably 500 or the likes
  76. logging.error(rid, "Unexpected response:", code, body);
  77. }
  78. error = error || e;
  79. body = null;
  80. break;
  81. }
  82. if (body && !body.length) {
  83. // empty response
  84. body = null;
  85. }
  86. callback(body, response, error);
  87. });
  88. };
  89. // helper method for get_from_options, no options required
  90. exp.get_from = function(rid, url, callback) {
  91. exp.get_from_options(rid, url, {}, function(body, response, err) {
  92. callback(body, response, err);
  93. });
  94. };
  95. // make a request to skins.miencraft.net
  96. // the skin url is taken from the HTTP redirect
  97. // type reference is above
  98. exp.get_username_url = function(rid, name, type, callback) {
  99. type = Number(type === "CAPE");
  100. exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
  101. if (!err) {
  102. if (response) {
  103. callback(err, response.statusCode === 404 ? null : response.headers.location);
  104. } else {
  105. callback(err, null);
  106. }
  107. } else {
  108. callback(err, null);
  109. }
  110. });
  111. };
  112. // gets the URL for a skin/cape from the profile
  113. // +type+ "SKIN" or "CAPE", specifies which to retrieve
  114. // callback: url, slim
  115. exp.get_uuid_info = function(profile, type, callback) {
  116. var properties = Object.get(profile, "properties") || [];
  117. properties.forEach(function(prop) {
  118. if (prop.name === "textures") {
  119. var json = new Buffer(prop.value, "base64").toString();
  120. profile = JSON.parse(json);
  121. }
  122. });
  123. var url = Object.get(profile, "textures." + type + ".url");
  124. var slim;
  125. if (type === "SKIN") {
  126. slim = Object.get(profile, "textures.SKIN.metadata.model") === "slim";
  127. }
  128. callback(url || null, !!slim);
  129. };
  130. // make a request to sessionserver for +uuid+
  131. // callback: error, profile
  132. exp.get_profile = function(rid, uuid, callback) {
  133. if (!uuid) {
  134. callback(null, null);
  135. } else {
  136. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  137. try {
  138. body = body ? JSON.parse(body) : null;
  139. callback(err || null, body);
  140. } catch(e) {
  141. if (e instanceof SyntaxError) {
  142. logging.warn(rid, "Failed to parse JSON", e);
  143. logging.debug(rid, body);
  144. callback(err || null, null);
  145. } else {
  146. throw e;
  147. }
  148. }
  149. });
  150. }
  151. };
  152. // get the skin URL and type for +userId+
  153. // +profile+ is used if +userId+ is a uuid
  154. // callback: error, url, slim
  155. exp.get_skin_info = function(rid, userId, profile, callback) {
  156. get_info(rid, userId, profile, "SKIN", callback);
  157. };
  158. // get the cape URL for +userId+
  159. // +profile+ is used if +userId+ is a uuid
  160. exp.get_cape_url = function(rid, userId, profile, callback) {
  161. get_info(rid, userId, profile, "CAPE", callback);
  162. };
  163. // download the +tex_hash+ image from the texture server
  164. // and save it in the +outpath+ file
  165. // callback: error, response, image buffer
  166. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  167. if (tex_hash) {
  168. var textureurl = textures_url + tex_hash;
  169. exp.get_from(rid, textureurl, function(img, response, err) {
  170. if (err) {
  171. callback(err, response, null);
  172. } else {
  173. skins.save_image(img, outpath, function(img_err) {
  174. callback(img_err, response, img);
  175. });
  176. }
  177. });
  178. } else {
  179. callback(null, null, null);
  180. }
  181. };
  182. module.exports = exp;