go rulez
This commit is contained in:
parent
96fa1cff84
commit
50c9aa699d
20
.github/workflows/jshint.yml
vendored
20
.github/workflows/jshint.yml
vendored
@ -1,20 +0,0 @@
|
|||||||
name: jshint
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v1
|
|
||||||
|
|
||||||
- name: apt
|
|
||||||
run: sudo apt-get install -y nodejs npm
|
|
||||||
|
|
||||||
- name: npm install
|
|
||||||
run: npm i
|
|
||||||
|
|
||||||
- name: npm test
|
|
||||||
run: npm test
|
|
31
Dockerfile
31
Dockerfile
@ -1,25 +1,12 @@
|
|||||||
# Stage 1 testing
|
FROM golang:1.16.3 as stage1
|
||||||
FROM node:15.2.1-alpine
|
COPY . /data
|
||||||
|
RUN cd /data && \
|
||||||
COPY .jshintrc /data/
|
go vet && \
|
||||||
COPY package.json /data/
|
go test ./... && \
|
||||||
COPY package-lock.json /data/
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build .
|
||||||
COPY src /data/src
|
|
||||||
|
|
||||||
RUN cd /data && npm i && npm test
|
|
||||||
|
|
||||||
# Stage 2 package
|
|
||||||
FROM node:15.2.1-alpine
|
|
||||||
|
|
||||||
COPY .jshintrc /data/
|
|
||||||
COPY package.json /data/
|
|
||||||
COPY package-lock.json /data/
|
|
||||||
COPY src /data/src
|
|
||||||
|
|
||||||
RUN cd /data && npm i --only=production
|
|
||||||
|
|
||||||
WORKDIR /data
|
|
||||||
|
|
||||||
|
FROM alpine:3.13.4
|
||||||
|
COPY --from=stage1 /data/auth_proxy /
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
CMD ["node", "src/index.js"]
|
CMD ["/auth_proxy"]
|
||||||
|
BIN
auth_proxy
Executable file
BIN
auth_proxy
Executable file
Binary file not shown.
92
main.go
Normal file
92
main.go
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var logger = log.Default()
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/api/minetest/channel", MinetestEndpoint)
|
||||||
|
mux.HandleFunc("/api/login", LoginEndpoint)
|
||||||
|
|
||||||
|
logger.Printf("Listening on port %d", 8080)
|
||||||
|
err := http.ListenAndServe(":8080", mux)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var toMt = make(chan json.RawMessage)
|
||||||
|
var fromMt = make(chan json.RawMessage)
|
||||||
|
|
||||||
|
func SendError(w http.ResponseWriter, msg string) {
|
||||||
|
logger.Printf("Error: %s", msg)
|
||||||
|
w.WriteHeader(500)
|
||||||
|
w.Write([]byte(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
func MinetestEndpoint(w http.ResponseWriter, req *http.Request) {
|
||||||
|
logger.Printf("Got mod-request from %s, method: %s", req.Host, req.Method)
|
||||||
|
|
||||||
|
if req.Method == http.MethodGet {
|
||||||
|
select {
|
||||||
|
case msg := <-toMt:
|
||||||
|
logger.Printf("Relaying message to mod")
|
||||||
|
err := json.NewEncoder(w).Encode(msg)
|
||||||
|
if err != nil {
|
||||||
|
SendError(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case <-time.After(30 * time.Second):
|
||||||
|
// timed out without data
|
||||||
|
w.WriteHeader(204)
|
||||||
|
}
|
||||||
|
} else if req.Method == http.MethodPost {
|
||||||
|
msg := json.RawMessage{}
|
||||||
|
err := json.NewDecoder(req.Body).Decode(&msg)
|
||||||
|
if err != nil {
|
||||||
|
SendError(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Printf("Received message from mod: %s", string(msg))
|
||||||
|
select {
|
||||||
|
case fromMt <- msg:
|
||||||
|
default:
|
||||||
|
SendError(w, "no receiver available")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoginEndpoint(w http.ResponseWriter, req *http.Request) {
|
||||||
|
logger.Printf("Got request from %s", req.Host)
|
||||||
|
|
||||||
|
msg := json.RawMessage{}
|
||||||
|
err := json.NewDecoder(req.Body).Decode(&msg)
|
||||||
|
if err != nil {
|
||||||
|
SendError(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case toMt <- msg:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
SendError(w, "timed out sending")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case msg = <-fromMt:
|
||||||
|
w.WriteHeader(200)
|
||||||
|
err = json.NewEncoder(w).Encode(msg)
|
||||||
|
if err != nil {
|
||||||
|
SendError(w, err.Error())
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
SendError(w, "timed out receiving")
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@
|
|||||||
Authorization proxy app for minetest
|
Authorization proxy app for minetest
|
||||||
=================
|
=================
|
||||||
|
|
||||||

|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
@ -53,4 +52,4 @@ Or just:
|
|||||||
|
|
||||||
A `Dockerfile` is included for container usage.
|
A `Dockerfile` is included for container usage.
|
||||||
|
|
||||||
Otherwise just `npm install && npm start`
|
Otherwise just `go run .` (needs a working go installation)
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
|
|
||||||
const app = require("../app");
|
|
||||||
const events = require("../events");
|
|
||||||
|
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
const jsonParser = bodyParser.json();
|
|
||||||
|
|
||||||
const debug = false;
|
|
||||||
|
|
||||||
var tx_queue = [];
|
|
||||||
|
|
||||||
events.on("channel-send", function(data){
|
|
||||||
tx_queue.push(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
// web -> mod
|
|
||||||
app.get('/api/minetest/channel', function(req, res){
|
|
||||||
function trySend(){
|
|
||||||
if (tx_queue.length > 0){
|
|
||||||
var obj = tx_queue.shift();
|
|
||||||
if (debug)
|
|
||||||
console.log("[tx]", obj);
|
|
||||||
res.json(obj);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const start = Date.now();
|
|
||||||
|
|
||||||
function loop(){
|
|
||||||
if (trySend())
|
|
||||||
return;
|
|
||||||
|
|
||||||
if ((Date.now() - start) < 10000){
|
|
||||||
setTimeout(loop, 200);
|
|
||||||
} else {
|
|
||||||
res.json(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loop();
|
|
||||||
});
|
|
||||||
|
|
||||||
// mod -> web
|
|
||||||
app.post('/api/minetest/channel', jsonParser, function(req, res){
|
|
||||||
if (debug)
|
|
||||||
console.log("[rx]", req.body);
|
|
||||||
|
|
||||||
events.emit("channel-recv", req.body);
|
|
||||||
|
|
||||||
res.end();
|
|
||||||
});
|
|
@ -1,29 +0,0 @@
|
|||||||
|
|
||||||
const app = require("../app");
|
|
||||||
|
|
||||||
|
|
||||||
const doLogin = require("../promise/login");
|
|
||||||
|
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
const jsonParser = bodyParser.json();
|
|
||||||
|
|
||||||
app.post('/api/login', jsonParser, function(req, res){
|
|
||||||
|
|
||||||
if (!req.body.username || !req.body.password){
|
|
||||||
res.status(500).end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
doLogin(req.body.username, req.body.password)
|
|
||||||
.then(result => {
|
|
||||||
res.json({
|
|
||||||
success: result.success,
|
|
||||||
message: result.message,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
res.status(500).end();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
@ -1,7 +0,0 @@
|
|||||||
const express = require('express');
|
|
||||||
|
|
||||||
const app = express();
|
|
||||||
app.use(express.static('public'));
|
|
||||||
app.disable('etag');
|
|
||||||
|
|
||||||
module.exports = app;
|
|
@ -1,5 +0,0 @@
|
|||||||
|
|
||||||
const EventEmitter = require('events');
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = new EventEmitter();
|
|
@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
const app = require("./app");
|
|
||||||
require("./api/channel");
|
|
||||||
require("./api/login");
|
|
||||||
|
|
||||||
app.listen(8080, () => console.log('Listening on http://127.0.0.1:8080'));
|
|
@ -1,29 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
const events = require("../events");
|
|
||||||
|
|
||||||
module.exports = (username, password) => new Promise(function(resolve, reject){
|
|
||||||
|
|
||||||
events.emit("channel-send", {
|
|
||||||
type: "auth",
|
|
||||||
data: {
|
|
||||||
name: username,
|
|
||||||
password: password
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function handleEvent(result){
|
|
||||||
if (result.type == "auth" && result.data && result.data.name == username){
|
|
||||||
events.removeListener("channel-recv", handleEvent);
|
|
||||||
clearTimeout(handle);
|
|
||||||
resolve(result.data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
events.on("channel-recv", handleEvent);
|
|
||||||
|
|
||||||
var handle = setTimeout(function(){
|
|
||||||
events.removeListener("channel-recv", handleEvent);
|
|
||||||
reject("mod-comm timeout");
|
|
||||||
}, 1000);
|
|
||||||
});
|
|
Loading…
x
Reference in New Issue
Block a user