Skip to content

Commit

Permalink
Merge pull request #157 from jiro4989/feature/#153-upgrade
Browse files Browse the repository at this point in the history
X-Forwarded-forをログに残す #153 #154
  • Loading branch information
jiro4989 authored Apr 7, 2020
2 parents 51492ac + 0f17ac4 commit 09e8fa1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions proxy/conf.d/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ server {
}

add_header Access-Control-Allow-Origin '*' always;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.1.1:5000/;
Expand Down
5 changes: 3 additions & 2 deletions websh_server/src/websh_remover.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import os, json, times
from strformat import `&`

let
tmpDir = getCurrentDir() / "tmp"

when isMainModule and not defined modeTest:
echo %*{"time": $now(), "level": "info", "msg": "remover begin", "nimVersion": NimVersion}
while true:
# containerDir = tmp/containerName
for containerDir in walkDirs(tmpDir/"*"):
Expand All @@ -18,7 +19,7 @@ when isMainModule and not defined modeTest:
# lockだけは一番最後に削除する必要がある
for dir in walkDirs(containerDir/"*"):
let (_, base, _) = splitFile(dir)
debugEcho "base = " & base
echo %*{"time": $now(), "level": "info", "msg": "remove " & base}
if base == "lock": continue
removeDir(dir)
if base in ["images", "media", "script"]:
Expand Down
22 changes: 13 additions & 9 deletions websh_server/src/websh_server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ proc createMediaFiles(dir: string, medias: seq[string]) =

router myrouter:
post "/shellgei":
let now = now()
let uuid = $genUUID()
const
xForHeader = "X-Forwarded-for"
let
now = now()
uuid = $genUUID()
xFor = request.headers().getOrDefault(xForHeader)
try:
var respJson = request.body().parseJson().to(ReqShellgeiJSON)

# 一連の処理開始のログ
echo %*{"time": $now(), "level": "info", "uuid": uuid, "code": respJson.code, "msg": "begin"}
echo %*{xForHeader: xFor, "time": $now(), "level": "info", "uuid": uuid, "code": respJson.code, "msg": "request begin"}

let
containersCount = getEnv("WEBSH_CONTAINERS_COUNT", "4").parseInt()
Expand Down Expand Up @@ -154,27 +158,27 @@ router myrouter:
case status
of statusOk: discard
of statusTimeout:
echo %*{"time": $now(), "level": "info", "uuid": uuid, "code": systemMsg}
echo %*{xForHeader: xFor, "time": $now(), "level": "info", "uuid": uuid, "code": systemMsg}
else:
echo %*{"time": $now(), "level": "error", "uuid": uuid, "code": systemMsg}
echo %*{xForHeader: xFor, "time": $now(), "level": "error", "uuid": uuid, "code": systemMsg}

let images = getImages(imageDir)

let elapsedTime = $(now() - now).inMilliseconds & "milsec"
echo %*{"time": $now(), "level": "info", "uuid": uuid, "elapsedTime": elapsedTime, "msg": "end"}
let elapsedTime = (now() - now).inMilliseconds
echo %*{xForHeader: xFor, "time": $now(), "level": "info", "uuid": uuid, "elapsedTime": elapsedTime, "msg": "request end"}

resp %*{
"status": status,
"system_message": systemMsg,
"stdout": stdoutStr,
"stderr": stderrStr,
"images": images,
"elapsed_time": elapsedTime,
"elapsed_time": $elapsedTime & "milsec",
}
except:
let msg = getCurrentExceptionMsg()
let elapsedTime = $(now() - now).inMilliseconds & "milsec"
echo %*{"time": $now(), "level": "error", "uuid": uuid, "elapsedTime": elapsedTime, "msg": msg}
echo %*{xForHeader: xFor, "time": $now(), "level": "error", "uuid": uuid, "elapsedTime": elapsedTime, "msg": msg}

resp %*{
"status": statusSystemError,
Expand Down

0 comments on commit 09e8fa1

Please sign in to comment.