From 31e3418e261851e0b5df47a4280b34b735bfd26a Mon Sep 17 00:00:00 2001 From: Nishi Date: Tue, 1 Oct 2024 15:42:33 +0000 Subject: [PATCH] stuff git-svn-id: file:///raid/svn-personal/koakuma/trunk@4 219d0f9c-2d94-d447-890a-813e76b88fe9 --- Makefile | 5 +-- Tool/create-project.in | 34 ++++++++++++++++++ Tool/launch-job | 2 -- Tool/{create-project => launch-job.in} | 0 Utility/rpc.tcl | 48 ++++++++++++++++++++++++++ apache.conf.in | 4 +-- koakuma.cgi.in | 21 +++++++++-- 7 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 Tool/create-project.in delete mode 100644 Tool/launch-job rename Tool/{create-project => launch-job.in} (100%) diff --git a/Makefile b/Makefile index 706ec0a..590a324 100644 --- a/Makefile +++ b/Makefile @@ -16,13 +16,14 @@ install: Component/* Tool/* Utility/* koakuma.cgi.in apache.conf.in if [ ! -e "$(PREFIX)/lib/koakuma/db/projects.db" ] ; then echo "" > $(PREFIX)/lib/koakuma/db/projects.db ; fi cp -rf Component/* $(PREFIX)/lib/koakuma/component/ cp -rf Utility/* $(PREFIX)/lib/koakuma/utility/ - cp -rf Tool/* $(PREFIX)/bin/ + $(REPLACE) Tool/create-project.in > $(PREFIX)/bin/create-project + $(REPLACE) Tool/launch-job.in > $(PREFIX)/bin/launch-job cp -rf koakuma.png $(PREFIX)/lib/koakuma/htdocs/static/ cp style.css $(PREFIX)/lib/koakuma/htdocs/static/ $(REPLACE) koakuma.cgi.in > $(PREFIX)/lib/koakuma/cgi-bin/koakuma.cgi $(REPLACE) apache.conf.in > $(PREFIX)/etc/koakuma/apache.conf chmod +x $(PREFIX)/lib/koakuma/cgi-bin/koakuma.cgi - chmod +x $(PREFIX)/bin/create-task + chmod +x $(PREFIX)/bin/create-project chmod +x $(PREFIX)/bin/launch-job @echo @echo Make sure $(PREFIX)/lib/koakuma/db is writable by your HTTPd user. diff --git a/Tool/create-project.in b/Tool/create-project.in new file mode 100644 index 0000000..76aae3e --- /dev/null +++ b/Tool/create-project.in @@ -0,0 +1,34 @@ +#!/usr/bin/env tclsh +# $Id$ +set RPC_URL "http://127.0.0.1/koakuma/rpc" +foreach arg $argv { + if { [string range "$arg" 0 0] == "-" } { + if { "$arg" == "-h" || "$arg" == "--help" } { + puts "Usage: create-project" + puts "You can set the environment variable `KOAKUMA_RPC' to override default RPC URL." + puts "Default: $RPC_URL" + exit 0 + } + } +} +foreach path [glob "@@PREFIX@@/lib/koakuma/utility/*.tcl"] { + source $path +} +if { [info exists "env(KOAKUMA_RPC)"] } { + set RPC_URL "$env(KOAKUMA_RPC)" +} +puts -nonewline "Authentication: " +set status [::rpc::require-auth] +if { $status == 1 } { + puts "Required" + if { ![::rpc::ask-auth] } { + puts "Authentication failure" + exit 1 + } +} elseif { $status < 0 } { + puts "Got forbidden, cannot continue" + exit 1 +} else { + puts "Not required" +} +::rpc::send diff --git a/Tool/launch-job b/Tool/launch-job deleted file mode 100644 index 614af38..0000000 --- a/Tool/launch-job +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env tclsh -# $Id$ diff --git a/Tool/create-project b/Tool/launch-job.in similarity index 100% rename from Tool/create-project rename to Tool/launch-job.in diff --git a/Utility/rpc.tcl b/Utility/rpc.tcl index fbbab2d..609782f 100644 --- a/Utility/rpc.tcl +++ b/Utility/rpc.tcl @@ -1 +1,49 @@ # $Id$ +package require http +package require base64 +package require term::ansi::ctrl::unix + +namespace eval rpc { + proc require-auth {} { + global RPC_URL + set tok [::http::geturl "$RPC_URL"] + set code [::http::ncode $tok] + ::http::cleanup $tok + if { $code == 401 } { + return 1 + } elseif { $code == 403 } { + return -1 + } else { + return 0 + } + } + set username "" + set password "" + proc ask-auth {} { + upvar 1 username username + upvar 1 password password + puts -nonewline "Username: " + flush stdout + set username "[gets stdin]" + puts -nonewline "Password: " + flush stdout + exec stty -echo + set password "[gets stdin]" + exec stty echo + puts "" + + set headers "" + lappend headers "Authorization" + lappend headers "Basic [::base64::encode -wrapchar "" "$username:$password"]" + + global RPC_URL + set tok [::http::geturl "$RPC_URL" -headers $headers] + set code [::http::ncode $tok] + ::http::cleanup $tok + + if { $code == 200 } { + return 1 + } + return 0 + } +} diff --git a/apache.conf.in b/apache.conf.in index 817411c..76276c2 100644 --- a/apache.conf.in +++ b/apache.conf.in @@ -14,9 +14,7 @@ DocumentRoot @@PREFIX@@/lib/koakuma/htdocs AuthName "Koakuma RPC" AuthUserFile @@PREFIX@@/etc/koakuma/passwd Require valid-user - - - Require all granted + Require ip 127.0.0.1 ::1 ScriptAlias /koakuma @@PREFIX@@/lib/koakuma/cgi-bin/koakuma.cgi diff --git a/koakuma.cgi.in b/koakuma.cgi.in index 74251e2..9a7aeb6 100644 --- a/koakuma.cgi.in +++ b/koakuma.cgi.in @@ -52,6 +52,9 @@ if { ![info exists env(PATH_INFO)] } { if { [catch { package require tdom + dom createNodeCmd -tagName "rpc" elementNode rootXML + dom createNodeCmd -tagName "version" -jsonType NONE elementNode keyVersion + dom createNodeCmd -jsonType STRING textNode valueString }] } { crash "Failed to load tDOM" } @@ -205,7 +208,11 @@ proc add_toc2 {data} { if { [catch { set path "[regsub -all {/+} "$env(PATH_INFO)" "/"]" - rputs "Content-Type: text/html" + if { [regexp {^/rpc(/.*)?$} "$path"] } { + rputs "Content-Type: application/json" + } else { + rputs "Content-Type: text/html" + } if { "$path" == "/" } { add_toc "Tcl Information" tputs "" @@ -257,7 +264,17 @@ if { [catch { start_html "Main" 1 rputs "$content" end_html 1 - } elseif { [regexp {^/rpc.*$} "$path"] } { + } elseif { [regexp {^/rpc(/.*)?$} "$path"] } { + rputs "" + regexp {^/rpc(/.*)?$} "$path" -> api + set doc [dom createDocumentNode] + set root $doc + if { "$api" == "" || "$api" == "/" } { + $root appendFromScript { + keyVersion {valueString "$KOAKUMA_VERSION"} + } + rputs "[$doc asJSON]" + } } elseif { [regexp {^/project/[^/]+.*$} "$path"] } { regexp {^/project/([^/]+).*$} "$path" -> projname open_projects -- 2.45.2