Refactor dispatcher.go

This commit is contained in:
Andy Wang 2020-10-17 11:15:21 +01:00
parent c6d6f40021
commit e092e280c1
1 changed files with 20 additions and 26 deletions

View File

@ -191,22 +191,21 @@ func dispatchConnection(conn net.Conn, sta *State) {
// added to the userinfo database. The distinction between going into the admin mode // added to the userinfo database. The distinction between going into the admin mode
// and normal proxy mode is that sessionID needs == 0 for admin mode // and normal proxy mode is that sessionID needs == 0 for admin mode
if bytes.Equal(ci.UID, sta.AdminUID) && ci.SessionId == 0 { if bytes.Equal(ci.UID, sta.AdminUID) && ci.SessionId == 0 {
sesh := mux.MakeSession(0, seshConfig)
preparedConn, err := finishHandshake(conn, sessionKey, sta.WorldState.Rand) preparedConn, err := finishHandshake(conn, sessionKey, sta.WorldState.Rand)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return return
} }
log.Trace("finished handshake") log.Trace("finished handshake")
sesh := mux.MakeSession(0, seshConfig)
sesh.AddConnection(preparedConn) sesh.AddConnection(preparedConn)
//TODO: Router could be nil in cnc mode //TODO: Router could be nil in cnc mode
log.WithField("remoteAddr", preparedConn.RemoteAddr()).Info("New admin session") log.WithField("remoteAddr", preparedConn.RemoteAddr()).Info("New admin session")
err = http.Serve(sesh, usermanager.APIRouterOf(sta.Panel.Manager)) err = http.Serve(sesh, usermanager.APIRouterOf(sta.Panel.Manager))
if err != nil { // http.Serve never returns with non-nil error
log.Error(err) log.Error(err)
return return
} }
}
var user *ActiveUser var user *ActiveUser
if sta.IsBypass(ci.UID) { if sta.IsBypass(ci.UID) {
@ -231,7 +230,6 @@ func dispatchConnection(conn net.Conn, sta *State) {
return return
} }
if existing {
preparedConn, err := finishHandshake(conn, sesh.SessionKey, sta.WorldState.Rand) preparedConn, err := finishHandshake(conn, sesh.SessionKey, sta.WorldState.Rand)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
@ -239,22 +237,19 @@ func dispatchConnection(conn net.Conn, sta *State) {
} }
log.Trace("finished handshake") log.Trace("finished handshake")
sesh.AddConnection(preparedConn) sesh.AddConnection(preparedConn)
return
}
preparedConn, err := finishHandshake(conn, sessionKey, sta.WorldState.Rand)
if err != nil {
log.Error(err)
return
}
log.Trace("finished handshake")
if !existing {
// if the session was newly made, we serve connections from the session streams to the proxy server
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"UID": b64(ci.UID), "UID": b64(ci.UID),
"sessionID": ci.SessionId, "sessionID": ci.SessionId,
}).Info("New session") }).Info("New session")
sesh.AddConnection(preparedConn)
serveSession(sesh, ci, user, sta)
}
}
func serveSession(sesh *mux.Session, ci ClientInfo, user *ActiveUser, sta *State) error {
for { for {
newStream, err := sesh.Accept() newStream, err := sesh.Accept()
if err != nil { if err != nil {
@ -265,9 +260,9 @@ func dispatchConnection(conn net.Conn, sta *State) {
"reason": sesh.TerminalMsg(), "reason": sesh.TerminalMsg(),
}).Info("Session closed") }).Info("Session closed")
user.CloseSession(ci.SessionId, "") user.CloseSession(ci.SessionId, "")
return return nil
} else { } else {
// TODO: other errors log.Errorf("unhandled error on session.Accept(): %v", err)
continue continue
} }
} }
@ -276,7 +271,7 @@ func dispatchConnection(conn net.Conn, sta *State) {
if err != nil { if err != nil {
log.Errorf("Failed to connect to %v: %v", ci.ProxyMethod, err) log.Errorf("Failed to connect to %v: %v", ci.ProxyMethod, err)
user.CloseSession(ci.SessionId, "Failed to connect to proxy server") user.CloseSession(ci.SessionId, "Failed to connect to proxy server")
continue return err
} }
log.Tracef("%v endpoint has been successfully connected", ci.ProxyMethod) log.Tracef("%v endpoint has been successfully connected", ci.ProxyMethod)
@ -294,5 +289,4 @@ func dispatchConnection(conn net.Conn, sta *State) {
} }
}() }()
} }
} }