networking.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 textures_url = "http://textures.minecraft.net/texture/";
  9. var exp = {};
  10. // performs a GET request to the +url+
  11. // +options+ object includes these options:
  12. // encoding (string), default is to return a buffer
  13. // callback: the body, response,
  14. // and error buffer. get_from helper method is available
  15. exp.get_from_options = function(rid, url, options, callback) {
  16. request.get({
  17. url: url,
  18. headers: {
  19. "User-Agent": "Crafatar (+https://crafatar.com)"
  20. },
  21. timeout: config.server.http_timeout,
  22. followRedirect: false,
  23. encoding: options.encoding || null,
  24. }, function(error, response, body) {
  25. // log url + code + description
  26. var code = response && response.statusCode;
  27. var logfunc = code && code < 405 ? logging.debug : logging.warn;
  28. logfunc(rid, url, code || error && error.code, http_code[code]);
  29. // not necessarily used
  30. var e = new Error(code);
  31. e.name = "HTTP";
  32. e.code = "HTTPERROR";
  33. switch (code) {
  34. case 200:
  35. case 301:
  36. case 302: // never seen, but mojang might use it in future
  37. case 307: // never seen, but mojang might use it in future
  38. case 308: // never seen, but mojang might use it in future
  39. // these are okay
  40. break;
  41. case 204: // no content, used like 404 by mojang. making sure it really has no content
  42. case 404:
  43. // can be cached as null
  44. body = null;
  45. break;
  46. case 429: // this shouldn't usually happen, but occasionally does
  47. case 500:
  48. case 502: // CloudFront can't reach mojang origin
  49. case 503:
  50. case 504:
  51. // we don't want to cache this
  52. error = error || e;
  53. body = null;
  54. break;
  55. default:
  56. if (!error) {
  57. // Probably 500 or the likes
  58. logging.error(rid, "Unexpected response:", code, body);
  59. }
  60. error = error || e;
  61. body = null;
  62. break;
  63. }
  64. if (body && !body.length) {
  65. // empty response
  66. body = null;
  67. }
  68. callback(body, response, error);
  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. // gets the URL for a skin/cape from the profile
  78. // +type+ "SKIN" or "CAPE", specifies which to retrieve
  79. // callback: url, slim
  80. exp.get_uuid_info = function(profile, type, callback) {
  81. var properties = Object.get(profile, "properties") || [];
  82. properties.forEach(function(prop) {
  83. if (prop.name === "textures") {
  84. var json = new Buffer.from(prop.value, "base64").toString();
  85. profile = JSON.parse(json);
  86. }
  87. });
  88. var url = Object.get(profile, "textures." + type + ".url");
  89. var slim;
  90. if (type === "SKIN") {
  91. slim = Object.get(profile, "textures.SKIN.metadata.model") === "slim";
  92. }
  93. callback(null, url || null, !!slim);
  94. };
  95. // make a request to sessionserver for +uuid+
  96. // callback: error, profile
  97. exp.get_profile = function(rid, uuid, callback) {
  98. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  99. try {
  100. body = body ? JSON.parse(body) : null;
  101. callback(err || null, body);
  102. } catch(e) {
  103. if (e instanceof SyntaxError) {
  104. logging.warn(rid, "Failed to parse JSON", e);
  105. logging.debug(rid, body);
  106. callback(err || null, null);
  107. } else {
  108. throw e;
  109. }
  110. }
  111. });
  112. };
  113. // get the skin URL and type for +userId+
  114. // +profile+ is used if +userId+ is a uuid
  115. // callback: error, url, slim
  116. exp.get_skin_info = function(rid, userId, profile, callback) {
  117. exp.get_uuid_info(profile, "SKIN", callback);
  118. };
  119. // get the cape URL for +userId+
  120. // +profile+ is used if +userId+ is a uuid
  121. exp.get_cape_url = function(rid, userId, profile, callback) {
  122. exp.get_uuid_info(profile, "CAPE", callback);
  123. };
  124. // download the +tex_hash+ image from the texture server
  125. // and save it in the +outpath+ file
  126. // callback: error, response, image buffer
  127. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  128. if (tex_hash) {
  129. var textureurl = textures_url + tex_hash;
  130. exp.get_from(rid, textureurl, function(img, response, err) {
  131. if (err) {
  132. callback(err, response, null);
  133. } else {
  134. skins.save_image(img, outpath, function(img_err) {
  135. callback(img_err, response, img);
  136. });
  137. }
  138. });
  139. } else {
  140. callback(null, null, null);
  141. }
  142. };
  143. module.exports = exp;