forked from pagekite/libpagekite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpkite.c
128 lines (103 loc) · 3.42 KB
/
httpkite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/******************************************************************************
httpkite.c - A trivial (example) PageKite HTTP server.
Usage: httpkite NAME.pagekite.me SECRET
*******************************************************************************
This file is Copyright 2011-2013, The Beanstalks Project ehf.
This program is free software: you can redistribute it and/or modify it under
the terms of the Apache License 2.0 as published by the Apache Software
Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the Apache License for more details.
You should have received a copy of the Apache License along with this program.
If not, see: <http://www.apache.org/licenses/>
Note: For alternate license terms, see the file COPYING.md.
******************************************************************************/
#include "common.h"
#include "pkstate.h"
#include "pkerror.h"
#include "pkconn.h"
#include "pkproto.h"
#include "pkblocker.h"
#include "pkmanager.h"
#include "pklogging.h"
#include "utils.h"
void usage(void) {
printf("Usage: httpkite your.kitename.com SECRET\n\n");
printf("Note: DNS needs to already be configured for the kite name, and\n");
printf(" a running front-end on the IP address it points to. This\n");
printf(" is easiest to do by using the pagekite.net service and\n");
printf(" creating the kite first using pagekite.py.\n");
}
void handle_request(void* data, struct pk_chunk *chunk) {
char buffer[4096];
char *hi = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello World\n";
struct pk_conn* pkc = data;
int bytes;
pk_log_chunk(chunk);
if (chunk->ping) {
bytes = pk_format_pong(buffer);
pkc_write(pkc, buffer, bytes);
}
else if (chunk->sid) {
if (chunk->eof) {
/* Ignored, for now */
}
else if (!chunk->noop) {
/* Send a reply, and close this channel right away */
bytes = pk_format_reply(buffer, chunk->sid, strlen(hi), hi);
pkc_write(pkc, buffer, bytes);
bytes = pk_format_eof(buffer, chunk->sid, PK_EOF);
pkc_write(pkc, buffer, bytes);
}
}
else {
/* Weirdness ... */
}
}
int main(int argc, char **argv) {
char pbuffer[64000];
struct pk_conn pkc;
struct pk_parser* pkp;
struct pk_pagekite kite;
struct pk_kite_request kite_r;
struct pk_kite_request* kite_rp;
SSL_CTX* ctx;
if (argc < 3) {
usage();
exit(1);
}
pks_global_init(PK_LOG_ALL);
pk_state.log_file = NULL;
PKS_SSL_INIT(ctx);
kite_r.kite = &kite;
strcpy(kite.protocol, "http");
strncpyz(kite.public_domain, argv[1], PK_DOMAIN_LENGTH);
kite.public_port = 0;
strncpyz(kite.auth_secret, argv[2], PK_SECRET_LENGTH);
kite_r.bsalt[0] = '\0';
kite_r.fsalt[0] = '\0';
kite_rp = &kite_r;
srand(time(0) ^ getpid());
if (0 > pk_connect(&pkc, argv[1], 443, 1, &kite_r, NULL, ctx)) {
pk_perror(argv[1]);
usage();
return 1;
}
pkp = pk_parser_init(sizeof(pbuffer), pbuffer, &handle_request, &pkc);
if (NULL == pkp) {
pk_perror(argv[1]);
usage();
return 1;
}
fprintf(stderr, "*** Connected! ***\n");
while (pkc_wait(&pkc, -1)) {
pkc_read(&pkc);
pk_parser_parse(pkp, pkc.in_buffer_pos, (char *) pkc.in_buffer);
pkc.in_buffer_pos = 0;
}
pkc_reset_conn(&pkc, 0);
/* -Wall dislikes unused variables */
kite_rp++;
return 0;
}