Commit 1eba7b42 authored by Joel Martin's avatar Joel Martin

Update C proxy to WS protocol version 76.

Pull in LGPL md5.c and md5.h files (written by Ulrich Drepper).

Now both python and C version of the proxy support both protocol 75
and protocol 76 (hybi 00).

Reorganize websocket.py slightly to match websocket.c.
parent 5235b29d
...@@ -4,7 +4,7 @@ Short Term: ...@@ -4,7 +4,7 @@ Short Term:
- Support Opera 10.60 (WebSocket frames dropped). - Support Opera 10.60 (WebSocket frames dropped).
- Possibly support IE <= 8.0 using excanvas of fxcanvas: - Possibly support IE <= 8.0 using excanvas or fxcanvas:
http://excanvas.sourceforge.net/ http://excanvas.sourceforge.net/
http://code.google.com/p/fxcanvas/ http://code.google.com/p/fxcanvas/
......
wsproxy: wsproxy.o websocket.o wsproxy: wsproxy.o websocket.o md5.o
$(CC) $^ -l ssl -l resolv -o $@ $(CC) $^ -l ssl -l resolv -o $@
websocket.o wsproxy.o: websocket.h websocket.o: websocket.c websocket.h md5.h
wsproxy.o: wsproxy.c websocket.h
md5.o: md5.c md5.h
$(CC) -c -o $@ $*.c -DHAVE_MEMCPY -DSTDC_HEADERS
clean: clean:
rm -f wsproxy wsproxy.o websocket.o rm -f wsproxy wsproxy.o websocket.o
......
This diff is collapsed.
/* Declaration of functions and data types used for MD5 sum computing
library functions.
Copyright (C) 1995-1997,1999,2000,2001,2004,2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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 GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#ifndef _MD5_H
#define _MD5_H 1
#include <stdio.h>
#if defined HAVE_LIMITS_H || _LIBC
# include <limits.h>
#endif
#define MD5_DIGEST_SIZE 16
#define MD5_BLOCK_SIZE 64
/* The following contortions are an attempt to use the C preprocessor
to determine an unsigned integral type that is 32 bits wide. An
alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
doing that would require that the configure script compile and *run*
the resulting executable. Locally running cross-compiled executables
is usually not possible. */
#ifdef _LIBC
# include <stdint.h>
typedef uint32_t md5_uint32;
typedef uintptr_t md5_uintptr;
#else
# if defined __STDC__ && __STDC__
# define UINT_MAX_32_BITS 4294967295U
# else
# define UINT_MAX_32_BITS 0xFFFFFFFF
# endif
/* If UINT_MAX isn't defined, assume it's a 32-bit type.
This should be valid for all systems GNU cares about because
that doesn't include 16-bit systems, and only modern systems
(that certainly have <limits.h>) have 64+-bit integral types. */
# ifndef UINT_MAX
# define UINT_MAX UINT_MAX_32_BITS
# endif
# if UINT_MAX == UINT_MAX_32_BITS
typedef unsigned int md5_uint32;
# else
# if USHRT_MAX == UINT_MAX_32_BITS
typedef unsigned short md5_uint32;
# else
# if ULONG_MAX == UINT_MAX_32_BITS
typedef unsigned long md5_uint32;
# else
/* The following line is intended to evoke an error.
Using #error is not portable enough. */
"Cannot determine unsigned 32-bit data type."
# endif
# endif
# endif
/* We have to make a guess about the integer type equivalent in size
to pointers which should always be correct. */
typedef unsigned long int md5_uintptr;
#endif
/* Structure to save state of computation between the single steps. */
struct md5_ctx
{
md5_uint32 A;
md5_uint32 B;
md5_uint32 C;
md5_uint32 D;
md5_uint32 total[2];
md5_uint32 buflen;
char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
};
/*
* The following three functions are build up the low level used in
* the functions `md5_stream' and `md5_buffer'.
*/
/* Initialize structure containing state of computation.
(RFC 1321, 3.3: Step 3) */
extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is necessary that LEN is a multiple of 64!!! */
extern void __md5_process_block (const void *buffer, size_t len,
struct md5_ctx *ctx) __THROW;
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is NOT required that LEN is a multiple of 64. */
extern void __md5_process_bytes (const void *buffer, size_t len,
struct md5_ctx *ctx) __THROW;
/* Process the remaining bytes in the buffer and put result from CTX
in first 16 bytes following RESBUF. The result is always in little
endian byte order, so that a byte-wise output yields to the wanted
ASCII representation of the message digest.
IMPORTANT: On some systems it is required that RESBUF is correctly
aligned for a 32 bits value. */
extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
/* Put result from CTX in first 16 bytes following RESBUF. The result is
always in little endian byte order, so that a byte-wise output yields
to the wanted ASCII representation of the message digest.
IMPORTANT: On some systems it is required that RESBUF is correctly
aligned for a 32 bits value. */
extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
/* Compute MD5 message digest for bytes read from STREAM. The
resulting message digest number will be written into the 16 bytes
beginning at RESBLOCK. */
extern int __md5_stream (FILE *stream, void *resblock) __THROW;
/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
result is always in little endian byte order, so that a byte-wise
output yields to the wanted ASCII representation of the message
digest. */
extern void *__md5_buffer (const char *buffer, size_t len,
void *resblock) __THROW;
#endif /* md5.h */
int main () {
printf("hello world\n");
}
...@@ -24,10 +24,10 @@ ...@@ -24,10 +24,10 @@
const char server_handshake[] = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n\ const char server_handshake[] = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n\
Upgrade: WebSocket\r\n\ Upgrade: WebSocket\r\n\
Connection: Upgrade\r\n\ Connection: Upgrade\r\n\
WebSocket-Origin: %s\r\n\ %sWebSocket-Origin: %s\r\n\
WebSocket-Location: %s://%s%s\r\n\ %sWebSocket-Location: %s://%s%s\r\n\
WebSocket-Protocol: sample\r\n\ %sWebSocket-Protocol: sample\r\n\
\r\n"; \r\n%s";
const char policy_response[] = "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\n"; const char policy_response[] = "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\n";
...@@ -272,11 +272,113 @@ int decode(char *src, size_t srclength, u_char *target, size_t targsize) { ...@@ -272,11 +272,113 @@ int decode(char *src, size_t srclength, u_char *target, size_t targsize) {
return retlen; return retlen;
} }
int parse_handshake(char *handshake, headers_t *headers) {
char *start, *end;
if ((strlen(handshake) < 92) || (bcmp(handshake, "GET ", 4) != 0)) {
return 0;
}
start = handshake+4;
end = strstr(start, " HTTP/1.1");
if (!end) { return 0; }
strncpy(headers->path, start, end-start);
headers->path[end-start] = '\0';
start = strstr(handshake, "\r\nHost: ");
if (!start) { return 0; }
start += 8;
end = strstr(start, "\r\n");
strncpy(headers->host, start, end-start);
headers->host[end-start] = '\0';
start = strstr(handshake, "\r\nOrigin: ");
if (!start) { return 0; }
start += 10;
end = strstr(start, "\r\n");
strncpy(headers->origin, start, end-start);
headers->origin[end-start] = '\0';
start = strstr(handshake, "\r\n\r\n");
if (!start) { return 0; }
start += 4;
if (strlen(start) == 8) {
strncpy(headers->key3, start, 8);
headers->key3[8] = '\0';
start = strstr(handshake, "\r\nSec-WebSocket-Key1: ");
if (!start) { return 0; }
start += 22;
end = strstr(start, "\r\n");
strncpy(headers->key1, start, end-start);
headers->key1[end-start] = '\0';
start = strstr(handshake, "\r\nSec-WebSocket-Key2: ");
if (!start) { return 0; }
start += 22;
end = strstr(start, "\r\n");
strncpy(headers->key2, start, end-start);
headers->key2[end-start] = '\0';
} else {
headers->key1[0] = '\0';
headers->key2[0] = '\0';
headers->key3[0] = '\0';
}
return 1;
}
int gen_md5(headers_t *headers, char *target) {
unsigned int i, spaces1 = 0, spaces2 = 0;
unsigned long num1 = 0, num2 = 0;
unsigned char buf[17];
for (i=0; i < strlen(headers->key1); i++) {
if (headers->key1[i] == ' ') {
spaces1 += 1;
}
if ((headers->key1[i] >= 48) && (headers->key1[i] <= 57)) {
num1 = num1 * 10 + (headers->key1[i] - 48);
}
}
num1 = num1 / spaces1;
for (i=0; i < strlen(headers->key2); i++) {
if (headers->key2[i] == ' ') {
spaces2 += 1;
}
if ((headers->key2[i] >= 48) && (headers->key2[i] <= 57)) {
num2 = num2 * 10 + (headers->key2[i] - 48);
}
}
num2 = num2 / spaces2;
/* Pack it big-endian */
buf[0] = (num1 & 0xff000000) >> 24;
buf[1] = (num1 & 0xff0000) >> 16;
buf[2] = (num1 & 0xff00) >> 8;
buf[3] = num1 & 0xff;
buf[4] = (num2 & 0xff000000) >> 24;
buf[5] = (num2 & 0xff0000) >> 16;
buf[6] = (num2 & 0xff00) >> 8;
buf[7] = num2 & 0xff;
strncpy(buf+8, headers->key3, 8);
buf[16] = '\0';
md5_buffer(buf, 16, target);
target[16] = '\0';
return 1;
}
ws_ctx_t *do_handshake(int sock) { ws_ctx_t *do_handshake(int sock) {
char handshake[4096], response[4096]; char handshake[4096], response[4096], trailer[17];
char *scheme, *line, *path, *host, *origin; char *scheme, *pre;
headers_t headers;
char *args_start, *args_end, *arg_idx; char *args_start, *args_end, *arg_idx;
int len; int len, ret;
ws_ctx_t * ws_ctx; ws_ctx_t * ws_ctx;
// Reset settings // Reset settings
...@@ -287,7 +389,11 @@ ws_ctx_t *do_handshake(int sock) { ...@@ -287,7 +389,11 @@ ws_ctx_t *do_handshake(int sock) {
// Peek, but don't read the data // Peek, but don't read the data
len = recv(sock, handshake, 1024, MSG_PEEK); len = recv(sock, handshake, 1024, MSG_PEEK);
handshake[len] = 0; handshake[len] = 0;
if (bcmp(handshake, "<policy-file-request/>", 22) == 0) { if (len == 0) {
printf("Ignoring empty handshake\n");
close(sock);
return NULL;
} else if (bcmp(handshake, "<policy-file-request/>", 22) == 0) {
len = recv(sock, handshake, 1024, 0); len = recv(sock, handshake, 1024, 0);
handshake[len] = 0; handshake[len] = 0;
printf("Sending flash policy response\n"); printf("Sending flash policy response\n");
...@@ -313,27 +419,25 @@ ws_ctx_t *do_handshake(int sock) { ...@@ -313,27 +419,25 @@ ws_ctx_t *do_handshake(int sock) {
} }
len = ws_recv(ws_ctx, handshake, 4096); len = ws_recv(ws_ctx, handshake, 4096);
handshake[len] = 0; handshake[len] = 0;
//printf("handshake: %s\n", handshake);
if ((len < 92) || (bcmp(handshake, "GET ", 4) != 0)) { if (!parse_handshake(handshake, &headers)) {
fprintf(stderr, "Invalid WS request\n"); fprintf(stderr, "Invalid WS request\n");
close(sock);
return NULL; return NULL;
} }
strtok(handshake, " "); // Skip "GET "
path = strtok(NULL, " "); // Extract path
strtok(NULL, "\n"); // Skip to Upgrade line
strtok(NULL, "\n"); // Skip to Connection line
strtok(NULL, "\n"); // Skip to Host line
strtok(NULL, " "); // Skip "Host: "
host = strtok(NULL, "\r"); // Extract host
strtok(NULL, " "); // Skip "Origin: "
origin = strtok(NULL, "\r"); // Extract origin
//printf("path: %s\n", path); if (headers.key3[0] != '\0') {
//printf("host: %s\n", host); gen_md5(&headers, trailer);
//printf("origin: %s\n", origin); pre = "Sec-";
printf(" using protocol version 76\n");
} else {
trailer[0] = '\0';
pre = "";
printf(" using protocol version 75\n");
}
// Parse client settings from the GET path // Parse client settings from the GET path
args_start = strstr(path, "?"); args_start = strstr(headers.path, "?");
if (args_start) { if (args_start) {
if (strstr(args_start, "#")) { if (strstr(args_start, "#")) {
args_end = strstr(args_start, "#"); args_end = strstr(args_start, "#");
...@@ -352,7 +456,8 @@ ws_ctx_t *do_handshake(int sock) { ...@@ -352,7 +456,8 @@ ws_ctx_t *do_handshake(int sock) {
} }
} }
sprintf(response, server_handshake, origin, scheme, host, path); sprintf(response, server_handshake, pre, headers.origin, pre, scheme,
headers.host, headers.path, pre, trailer);
//printf("response: %s\n", response); //printf("response: %s\n", response);
ws_send(ws_ctx, response, strlen(response)); ws_send(ws_ctx, response, strlen(response));
......
...@@ -22,6 +22,15 @@ typedef struct { ...@@ -22,6 +22,15 @@ typedef struct {
int seq_num; int seq_num;
} client_settings_t; } client_settings_t;
typedef struct {
char path[1024+1];
char host[1024+1];
char origin[1024+1];
char key1[1024+1];
char key2[1024+1];
char key3[8+1];
} headers_t;
ssize_t ws_recv(ws_ctx_t *ctx, void *buf, size_t len); ssize_t ws_recv(ws_ctx_t *ctx, void *buf, size_t len);
......
...@@ -43,6 +43,20 @@ def traffic(token="."): ...@@ -43,6 +43,20 @@ def traffic(token="."):
sys.stdout.write(token) sys.stdout.write(token)
sys.stdout.flush() sys.stdout.flush()
def encode(buf):
global send_seq
if client_settings['b64encode']:
buf = b64encode(buf)
else:
# Modified UTF-8 encode
buf = buf.decode('latin-1').encode('utf-8').replace("\x00", "\xc4\x80")
if client_settings['seq_num']:
send_seq += 1
return "\x00%d:%s\xff" % (send_seq-1, buf)
else:
return "\x00%s\xff" % buf
def decode(buf): def decode(buf):
""" Parse out WebSocket packets. """ """ Parse out WebSocket packets. """
if buf.count('\xff') > 1: if buf.count('\xff') > 1:
...@@ -57,19 +71,32 @@ def decode(buf): ...@@ -57,19 +71,32 @@ def decode(buf):
else: else:
return [buf[1:-1].replace("\xc4\x80", "\x00").decode('utf-8').encode('latin-1')] return [buf[1:-1].replace("\xc4\x80", "\x00").decode('utf-8').encode('latin-1')]
def encode(buf): def parse_handshake(handshake):
global send_seq ret = {}
if client_settings['b64encode']: req_lines = handshake.split("\r\n")
buf = b64encode(buf) if not req_lines[0].startswith("GET "):
else: raise Exception("Invalid handshake: no GET request line")
# Modified UTF-8 encode ret['path'] = req_lines[0].split(" ")[1]
buf = buf.decode('latin-1').encode('utf-8').replace("\x00", "\xc4\x80") for line in req_lines[1:]:
if line == "": break
var, delim, val = line.partition(": ")
ret[var] = val
if client_settings['seq_num']: if req_lines[-2] == "":
send_seq += 1 ret['key3'] = req_lines[-1]
return "\x00%d:%s\xff" % (send_seq-1, buf)
else: return ret
return "\x00%s\xff" % buf
def gen_md5(keys):
key1 = keys['Sec-WebSocket-Key1']
key2 = keys['Sec-WebSocket-Key2']
key3 = keys['key3']
spaces1 = key1.count(" ")
spaces2 = key2.count(" ")
num1 = int("".join([c for c in key1 if c.isdigit()])) / spaces1
num2 = int("".join([c for c in key2 if c.isdigit()])) / spaces2
return md5(struct.pack('>II8s', num1, num2, key3)).digest()
def do_handshake(sock): def do_handshake(sock):
...@@ -82,7 +109,11 @@ def do_handshake(sock): ...@@ -82,7 +109,11 @@ def do_handshake(sock):
# Peek, but don't read the data # Peek, but don't read the data
handshake = sock.recv(1024, socket.MSG_PEEK) handshake = sock.recv(1024, socket.MSG_PEEK)
#print "Handshake [%s]" % repr(handshake) #print "Handshake [%s]" % repr(handshake)
if handshake.startswith("<policy-file-request/>"): if handshake == "":
print "Ignoring empty handshake"
sock.close()
return False
elif handshake.startswith("<policy-file-request/>"):
handshake = sock.recv(1024) handshake = sock.recv(1024)
print "Sending flash policy response" print "Sending flash policy response"
sock.send(policy_response) sock.send(policy_response)
...@@ -120,9 +151,11 @@ def do_handshake(sock): ...@@ -120,9 +151,11 @@ def do_handshake(sock):
if h.get('key3'): if h.get('key3'):
trailer = gen_md5(h) trailer = gen_md5(h)
pre = "Sec-" pre = "Sec-"
print " using protocol version 76"
else: else:
trailer = "" trailer = ""
pre = "" pre = ""
print " using protocol version 75"
response = server_handshake % (pre, h['Origin'], pre, scheme, response = server_handshake % (pre, h['Origin'], pre, scheme,
h['Host'], h['path'], pre, trailer) h['Host'], h['path'], pre, trailer)
...@@ -131,33 +164,6 @@ def do_handshake(sock): ...@@ -131,33 +164,6 @@ def do_handshake(sock):
retsock.send(response) retsock.send(response)
return retsock return retsock
def parse_handshake(handshake):
ret = {}
req_lines = handshake.split("\r\n")
if not req_lines[0].startswith("GET "):
raise "Invalid handshake: no GET request line"
ret['path'] = req_lines[0].split(" ")[1]
for line in req_lines[1:]:
if line == "": break
var, delim, val = line.partition(": ")
ret[var] = val
if req_lines[-2] == "":
ret['key3'] = req_lines[-1]
return ret
def gen_md5(keys):
key1 = keys['Sec-WebSocket-Key1']
key2 = keys['Sec-WebSocket-Key2']
key3 = keys['key3']
spaces1 = key1.count(" ")
spaces2 = key2.count(" ")
num1 = int("".join([c for c in key1 if c.isdigit()])) / spaces1
num2 = int("".join([c for c in key2 if c.isdigit()])) / spaces2
return md5(struct.pack('>II8s', num1, num2, key3)).digest()
def daemonize(): def daemonize():
os.umask(0) os.umask(0)
os.chdir('/') os.chdir('/')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment