#include <eggdrop/eggdrop.h>
#include <errno.h>
#include <netinet/in.h>
#include "proxy.h"
Go to the source code of this file.
Data Structures | |
struct | proxy_info_t |
Functions | |
static int | http_on_connect (void *client_data, int idx, const char *peer_ip, int peer_port) |
static int | http_on_eof (void *client_data, int idx, int err, const char *errmsg) |
static int | http_on_read (void *client_data, int idx, char *data, int len) |
static int | http_on_delete (event_owner_t *owner, void *client_data) |
static int | http_on_their_delete (void *client_data, int idx) |
static void | http_err (proxy_info_t *info, int err, const char *errmsg) |
static char * | make_password (proxy_info_t *info) |
int | http_reconnect (int idx, const char *host, int port) |
Variables | |
event_owner_t | http_owner |
static sockbuf_handler_t | http_events |
static sockbuf_filter_t | delete_listener |
static void http_err | ( | proxy_info_t * | info, | |
int | err, | |||
const char * | errmsg | |||
) | [static] |
Definition at line 42 of file http.c.
References proxy_info_t::our_idx, sockbuf_delete(), SOCKBUF_LEVEL_INTERNAL, sockbuf_on_eof(), and proxy_info_t::their_idx.
Referenced by http_on_eof(), and http_on_read().
00043 { 00044 int idx = info->their_idx; 00045 errno = err; 00046 sockbuf_delete(info->our_idx); 00047 sockbuf_on_eof(idx, SOCKBUF_LEVEL_INTERNAL, err, errmsg); 00048 }
static int http_on_connect | ( | void * | client_data, | |
int | idx, | |||
const char * | peer_ip, | |||
int | peer_port | |||
) | [static] |
Definition at line 121 of file http.c.
References egg_iprintf(), proxy_info_t::host, proxy_info_t::http_host, make_password(), proxy_info_t::password, proxy_info_t::port, and proxy_info_t::username.
00122 { 00123 proxy_info_t *info = client_data; 00124 00125 egg_iprintf(idx, "CONNECT %s:%d HTTP/1.0\r\n", info->host, info->port); 00126 egg_iprintf(idx, "Host: %s\r\n", info->http_host); 00127 if (info->username && info->password && strlen(info->username)) { 00128 char *auth; 00129 00130 auth = make_password(info); 00131 egg_iprintf(idx, "Proxy-authenticate: basic %s\r\n", auth); 00132 free(auth); 00133 } 00134 egg_iprintf(idx, "\r\n"); 00135 00136 return(0); 00137 }
static int http_on_delete | ( | event_owner_t * | owner, | |
void * | client_data | |||
) | [static] |
Definition at line 50 of file http.c.
References proxy_info_t::host, proxy_info_t::password, sockbuf_detach_filter(), proxy_info_t::their_idx, and proxy_info_t::username.
00051 { 00052 proxy_info_t *info = client_data; 00053 sockbuf_detach_filter(info->their_idx, &delete_listener, info); 00054 if (info->host) free(info->host); 00055 if (info->username) free(info->username); 00056 if (info->password) free(info->password); 00057 free(info); 00058 return(0); 00059 }
static int http_on_eof | ( | void * | client_data, | |
int | idx, | |||
int | err, | |||
const char * | errmsg | |||
) | [static] |
Definition at line 97 of file http.c.
References http_err().
00098 { 00099 proxy_info_t *info = client_data; 00100 00101 if (!err) err = ECONNREFUSED; 00102 http_err(info, err, "Unexpected EOF from HTTP server"); 00103 return(0); 00104 }
static int http_on_read | ( | void * | client_data, | |
int | idx, | |||
char * | data, | |||
int | len | |||
) | [static] |
Definition at line 61 of file http.c.
References proxy_info_t::host, http_err(), proxy_info_t::our_idx, proxy_info_t::port, sockbuf_delete(), sockbuf_detach_filter(), sockbuf_get_sock(), SOCKBUF_LEVEL_INTERNAL, sockbuf_on_connect(), sockbuf_set_sock(), proxy_info_t::status, and proxy_info_t::their_idx.
00062 { 00063 proxy_info_t *info = client_data; 00064 int sock; 00065 00066 if (!strncasecmp("HTTP/1.0 200", data, 12)) { 00067 /* Yay, we're approved. */ 00068 info->status = 1; 00069 return(0); 00070 } 00071 00072 if (len != 0) { 00073 /* Just some header that we don't care about. */ 00074 return(0); 00075 } 00076 00077 /* It's the blank line, so the http response is done. */ 00078 00079 if (info->status != 1) { 00080 /* Boo, we weren't approved. */ 00081 http_err(info, ECONNREFUSED, "HTTP server refused to relay connection"); 00082 return(0); 00083 } 00084 00085 /* Successful, switch it over. */ 00086 sockbuf_detach_filter(info->their_idx, &delete_listener, info); 00087 sock = sockbuf_get_sock(info->our_idx); 00088 sockbuf_set_sock(info->our_idx, -1, 0); 00089 sockbuf_set_sock(info->their_idx, sock, 0); 00090 sockbuf_on_connect(info->their_idx, SOCKBUF_LEVEL_INTERNAL, info->host, info->port); 00091 info->their_idx = -1; 00092 sockbuf_delete(info->our_idx); 00093 00094 return(0); 00095 }
static int http_on_their_delete | ( | void * | client_data, | |
int | idx | |||
) | [static] |
Definition at line 140 of file http.c.
References proxy_info_t::our_idx, and sockbuf_delete().
00141 { 00142 proxy_info_t *info = client_data; 00143 sockbuf_delete(info->our_idx); 00144 return(0); 00145 }
int http_reconnect | ( | int | idx, | |
const char * | host, | |||
int | port | |||
) |
Definition at line 147 of file http.c.
References egg_client(), proxy_info_t::host, proxy_config_t::host, proxy_info_t::http_host, linemode_on(), NULL, proxy_info_t::our_idx, proxy_info_t::password, proxy_config_t::password, proxy_info_t::port, proxy_config_t::port, proxy_config, sockbuf_attach_filter(), sockbuf_new(), sockbuf_set_handler(), proxy_info_t::status, proxy_info_t::their_idx, proxy_info_t::username, and proxy_config_t::username.
00148 { 00149 int our_idx; 00150 proxy_info_t *info; 00151 00152 if (!proxy_config.host) return(-1); 00153 00154 our_idx = egg_client(-1, proxy_config.host, proxy_config.port, NULL, 0, -1); 00155 if (our_idx < 0) return(-1); 00156 00157 /* Save our destination. */ 00158 info = (proxy_info_t *)calloc(1, sizeof(*info)); 00159 info->host = strdup(host); 00160 info->port = port; 00161 /* Also save the proxy info, in case the config changes while we're 00162 * connecting. */ 00163 info->http_host = strdup(proxy_config.host); 00164 if (proxy_config.username) info->username = strdup(proxy_config.username); 00165 if (proxy_config.password) info->password = strdup(proxy_config.password); 00166 00167 info->our_idx = our_idx; 00168 linemode_on(info->our_idx); 00169 00170 if (idx >= 0) info->their_idx = idx; 00171 else info->their_idx = sockbuf_new(); 00172 00173 info->status = 0; 00174 00175 sockbuf_set_handler(info->our_idx, &http_events, info, &http_owner); 00176 sockbuf_attach_filter(info->their_idx, &delete_listener, info); 00177 00178 return(info->their_idx); 00179 }
static char* make_password | ( | proxy_info_t * | info | ) | [static] |
Definition at line 106 of file http.c.
References b64enc_buf(), egg_mprintf(), proxy_info_t::password, and proxy_info_t::username.
Referenced by http_on_connect().
00107 { 00108 char *buf, *dest; 00109 int len; 00110 00111 buf = egg_mprintf("%s %s", info->username, info->password); 00112 len = strlen(buf); 00113 dest = malloc(len * 4 + 1); 00114 b64enc_buf(buf, len, dest); 00115 free(buf); 00116 return(dest); 00117 }
sockbuf_filter_t delete_listener [static] |
Initial value:
{ "http proxy", SOCKBUF_LEVEL_PROXY, NULL, NULL, NULL, NULL, NULL, NULL, NULL, http_on_their_delete }
sockbuf_handler_t http_events [static] |
Initial value:
{ "http proxy", http_on_connect, http_on_eof, NULL, http_on_read, NULL }