connect_br.tcl 11.3 KB
Newer Older
runge's avatar
runge committed
1
#!/usr/bin/wish
2

runge's avatar
runge committed
3 4 5 6 7 8 9 10 11
proc check_callback {} {
	global debug
	if {$debug} {
		puts stderr "."
	}
	check_closed
	after 1000 check_callback 
}

12 13 14 15 16 17 18 19 20 21 22 23
proc getout {} {
	global client_fh server_fh
	
	set delay 50
	catch {flush $client_fh}
	after $delay
	catch {close $client_fh}
	after $delay
	catch {flush $server_fh}
	after $delay
	catch {close $server_fh}
	after $delay
24 25 26 27 28

	global bmesg_cnt
	if [info exists bmesg_cnt] {
		catch {tkwait window .bmesg$bmesg_cnt}
	}
29 30 31 32
	destroy .
	exit
}

runge's avatar
runge committed
33
proc check_closed {} {
34 35
	global got_connection debug
	global client_fh server_fh
runge's avatar
runge committed
36 37 38 39 40 41 42 43

	if {! $got_connection} {
		return
	}
	if {$client_fh != "" && [eof $client_fh]} {
		if {$debug} {
			puts stderr "client_fh EOF"
		}
44
		getout
runge's avatar
runge committed
45 46 47 48 49
	}
	if {$server_fh != "" && [eof $server_fh]} {
		if {$debug} {
			puts stderr "server_fh EOF"
		}
50
		getout
runge's avatar
runge committed
51 52 53 54 55 56
	}
}

proc xfer_in_to_out {} {
	global client_fh server_fh debug
	if {$client_fh != "" && ![eof $client_fh]} {
57 58
		set str ""
		catch {set str [read $client_fh 4096]}
runge's avatar
runge committed
59 60 61
		if {$debug} {
			puts stderr "xfer_in_to_out: $str"
		}
62
		if {$server_fh != "" && $str != ""} {
runge's avatar
runge committed
63 64 65 66 67 68 69 70 71 72
			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]} {
73 74
		set str ""
		catch {set str [read $server_fh 4096]}
runge's avatar
runge committed
75 76 77
		if {$debug} {
			puts stderr "xfer_out_to_in: $str"
		}
78
		if {$client_fh != "" && $str != ""} {
runge's avatar
runge committed
79 80 81 82 83 84 85
			puts -nonewline $client_fh $str
			flush $client_fh
		}
	}
	check_closed
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
proc bmesg {msg} {
	return

	global bmesg_cnt
	if {! [info exists bmesg_cnt]} {
		set bmesg_cnt 0
	}
	incr bmesg_cnt
	set w .bmesg$bmesg_cnt
	catch {destroy $w}
	toplevel $w
	label $w.l -width 70 -text "$msg"
	pack $w.l
	update
}

102 103
proc do_connect_http {sock hostport which} {
	global debug cur_proxy
runge's avatar
runge committed
104
	set con ""
105 106 107 108 109
	append con "CONNECT $hostport HTTP/1.1\r\n"
	append con "Host: $hostport\r\n"
	append con "Connection: close\r\n\r\n"

	puts stderr "pxy=$which CONNECT $hostport HTTP/1.1 via $cur_proxy"
110
	bmesg "H: $which CONNECT $hostport HTTP/1.1 $cur_proxy";
runge's avatar
runge committed
111 112 113 114 115 116 117

	puts -nonewline $sock $con
	flush $sock

	set r ""
	set cnt 0
	while {1} {
118
		incr cnt
runge's avatar
runge committed
119 120 121 122 123 124 125 126 127
		set c [read $sock 1]
		if {$c == ""} {
			check_closed
			after 20
		}
		append r $c
		if {[regexp "\r\n\r\n" $r] || [regexp "a--no--\n\n" $r]} {
			break
		}
128
		if {$cnt > 30000} {
runge's avatar
runge committed
129 130 131 132 133
			break
		}
	}
	if {! [regexp {HTTP/.* 200} $r]} {
		puts stderr "did not find HTTP 200 #1"
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
		destroy .
		exit 1
	}
}

proc do_connect_socks4 {sock hostport which} {
	global debug cur_proxy

	set s [split $hostport ":"]
	set host [lindex $s 0]
	set port [lindex $s 1]

	set i1 ""
	set i2 ""
	set i3 ""
	set i4 ""

	set socks4a 0

	if {$host == "localhost" || $host == "127.0.0.1"} {
		set i1 127
		set i2 0
		set i3 0
		set i4 1
		
	} elseif [regexp {^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$} $host] {
		set n [split $host "."]
		set i1 [lindex $n 0]
		set i2 [lindex $n 1]
		set i3 [lindex $n 2]
		set i4 [lindex $n 3]
	} else {
		set i1 0
		set i2 0
		set i3 0
		set i4 3
		
		set socks4a 1
	}

	if {$socks4a} {
		puts stderr "pxy=$which socks4a connection to $host:$port via $cur_proxy"
	} else {
		puts stderr "pxy=$which socks4  connection to $host:$port via $cur_proxy"
	}

	set p1 [binary format ccScccc 4 1 $port $i1 $i2 $i3 $i4]
	set p2 "nobody"
	set p3 [binary format c 0]

	puts -nonewline $sock $p1
	puts -nonewline $sock $p2
	puts -nonewline $sock $p3
	if {$socks4a} {
		puts -nonewline $sock $host
		puts -nonewline $sock $p3
	}
	flush $sock

	set r ""; set s ""; set i 0; set cnt 0
	set ok 1
	while {$cnt < 30000 && $i < 8} {
		incr cnt
		set c [read $sock 1]
		if {$c == ""} {
			check_closed
			after 20
			continue
		}
		
		binary scan $c c s
		if {$i == 0 && $s != 0} {
			puts stderr "socks4: $i - $s"
			set ok 0
runge's avatar
runge committed
208
		}
209 210 211 212 213 214 215 216 217 218 219
		if {$i == 1 && $s != 90} {
			puts stderr "socks4: $i - $s"
			set ok 0
		}
		set r "$r,$s"
		incr i
	}
	if {! $ok} {
		puts stderr "socks4 failure: $r"
		destroy .
		exit 1
runge's avatar
runge committed
220
	}
221
}
runge's avatar
runge committed
222

223 224
proc do_connect_socks5 {sock hostport which} {
	global debug cur_proxy
runge's avatar
runge committed
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	set s [split $hostport ":"]
	set host [lindex $s 0]
	set port [lindex $s 1]

	set p1 [binary format ccc 5 1 0]
	puts -nonewline $sock $p1
	flush $sock

	set r ""; set s ""; set i 0; set cnt 0
	set ok 1
	while {$cnt < 30000 && $i < 2} {
		incr cnt
		set c [read $sock 1]
		if {$c == ""} {
			check_closed
			after 20
			continue
		}
		
		binary scan $c c s
		if {$i == 0 && $s != 5} {
			puts stderr "$i - $s"
			set ok 0
		}
		if {$i == 1 && $s != 0} {
			puts stderr "$i - $s"
			set ok 0
		}
		set r "$r,$s"
		incr i
	}
	if {! $ok} {
		puts stderr "socks5 failure: $r"
		destroy .
		exit 1
	}
runge's avatar
runge committed
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	set len [string length $host]
	set p1 [binary format ccccc 5 1 0 3 $len]
	set p2 $host

	set n1 [expr int($port/256)]
	set n2 [expr "$port - $n1 * 256"]
	set p3 [binary format cc $n1 $n2]

	puts stderr "pxy=$which socks5  connection to $host:$port via $cur_proxy"

	puts -nonewline $sock $p1
	puts -nonewline $sock $p2
	puts -nonewline $sock $p3
	flush $sock

	set i1 ""; set i2 ""; set i3 ""; set i4 ""
	set r ""; set s ""; set i 0; set cnt 0
	set ok 1
	while {$cnt < 30000 && $i < 4} {
		incr cnt
		set c [read $sock 1]
		if {$c == ""} {
			check_closed
			after 20
			continue
		}
		
		binary scan $c c s
		if {$i == 0} {
			set i1 $s
		} elseif {$i == 1} {
			set i2 $s
		} elseif {$i == 2} {
			set i3 $s
		} elseif {$i == 3} {
			set i4 $s
		}
		incr i
	}
	set r "i1=$i1,i2=$i2,i3=$i3,i4=$i4"

	if {$i4 == 1} {
		set n 6
	} elseif {$i4 == 3} {
		set c ""
		for {set i 0} {$i < 1000} {incr i} {
runge's avatar
runge committed
309 310 311 312
			set c [read $sock 1]
			if {$c == ""} {
				check_closed
				after 20
313
				continue
runge's avatar
runge committed
314
			}
315
			break;
runge's avatar
runge committed
316
		}
317 318
		if {$c == ""} {
			puts stderr "socks5 failure c: $r"
runge's avatar
runge committed
319 320 321
			destroy .
			exit 1
		}
322 323 324 325 326 327 328 329
		binary scan $c c s
		set n [expr $s + 2] 
	} elseif {$i4 == 4} {
		set n 18 
	} else {
		puts stderr "socks5 failure x: $r"
		destroy .
		exit 1
runge's avatar
runge committed
330
	}
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	#puts "n=$n --- $r"

	set i 0; set cnt 0
	while {$cnt < 30000 && $i < $n} {
		incr cnt
		set c [read $sock 1]
		if {$c == ""} {
			check_closed
			after 20
			continue
		}
		incr i
	}
	if {$i1 != 5 || $i2 != 0 || $i3 != 0} {
		puts stderr "socks failure $r"
		destroy .
		exit 1
	}
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
proc do_connect_repeater {sock hostport which repeater} {
	global debug cur_proxy

	# 250 is UltraVNC buffer size.
	set con [binary format a250 $repeater]

	puts stderr "pxy=$which REPEATER $repeater via $cur_proxy"
	bmesg "R: $which CONNECT $hostport | $repeater $cur_proxy";

	puts -nonewline $sock $con
	flush $sock

	set r ""
	set cnt 0
	while {1} {
		incr cnt
		set c [read $sock 1]
		if {$c == ""} {
			check_closed
			after 20
		}
		append r $c
		if {[string length $r] >= 12} {
			puts stderr "do_connect_repeater: $r"
			break
		}
		if {$cnt > 30000} {
			break
		}
	}
}

383 384 385 386 387 388 389
proc do_connect {sock type hostport which} {
	if {$type == "http"} 	{
		do_connect_http $sock $hostport $which
	} elseif {$type == "socks"} {
		do_connect_socks4 $sock $hostport $which
	} elseif {$type == "socks5"} {
		do_connect_socks5 $sock $hostport $which
390 391 392
	} elseif [regexp -nocase {^repeater:} $type] {
		regsub -nocase {^repeater:} $type "" repeater
		do_connect_repeater $sock $hostport $which $repeater
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	}
}

proc handle_connection {fh host port} {
	global proxy1_host proxy1_port proxy1_type
	global proxy2_host proxy2_port proxy2_type
	global proxy3_host proxy3_port proxy3_type
	global proxy1 proxy2 proxy3 dest
	global debug cur_proxy
	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 rc [catch {set sock [socket $proxy1_host $proxy1_port]}]
	if {$rc != 0} {
		puts stderr "error connecting"	
		catch {close $sock}
		destroy .
		exit
	}

	if {$debug} {
		puts stderr "got sock: $sock"	
	}
	
	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
runge's avatar
runge committed
433 434 435

	fileevent $fh   readable xfer_in_to_out
	fileevent $sock readable xfer_out_to_in
436 437 438

	set cur_proxy $proxy1
	if {$proxy2 != ""} {
439
		do_connect $sock $proxy1_type "$proxy2_host:$proxy2_port" 1
440 441 442

		set cur_proxy $proxy2
		if {$proxy3 != ""} {
443
			do_connect $sock $proxy2_type "$proxy3_host:$proxy3_port" 2
444 445 446 447 448 449 450 451 452 453

			set cur_proxy $proxy3
			do_connect $sock $proxy3_type $dest 3

		} else {
			do_connect $sock $proxy2_type $dest 2
		}
	} else {
		do_connect $sock $proxy1_type $dest 1
	}
runge's avatar
runge committed
454
}
455 456 457 458 459 460 461 462 463 464 465 466 467 468

proc proxy_type {proxy} {
	if [regexp -nocase {^socks://} $proxy] {
		return "socks"
	} elseif [regexp -nocase {^socks4://} $proxy] {
		return "socks"
	} elseif [regexp -nocase {^socks4a://} $proxy] {
		return "socks"
	} elseif [regexp -nocase {^socks5://} $proxy] {
		return "socks5"
	} elseif [regexp -nocase {^http://} $proxy] {
		return "http"
	} elseif [regexp -nocase {^https://} $proxy] {
		return "http"
469 470
	} elseif [regexp -nocase {^repeater://.*\+(.*)$} $proxy mat idstr] {
		return "repeater:$idstr"
471 472 473 474 475
	} else {
		return "http"
	}
}

476 477 478 479 480 481 482 483 484
proc proxy_hostport {proxy} {
	regsub -nocase {^[a-z][a-z]*://} $proxy "" hp
	regsub {\+.*$} $hp "" hp
	if {! [regexp {:[0-9]} $hp] && [regexp {^repeater:} $proxy]} {
		set hp "$hp:5900"
	}
	return $hp
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
global env

set proxy1 ""
set proxy2 ""
set proxy3 ""
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"
	}
} else {
	if {! [info exists env(SSVNC_DEST)]} {
		destroy .; exit;
	}
	if {! [info exists env(SSVNC_PROXY)]} {
		destroy .; exit;
	}
511
	if {! [info exists env(SSVNC_LISTEN)] && ! [info exists env(SSVNC_REVERSE)]} {
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
		destroy .; exit;
	}
}

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]
	set proxy3 [lindex $s 2]
} else {
	set proxy1 $env(SSVNC_PROXY)
}

527 528
set proxy1_type [proxy_type     $proxy1]
set proxy1_hp   [proxy_hostport $proxy1]
529

530
set s [split $proxy1_hp ":"]
531 532 533 534 535 536 537 538
set proxy1_host [lindex $s 0]
set proxy1_port [lindex $s 1]

set proxy2_type ""
set proxy2_host ""
set proxy2_port ""

if {$proxy2 != ""} {
539 540 541
	set proxy2_type [proxy_type     $proxy2]
	set proxy2_hp   [proxy_hostport $proxy2]
	set s [split $proxy2_hp ":"]
542 543 544 545
	set proxy2_host [lindex $s 0]
	set proxy2_port [lindex $s 1]
}

546 547 548 549
set proxy3_type ""
set proxy3_host ""
set proxy3_port ""

550
if {$proxy3 != ""} {
551 552 553
	set proxy3_type [proxy_type     $proxy3]
	set proxy3_hp   [proxy_hostport $proxy3]
	set s [split $proxy3_hp ":"]
554 555 556 557
	set proxy3_host [lindex $s 0]
	set proxy3_port [lindex $s 1]
}

558 559 560
bmesg "1: '$proxy1_host' '$proxy1_port' '$proxy1_type'";
bmesg "2: '$proxy2_host' '$proxy2_port' '$proxy2_type'";
bmesg "3: '$proxy3_host' '$proxy3_port' '$proxy3_type'";
561 562 563

set got_connection 0

564
proc setb {} {
565
	wm withdraw .
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	button .b -text "CONNECT_BR" -command {destroy .}
	pack .b
	after 1000 check_callback 
}

if [info exists env(SSVNC_REVERSE)] {
	set s [split $env(SSVNC_REVERSE) ":"]
	set rhost [lindex $s 0]
	set rport [lindex $s 1]
	set rc [catch {set lsock [socket $rhost $rport]}]
	if {$rc != 0} {
		puts stderr "error reversing"	
		destroy .; exit 1
	}
	puts stderr "SSVNC_REVERSE to $rhost $rport OK";
	setb
	handle_connection $lsock $rhost $rport
} else {
	set lport $env(SSVNC_LISTEN)
	set rc [catch {set lsock [socket -myaddr 127.0.0.1 -server handle_connection $lport]}]
	if {$rc != 0} {
		puts stderr "error listening"	
		destroy .; exit 1
	}
	puts stderr "SSVNC_LISTEN on $lport OK";
	setb
592
}