TCP LogStash PHP steam socket working in shell but not file - php

I have a a logstash tcp server running which accepts a tcp socket connection, I can write to that connection on the php shell.
If I use the same code in a file I get the following lines in logstash debug console and nothing goes into my elasticsearch instance
[DEBUG] 2020-07-15 10:31:40.987 [nioEventLoopGroup-2-3] jsonlines - config LogStash::Codecs::JSONLines/#charset = "UTF-8"
[DEBUG] 2020-07-15 10:31:40.987 [nioEventLoopGroup-2-3] jsonlines - config LogStash::Codecs::JSONLines/#id = "json_lines_dbda8bcd-69ed-4356-81af-381355f76e2f"
[DEBUG] 2020-07-15 10:31:40.987 [nioEventLoopGroup-2-3] jsonlines - config LogStash::Codecs::JSONLines/#enable_metric = true
[DEBUG] 2020-07-15 10:31:40.987 [nioEventLoopGroup-2-3] jsonlines - config LogStash::Codecs::JSONLines/#delimiter = "\n"
Logstash config
input {
tcp {
port => 5080
codec => "json"
id => "PHP_TCP_LOGS"
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "%{[xx]}"
}
}
PHP File
$socket = stream_socket_client('tcp://localhost:5080', $errorNumber, $error, 30);
$a = ["foo"=>"barr"];
fwrite($socket, json_encode($a) . "\n");
echo $error;

Related

Cannot get SOAP response from world server

When using the following connection script:
<?php
ini_set('default_socket_timeout', 120);
$soap_connection_info = array(
'soap_uri' => 'urn:AC',
'soap_host' => '127.0.0.1',
'soap_port' => '7878',
'account_name' => 'acore',
'account_password' => 'password'
);
function RemoteCommandWithSOAP($username, $password, $COMMAND)
{
global $soap_connection_info;
$result = '';
try {
$conn = new SoapClient(NULL, array(
'location' => 'http://' . $soap_connection_info['soap_host'] . ':' . $soap_connection_info['soap_port'] . '/',
'uri' => $soap_connection_info['soap_uri'],
'style' => SOAP_RPC,
'login' => $username,
'password' => $password,
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
));
$result = $conn->executeCommand(new SoapParam($COMMAND, 'command'));
unset($conn);
} catch (Exception $e) {
$result = "\nHave error on soap!\n" . $e . "\n";
if (strpos($e, 'There is no such command') !== false) {
$result = 'There is no such command!';
}
}
return $result;
}
echo RemoteCommandWithSOAP($soap_connection_info['account_name'], $soap_connection_info['account_password'], ".server info");
?>
With the world server running and listening to port 7878, I get:
local-iMac $ php test.php
Have error on soap!
SoapFault exception: [HTTP] Error Fetching http headers in /Users/dan/dev/test.php:26
Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://127.0.0....', 'urn:AC#executeC...', 1, false)
#1 /Users/dan/dev/test.php(26): SoapClient->__call('executeCommand', Array)
#2 /Users/dan/dev/test.php(39): RemoteCommandWithSOAP('acore', 'password', '.server info')
#3 {main}
I know error fetching headers points to a possible timeout when waiting for a response but this error comes up instantly as if there was no wait. However I added in the ini_set('default_socket_timeout', 120); in my script just to be sure.
My worldserver.conf:
# - the DB hostname will be the name of the database docker container
LoginDatabaseInfo = "ac-database;3306;root;password;acore_auth"
WorldDatabaseInfo = "ac-database;3306;root;password;acore_world"
CharacterDatabaseInfo = "ac-database;3306;root;password;acore_characters"
# Add more configuration overwrites by copying settings from worldserver.conf.dist
LogLevel = 5
Appender.File=2,5,7,debug.log,w
Logger.network.soap=5,File
Logger.server.worldserver=5,File
# Disable idle connections automatic kick since it doesn't work well on macOS + Docker
CloseIdleConnections = 0
Updates.EnableDatabases = 1
Death.SicknessLevel = -10
SOAP.Enabled = 1
SOAP.IP = "127.0.0.1"
SOAP.Port = 7878
And my docker-compose is unchanged:
https://github.com/azerothcore/azerothcore-wotlk/blob/master/docker-compose.yml
Interestingly if I down the world container I will get a different response as if it was connecting when it was up.
local-iMac $ php test.php
Have error on soap!
SoapFault exception: [HTTP] Could not connect to host in /Users/dan/dev/test.php:26
Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://127.0.0....', 'urn:AC#executeC...', 1, false)
#1 /Users/dan/dev/test.php(26): SoapClient->__call('executeCommand', Array)
#2 /Users/dan/dev/test.php(39): RemoteCommandWithSOAP('acore', 'password', '.server info')
#3 {main}
Also I enabled worldserver logs to see what was going on in SOAP:
2021-09-23_20:41:17 INFO [server.worldserver] > Using configuration file /azerothcore/env/dist/etc/worldserver.conf
2021-09-23_20:41:17 INFO [server.worldserver] > Using SSL version: OpenSSL 1.1.1f 31 Mar 2020 (library: OpenSSL 1.1.1f 31 Mar 2020)
2021-09-23_20:41:17 INFO [server.worldserver] > Using Boost version: 1.71.0
2021-09-23_20:41:17 INFO [server.worldserver]
2021-09-23_20:41:17 INFO [server.worldserver] Process priority class set to -15
2021-09-23_20:41:31 INFO [network.soap] ACSoap: bound to http://127.0.0.1:7878
Also I know container is correctly listening to the port:
f166a28ffdf5 acore/ac-wotlk-worldserver-local:master "./acore.sh run-worl…" 3 weeks ago Up 48 minutes 0.0.0.0:7878->7878/tcp, :::7878->7878/tcp, 0.0.0.0:8085->8085/tcp, :::8085->8085/tcp azerothcore-wotlk_ac-worldserver_1
Some stats on my local:
MacOS Mojave 10.14.6
local-iMac $ docker -v
Docker version 20.10.7, build f0df350
local-iMac $ php -v
PHP 8.0.10 (cli) (built: Aug 26 2021 15:37:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
with Zend OPcache v8.0.10, Copyright (c), by Zend Technologies
Docker acts like a DHCP server and assigns IP's to the containers. You need to check for what the 'local' IP is and that means 'local' to docker.
docker inspect <containerID> | grep IPAddress
That is the ip you need to bind to in your config.
SOAP.Enabled = 1
SOAP.IP = "172.19.0.3"
SOAP.Port = 7878

PHP/Python socket remote connection problems

I have this python code:
from socket import *
import threading
import thread
import time
import json
def handler(clientsock,addr):
while 1:
time.sleep(2)
data = clientsock.recv(65535);
if not data:
break
object = json.loads(data)
object['status'] = 1
object['timestamp'] = time.time()
output = json.dumps(object)
msg = output
clientsock.send(msg)
clientsock.close()
if __name__ == '__main__':
HOST = '192.168.0.28'
PORT = 5555
BUFSIZ = 65535
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(5)
while 1:
print 'waiting for connection...'
clientsock, addr = serversock.accept()
print '...connected from: ', addr
thread.start_new_thread(handler, (clientsock, addr))
and this PHP code:
<?php
/**
*
* PHP JSON Echo Server client
*
*/
// python server socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, '192.168.0.28', 5555);
//Create a message, and send it to the server on $socket.
$data = array(
'username' => 'sysadmin',
'key' => '093ufj408xr0289u3r0x9u2m309x',
'action' => 'login',
);
$json = json_encode($data);
socket_send($socket, $json, strlen($json), MSG_EOF);
$data = socket_read($socket, 65535);
$object = json_decode($data);
if($object->status) {
echo '<p>Data received successfully.';
} else {
echo '<p>Error. Data not read correctly!';
}
echo '<p>'.$data;
//Close the socket.
socket_close($socket);
?>
If I run it in my local network it works without a problem. But when I execute the PHP script on an external hosting it doesn't work anymore. I've changed the IP address to my WAN IP address and even to the DNS offered by the ISP. Nothing works.
This is the output:
Warning: socket_connect(): unable to connect [111]: Connection refused in /home/usr/public_html/webclient.php on line 9
Warning: socket_send(): unable to write to socket [32]: Broken pipe in /home/usr/public_html/webclient.php on line 17
Warning: socket_read(): unable to read from socket [107]: Transport endpoint is not connected in /home/usr/public_html/webclient.php on line 18
Error. Data not read correctly!
I tried scanning for open ports with this service: http://www.ipfingerprints.com/portscan.php and port 5555 is open. I also got a message from the server:
waiting for connection...
...connected from: ('5.79.68.210', 36080)
Where is the problem?
on this code :
print '...connected from: ', addr
you cant use this code, for know open port you need use this code:
print '...connect from: ' + addr[0] + ':' + str(addr[1])
only that
and you arnt using the sockets coorectly:
serversock = socket(AF_INET, SOCK_STREAM)
use this its will be better:
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

My PHP application dies with fastcgi: unexpected end-of-file

I updated to PHP 7 at my localhost, but since then anytime i want to redirect from one page to another in my nette application, I'll receive error: 500 - Internal Server Error.
I was searching through stack overflow and found a problem that is quite similar to mine here: How to solve "mod_fastcgi.c.2566 unexpected end-of-file (perhaps the fastcgi process died)" when calling .php that takes long time to execute? . However, I don't work with large files and my connection dies immediately.
My /var/log/lighttpd/error.log
2016-03-06 10:54:11: (server.c.1456) [note] graceful shutdown started
2016-03-06 10:54:11: (server.c.1572) server stopped by UID = 0 PID = 351
2016-03-06 11:03:48: (log.c.194) server started
2016-03-06 11:07:17: (mod_fastcgi.c.2390) unexpected end-of-file (perhaps the fastcgi process died): pid: 21725 socket: unix:/run/lighttpd/php-fastcgi.sock-3
2016-03-06 11:07:17: (mod_fastcgi.c.3171) response not received, request sent: 1029 on socket: unix:/run/lighttpd/php-fastcgi.sock-3 for /~rost/lp/web/www/index.php?, closing connection
2016-03-06 11:09:01: (mod_fastcgi.c.2390) unexpected end-of-file (perhaps the fastcgi process died): pid: 21725 socket: unix:/run/lighttpd/php-fastcgi.sock-3
2016-03-06 11:09:01: (mod_fastcgi.c.3171) response not received, request sent: 1061 on socket: unix:/run/lighttpd/php-fastcgi.sock-3 for /~rost/lp/web/www/index.php?action=list&presenter=Campaign, closing connection
2016-03-06 11:09:06: (mod_fastcgi.c.2390) unexpected end-of-file (perhaps the fastcgi process died): pid: 21725 socket: unix:/run/lighttpd/php-fastcgi.sock-3
2016-03-06 11:09:06: (mod_fastcgi.c.3171) response not received, request sent: 942 on socket: unix:/run/lighttpd/php-fastcgi.sock-3 for /~rost/lp/web/www/index.php?, closing connection
2016-03-06 11:09:14: (mod_fastcgi.c.2390) unexpected end-of-file (perhaps the fastcgi process died): pid: 21725 socket: unix:/run/lighttpd/php-fastcgi.sock-3
2016-03-06 11:09:14: (mod_fastcgi.c.3171) response not received, request sent: 1051 on socket: unix:/run/lighttpd/php-fastcgi.sock-3 for /~rost/lp/web/www/index.php?action=out&presenter=Sign, closing connection
My /etc/lighttpd/lighttpd.conf
server.modules = ( "mod_userdir",
"mod_access",
"mod_accesslog",
"mod_fastcgi",
"mod_rewrite",
"mod_auth"
)
server.port = 80
server.username = "http"
server.groupname = "http"
server.document-root = "/srv/http"
server.errorlog = "/var/log/lighttpd/error.log"
dir-listing.activate = "enable"
index-file.names = ( "index.html" )
# Rewrite URL without dots to index.php
#url.rewrite-once = ( "/^[^.?]*$/" => "/index.php" )
mimetype.assign = ( ".html" => "text/html",
".htm" => "text/html",
".txt" => "text/plain",
".properties" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png",
".svg" => "image/svg+xml",
".gif" => "image/gif",
".css" => "text/css",
".js" => "application/x-javascript",
"" => "application/octet-stream"
)
userdir.path = "public_html"
# Fast CGI
include "conf.d/fastcgi.conf"
My /etc/lighttpd/conf.d/fastcgi.conf
server.modules += ( "mod_fastcgi" )
#server.indexfiles += ( "index.php" ) #this is deprecated
index-file.names += ( "index.php" )
fastcgi.server = (
".php" => (
"localhost" => (
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/run/lighttpd/php-fastcgi.sock",
"max-procs" => 4, # default value
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "1", # default value
),
"broken-scriptfilename" => "enable"
))
)
Variables from /etc/php/php.ini
cat /etc/php/php.ini | grep max_execution_time
max_execution_time = 30
cat /etc/php/php.ini | grep default_socket_timeout
default_socket_timeout = 60
Update 7.3.2016
I switched from php fast cgi to php-fpm and interesting thing is that problem prevails, but is less often. Sometimes the redirect jump to 500 and sometimes not. And error log again:
2016-03-07 22:23:32: (mod_fastcgi.c.2390) unexpected end-of-file (perhaps the fastcgi process died): pid: 0 socket: unix:/run/php-fpm/php-fpm.sock
2016-03-07 22:23:32: (mod_fastcgi.c.3171) response not received, request sent: 1084 on socket: unix:/run/php-fpm/php-fpm.sock for /~rost/lp/web/www/index.php?action=out&presenter=Sign, closing connection
Also, try to delete Nette cache first.
I've finally found a solution. It is probably Nette / Cassandra related problem.
The error was appearing because of object Nette\Security\Identity, after I assigned user data into it:
public function authenticate(array $credentials) {
// Retrieve username and password
list($email, $passwd) = $credentials;
// Select user with given email from database
$usr = $this->daoManager->getDao("AppUser")->loadByEmail($email);
if ($usr == null || count($usr) == 0) {
$msg = 'The email is incorrect.';
$arg = self::IDENTITY_NOT_FOUND;
throw new Nette\Security\AuthenticationException($msg, $arg);
}
// TODO Check user password
// TODO Check verification
// Create identity - THE PROBLEM WAS HERE
return new Identity($email, $usr['role'], $usr);
}
It was caused by value 'registered' in $usr array which was of type Cassandra\Timestamp. Since then almost every redirect crashed with above mentioned error.
Following code fixed the issue:
return new Identity($email, $usr['role'], $this->fixUserArray($usr));
Where:
protected function fixUserArray(array $user) {
$result = array();
foreach ($user as $key => $val) {
if ($key === "registered") {
$result[$key] = $val->time();
} else {
$result[$key] = $val;
}
}
return $result;
}

listen to serial port on ubuntu with php

i need to get some data like live temperature and wind speed from a device that connected to my ubuntu server serial port
also i need to send some command to device too
,for this i found a script like this:
<?php
$portName = 'com9:';
$baudRate = 9600;
$bits = 8;
$spotBit = 1;
header( 'Content-type: text/plain; charset=utf-8' );
?>
Serial Port Test
================
<?php
function echoFlush($string){
echo $string . "\n";
flush();
ob_flush();
}
if(!extension_loaded('dio')){
echoFlush( "PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php" );
exit;
}
try{
//the serial port resource
$bbSerialPort;
echoFlush( "Connecting to serial port: {$portName}" );
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
$bbSerialPort = dio_open($portName, O_RDWR );
//we're on windows configure com port from command line
exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on");
}else{
$bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
//we're on 'nix configure com from php direct io function
dio_tcsetattr($bbSerialPort, array(
'baud' => $baudRate,
'bits' => $bits,
'stop' => $spotBit,
'parity' => 0
));
}
if(!$bbSerialPort){
echoFlush( "Could not open Serial port {$portName} ");
exit;
}
// send data
$dataToSend = "HELLO WORLD!";
echoFlush( "Writing to serial port data: \"{$dataToSend}\"" );
$bytesSent = dio_write($bbSerialPort, $dataToSend );
echoFlush( "Sent: {$bytesSent} bytes" );
//date_default_timezone_set ("Europe/London");
$runForSeconds = new DateInterval("PT10S"); //10 seconds
$endTime = (new DateTime())->add($runForSeconds);
echoFlush( "Waiting for {$runForSeconds->format('%S')} seconds to recieve data on serial port" );
while (new DateTime() < $endTime) {
$data = dio_read($bbSerialPort, 256); //this is a blocking call
if ($data){
echoFlush( "Data Recieved: ". $data );
}
}
echoFlush( "Closing Port" );
dio_close($bbSerialPort);
}
catch (Exception $e){
echoFlush( $e->getMessage() );
exit(1);
}
?>
this will send a command to serial port and immanently read any update from port
but i need to listen to port any time
i mean each second i will receive any update from device and i must possess it and i cant wait for next update cycle
is there any way for real time port listening or i must write a wrapper with c o python for my Apache web-server?
is there any way for real time port listening or i must write a wrapper with c o python for my Apache web-server?
actually yes,its better to write a wrapper with c or python for jobs like this.
python have good tools for handle exceptions that needed for listening a port
see the link for complete information:
Full examples of using pySerial package

How can I ping a server port with PHP?

I want a PHP script which allows you to ping an IP address and a port number (ip:port). I found a similar script but it works only for websites, not ip:port.
<?php
function ping($host, $port, $timeout)
{
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "down"; }
$tA = microtime(true);
return round((($tA - $tB) * 1000), 0)." ms";
}
//Echoing it will display the ping if the host is up, if not it'll say "down".
echo ping("www.google.com", 80, 10);
?>
I want this for a game server.
The idea is that I can type in the IP address and port number, and I get the ping response.
I think the answer to this question pretty much sums up the problem with your question.
If what you want to do is find out whether a given host will accept
TCP connections on port 80, you can do this:
$host = '193.33.186.70';
$port = 80;
$waitTimeoutInSeconds = 1;
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
// It worked
} else {
// It didn't work
}
fclose($fp);
For anything other than TCP it will be more difficult (although since
you specify 80, I guess you are looking for an active HTTP server, so
TCP is what you want). TCP is sequenced and acknowledged, so you will
implicitly receive a returned packet when a connection is successfully
made. Most other transport protocols (commonly UDP, but others as
well) do not behave in this manner, and datagrams will not be
acknowledged unless the overlayed Application Layer protocol
implements it.
The fact that you are asking this question in this manner tells me you
have a fundamental gap in your knowledge on Transport Layer protocols.
You should read up on ICMP and TCP, as well as the OSI Model.
Also, here's a slightly cleaner version to ping to hosts.
// Function to check response time
function pingDomain($domain){
$starttime = microtime(true);
$file = fsockopen ($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file) $status = -1; // Site is down
else {
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
In case the OP really wanted an ICMP-Ping, there are some proposals within the User Contributed Notes to socket_create() [link], which use raw sockets. Be aware that on UNIX like systems root access is required.
Update: note that the usec argument has no function on windows. Minimum timeout is 1 second.
In any case, this is the code of the top voted ping function:
function ping($host, $timeout = 1) {
/* ICMP ping packet with a pre-calculated checksum */
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $host, null);
$ts = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
$result = microtime(true) - $ts;
} else {
$result = false;
}
socket_close($socket);
return $result;
}
Test different ports:
$wait = 1; // wait Timeout In Seconds
$host = 'example.com';
$ports = [
'http' => 80,
'https' => 443,
'ftp' => 21,
];
foreach ($ports as $key => $port) {
$fp = #fsockopen($host, $port, $errCode, $errStr, $wait);
echo "Ping $host:$port ($key) ==> ";
if ($fp) {
echo 'SUCCESS';
fclose($fp);
} else {
echo "ERROR: $errCode - $errStr";
}
echo PHP_EOL;
}
// Ping example.com:80 (http) ==> SUCCESS
// Ping example.com:443 (https) ==> SUCCESS
// Ping example.com:21 (ftp) ==> ERROR: 110 - Connection timed out
Try this :
echo exec('ping -n 1 -w 1 72.10.169.28');
function ping($ip){
$output = shell_exec("ping $ip");
var_dump($output);
}
ping('127.0.0.1');
UPDATE:
If you pass an hardcoded IP (like in this example and most of the real-case scenarios), this function can be enough.
But since some users seem to be very concerned about safety, please remind to never pass user generated inputs to the shell_exec function:
If the IP comes from an untrusted source, at least check it with a filter before using it.
You can use exec function
exec("ping ".$ip);
here an example
You don't need any exec or shell_exec hacks to do that, it is possible to do it in PHP. The book 'You want to do WHAT with PHP?' by Kevin Schroeder, show's how.
It uses sockets and the pack() function which lets you read and write binary protocols. What you need to do is to create an ICMP packet, which you can do by using the 'CCnnnA*' format to create your packet.
socket_create needs to be run as root on a UNIX system with;
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
If you want to send ICMP packets in php you can take a look at this Native-PHP ICMP ping implementation, but I didn't test it.
EDIT:
Maybe the site was hacked because it seems that the files got deleted, there is copy in archive.org but you can't download the tar ball file, there are no contact email only contact form, but this will not work at archive.org, we can only wait until the owner will notice that sit is down.

Categories