Posted to tcl by rudenstam at Sun Oct 14 21:01:53 GMT 2007view raw
- #!/usr/bin/tclsh
- package require tls
- package require mysqltcl
- set ::mysql(user) ""
- set ::mysql(pass) ""
- set ::mysql(host) ""
- set ::mysql(port) ""
- set ::mysql(db) ""
- set ::mysql(table) ""
- set port 32081
- proc mysql_connect { } {
- global mysql
- return [::mysql::connect -user $mysql(user) -pass $mysql(pass) -host $mysql(host) -port $mysql(port) -db $mysql(db)]
- }
- ###########
- # stdin #
- ###########
- fileevent stdin readable {
- if { [gets stdin in] > 0 } {
- switch $in {
- exit { exit }
- rehash {
- if { [catch { source tlsrelay.tcl } err] } {
- puts "Failed to rehash: \n$err"
- } else {
- puts "Rehashed successfully"
- }
- }
- default {
- if { [catch {puts [eval $in]} msg] } {
- puts $msg
- }
- }
- }
- }
- }
- ####################################
- # Print status once every minute #
- ####################################
- proc status { } {
- puts "[clock format [clock scan now] -format "%Y-%m-%d %H:%M:%S"] <--> [expr ([llength [file channels]]-3)/2] clients connected"
- after 60000 status
- }
- ##############################
- # Accepnt a new connection #
- ##############################
- proc accept { source h p } {
- puts "$source connected from $h using $p"
- set destin [socket 127.0.0.1 32091]
- fconfigure $destin -buffering line -translation binary
- fconfigure $source -buffering line -translation binary
- set ::${source}(host) $h
- fileevent $source readable [list forward $source $destin]
- fileevent $destin readable [list forward $destin $source]
- }
- ###########################################
- # Forward traffic from one to the other #
- ###########################################
- proc forward { source destin } {
- # if either socket closed, close both
- if { [eof $source] || [eof $destin] } {
- puts "eof"
- if { [catch {close $source} err] } { puts "unable to close $source, $err" }
- if { [catch {close $destin} err] } { puts "unable to close $destin, $err" }
- if { [info exist ::$source] } {
- puts "$source closed ([set ::${source}(host)])"
- unset ::$source
- }
- if { [info exist ::$destin] } {
- puts "$destin closed ([set ::${destin}(host)])"
- unset ::$destin
- }
- return
- }
- # if there's a problem reading source, close both sockets
- if { [catch {set data [gets $source]} err] } {
- puts "$source from [set ::${source}(host)] errored, disconnecting, error: $err"
- if { [catch {close $source} err] } { puts "unable to close $source, $err" }
- if { [catch {close $destin} err] } { puts "unable to close $destin, $err" }
- if { [info exist ::$source] } { unset ::$source }
- if { [info exist ::$destin] } { unset ::$destin }
- return
- }
- # login thingy, need to check ip here as the real server will just see 127.0.0.1 on all clients
- if { [string match -nocase ".login*" $data] } {
- set sql [mysql_connect]
- set query "SELECT accountUsername FROM $::mysql(table) WHERE accountUsername = '[lindex $data 1]' AND ('[set ::${source}(host)]' LIKE accountHost || '[set ::${source}(host)]' LIKE accountHostAlt)";
- set res [mysql::sel $sql $query]
- mysql::close $sql
- # if no results with the sql, print errors, close sockets and return
- if { $res == 0 } {
- puts "wrong ip"
- puts $source "system|warning|wrong username/pass/ip"
- if { [catch {
- close $destin
- close $source
- unset ::$source
- } err] } { puts "error: \n$err" }
- return
- }
- }
- # otherwise, forward data
- if { [string trim $data] != "" } {
- puts $destin $data
- }
- }
- ########################
- # First time startup #
- ########################
- if { ![info exist serverstate(running)] } {
- tls::socket -server accept -certfile server.cert -keyfile server.key -ssl2 1 -ssl3 1 -tls1 1 -request 0 -require 0 $port
- puts "Accepting connections on $port"
- set serverstate(running) 1
- after 60000 status
- vwait forever
- }