1
0
forked from x/ContextOS

vpn pro context 修改了一些小东西

This commit is contained in:
shaoying 2017-11-08 17:31:11 +08:00
parent 8fe0d03eda
commit 9cdea710d9
4 changed files with 80 additions and 17 deletions

View File

@ -77,8 +77,12 @@ func (cli *CLI) parse() bool { // {{{
} }
if len(line) == 1 { if len(line) == 1 {
if len(cli.ins) == 1 {
line = cli.history[len(cli.history)-1]["cli"]
} else {
return true return true
} }
}
} else { } else {
line = cli.next line = cli.next
cli.next = "" cli.next = ""
@ -89,6 +93,7 @@ func (cli *CLI) parse() bool { // {{{
} }
} }
back:
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if line[0] == '#' { if line[0] == '#' {
return true return true
@ -118,6 +123,7 @@ func (cli *CLI) parse() bool { // {{{
for i := 0; i < len(ls); i++ { for i := 0; i < len(ls); i++ {
ls[i] = strings.TrimSpace(ls[i]) ls[i] = strings.TrimSpace(ls[i])
if ls[i][0] == '#' { if ls[i][0] == '#' {
break break
} }
@ -126,6 +132,13 @@ func (cli *CLI) parse() bool { // {{{
} }
} }
ls = msg.Meta["detail"]
if n, e := strconv.Atoi(ls[0]); e == nil && 0 <= n && n < len(cli.history) && ls[0] != cli.history[n]["cli"] {
line = cli.history[n]["cli"]
msg.Meta["detail"] = nil
goto back
}
msg.Post(cli.Context) msg.Post(cli.Context)
for _, v := range msg.Meta["result"] { for _, v := range msg.Meta["result"] {
@ -426,6 +439,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
cli.alias[arg[0]] = arg[1] cli.alias[arg[0]] = arg[1]
m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]]) m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]])
} }
default:
cli.alias[arg[0]] = strings.Join(arg[1:], " ")
m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]])
} }
return "" return ""
// }}} // }}}
@ -441,7 +457,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
n, e := strconv.Atoi(arg[0]) n, e := strconv.Atoi(arg[0])
if e == nil && 0 <= n && n < len(cli.history) { if e == nil && 0 <= n && n < len(cli.history) {
log.Println("shy log why:", cli.history[n]["cli"]) log.Println("shy log why:", cli.history[n]["cli"])
return cli.history[n]["cli"] cli.next = cli.history[n]["cli"]
}
default:
n, e := strconv.Atoi(arg[0])
if e == nil && 0 <= n && n < len(cli.history) {
cli.history[n]["cli"] = strings.Join(arg[1:], " ")
} }
} }
return "" return ""
@ -482,6 +503,8 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
} }
case 2: case 2:
switch arg[0] { switch arg[0] {
case "void":
m.Target.Conf(arg[1], "")
case "delete": case "delete":
if _, ok := m.Target.Configs[arg[1]]; ok { if _, ok := m.Target.Configs[arg[1]]; ok {
delete(m.Target.Configs, arg[1]) delete(m.Target.Configs, arg[1])
@ -569,7 +592,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
Messages: make(chan *ctx.Message, 10), Messages: make(chan *ctx.Message, 10),
} }
func init() { // {{{ func init() {
cli := &CLI{alias: map[string]string{ cli := &CLI{alias: map[string]string{
"~": "context", "~": "context",
"!": "history", "!": "history",
@ -581,5 +604,3 @@ func init() { // {{{
cli.Context = Index cli.Context = Index
ctx.Index.Register(Index, cli) ctx.Index.Register(Index, cli)
} }
// }}}

View File

@ -354,7 +354,11 @@ func (c *Context) Start(m *Message) bool { // {{{
func (c *Context) Spawn(m *Message, arg ...string) *Context { // {{{ func (c *Context) Spawn(m *Message, arg ...string) *Context { // {{{
s := &Context{Name: arg[0], Help: c.Help} s := &Context{Name: arg[0], Help: c.Help}
m.Target = s m.Target = s
if c.Server != nil {
c.Register(s, c.Server.Spawn(s, m, arg...)).Begin(m) c.Register(s, c.Server.Spawn(s, m, arg...)).Begin(m)
} else {
c.Register(s, nil).Begin(m)
}
return s return s
} }
@ -652,7 +656,7 @@ func (c *Context) Del(arg ...string) { // {{{
// }}} // }}}
func (c *Context) Create(s *Context) *Message { // {{{ func (c *Context) Storm(s *Context) *Message { // {{{
msg := &Message{ msg := &Message{
Time: time.Now(), Time: time.Now(),
@ -716,6 +720,9 @@ func (c *Context) Conf(key string, arg ...string) string { // {{{
if s == c { if s == c {
panic(errors.New(x.Name + "配置项已存在")) panic(errors.New(x.Name + "配置项已存在"))
} }
if c.Configs == nil {
c.Configs = make(map[string]*Config)
}
c.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: x.Hand} c.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: x.Hand}
return arg[1] return arg[1]
default: default:
@ -752,7 +759,7 @@ func (c *Context) Cap(key string, arg ...string) string { // {{{
return x.Value return x.Value
case 1: case 1:
if x.Hand != nil { if x.Hand != nil {
x.Value = x.Hand(c, x, x.Value) x.Value = x.Hand(c, x, arg[0])
} else { } else {
x.Value = arg[0] x.Value = arg[0]
} }
@ -761,6 +768,9 @@ func (c *Context) Cap(key string, arg ...string) string { // {{{
if s == c { if s == c {
panic(errors.New(key + "缓存项已存在")) panic(errors.New(key + "缓存项已存在"))
} }
if c.Caches == nil {
c.Caches = make(map[string]*Cache)
}
c.Caches[key] = &Cache{arg[0], arg[1], arg[2], x.Hand} c.Caches[key] = &Cache{arg[0], arg[1], arg[2], x.Hand}
return arg[1] return arg[1]
default: default:

View File

@ -32,13 +32,28 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
log.Println(web.Name, r.RemoteAddr, r.Method, r.URL) log.Println(web.Name, r.RemoteAddr, r.Method, r.URL)
defer log.Println() defer log.Println()
if web.Conf("logheaders") == "yes" {
for k, v := range r.Header {
log.Println(k+":", v[0])
}
log.Println()
}
r.ParseForm() r.ParseForm()
if len(r.PostForm) > 0 {
for k, v := range r.PostForm { for k, v := range r.PostForm {
log.Printf("%s: %s", k, v[0]) log.Printf("%s: %s", k, v[0])
} }
log.Println() log.Println()
}
web.ServeMux.ServeHTTP(w, r) web.ServeMux.ServeHTTP(w, r)
if web.Conf("logheaders") == "yes" {
for k, v := range w.Header() {
log.Println(k+":", v[0])
}
}
} }
// }}} // }}}
@ -108,6 +123,7 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务",
"status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"}, "status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"},
}, },
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"logheaders": &ctx.Config{Name: "logheaders", Value: "yes", Help: "日志输出请求头"},
"directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"}, "directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"},
"protocol": &ctx.Config{Name: "protocol", Value: "https", Help: "服务协议"}, "protocol": &ctx.Config{Name: "protocol", Value: "https", Help: "服务协议"},
"address": &ctx.Config{Name: "address", Value: ":443", Help: "监听地址"}, "address": &ctx.Config{Name: "address", Value: ":443", Help: "监听地址"},

View File

@ -24,6 +24,22 @@ func FmtSize(size int64) string {
return fmt.Sprintf("%dB", size) return fmt.Sprintf("%dB", size)
} }
func FmtNano(nano int64) string {
if nano > 1000000000 {
return fmt.Sprintf("%d.%ds", nano/1000000000, nano/100000000%100)
}
if nano > 1000000 {
return fmt.Sprintf("%d.%dms", nano/100000, nano/100000%100)
}
if nano > 1000 {
return fmt.Sprintf("%d.%dus", nano/1000, nano/100%100)
}
return fmt.Sprintf("%dns", nano)
}
func Check(e error) bool { func Check(e error) bool {
if e != nil { if e != nil {
panic(e) panic(e)