Commit 6470ebfb authored by nanahira's avatar nanahira

finish

parent a85f2342
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
.dockerignore
.git*
Dockerfile
/certs
*.png
......@@ -59,3 +59,6 @@ typings/
# next.js build output
.next
/certs
*.png
FROM 12.12.0-buster-slim
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN npm ci
CMD node index.js 443
"use strict";
const https = require("https");
const fs = require("fs");
const _request = require("request");
const url = require("url");
const https_options = {
cert: fs.readFileSync("./certs/fullchain.pem"),
key: fs.readFileSync("./certs/privkey.pem")
};
const https_server = https.createServer(https_options, (request, response) => {
const u = url.parse(request.url, false);
const path_match = u.pathname.match(/^\/(.+)\.png$/);
if (!path_match) {
response.writeHead(403);
response.end("Invalid pathname.");
return;
}
const username = path_match[1];
_request({
url: "https://ygobbs.com/users/" + username + ".json",
json: true
}, (error, res, body) => {
if (error) {
response.writeHead(500);
response.end("Username error.");
console.error("USERNAME ERROR", error)
} else if (typeof (body) === "string") {
response.writeHead(500);
response.end("Bad username JSON.");
console.log("BAD USERNAME JSON", body);
} else if (!body.users) {
response.writeHead(404);
response.end("Not found.");
} else {
const real_username = encodeURIComponent(body.users[0].username);
_request({
url: "https://api.moecube.com/accounts/users/" + real_username + ".avatar"
}, (error, res, body) => {
if (error) {
response.writeHead(500);
response.end("Request error.");
console.error("REQUEST ERROR", error);
} else if (body == "{\"message\":\"Not Found\"}") {
response.writeHead(404);
response.end("Not found.");
} else {
//console.log(body);
_request({
url: body,
encoding: null
}, (error, res, body) => {
if (error) {
response.writeHead(500);
response.end("Avatar error.");
console.error("AVATAR ERROR", error);
} else {
var recv_buf = Buffer.from(body, 'binary');
response.writeHead(200, { "Content-Type": "image/png" });
fs.writeFileSync("./test.png", recv_buf);
response.end(recv_buf);
}
});
}
});
}
});
});
https_server.listen(parseInt(process.argv[2]));
This diff is collapsed.
{
"name": "avatar-helper",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mycard/avatar-helper.git"
},
"author": "Nanahira",
"license": "AGPL-3.0",
"bugs": {
"url": "https://github.com/mycard/avatar-helper/issues"
},
"homepage": "https://github.com/mycard/avatar-helper#readme",
"dependencies": {
"request": "^2.88.0"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment