1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 08:48:06 +08:00

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,7 +77,11 @@ func (cli *CLI) parse() bool { // {{{
}
if len(line) == 1 {
return true
if len(cli.ins) == 1 {
line = cli.history[len(cli.history)-1]["cli"]
} else {
return true
}
}
} else {
line = cli.next
@ -89,6 +93,7 @@ func (cli *CLI) parse() bool { // {{{
}
}
back:
line = strings.TrimSpace(line)
if line[0] == '#' {
return true
@ -118,6 +123,7 @@ func (cli *CLI) parse() bool { // {{{
for i := 0; i < len(ls); i++ {
ls[i] = strings.TrimSpace(ls[i])
if ls[i][0] == '#' {
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)
for _, v := range msg.Meta["result"] {
@ -426,6 +439,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
cli.alias[arg[0]] = arg[1]
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 ""
// }}}
@ -441,7 +457,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
n, e := strconv.Atoi(arg[0])
if e == nil && 0 <= n && n < len(cli.history) {
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 ""
@ -482,6 +503,8 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
}
case 2:
switch arg[0] {
case "void":
m.Target.Conf(arg[1], "")
case "delete":
if _, ok := m.Target.Configs[arg[1]]; ok {
delete(m.Target.Configs, arg[1])
@ -569,7 +592,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
Messages: make(chan *ctx.Message, 10),
}
func init() { // {{{
func init() {
cli := &CLI{alias: map[string]string{
"~": "context",
"!": "history",
@ -581,5 +604,3 @@ func init() { // {{{
cli.Context = Index
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 { // {{{
s := &Context{Name: arg[0], Help: c.Help}
m.Target = s
c.Register(s, c.Server.Spawn(s, m, arg...)).Begin(m)
if c.Server != nil {
c.Register(s, c.Server.Spawn(s, m, arg...)).Begin(m)
} else {
c.Register(s, nil).Begin(m)
}
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{
Time: time.Now(),
@ -716,6 +720,9 @@ func (c *Context) Conf(key string, arg ...string) string { // {{{
if s == c {
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}
return arg[1]
default:
@ -752,7 +759,7 @@ func (c *Context) Cap(key string, arg ...string) string { // {{{
return x.Value
case 1:
if x.Hand != nil {
x.Value = x.Hand(c, x, x.Value)
x.Value = x.Hand(c, x, arg[0])
} else {
x.Value = arg[0]
}
@ -761,6 +768,9 @@ func (c *Context) Cap(key string, arg ...string) string { // {{{
if s == c {
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}
return arg[1]
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)
defer log.Println()
r.ParseForm()
for k, v := range r.PostForm {
log.Printf("%s: %s", k, v[0])
if web.Conf("logheaders") == "yes" {
for k, v := range r.Header {
log.Println(k+":", v[0])
}
log.Println()
}
r.ParseForm()
if len(r.PostForm) > 0 {
for k, v := range r.PostForm {
log.Printf("%s: %s", k, v[0])
}
log.Println()
}
log.Println()
web.ServeMux.ServeHTTP(w, r)
if web.Conf("logheaders") == "yes" {
for k, v := range w.Header() {
log.Println(k+":", v[0])
}
}
}
// }}}
@ -108,11 +123,12 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务",
"status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"},
},
Configs: map[string]*ctx.Config{
"directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"},
"protocol": &ctx.Config{Name: "protocol", Value: "https", Help: "服务协议"},
"address": &ctx.Config{Name: "address", Value: ":443", Help: "监听地址"},
"route": &ctx.Config{Name: "route", Value: "/", Help: "请求路径"},
"default": &ctx.Config{Name: "default", Value: "hello web world", Help: "默认响应体"},
"logheaders": &ctx.Config{Name: "logheaders", Value: "yes", Help: "日志输出请求头"},
"directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"},
"protocol": &ctx.Config{Name: "protocol", Value: "https", Help: "服务协议"},
"address": &ctx.Config{Name: "address", Value: ":443", Help: "监听地址"},
"route": &ctx.Config{Name: "route", Value: "/", Help: "请求路径"},
"default": &ctx.Config{Name: "default", Value: "hello web world", Help: "默认响应体"},
},
Commands: map[string]*ctx.Command{
"listen": &ctx.Command{Name: "listen [route [address protocol [directory]]]", Help: "开启网页服务", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {

View File

@ -24,6 +24,22 @@ func FmtSize(size int64) string {
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 {
if e != nil {
panic(e)