MCPcopy
hub / github.com/jpillora/chisel / handleWebsocket

Method handleWebsocket

server/server_handler.go:51–171  ·  view source on GitHub ↗

handleWebsocket is responsible for handling the websocket connection

(w http.ResponseWriter, req *http.Request)

Source from the content-addressed store, hash-verified

49
50// handleWebsocket is responsible for handling the websocket connection
51func (s *Server) handleWebsocket(w http.ResponseWriter, req *http.Request) {
52 id := atomic.AddInt32(&s.sessCount, 1)
53 l := s.Fork("session#%d", id)
54 wsConn, err := upgrader.Upgrade(w, req, nil)
55 if err != nil {
56 l.Debugf("Failed to upgrade (%s)", err)
57 return
58 }
59 conn := cnet.NewWebSocketConn(wsConn)
60 // perform SSH handshake on net.Conn
61 l.Debugf("Handshaking with %s...", req.RemoteAddr)
62 sshConn, chans, reqs, err := ssh.NewServerConn(conn, s.sshConfig)
63 if err != nil {
64 s.Debugf("Failed to handshake (%s)", err)
65 return
66 }
67 // pull the users from the session map
68 var user *settings.User
69 if s.users.Len() > 0 {
70 sid := string(sshConn.SessionID())
71 u, ok := s.sessions.Get(sid)
72 if !ok {
73 panic("bug in ssh auth handler")
74 }
75 user = u
76 s.sessions.Del(sid)
77 }
78 // chisel server handshake (reverse of client handshake)
79 // verify configuration
80 l.Debugf("Verifying configuration")
81 // wait for request, with timeout
82 var r *ssh.Request
83 select {
84 case r = <-reqs:
85 case <-time.After(settings.EnvDuration("CONFIG_TIMEOUT", 10*time.Second)):
86 l.Debugf("Timeout waiting for configuration")
87 sshConn.Close()
88 return
89 }
90 failed := func(err error) {
91 l.Debugf("Failed: %s", err)
92 r.Reply(false, []byte(err.Error()))
93 }
94 if r.Type != "config" {
95 failed(s.Errorf("expecting config request"))
96 return
97 }
98 c, err := settings.DecodeConfig(r.Payload)
99 if err != nil {
100 failed(s.Errorf("invalid config"))
101 return
102 }
103 //print if client and server versions dont match
104 cv := strings.TrimPrefix(c.Version, "v")
105 if cv == "" {
106 cv = "<unknown>"
107 }
108 sv := strings.TrimPrefix(chshare.BuildVersion, "v")

Callers 1

handleClientHandlerMethod · 0.95

Calls 15

HasAccessMethod · 0.95
NewWebSocketConnFunction · 0.92
EnvDurationFunction · 0.92
DecodeConfigFunction · 0.92
NewFunction · 0.92
ForkMethod · 0.80
DebugfMethod · 0.80
LenMethod · 0.80
GetMethod · 0.80
DelMethod · 0.80
ErrorfMethod · 0.80
InfofMethod · 0.80

Tested by

no test coverage detected