gpt4 book ai didi

c - 如何通过C接收webhook

转载 作者:行者123 更新时间:2023-11-30 16:19:50 24 4
gpt4 key购买 nike

我想通过我的C程序接收kazoo的webhook(HTTP POST)。我认为它提高了我的知识,而且很有趣(我想早些时候)。

我 try catch HTTP POST 请求,但听不到任何声音。我的做法和理解有误吗?

我可以将 Webhook 定位到它可以执行的二进制文件/脚本,然后生成事件。

例如(“http://server/c/hookreceiver.bin ”)和完整示例

curl -X PUT -H "X-Auth-Token: $AUTH_TOKEN" -d '{"data":{"name": "hookreceiver.bin", "uri": "http://server/c/hookreceiver.bin","http_verb": "post","hook": "channel_create","retries":4}}'

Apache 日志:

"POST /c/hookreceiver.bin HTTP/1.1" 500 527 "-" "-"

如果我设置了 webhook 我的脚本 (webhookExample.php),我可以通过 php 捕获此 POST

但是如果我尝试捕捉 C 的这个帖子,我什么也没有:(

为什么我无法通过 c 接收任何内容,而使用 php 可以接收任何内容?

很抱歉提出奇怪的问题感谢您抽出时间

附注我如何在这种模式下通过 gdb 或其他方式调试我的程序,然后我的二进制文件调用其他应用程序?

webhookExample.php

<?php
$data = file_get_contents("php://input");
$my_file = 'MYfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $data);
fclose($handle);
?>

file hookreceiver.bin
int main(int argc,char *argv[])
{
int portno = 80;
char *host = "SERVER_NAME";

struct hostent *server;
struct sockaddr_in serv_addr;
int sockfd, bytes, received, total;
char response[4096];

FILE *fp;
char name1[] = "./MY_FLAG.txt";
char name[100];
sprintf(name, "%s_%d",name1, getpid() );
if ((fp = fopen(name, "w")) == NULL) {
// Error open
return 0;
}

// create the socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) fprintf(fp, "ERROR opening socket");
else fprintf(fp, "sock ok\n");

// lookup the ip address
server = gethostbyname(host);
if (server == NULL) fprintf(fp, "ERROR, no such host");

// fill in the structure
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);

// connect the socket
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
fprintf(fp, "ERROR connecting");
else
fprintf(fp, "ok - conn |%d|\n", sockfd );
fflush(fp);

bytes = read(sockfd,response+received,4096,NULL, MSG_NOSIGNAL);
if (bytes < 0)
fprintf(fp, "ERROR reading response from socket\n");
else
fprintf(fp, "ok - bytes > 0 %d |%s|\n",bytes, response);
// close the socket
close(sockfd);
fprintf(fp, "ok - END\n");
fclose(fp);
return 0;
}

php 脚本的结果

cat MYfile.txt

account_id=24..7&owner_id=3e03&request=udev2Cnsl%40server.local&reseller_id=2c7&timestamp=63720841342&...

C 程序的结果

cat MY_FLAG.txt_27968 
sock ok
ok - conn |4|
ok - prepare for receive
ok - bytes > 0 0 ||
ok - received == total
ok - END

最佳答案

答案极其便宜。

服务器只是将文本写入标准输入。

为了处理 webhook 服务器,请执行 HTTP POST 请求“POST/c/hookreceiver.bin HTTP/1.1”

并调用二进制hookreceiver.bin

int main(int argc,char *argv[])
{
FILE *fp;
char name1[] = "./CATCHED_HOOK.txt";
char buf[1];

sprintf(name, "%s_%d",name1, getpid() );

if ((fp = fopen(name, "w")) == NULL) {
printf("File not opened\n");
return 0;
}

while(read(0,buf, sizeof(mbuf))>0) {
fprintf(fp,buf);
}
fclose(fp);
return 0;
}

猫CATCHED_HOOK.txt_1212232

account_id=2424c7&authorizing_id=0b3316d6&authorizing_type=device&call_direction=inbound&call_id=1169696&caller_id_name=udev1Cnsl&caller_id_number=udev1Cnsl&&&emergency_resource_used=false&from=udev1Cnsl40ce.local&local_resource_used=true&is_ Internal_leg=false&owner_id=866ae5&request=300240ce.local&reseller_id=244e36be95324c7&timestamp=63722050107&to=300240ce.local&hook_event=channel_createok - END

关于c - 如何通过C接收webhook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55481673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com