Allow an terminal message to be set as a session terminates

This commit is contained in:
Qian Wang 2019-07-24 14:25:57 +01:00
parent 299f08270c
commit 9b552f55a4
2 changed files with 16 additions and 1 deletions

View File

@ -186,7 +186,7 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
newStream, err := sesh.Accept()
if err != nil {
if err == mux.ErrBrokenSession {
log.Printf("Session closed for UID:%v, sessionID:%v\n", b64.EncodeToString(UID), sessionID)
log.Printf("Session closed for UID:%v, sessionID:%v, reason:%v\n", b64.EncodeToString(UID), sessionID, sesh.TerminalMsg())
user.DelSession(sessionID)
return
} else {

View File

@ -41,6 +41,8 @@ type Session struct {
broken uint32
die chan struct{}
suicide sync.Once
terminalMsg atomic.Value
}
func MakeSession(id uint32, valve *Valve, obfs Obfser, deobfs Deobfser, obfsedRead func(net.Conn, []byte) (int, error)) *Session {
@ -124,6 +126,19 @@ func (sesh *Session) getStream(id uint32, closingFrame bool) *Stream {
}
}
func (sesh *Session) SetTerminalMsg(msg string) {
sesh.terminalMsg.Store(msg)
}
func (sesh *Session) TerminalMsg() string {
msg := sesh.terminalMsg.Load()
if msg != nil {
return msg.(string)
} else {
return ""
}
}
func (sesh *Session) Close() error {
// Because closing a closed channel causes panic
sesh.suicide.Do(func() { close(sesh.die) })