From 4b02000c3d1b2b9b79d08063d4d679180329e6c0 Mon Sep 17 00:00:00 2001 From: shaoying Date: Fri, 8 Nov 2019 04:00:19 +0800 Subject: [PATCH] opt auto.sh --- etc/conf/auto.sh | 55 +++++++++++++++++++++++++++------------ src/contexts/cli/cli.go | 3 --- src/contexts/web/web.go | 3 ++- src/examples/code/code.go | 3 ++- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/etc/conf/auto.sh b/etc/conf/auto.sh index e0f438d6..073a44c0 100644 --- a/etc/conf/auto.sh +++ b/etc/conf/auto.sh @@ -1,6 +1,6 @@ #!/bin/sh -if [ "$ctx_dev" = "" ] || [ "$ctx_dev" = "-" ]; then +if [ "${ctx_dev}" = "" ] || [ "${ctx_dev}" = "-" ]; then ctx_dev="http://localhost:9095" fi @@ -11,26 +11,34 @@ ctx_sid=${ctx_sid:=""} ctx_welcome=${ctx_welcome:="^_^ Welcome to Context world ^_^"} ctx_goodbye=${ctx_goodbye:="^_^ Welcome to Context world ^_^"} +ShyLine() { + echo "$*"|sed -e 's/\"/\\\"/g' -e 's/\n/\\n/g' +} ShyJSON() { + [ $# -eq 1 ] && echo \"`ShyLine "$1"`\" && return echo -n "{" - [ -n "$1" ] && echo -n \"$1\"\:\"$2\" && shift 2 && while [ -n "$1" ]; do - echo -n \, && echo -n \"$1\"\:\"$2\" && shift 2 + while [ $# -gt 1 ]; do + echo -n \"`ShyLine "$1"`\"\:\"`ShyLine "$2"`\" + shift 2 && [ $# -gt 1 ] && echo -n "," done echo -n "}" } ShyPost() { - ShyJSON "$@" pwd "$(pwd)" sid "${ctx_sid}"| xargs -d'\n' -n1 curl -s "${ctx_url}" -H "${ctx_head}" -d 2>/dev/null + local data=`ShyJSON "$@" SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"` + curl -s "${ctx_url}" -H "${ctx_head}" -d "${data}" } -ShySync() { - case "$1" in - "history") tail -n0 -f $HISTFILE | while true; do read line - ShyPost arg "$line" cmd history SHELL $SHELL - echo $line - done;; - "input") - curl -s "${ctx_url}?cmd=input&arg=$READLINE_LINE" &>/dev/null - ;; - esac +ShyWord() { + echo "$*"|sed -e 's/\ /%20/g' -e 's/\n/\\n/g' +} +ShyForm() { + while [ $# -gt 1 ]; do + echo -n "`ShyWord "$1"`=`ShyWord "$2"`" + shift 2 && [ $# -gt 1 ] && echo -n "&" + done +} +ShyGet() { + local data=`ShyForm "$@" SHELL "${SHELL}" pwd "${PWD}" sid "${ctx_sid}"` + curl -s "${ctx_url}?${data}" } Shy() { local ctx_res=`ShyPost cmd "$1" arg "$2"` @@ -40,6 +48,18 @@ Shy() { esac } +ShySync() { + case "$1" in + "history") tail -n0 -f $HISTFILE | while true; do read line + line=`ShyLine $line` + Shy history "$line" + echo $line + done;; + "input") + ShyGet arg "$READLINE_LINE" cmd "input" SHELL "$SHELL" + ;; + esac +} ShyHistory() { case "$SHELL" in @@ -52,12 +72,13 @@ ShyHistory() { } ShyLogout() { echo ${ctx_goodbye} - ShyPost cmd logout + Shy logout } ShyLogin() { - ctx_sid=`ShyPost cmd login pid "$$" pane "${TMUX_PANE}" hostname "$(hostname)" username "${USER}"` + HOST=`hostname` ctx_sid=`ShyPost cmd login pid "$$" pane "${TMUX_PANE}" hostname "${HOST}" username "${USER}"` echo ${ctx_welcome} - echo "sid: ${ctx_sid}" + echo "url: ${ctx_url}" + echo "sid: ${ctx_sid:0:6}" echo "pid: $$" } diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index f2ff59ef..e7ebaf4d 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -471,9 +471,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", m.Echo(err.String()) return } - if cmd.ProcessState.ExitCode() != 0 { - return - } // 解析结果 if format(m, out); m.Has("cmd_select") { diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 344bd099..227a022c 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -155,7 +155,7 @@ func (web *WEB) Login(msg *ctx.Message, w http.ResponseWriter, r *http.Request) } // 本地用户 - if !msg.Options("username") && kit.IsLocalIP(msg.Option("remote_ip")) && msg.Confs("web.login", "local") { + if !msg.Options("username") && kit.IsLocalIP(msg.Option("remote_ip")) && msg.Confs("web.login", "local") && !strings.HasPrefix(msg.Option("agent"), "curl") { msg.Cmd("aaa.role", "root", "user", msg.Cmdx("ssh.work", "create")) msg.Log("info", "%s: %s", msg.Option("remote_ip"), msg.Option("username", msg.Conf("runtime", "work.name"))) Cookie(msg, w, r) @@ -169,6 +169,7 @@ func (web *WEB) HandleCmd(m *ctx.Message, key string, cmd *ctx.Command) { defer func() { msg.Log("time", "serve: %v", msg.Format("cost")) }() + msg.Option("agent", r.Header.Get("User-Agent")) msg.Option("remote_addr", r.RemoteAddr) msg.Option("remote_ip", r.Header.Get("remote_ip")) msg.Option("index_url", r.Header.Get("index_url")) diff --git a/src/examples/code/code.go b/src/examples/code/code.go index 5c47e786..28a7c630 100644 --- a/src/examples/code/code.go +++ b/src/examples/code/code.go @@ -46,7 +46,8 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", "shy": Dockfile, }}, "git": {Name: "git", Help: "记录", Value: map[string]interface{}{ - "alias": map[string]interface{}{"s": "status", "b": "branch"}, + "alias": map[string]interface{}{"s": "status", "b": "branch"}, + "config": []interface{}{map[string]interface{}{"conf": "merge.tool", "value": "vimdiff"}}, }}, "vim": {Name: "vim", Help: "记录", Value: map[string]interface{}{ "opens": map[string]interface{}{},