mirror of
https://github.com/rancher/norman.git
synced 2025-09-18 08:14:56 +00:00
Fix broken windows agent
This commit is contained in:
@@ -40,8 +40,8 @@ func connectToProxy(proxyURL string, headers http.Header, auth ConnectAuthorizer
|
||||
}
|
||||
}
|
||||
|
||||
session := newClientSession(auth, ws)
|
||||
_, err = session.serve()
|
||||
session := NewClientSession(auth, ws)
|
||||
_, err = session.Serve()
|
||||
session.Close()
|
||||
return err
|
||||
}
|
||||
|
@@ -19,11 +19,11 @@ type connection struct {
|
||||
buf chan []byte
|
||||
readBuf []byte
|
||||
addr addr
|
||||
session *session
|
||||
session *Session
|
||||
connID int64
|
||||
}
|
||||
|
||||
func newConnection(connID int64, session *session, proto, address string) *connection {
|
||||
func newConnection(connID int64, session *Session, proto, address string) *connection {
|
||||
c := &connection{
|
||||
addr: addr{
|
||||
proto: proto,
|
||||
|
@@ -96,7 +96,7 @@ outer:
|
||||
continue
|
||||
}
|
||||
|
||||
session := newClientSession(func(string, string) bool { return true }, ws)
|
||||
session := NewClientSession(func(string, string) bool { return true }, ws)
|
||||
session.dialer = func(network, address string) (net.Conn, error) {
|
||||
parts := strings.SplitN(network, "::", 2)
|
||||
if len(parts) != 2 {
|
||||
@@ -106,7 +106,7 @@ outer:
|
||||
}
|
||||
|
||||
s.sessions.addListener(session)
|
||||
_, err = session.serve()
|
||||
_, err = session.Serve()
|
||||
s.sessions.removeListener(session)
|
||||
session.Close()
|
||||
|
||||
|
@@ -70,8 +70,8 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
session := s.sessions.add(clientKey, wsConn, peer)
|
||||
defer s.sessions.remove(session)
|
||||
|
||||
// Don't need to associate req.Context() to the session, it will cancel otherwise
|
||||
code, err := session.serve()
|
||||
// Don't need to associate req.Context() to the Session, it will cancel otherwise
|
||||
code, err := session.Serve()
|
||||
if err != nil {
|
||||
// Hijacked so we can't write to the client
|
||||
logrus.Infof("error in remotedialer server [%d]: %v", code, err)
|
||||
|
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type session struct {
|
||||
type Session struct {
|
||||
sync.Mutex
|
||||
|
||||
nextConnID int64
|
||||
@@ -42,8 +42,8 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
func newClientSession(auth ConnectAuthorizer, conn *websocket.Conn) *session {
|
||||
return &session{
|
||||
func NewClientSession(auth ConnectAuthorizer, conn *websocket.Conn) *Session {
|
||||
return &Session{
|
||||
clientKey: "client",
|
||||
conn: newWSConn(conn),
|
||||
conns: map[int64]*connection{},
|
||||
@@ -52,8 +52,8 @@ func newClientSession(auth ConnectAuthorizer, conn *websocket.Conn) *session {
|
||||
}
|
||||
}
|
||||
|
||||
func newSession(sessionKey int64, clientKey string, conn *websocket.Conn) *session {
|
||||
return &session{
|
||||
func newSession(sessionKey int64, clientKey string, conn *websocket.Conn) *Session {
|
||||
return &Session{
|
||||
nextConnID: 1,
|
||||
clientKey: clientKey,
|
||||
sessionKey: sessionKey,
|
||||
@@ -63,7 +63,7 @@ func newSession(sessionKey int64, clientKey string, conn *websocket.Conn) *sessi
|
||||
}
|
||||
}
|
||||
|
||||
func (s *session) startPings() {
|
||||
func (s *Session) startPings() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
s.pingCancel = cancel
|
||||
s.pingWait.Add(1)
|
||||
@@ -90,7 +90,7 @@ func (s *session) startPings() {
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *session) stopPings() {
|
||||
func (s *Session) stopPings() {
|
||||
if s.pingCancel == nil {
|
||||
return
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func (s *session) stopPings() {
|
||||
s.pingWait.Wait()
|
||||
}
|
||||
|
||||
func (s *session) serve() (int, error) {
|
||||
func (s *Session) Serve() (int, error) {
|
||||
if s.client {
|
||||
s.startPings()
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (s *session) serve() (int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *session) serveMessage(reader io.Reader) error {
|
||||
func (s *Session) serveMessage(reader io.Reader) error {
|
||||
message, err := newServerMessage(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -180,10 +180,10 @@ func parseAddress(address string) (string, int, error) {
|
||||
return parts[0], v, err
|
||||
}
|
||||
|
||||
func (s *session) addRemoteClient(address string) error {
|
||||
func (s *Session) addRemoteClient(address string) error {
|
||||
clientKey, sessionKey, err := parseAddress(address)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid remote session %s: %v", address, err)
|
||||
return fmt.Errorf("invalid remote Session %s: %v", address, err)
|
||||
}
|
||||
|
||||
keys := s.remoteClientKeys[clientKey]
|
||||
@@ -200,10 +200,10 @@ func (s *session) addRemoteClient(address string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *session) removeRemoteClient(address string) error {
|
||||
func (s *Session) removeRemoteClient(address string) error {
|
||||
clientKey, sessionKey, err := parseAddress(address)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid remote session %s: %v", address, err)
|
||||
return fmt.Errorf("invalid remote Session %s: %v", address, err)
|
||||
}
|
||||
|
||||
keys := s.remoteClientKeys[clientKey]
|
||||
@@ -219,7 +219,7 @@ func (s *session) removeRemoteClient(address string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *session) closeConnection(connID int64, err error) {
|
||||
func (s *Session) closeConnection(connID int64, err error) {
|
||||
s.Lock()
|
||||
conn := s.conns[connID]
|
||||
delete(s.conns, connID)
|
||||
@@ -233,7 +233,7 @@ func (s *session) closeConnection(connID int64, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *session) clientConnect(message *message) {
|
||||
func (s *Session) clientConnect(message *message) {
|
||||
conn := newConnection(message.connID, s, message.proto, message.address)
|
||||
|
||||
s.Lock()
|
||||
@@ -246,7 +246,7 @@ func (s *session) clientConnect(message *message) {
|
||||
go clientDial(s.dialer, conn, message)
|
||||
}
|
||||
|
||||
func (s *session) serverConnect(deadline time.Duration, proto, address string) (net.Conn, error) {
|
||||
func (s *Session) serverConnect(deadline time.Duration, proto, address string) (net.Conn, error) {
|
||||
connID := atomic.AddInt64(&s.nextConnID, 1)
|
||||
conn := newConnection(connID, s, proto, address)
|
||||
|
||||
@@ -266,14 +266,14 @@ func (s *session) serverConnect(deadline time.Duration, proto, address string) (
|
||||
return conn, err
|
||||
}
|
||||
|
||||
func (s *session) writeMessage(message *message) (int, error) {
|
||||
func (s *Session) writeMessage(message *message) (int, error) {
|
||||
if PrintTunnelData {
|
||||
logrus.Debug("WRITE ", message)
|
||||
}
|
||||
return message.WriteTo(s.conn)
|
||||
}
|
||||
|
||||
func (s *session) Close() {
|
||||
func (s *Session) Close() {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
@@ -286,7 +286,7 @@ func (s *session) Close() {
|
||||
s.conns = map[int64]*connection{}
|
||||
}
|
||||
|
||||
func (s *session) sessionAdded(clientKey string, sessionKey int64) {
|
||||
func (s *Session) sessionAdded(clientKey string, sessionKey int64) {
|
||||
client := fmt.Sprintf("%s/%d", clientKey, sessionKey)
|
||||
_, err := s.writeMessage(newAddClient(client))
|
||||
if err != nil {
|
||||
@@ -294,7 +294,7 @@ func (s *session) sessionAdded(clientKey string, sessionKey int64) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *session) sessionRemoved(clientKey string, sessionKey int64) {
|
||||
func (s *Session) sessionRemoved(clientKey string, sessionKey int64) {
|
||||
client := fmt.Sprintf("%s/%d", clientKey, sessionKey)
|
||||
_, err := s.writeMessage(newRemoveClient(client))
|
||||
if err != nil {
|
||||
|
@@ -17,20 +17,20 @@ type sessionListener interface {
|
||||
|
||||
type sessionManager struct {
|
||||
sync.Mutex
|
||||
clients map[string][]*session
|
||||
peers map[string][]*session
|
||||
clients map[string][]*Session
|
||||
peers map[string][]*Session
|
||||
listeners map[sessionListener]bool
|
||||
}
|
||||
|
||||
func newSessionManager() *sessionManager {
|
||||
return &sessionManager{
|
||||
clients: map[string][]*session{},
|
||||
peers: map[string][]*session{},
|
||||
clients: map[string][]*Session{},
|
||||
peers: map[string][]*Session{},
|
||||
listeners: map[sessionListener]bool{},
|
||||
}
|
||||
}
|
||||
|
||||
func toDialer(s *session, prefix string, deadline time.Duration) Dialer {
|
||||
func toDialer(s *Session, prefix string, deadline time.Duration) Dialer {
|
||||
return func(proto, address string) (net.Conn, error) {
|
||||
if prefix == "" {
|
||||
return s.serverConnect(deadline, proto, address)
|
||||
@@ -85,10 +85,10 @@ func (sm *sessionManager) getDialer(clientKey string, deadline time.Duration) (D
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("failed to find session for client %s", clientKey)
|
||||
return nil, fmt.Errorf("failed to find Session for client %s", clientKey)
|
||||
}
|
||||
|
||||
func (sm *sessionManager) add(clientKey string, conn *websocket.Conn, peer bool) *session {
|
||||
func (sm *sessionManager) add(clientKey string, conn *websocket.Conn, peer bool) *Session {
|
||||
sessionKey := rand.Int63()
|
||||
session := newSession(sessionKey, clientKey, conn)
|
||||
|
||||
@@ -108,12 +108,12 @@ func (sm *sessionManager) add(clientKey string, conn *websocket.Conn, peer bool)
|
||||
return session
|
||||
}
|
||||
|
||||
func (sm *sessionManager) remove(s *session) {
|
||||
func (sm *sessionManager) remove(s *Session) {
|
||||
sm.Lock()
|
||||
defer sm.Unlock()
|
||||
|
||||
for _, store := range []map[string][]*session{sm.clients, sm.peers} {
|
||||
var newSessions []*session
|
||||
for _, store := range []map[string][]*Session{sm.clients, sm.peers} {
|
||||
var newSessions []*Session
|
||||
|
||||
for _, v := range store[s.clientKey] {
|
||||
if v.sessionKey == s.sessionKey {
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (s *session) startPingsWhileWindows(rootCtx context.Context) {
|
||||
func (s *Session) startPingsWhileWindows(rootCtx context.Context) {
|
||||
ctx, cancel := context.WithCancel(rootCtx)
|
||||
s.pingCancel = cancel
|
||||
s.pingWait.Add(1)
|
||||
@@ -35,7 +35,7 @@ func (s *session) startPingsWhileWindows(rootCtx context.Context) {
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *session) serveWhileWindows(ctx context.Context) (int, error) {
|
||||
func (s *Session) ServeWhileWindows(ctx context.Context) (int, error) {
|
||||
if s.client {
|
||||
s.startPingsWhileWindows(ctx)
|
||||
}
|
||||
|
Reference in New Issue
Block a user