forked from x/ContextOS
mac syn 0.2.0
This commit is contained in:
parent
6cfef174ee
commit
7d5942bd42
@ -2,15 +2,14 @@ package aaa // {{{
|
||||
// }}}
|
||||
import ( // {{{
|
||||
"context"
|
||||
_ "context/cli"
|
||||
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// }}}
|
||||
@ -29,9 +28,7 @@ func (aaa *AAA) session(meta string) string { // {{{
|
||||
// }}}
|
||||
|
||||
func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||
c.Caches = map[string]*ctx.Cache{
|
||||
"sessid": &ctx.Cache{Name: "会话标识", Value: "", Help: "用户的会话标识"},
|
||||
}
|
||||
c.Caches = map[string]*ctx.Cache{}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
|
||||
s := new(AAA)
|
||||
@ -41,21 +38,21 @@ func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
||||
|
||||
// }}}
|
||||
func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
aaa.Caches["group"] = &ctx.Cache{Name: "用户组", Value: m.Conf("rootname"), Help: "用户组"}
|
||||
aaa.Caches["username"] = &ctx.Cache{Name: "用户名", Value: m.Conf("rootname"), Help: "用户名"}
|
||||
aaa.Caches["group"] = &ctx.Cache{Name: "用户组", Value: "", Help: "用户组"}
|
||||
aaa.Caches["username"] = &ctx.Cache{Name: "用户名", Value: "", Help: "用户名"}
|
||||
aaa.Caches["password"] = &ctx.Cache{Name: "密码", Value: "", Help: "用户密码,加密存储", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
||||
if len(arg) > 0 { // {{{
|
||||
if x.Value == "" {
|
||||
bs := md5.Sum([]byte(fmt.Sprintln("用户密码:%s", arg[0])))
|
||||
m.Assert(x.Value == "" || x.Value == hex.EncodeToString(bs[:]), "密码错误")
|
||||
m.Cap("expire", fmt.Sprintf("%d", time.Now().Unix()+int64(m.Confi("expire"))))
|
||||
return hex.EncodeToString(bs[:])
|
||||
} else {
|
||||
bs := md5.Sum([]byte(fmt.Sprintln("用户密码:%s", arg[0])))
|
||||
m.Assert(x.Value == hex.EncodeToString(bs[:]), "密码错误")
|
||||
}
|
||||
}
|
||||
return x.Value
|
||||
// }}}
|
||||
}}
|
||||
|
||||
aaa.Caches["expire"] = &ctx.Cache{Name: "会话超时", Value: "", Help: "用户的会话标识"}
|
||||
aaa.Caches["sessid"] = &ctx.Cache{Name: "会话标识", Value: "", Help: "用户的会话标识"}
|
||||
aaa.Caches["time"] = &ctx.Cache{Name: "登录时间", Value: fmt.Sprintf("%d", time.Now().Unix()), Help: "用户登录时间", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
||||
if len(arg) > 0 { // {{{
|
||||
return x.Value
|
||||
@ -63,34 +60,38 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
|
||||
n, e := strconv.Atoi(x.Value)
|
||||
m.Assert(e)
|
||||
|
||||
return time.Unix(int64(n), 0).Format("15:03:04")
|
||||
// }}}
|
||||
}}
|
||||
|
||||
if len(arg) > 0 {
|
||||
m.Cap("username", arg[0])
|
||||
m.Cap("group", arg[0])
|
||||
}
|
||||
if len(arg) > 1 {
|
||||
m.Cap("group", arg[1])
|
||||
}
|
||||
m.Capi("nuser", 1)
|
||||
|
||||
m.Log("info", "context %s", aaa.Context.Name)
|
||||
aaa.Owner = aaa.Context
|
||||
return aaa
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
if len(arg) > 1 {
|
||||
if m.Cap("sessid") == "" {
|
||||
m.Cap("sessid", aaa.session(arg[1]))
|
||||
m.Capi("nuser", 1)
|
||||
}
|
||||
m.Log("info", "%s(%s): login %s", m.Source.Name, m.Cap("group", arg[0]), m.Cap("username", arg[1]))
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||
m.Master = Index
|
||||
if m.Target == aaa.Context && aaa.Owner == aaa.Context {
|
||||
if m.Cap("username") != m.Conf("rootname") {
|
||||
m.Log("info", "%s(%s): logout %s", aaa.Name, m.Cap("group"), m.Cap("username"))
|
||||
m.Capi("nuser", -1)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@ -102,62 +103,48 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
},
|
||||
Configs: map[string]*ctx.Config{
|
||||
"rootname": &ctx.Config{Name: "根用户名", Value: "root", Help: "根用户名"},
|
||||
"expire": &ctx.Config{Name: "会话超时(s)", Value: "120", Help: "会话超时"},
|
||||
},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||
m.Master = m.Target // {{{
|
||||
aaa := m.Target.Server.(*AAA)
|
||||
m.Target, m.Master = c, c // {{{
|
||||
aaa := c.Server.(*AAA)
|
||||
|
||||
switch len(arg) {
|
||||
case 0:
|
||||
m.Travel(m.Target, func(m *ctx.Message) bool {
|
||||
m.Travel(c, func(m *ctx.Message) bool {
|
||||
m.Echo("%s(%s): %s\n", m.Target.Name, m.Cap("group"), m.Cap("time"))
|
||||
return true
|
||||
})
|
||||
case 1:
|
||||
target := m.Target
|
||||
if s, ok := aaa.sessions[arg[0]]; ok {
|
||||
m.Target = s
|
||||
m.Source.Owner = s
|
||||
m.Log("info", "%s: logon %s", aaa.Name, m.Cap("username"))
|
||||
return m.Cap("username")
|
||||
}
|
||||
m.Target = target
|
||||
case 2:
|
||||
if arg[0] == m.Conf("rootname") {
|
||||
m.Cap("password", arg[1])
|
||||
m.Source.Owner = aaa.Context
|
||||
m.Travel(m.Target.Root, func(m *ctx.Message) bool {
|
||||
if m.Target.Owner == nil {
|
||||
m.Target.Owner = aaa.Context
|
||||
}
|
||||
return true
|
||||
})
|
||||
if m.Target = s; int64(m.Capi("expire")) < time.Now().Unix() {
|
||||
s.Close(m)
|
||||
return ""
|
||||
}
|
||||
|
||||
source := m.Source
|
||||
if msg := m.Find(arg[0]); msg == nil {
|
||||
m.Start(arg[0], arg[0], arg[0])
|
||||
m.Cap("sessid", aaa.session(arg[0]))
|
||||
m.Cap("time", fmt.Sprintf("%d", time.Now().Unix()))
|
||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||
m.Log("info", "%s(%s): logon %s", m.Source.Name, m.Cap("group"), m.Cap("username"))
|
||||
return m.Cap("username")
|
||||
}
|
||||
case 2, 3:
|
||||
group, username, password := arg[0], arg[0], arg[1]
|
||||
if len(arg) == 3 {
|
||||
username, password = arg[1], arg[2]
|
||||
}
|
||||
|
||||
if username == m.Conf("rootname") {
|
||||
m.Set("detail", group, username).Target.Start(m)
|
||||
} else if msg := m.Find(username); msg == nil {
|
||||
m.Start(username, group, group, username)
|
||||
} else {
|
||||
m.Target = msg.Target
|
||||
}
|
||||
|
||||
m.Cap("password", arg[1])
|
||||
m.Log("info", "%s: login", m.Target.Name)
|
||||
m.Cap("password", password)
|
||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||
aaa.sessions[m.Cap("sessid")] = m.Target
|
||||
|
||||
m.Target.Owner = m.Target
|
||||
source.Owner = m.Target
|
||||
source.Group = m.Cap("group")
|
||||
|
||||
m.Log("info", "%s: login", source.Name)
|
||||
return m.Cap("sessid")
|
||||
|
||||
case 3:
|
||||
m.Start(arg[0], arg[0], arg[0])
|
||||
}
|
||||
return ""
|
||||
// }}}
|
||||
@ -165,9 +152,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
},
|
||||
Index: map[string]*ctx.Context{
|
||||
"void": &ctx.Context{Name: "void",
|
||||
Commands: map[string]*ctx.Command{
|
||||
"login": &ctx.Command{},
|
||||
},
|
||||
Commands: map[string]*ctx.Command{"login": &ctx.Command{}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user