forked from amitbet/vncproxy
/
server.go
118 lines (99 loc) · 2.7 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package server
import (
"fmt"
"io"
"log"
"net"
"github.com/JVisi/proxy_vnc/common"
)
var DefaultClientMessages = []common.ClientMessage{
&MsgSetPixelFormat{},
&MsgSetEncodings{},
&MsgFramebufferUpdateRequest{},
&MsgKeyEvent{},
&MsgPointerEvent{},
&MsgClientCutText{},
&MsgClientQemuExtendedKey{},
}
// FramebufferUpdate holds a FramebufferUpdate wire format message.
type FramebufferUpdate struct {
_ [1]byte // padding
NumRect uint16 // number-of-rectangles
Rects []*common.Rectangle // rectangles
}
type ServerHandler func(*ServerConfig, *ServerConn) error
type ServerConfig struct {
SecurityHandlers []SecurityHandler
Encodings []common.IEncoding
PixelFormat *common.PixelFormat
ColorMap *common.ColorMap
ClientMessages []common.ClientMessage
DesktopName []byte
Height uint16
Width uint16
UseDummySession bool
//handler to allow for registering for messages, this can't be a channel
//because of the websockets handler function which will kill the connection on exit if conn.handle() is run on another thread
NewConnHandler ServerHandler
}
func wsHandlerFunc(ws io.ReadWriter, cfg *ServerConfig, sessionId string) {
err := attachNewServerConn(ws, cfg, sessionId)
if err != nil {
log.Fatalf("Error attaching new connection. %v", err)
}
}
func WsServe(url string, cfg *ServerConfig) error {
server := WsServer{cfg}
server.Listen(url, WsHandler(wsHandlerFunc))
return nil
}
func TcpServe(url string, cfg *ServerConfig) error {
ln, err := net.Listen("tcp", url)
if err != nil {
log.Fatalf("Error listen. %v", err)
}
for {
c, err := ln.Accept()
if err != nil {
return err
}
go attachNewServerConn(c, cfg, "dummySession")
}
}
func attachNewServerConn(c io.ReadWriter, cfg *ServerConfig, sessionId string) error {
conn, err := NewServerConn(c, cfg)
if err != nil {
return err
}
if err := ServerVersionHandler(cfg, conn); err != nil {
fmt.Printf("err: %v\n", err)
conn.Close()
return err
}
if err := ServerSecurityHandler(cfg, conn); err != nil {
conn.Close()
return err
}
//run the handler for this new incoming connection from a vnc-client
//this is done before the init sequence to allow listening to server-init messages (and maybe even interception in the future)
err = cfg.NewConnHandler(cfg, conn)
if err != nil {
conn.Close()
return err
}
if err := ServerClientInitHandler(cfg, conn); err != nil {
conn.Close()
return err
}
if err := ServerServerInitHandler(cfg, conn); err != nil {
conn.Close()
return err
}
conn.SessionId = sessionId
if cfg.UseDummySession {
conn.SessionId = "dummySession"
}
//go here will kill ws connections
conn.handle()
return nil
}