networking.js 6.2 KB

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