| 1 | #include <Constants.au3> |
|---|
| 2 | |
|---|
| 3 | ;; SETTINGS HERE, CHANGE THESE |
|---|
| 4 | Global Const $sAdminPass = "manadar" ;; Admin password. Change this please. |
|---|
| 5 | ;; END OF SETTINGS, STOP CHANGING!! |
|---|
| 6 | |
|---|
| 7 | ;; TCP Variables |
|---|
| 8 | Dim $sMaxConnections = 10 |
|---|
| 9 | Dim $hSocket[$sMaxConnections], $sBuffer[$sMaxConnections], $iAuth[$sMaxConnections], $hCommand[$sMaxConnections], $bIgnoreCommand[$sMaxConnections] |
|---|
| 10 | |
|---|
| 11 | ;; TCP Options |
|---|
| 12 | Dim $sIPAddress = @IPAddress1, $nPort = 5000 |
|---|
| 13 | |
|---|
| 14 | TCPStartup() |
|---|
| 15 | |
|---|
| 16 | $sMainSocket = TCPListen($sIPAddress, $nPort, 5) |
|---|
| 17 | If @error Then |
|---|
| 18 | Switch @error |
|---|
| 19 | Case 1 |
|---|
| 20 | _FatalError("The listening address was incorrect (Possibly another server was already running): " & $sIPAddress) |
|---|
| 21 | Case 2 |
|---|
| 22 | _FatalError("The listening port was incorrect (Possibly another server was already running): " & $nPort) |
|---|
| 23 | Case Else |
|---|
| 24 | _FatalError("Unable to set up a listening server on " & $sIPAddress & ":" & $nPort) |
|---|
| 25 | EndSwitch |
|---|
| 26 | EndIf |
|---|
| 27 | |
|---|
| 28 | While 1 |
|---|
| 29 | ;; Accept new incoming clients, and ask them to authorise. |
|---|
| 30 | $sNewSocket = TCPAccept($sMainSocket) |
|---|
| 31 | If $sNewSocket > -1 Then |
|---|
| 32 | For $x = 0 To UBound($hSocket) - 1 |
|---|
| 33 | If Not $hSocket[$x] Then |
|---|
| 34 | $hSocket[$x] = $sNewSocket |
|---|
| 35 | $iAuth[$x] = 0 |
|---|
| 36 | ;TCPSend($hSocket[$x], $sWelcomeMessage & @CRLF & @CRLF) |
|---|
| 37 | TCPSend($hSocket[$x], "Please enter the administrator password" & @CRLF & ">") |
|---|
| 38 | ExitLoop |
|---|
| 39 | EndIf |
|---|
| 40 | Next |
|---|
| 41 | EndIf |
|---|
| 42 | |
|---|
| 43 | ;; Loop through existing connections, check if they sent us any data |
|---|
| 44 | For $x = 0 To UBound($hSocket) - 1 |
|---|
| 45 | If $hSocket[$x] Then |
|---|
| 46 | |
|---|
| 47 | ;; Handle incoming data |
|---|
| 48 | $sData = TCPRecv($hSocket[$x], 100) |
|---|
| 49 | $sBuffer[$x] &= $sData |
|---|
| 50 | If @error Then |
|---|
| 51 | MsgBox(0,"test",@error) |
|---|
| 52 | TCPCloseSocket($hSocket[$x]) |
|---|
| 53 | $hSocket[$x] = "" |
|---|
| 54 | $sBuffer[$x] = "" |
|---|
| 55 | $iAuth[$x] = 0 |
|---|
| 56 | ProcessClose($hCommand[$x]) |
|---|
| 57 | ElseIf Asc($sData) = 0x8 Then ;backspace received |
|---|
| 58 | $len = StringLen($sBuffer[$x]) |
|---|
| 59 | $sBuffer[$x] = StringTrimRight($sBuffer[$x], 2) ; trim the buffer |
|---|
| 60 | If $len = 1 Then |
|---|
| 61 | TCPSend($hSocket[$x], ">") |
|---|
| 62 | Else |
|---|
| 63 | TCPSend($hSocket[$x], " " & Chr(0x8)) |
|---|
| 64 | EndIf |
|---|
| 65 | EndIf |
|---|
| 66 | |
|---|
| 67 | ;; Handle data, in case data is complete: ended with newline |
|---|
| 68 | If StringInStr($sBuffer[$x], @CRLF) Then |
|---|
| 69 | $sBuffer[$x] = StringTrimRight($sBuffer[$x], 2) |
|---|
| 70 | If StringLen($sBuffer[$x]) = 0 Then |
|---|
| 71 | $sBuffer[$x] = " " ; You cannot send "nothing" over TCP (no packet is sent), thus I replace it with something non obstrusive |
|---|
| 72 | EndIf |
|---|
| 73 | |
|---|
| 74 | ;; Check if user is authorised |
|---|
| 75 | If $iAuth[$x] == 0 Then |
|---|
| 76 | ;; Not authorised, user is typing password |
|---|
| 77 | If ($sBuffer[$x] == $sAdminPass) Then |
|---|
| 78 | $iAuth[$x] = 1 |
|---|
| 79 | $hCommand[$x] = Run("cmd", @ScriptDir, @SW_HIDE, $STDIN_CHILD + $STDERR_CHILD + $STDOUT_CHILD) |
|---|
| 80 | TCPSend($hSocket[$x], "Administrator authorisation granted." & @CRLF & @CRLF) |
|---|
| 81 | ConsoleWrite($x & " logged in with admin password." & @CRLF) |
|---|
| 82 | Else |
|---|
| 83 | TCPSend($hSocket[$x], "Access denied." & @CRLF & ">") |
|---|
| 84 | EndIf |
|---|
| 85 | Else |
|---|
| 86 | If $sBuffer[$x] <> "" Then ; stop making output if user starts typing, then punch him in the face or something for fucking it up |
|---|
| 87 | ConsoleWrite($sBuffer[$x] & @CRLF) |
|---|
| 88 | StdinWrite($hCommand[$x], $sBuffer[$x] & @CRLF) |
|---|
| 89 | $bIgnoreCommand[$x] = True |
|---|
| 90 | EndIf |
|---|
| 91 | EndIf |
|---|
| 92 | $sBuffer[$x] = "" |
|---|
| 93 | EndIf |
|---|
| 94 | |
|---|
| 95 | If $iAuth[$x] Then ; no @CRLF in buffer, but user is authed for output |
|---|
| 96 | $line = StdoutRead($hCommand[$x]) |
|---|
| 97 | If $line <> "" Then |
|---|
| 98 | If $bIgnoreCommand[$x] Then |
|---|
| 99 | $line = StringTrimLeft($line, StringInStr($line, @LF)+1) |
|---|
| 100 | $bIgnoreCommand[$x] = False |
|---|
| 101 | EndIf |
|---|
| 102 | ConsoleWrite($line & @CRLF) |
|---|
| 103 | TCPSend($hSocket[$x], $line) |
|---|
| 104 | EndIf |
|---|
| 105 | EndIf |
|---|
| 106 | EndIf |
|---|
| 107 | Next |
|---|
| 108 | WEnd |
|---|
| 109 | |
|---|
| 110 | Func MessageBroadcast($sMsg) |
|---|
| 111 | For $n = 0 To UBound($hSocket) - 1 |
|---|
| 112 | If $hSocket[$n] AND $iAuth[$n] == 1 Then |
|---|
| 113 | TCPSend($hSocket[$n], $sMsg) |
|---|
| 114 | EndIf |
|---|
| 115 | Next |
|---|
| 116 | EndFunc ;==>MessageBroadcast |
|---|
| 117 | |
|---|
| 118 | Func _FatalError($msg) |
|---|
| 119 | ConsoleWrite(@CRLF & "! " & $msg & @CRLF) |
|---|
| 120 | ;MsgBox(0, $sAppName & $nVersion, "A fatal error has occured and " & $sAppName & " has to be closed with the error message: " & @CRLF & $msg) |
|---|
| 121 | Exit |
|---|
| 122 | EndFunc |
|---|