networking.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. var logging = require("./logging");
  2. var request = require("request");
  3. var config = require("../config");
  4. var skins = require("./skins");
  5. var http = require("http");
  6. require("./object-patch");
  7. var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
  8. var textures_url = "https://textures.minecraft.net/texture/";
  9. // count requests made to session_url in the last 1000ms
  10. var session_requests = [];
  11. var exp = {};
  12. function req_count() {
  13. var index = session_requests.findIndex((i) => i >= Date.now() - 1000);
  14. if (index >= 0) {
  15. return session_requests.length - index;
  16. } else {
  17. return 0;
  18. }
  19. }
  20. exp.resetCounter = function() {
  21. var count = req_count();
  22. if (count) {
  23. var logfunc = count >= config.server.sessions_rate_limit ? logging.warn : logging.debug;
  24. logfunc('Clearing old session requests (count was ' + count + ')');
  25. session_requests.splice(0, session_requests.length - count);
  26. } else {
  27. session_requests = []
  28. }
  29. }
  30. // performs a GET request to the +url+
  31. // +options+ object includes these options:
  32. // encoding (string), default is to return a buffer
  33. // callback: the body, response,
  34. // and error buffer. get_from helper method is available
  35. exp.get_from_options = function(rid, url, options, callback) {
  36. var session_req = url.startsWith(session_url);
  37. // This is to prevent being blocked by CloudFront for exceeding the rate limit
  38. if (session_req && req_count() >= config.server.sessions_rate_limit) {
  39. var e = new Error("Skipped, rate limit exceeded");
  40. e.name = "HTTP";
  41. e.code = "RATELIMIT";
  42. var response = new http.IncomingMessage();
  43. response.statusCode = 403;
  44. callback(null, response, e);
  45. } else {
  46. session_req && session_requests.push(Date.now());
  47. request.get({
  48. url: url,
  49. headers: {
  50. "User-Agent": "Crafatar (+https://crafatar.com)"
  51. },
  52. timeout: config.server.http_timeout,
  53. followRedirect: false,
  54. encoding: options.encoding || null,
  55. }, function(error, response, body) {
  56. // log url + code + description
  57. var code = response && response.statusCode;
  58. var logfunc = code && (code < 400 || code === 404) ? logging.debug : logging.warn;
  59. logfunc(rid, url, code || error && error.code, http.STATUS_CODES[code]);
  60. // not necessarily used
  61. var e = new Error(code);
  62. e.name = "HTTP";
  63. e.code = "HTTPERROR";
  64. switch (code) {
  65. case 200:
  66. case 301:
  67. case 302: // never seen, but mojang might use it in future
  68. case 307: // never seen, but mojang might use it in future
  69. case 308: // never seen, but mojang might use it in future
  70. // these are okay
  71. break;
  72. case 204: // no content, used like 404 by mojang. making sure it really has no content
  73. case 404:
  74. // can be cached as null
  75. body = null;
  76. break;
  77. case 403: // Blocked by CloudFront :(
  78. case 429: // this shouldn't usually happen, but occasionally does
  79. case 500:
  80. case 502: // CloudFront can't reach mojang origin
  81. case 503:
  82. case 504:
  83. // we don't want to cache this
  84. error = error || e;
  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. error = error || e;
  93. body = null;
  94. break;
  95. }
  96. if (body && !body.length) {
  97. // empty response
  98. body = null;
  99. }
  100. callback(body, response, error);
  101. });
  102. }
  103. };
  104. // helper method for get_from_options, no options required
  105. exp.get_from = function(rid, url, callback) {
  106. exp.get_from_options(rid, url, {}, function(body, response, err) {
  107. callback(body, response, err);
  108. });
  109. };
  110. // gets the URL for a skin/cape from the profile
  111. // +type+ "SKIN" or "CAPE", specifies which to retrieve
  112. // callback: url, slim
  113. exp.get_uuid_info = function(profile, type, callback) {
  114. var properties = Object.get(profile, "properties") || [];
  115. properties.forEach(function(prop) {
  116. if (prop.name === "textures") {
  117. var json = new Buffer.from(prop.value, "base64").toString();
  118. profile = JSON.parse(json);
  119. }
  120. });
  121. var url = Object.get(profile, "textures." + type + ".url");
  122. var slim;
  123. if (type === "SKIN") {
  124. slim = Object.get(profile, "textures.SKIN.metadata.model") === "slim";
  125. }
  126. callback(null, url || null, !!slim);
  127. };
  128. // make a request to sessionserver for +uuid+
  129. // callback: error, profile
  130. exp.get_profile = function(rid, uuid, callback) {
  131. exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
  132. try {
  133. body = body ? JSON.parse(body) : null;
  134. callback(err || null, body);
  135. } catch(e) {
  136. if (e instanceof SyntaxError) {
  137. logging.warn(rid, "Failed to parse JSON", e);
  138. logging.debug(rid, body);
  139. callback(err || null, null);
  140. } else {
  141. throw e;
  142. }
  143. }
  144. });
  145. };
  146. // get the skin URL and type for +userId+
  147. // +profile+ is used if +userId+ is a uuid
  148. // callback: error, url, slim
  149. exp.get_skin_info = function(rid, userId, profile, callback) {
  150. exp.get_uuid_info(profile, "SKIN", callback);
  151. };
  152. // get the cape URL for +userId+
  153. // +profile+ is used if +userId+ is a uuid
  154. exp.get_cape_url = function(rid, userId, profile, callback) {
  155. exp.get_uuid_info(profile, "CAPE", callback);
  156. };
  157. // download the +tex_hash+ image from the texture server
  158. // and save it in the +outpath+ file
  159. // callback: error, response, image buffer
  160. exp.save_texture = function(rid, tex_hash, outpath, callback) {
  161. if (tex_hash) {
  162. var textureurl = textures_url + tex_hash;
  163. exp.get_from(rid, textureurl, function(img, response, err) {
  164. if (err) {
  165. callback(err, response, null);
  166. } else {
  167. skins.save_image(img, outpath, function(img_err) {
  168. callback(img_err, response, img);
  169. });
  170. }
  171. });
  172. } else {
  173. callback(null, null, null);
  174. }
  175. };
  176. module.exports = exp;