summaryrefslogtreecommitdiffstats
path: root/x11vnc/misc/enhanced_tightvnc_viewer/Windows/util/connect_br.tcl
blob: a023c1f4ccc5fb947debd0a50ed43bb565a822bb (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/wish

global env

set proxy1 ""
set proxy2 ""
set client_fh ""
set server_fh ""

set debug 0
if {$debug} {
	if {! [info exists env(SSVNC_DEST)]} {
		set env(SSVNC_DEST) "haystack:2037"
	}
	if {! [info exists env(SSVNC_PROXY)]} {
		set env(SSVNC_PROXY) "haystack:2037"
	}
	if {! [info exists env(SSVNC_LISTEN)]} {
		set env(SSVNC_LISTEN) "6789"
	}
}

set dest $env(SSVNC_DEST)

if [regexp {,} $env(SSVNC_PROXY)] {
	set s [split $env(SSVNC_PROXY) ","]
	set proxy1 [lindex $s 0]
	set proxy2 [lindex $s 1]
} else {
	set proxy1 $env(SSVNC_PROXY)
}

set s [split $proxy1 ":"]
set proxy1_host [lindex $s 0]
set proxy1_port [lindex $s 1]

if {$proxy2 != ""} {
	set s [split $proxy2 ":"]
	set proxy2_host [lindex $s 0]
	set proxy2_port [lindex $s 1]
}

set lport $env(SSVNC_LISTEN)

set got_connection 0
set lsock [socket -myaddr 127.0.0.1 -server handle_connection $lport]

if {1} {
	wm withdraw .
}
button .b -text "CONNECT_BR" -command {destroy .}
pack .b
after 1000 check_callback 

proc check_callback {} {
	global debug
	if {$debug} {
		puts stderr "."
	}
	check_closed
	after 1000 check_callback 
}

proc check_closed {} {
	global client_fh server_fh debug
	global got_connection

	if {! $got_connection} {
		return
	}
	set delay 100
	if {$client_fh != "" && [eof $client_fh]} {
		if {$debug} {
			puts stderr "client_fh EOF"
		}
		catch {flush $client_fh}
		after $delay
		catch {close $client_fh}
		after $delay
		catch {flush $server_fh}
		after $delay
		catch {close $server_fh}
		destroy .
		exit
	}
	if {$server_fh != "" && [eof $server_fh]} {
		if {$debug} {
			puts stderr "server_fh EOF"
		}
		catch {flush $server_fh}
		after $delay
		catch {close $server_fh}
		after $delay
		catch {flush $client_fh}
		after $delay
		catch {close $client_fh}
		destroy .
		exit
	}
}

proc xfer_in_to_out {} {
	global client_fh server_fh debug
	if {$client_fh != "" && ![eof $client_fh]} {
		set str [read $client_fh 4096]
		if {$debug} {
			puts stderr "xfer_in_to_out: $str"
		}
		if {$server_fh != ""} {
			puts -nonewline $server_fh $str
			flush $server_fh
		}
	}
	check_closed
}

proc xfer_out_to_in {} {
	global client_fh server_fh debug
	if {$server_fh != "" && ![eof $server_fh]} {
		set str [read $server_fh 4096]
		if {$debug} {
			puts stderr "xfer_out_to_in: $str"
		}
		if {$client_fh != ""} {
			puts -nonewline $client_fh $str
			flush $client_fh
		}
	}
	check_closed
}

proc handle_connection {fh host port} {
	global proxy1_host proxy1_port
	global proxy2_host proxy2_port
	global proxy1 proxy2
	global dest
	global debug
	global got_connection

	if {$got_connection} {
		catch {close $fh}
		return
	}
	set got_connection 1

	if {$debug} {
		puts stderr "connection from: $host $port"	
		puts stderr "socket $proxy1_host $proxy1_port"
	}

	set sock [socket $proxy1_host $proxy1_port]

	global client_fh server_fh
	set client_fh $fh
	set server_fh $sock

	fconfigure $fh   -translation binary -blocking 0
	fconfigure $sock -translation binary -blocking 0

	set con ""
	if {$proxy2 != ""} {
		append con "CONNECT $proxy2 HTTP/1.1\r\n"
		append con "Host: $proxy2\r\n\r\n"
	} else {
		append con "CONNECT $dest HTTP/1.1\r\n"
		append con "Host: $dest\r\n\r\n"
	}

	puts -nonewline $sock $con
	flush $sock

	set r ""
	set cnt 0
	while {1} {
		set c [read $sock 1]
		if {$c == ""} {
			check_closed
			after 20
		}
		incr cnt
		if {$debug} {
			.b configure -text "A $cnt -- $c"
			update
		}
		append r $c
		if {[regexp "\r\n\r\n" $r] || [regexp "a--no--\n\n" $r]} {
			break
		}
		if {$cnt > 3000} {
			break
		}
	}
	if {! [regexp {HTTP/.* 200} $r]} {
		puts stderr "did not find HTTP 200 #1"
		if {1} {
			destroy .
			exit 1
		}
	}

	if {$proxy2 != ""} {
		set con ""
		append con "CONNECT $dest HTTP/1.1\r\n"
		append con "Host: $dest\r\n\r\n"

		puts -nonewline $sock $con
		flush $sock

		set r ""
		set cnt 0
		while {1} {
			set c [read $sock 1]
			if {$c == ""} {
				check_closed
				after 20
			}
			incr cnt
			if {$debug} {
				.b configure -text "B $cnt -- $c"
				update
			}
			append r $c
			if {[regexp "\r\n\r\n" $r] || [regexp "a--no--\n\n" $r]} {
				break
			}
			if {$cnt > 3000} {
				break
			}
		}
		if {! [regexp {HTTP/.* 200} $r]} {
			puts stderr "did not find HTTP 200 #2"
			destroy .
			exit 1
		}
	}

	fileevent $fh   readable xfer_in_to_out
	fileevent $sock readable xfer_out_to_in
}