networking.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 || error && error.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 429: // this shouldn't usually happen, but occasionally does
  82. case 500:
  83. case 503:
  84. // we don't want to cache this
  85. body = null;
  86. break;
  87. default:
  88. if (!error) {
  89. // Probably 500 or the likes
  90. logging.error(rid, "Unexpected response:", code, body);
  91. }
  92. break;
  93. }
  94. if (body && !body.length) {
  95. // empty response
  96. body = null;
  97. }
  98. callback(body, response, error);
  99. });
  100. };
  101. // helper method for get_from_options, no options required
  102. exp.get_from = function(rid, url, callback) {
  103. exp.get_from_options(rid, url, {}, function(body, response, err) {
  104. callback(body, response, err);
  105. });
  106. };
  107. // make a request to skins.miencraft.net
  108. // the skin url is taken from the HTTP redirect
  109. // type reference is above
  110. exp.get_username_url = function(rid, name, type, callback) {
  111. exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
  112. if (!err) {
  113. if (response) {
  114. callback(err, response.statusCode === 404 ? null : response.headers.location);
  115. } else {
  116. callback(err, null);
  117. }
  118. } else {
  119. callback(err, null);
  120. }
  121. });
  122. };
  123. // gets the URL for a skin/cape from the profile
  124. // +type+ specifies which to retrieve
  125. exp.get_uuid_url = function(profile, type, callback) {
  126. var url = null;
  127. if (type === 0) {
  128. url = exp.extract_skin_url(profile);
  129. } else if (type === 1) {
  130. url = exp.extract_cape_url(profile);
  131. }
  132. callback(url || null);
  133. };
  134. // make a request to sessionserver for +uuid+
  135. // callback: error, profile
  136. exp.get_profile = function(rid, uuid, callback) {
  137. if (!uuid) {
  138. callback(null, null);
  139. } else {
  140. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  141. try {
  142. body = body ? JSON.parse(body) : null;
  143. callback(err || null, body);
  144. } catch(e) {
  145. if (e instanceof SyntaxError) {
  146. logging.warn(rid, "Failed to parse JSON", e);
  147. logging.debug(rid, body);
  148. callback(err || null, null);
  149. } else {
  150. throw e;
  151. }
  152. }
  153. });
  154. }
  155. };
  156. // get the skin URL for +userId+
  157. // +profile+ is used if +userId+ is a uuid
  158. exp.get_skin_url = function(rid, userId, profile, callback) {
  159. get_url(rid, userId, profile, 0, function(err, url) {
  160. callback(err, url);
  161. });
  162. };
  163. // get the cape URL for +userId+
  164. // +profile+ is used if +userId+ is a uuid
  165. exp.get_cape_url = function(rid, userId, profile, callback) {
  166. get_url(rid, userId, profile, 1, function(err, url) {
  167. callback(err, url);
  168. });
  169. };
  170. // download the +tex_hash+ image from the texture server
  171. // and save it in the +outpath+ file
  172. // callback: error, response, image buffer
  173. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  174. if (tex_hash) {
  175. var textureurl = textures_url + tex_hash;
  176. exp.get_from(rid, textureurl, function(img, response, err) {
  177. if (err) {
  178. callback(err, response, null);
  179. } else {
  180. skins.save_image(img, outpath, function(img_err) {
  181. callback(img_err, response, img);
  182. });
  183. }
  184. });
  185. } else {
  186. callback(null, null, null);
  187. }
  188. };
  189. module.exports = exp;