/
main.go
96 lines (79 loc) · 1.8 KB
/
main.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
package main
import (
"code.google.com/p/go.net/websocket"
"flag"
"fmt"
"net/http"
"runtime"
"strconv"
"strings"
)
var (
hostname = flag.String("h", "", "The RDP server we will connect to")
username = flag.String("u", "", "Username for the RDP server")
password = flag.String("p", "", "Password for the RDP server")
)
func getResolution(ws *websocket.Conn) (width int64, height int64) {
request := ws.Request()
dtsize := request.FormValue("dtsize")
if !strings.Contains(dtsize, "x") {
width = 800
height = 600
} else {
sizeparts := strings.Split(dtsize, "x")
width, _ = strconv.ParseInt(sizeparts[0], 10, 32)
height, _ = strconv.ParseInt(sizeparts[1], 10, 32)
if width < 400 {
width = 400
} else if width > 1920 {
width = 1920
}
if height < 300 {
height = 300
} else if height > 1080 {
height = 1080
}
}
return width, height
}
func processSendQ(ws *websocket.Conn, sendq chan []byte) {
for {
buf := <-sendq
err := websocket.Message.Send(ws, buf)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
}
func initSocket(ws *websocket.Conn) {
sendq := make(chan []byte, 100)
recvq := make(chan []byte, 5)
width, height := getResolution(ws)
fmt.Printf("User requested size %d x %d\n", width, height)
settings := &rdpConnectionSettings{
hostname,
username,
password,
int(width),
int(height),
}
go rdpconnect(sendq, recvq, settings)
go processSendQ(ws, sendq)
read := make([]byte, 1024, 1024)
for {
_, err := ws.Read(read)
if err != nil {
recvq <- []byte("1")
}
}
}
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
http.Handle("/", websocket.Handler(initSocket))
fmt.Printf("http://localhost:%d/\n", 4455)
err := http.ListenAndServe(":4455", nil)
if err != nil {
panic("ListenANdServe: " + err.Error())
}
}