networking.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var logging = require("./logging");
  2. var request = require("request");
  3. var config = require("./config");
  4. var fs = require("fs");
  5. var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
  6. var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
  7. var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
  8. var textures_url = "http://textures.minecraft.net/texture/";
  9. var mojang_urls = [skins_url, capes_url];
  10. var exp = {};
  11. function extract_url(profile, property) {
  12. var url = null;
  13. if (profile && profile.properties) {
  14. profile.properties.forEach(function(prop) {
  15. if (prop.name === "textures") {
  16. var json = new Buffer(prop.value, "base64").toString();
  17. var props = JSON.parse(json);
  18. url = props && props.textures && props.textures[property] && props.textures[property].url || null;
  19. }
  20. });
  21. }
  22. return url;
  23. }
  24. // exracts the skin url of a +profile+ object
  25. // returns null when no url found (user has no skin)
  26. exp.extract_skin_url = function(profile) {
  27. return extract_url(profile, 'SKIN');
  28. };
  29. // exracts the cape url of a +profile+ object
  30. // returns null when no url found (user has no cape)
  31. exp.extract_cape_url = function(profile) {
  32. return extract_url(profile, 'CAPE');
  33. };
  34. // makes a GET request to the +url+
  35. // +options+ hash includes these options:
  36. // encoding (string), default is to return a buffer
  37. // +callback+ contains the body, response,
  38. // and error buffer. get_from helper method is available
  39. exp.get_from_options = function(rid, url, options, callback) {
  40. request.get({
  41. url: url,
  42. headers: {
  43. "User-Agent": "https://crafatar.com"
  44. },
  45. timeout: config.http_timeout,
  46. followRedirect: false,
  47. encoding: (options.encoding || null),
  48. }, function(error, response, body) {
  49. // 200 or 301 depending on content type
  50. if (!error && (response.statusCode === 200 || response.statusCode === 301)) {
  51. // response received successfully
  52. logging.log(rid + url + " url received");
  53. callback(body, response, null);
  54. } else if (error) {
  55. callback(body || null, response, error);
  56. } else if (response.statusCode === 404 || response.statusCode === 204) {
  57. // page does not exist
  58. logging.log(rid + url + " url does not exist");
  59. callback(null, response, null);
  60. } else if (response.statusCode === 429) {
  61. // Too Many Requests exception - code 429
  62. logging.warn(rid + url + " Too many requests");
  63. callback(body || null, response, error);
  64. } else {
  65. logging.error(rid + url + " Unknown error:");
  66. logging.error(rid + JSON.stringify(response));
  67. callback(body || null, response, error);
  68. }
  69. });
  70. };
  71. // helper method for get_from_options, no options required
  72. exp.get_from = function(rid, url, callback) {
  73. exp.get_from_options(rid, url, {}, function(body, response, err) {
  74. callback(body, response, err);
  75. });
  76. };
  77. // make a request to skins.miencraft.net
  78. // the skin url is taken from the HTTP redirect
  79. // type reference is above
  80. exp.get_username_url = function(rid, name, type, callback) {
  81. exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
  82. if (!err) {
  83. callback(err, response ? (response.statusCode === 404 ? null : response.headers.location) : null);
  84. } else {
  85. callback(err, null);
  86. }
  87. });
  88. };
  89. // gets the URL for a skin/cape from the profile
  90. // +type+ specifies which to retrieve
  91. exp.get_uuid_url = function(profile, type, callback) {
  92. var url = null;
  93. if (type === 0) {
  94. url = exp.extract_skin_url(profile);
  95. } else if (type === 1) {
  96. url = exp.extract_cape_url(profile);
  97. }
  98. callback(url || null);
  99. };
  100. // make a request to sessionserver
  101. // profile is returned as json
  102. exp.get_profile = function(rid, uuid, callback) {
  103. if (!uuid) {
  104. callback(null, null);
  105. } else {
  106. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  107. callback(err || null, (body !== null ? JSON.parse(body) : null));
  108. });
  109. }
  110. };
  111. // +userId+ is likely a username and if so
  112. // +userId+ is used to get the url, otherwise
  113. // +profile+ will be used to get the url
  114. exp.get_skin_url = function(rid, userId, profile, callback) {
  115. get_url(rid, userId, profile, 0, function(url) {
  116. callback(url);
  117. });
  118. };
  119. // +userId+ is likely a username and if so
  120. // +userId+ is used to get the url, otherwise
  121. // +profile+ will be used to get the url
  122. exp.get_cape_url = function(rid, userId, profile, callback) {
  123. get_url(rid, userId, profile, 1, function(url) {
  124. callback(url);
  125. });
  126. };
  127. function get_url(rid, userId, profile, type, callback) {
  128. if (userId.length <= 16) {
  129. //username
  130. exp.get_username_url(rid, userId, type, function(err, url) {
  131. callback(url || null);
  132. });
  133. } else {
  134. exp.get_uuid_url(profile, type, function(url) {
  135. callback(url || null);
  136. });
  137. }
  138. }
  139. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  140. if (tex_hash) {
  141. var textureurl = textures_url + tex_hash;
  142. exp.get_from(rid, textureurl, function(img, response, err) {
  143. if (err) {
  144. logging.error(rid + "error while downloading texture");
  145. callback(err, response, null);
  146. } else {
  147. fs.writeFile(outpath, img, "binary", function(err) {
  148. if (err) {
  149. logging.log(rid + "error: " + err.stack);
  150. }
  151. callback(err, response, img);
  152. });
  153. }
  154. });
  155. } else {
  156. callback(null, null, null);
  157. }
  158. };
  159. module.exports = exp;