Created
March 16, 2019 15:04
-
-
Save chenqinghe/cadea0fc76f545154204b4dab35a4bef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"github.com/henrylee2cn/teleport" | |
"github.com/sirupsen/logrus" | |
"net/http" | |
"os" | |
) | |
func main() { | |
defer tp.FlushLogger() | |
go tp.GraceSignal() | |
// server peer | |
srv := tp.NewPeer(tp.PeerConfig{ | |
CountTime: true, | |
ListenPort: 9090, | |
PrintDetail: true, | |
}) | |
linkManager := manager.NewManager() | |
// router | |
//srv.RouteCall(new(Math)) | |
srv.RouteCall(&User{Manager: linkManager}) | |
// listen and serve | |
srv.ListenAndServe() | |
select{} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package manager | |
import ( | |
"github.com/henrylee2cn/teleport" | |
"sync" | |
) | |
type Manager struct { | |
mux *sync.RWMutex | |
clients map[string]*Client | |
} | |
func NewManager() *Manager { | |
return &Manager{ | |
clients: make(map[string]*Client), | |
mux: &sync.RWMutex{}, | |
} | |
} | |
func (m *Manager) Add(name string, client *Client) { | |
m.mux.Lock() | |
defer m.mux.Unlock() | |
m.clients[name] = client | |
} | |
func (m *Manager) Find(name string) (c *Client, ok bool) { | |
m.mux.RLock() | |
defer m.mux.RUnlock() | |
c, ok = m.clients[name] | |
return | |
} | |
type Client struct { | |
Session tp.Session | |
} | |
func NewClient(sess tp.Session) *Client { | |
return &Client{ | |
Session: sess, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type User struct { | |
Manager *manager.Manager | |
tp.CallCtx | |
} | |
func (u *User) Login(identify *[]string) (interface{}, *tp.Rerror) { | |
user, passwd := (*identify)[0], (*identify)[1] | |
if !check(user, passwd) { | |
go func() { // https://github.com/henrylee2cn/teleport/issues/22 | |
_ = u.Session().Close() | |
}() | |
} | |
u.Manager.Add(user, manager.NewClient(u.Session())) | |
return nil, &tp.Rerror{ | |
Code: 0, | |
} | |
} | |
func check(user, passwd string) bool { | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment