networking.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // 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.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. if (error) {
  70. logging.error(url, error);
  71. } else {
  72. var logfunc = code && code < 405 ? logging.log : logging.warn;
  73. logfunc(rid, url, code, http_code[code]);
  74. }
  75. // 200 or 301 depending on content type
  76. if (!error && (code === 200 || code === 301)) {
  77. // response received successfully
  78. callback(body, response, null);
  79. } else if (error) {
  80. callback(body || null, response, error);
  81. } else if (code === 404 || code === 204) {
  82. // page does not exist
  83. callback(null, response, null);
  84. } else if (code === 429) {
  85. // Too Many Requests exception - code 429
  86. // cause error so the image will not be cached
  87. callback(body || null, response, (error || "TooManyRequests"));
  88. } else {
  89. logging.error(rid, " Unknown reply:");
  90. logging.error(rid, JSON.stringify(response));
  91. callback(body || null, response, error);
  92. }
  93. });
  94. };
  95. // helper method for get_from_options, no options required
  96. exp.get_from = function(rid, url, callback) {
  97. exp.get_from_options(rid, url, {}, function(body, response, err) {
  98. callback(body, response, err);
  99. });
  100. };
  101. // make a request to skins.miencraft.net
  102. // the skin url is taken from the HTTP redirect
  103. // type reference is above
  104. exp.get_username_url = function(rid, name, type, callback) {
  105. exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
  106. if (!err) {
  107. if (response) {
  108. callback(err, response.statusCode === 404 ? null : response.headers.location);
  109. } else {
  110. callback(err, null);
  111. }
  112. } else {
  113. callback(err, null);
  114. }
  115. });
  116. };
  117. // gets the URL for a skin/cape from the profile
  118. // +type+ specifies which to retrieve
  119. exp.get_uuid_url = function(profile, type, callback) {
  120. var url = null;
  121. if (type === 0) {
  122. url = exp.extract_skin_url(profile);
  123. } else if (type === 1) {
  124. url = exp.extract_cape_url(profile);
  125. }
  126. callback(url || null);
  127. };
  128. // make a request to sessionserver for +uuid+
  129. // callback: error, profile
  130. exp.get_profile = function(rid, uuid, callback) {
  131. if (!uuid) {
  132. callback(null, null);
  133. } else {
  134. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  135. callback(err || null, (body !== null ? JSON.parse(body) : null));
  136. });
  137. }
  138. };
  139. // get the skin URL for +userId+
  140. // +profile+ is used if +userId+ is a uuid
  141. exp.get_skin_url = function(rid, userId, profile, callback) {
  142. get_url(rid, userId, profile, 0, function(err, url) {
  143. callback(err, url);
  144. });
  145. };
  146. // get the cape URL for +userId+
  147. // +profile+ is used if +userId+ is a uuid
  148. exp.get_cape_url = function(rid, userId, profile, callback) {
  149. get_url(rid, userId, profile, 1, function(err, url) {
  150. callback(err, url);
  151. });
  152. };
  153. // download the +tex_hash+ image from the texture server
  154. // and save it in the +outpath+ file
  155. // callback: error, response, image buffer
  156. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  157. if (tex_hash) {
  158. var textureurl = textures_url + tex_hash;
  159. exp.get_from(rid, textureurl, function(img, response, err) {
  160. if (err) {
  161. logging.error(rid, "error while downloading texture");
  162. callback(err, response, null);
  163. } else {
  164. fs.writeFile(outpath, img, "binary", function(fs_err) {
  165. if (fs_err) {
  166. logging.error(rid, "error:", fs_err.stack);
  167. }
  168. callback(fs_err, response, img);
  169. });
  170. }
  171. });
  172. } else {
  173. callback(null, null, null);
  174. }
  175. };
  176. module.exports = exp;