417 changes: 417 additions & 0 deletions manpage.wiki

Large diffs are not rendered by default.

28 changes: 11 additions & 17 deletions modules/ngx_tcp_proxy_module.c
Expand Up @@ -11,7 +11,6 @@ typedef struct ngx_tcp_proxy_conf_s {

ngx_str_t url;
size_t buffer_size;
ngx_msec_t timeout;

/*TODO: support for the variable in the proxy_pass*/
ngx_array_t *proxy_lengths;
Expand Down Expand Up @@ -44,13 +43,6 @@ static ngx_command_t ngx_tcp_proxy_commands[] = {
offsetof(ngx_tcp_proxy_conf_t, buffer_size),
NULL },

{ ngx_string("proxy_timeout"),
NGX_TCP_MAIN_CONF|NGX_TCP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_TCP_SRV_CONF_OFFSET,
offsetof(ngx_tcp_proxy_conf_t, timeout),
NULL },

{ ngx_string("proxy_connect_timeout"),
NGX_TCP_MAIN_CONF|NGX_TCP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
Expand Down Expand Up @@ -105,10 +97,12 @@ ngx_module_t ngx_tcp_proxy_module = {
void
ngx_tcp_upstream_proxy_generic_handler(ngx_tcp_session_t *s, ngx_tcp_upstream_t *u) {

ngx_tcp_proxy_conf_t *pcf;
ngx_tcp_proxy_ctx_t *pctx;
ngx_connection_t *c;
ngx_tcp_proxy_conf_t *pcf;
ngx_tcp_proxy_ctx_t *pctx;
ngx_connection_t *c;
ngx_tcp_core_srv_conf_t *cscf;

cscf = ngx_tcp_get_module_srv_conf(s, ngx_tcp_core_module);

c = s->connection;
c->log->action = "ngx_tcp_proxy_handler";
Expand Down Expand Up @@ -143,7 +137,8 @@ ngx_tcp_upstream_proxy_generic_handler(ngx_tcp_session_t *s, ngx_tcp_upstream_t
c->read->handler = ngx_tcp_proxy_handler;
c->write->handler = ngx_tcp_proxy_handler;

ngx_add_timer(s->connection->read, pcf->timeout);
ngx_add_timer(s->connection->read, cscf->timeout);

ngx_add_timer(c->read, pcf->upstream.read_timeout);
ngx_add_timer(c->write, pcf->upstream.send_timeout);

Expand Down Expand Up @@ -348,10 +343,13 @@ ngx_tcp_proxy_handler(ngx_event_t *ev) {
ngx_tcp_session_t *s;
ngx_tcp_proxy_conf_t *pcf;
ngx_tcp_proxy_ctx_t *pctx;
ngx_tcp_core_srv_conf_t *cscf;

c = ev->data;
s = c->data;

cscf = ngx_tcp_get_module_srv_conf(s, ngx_tcp_core_module);

if (ev->timedout) {
c->log->action = "proxying";

Expand Down Expand Up @@ -509,7 +507,7 @@ ngx_tcp_proxy_handler(ngx_event_t *ev) {
pcf = ngx_tcp_get_module_srv_conf(s, ngx_tcp_proxy_module);

if (c == s->connection) {
ngx_add_timer(c->read, pcf->timeout);
ngx_add_timer(c->read, cscf->timeout);
}

if (c == pctx->upstream->connection) {
Expand Down Expand Up @@ -566,8 +564,6 @@ ngx_tcp_proxy_create_conf(ngx_conf_t *cf) {

pcf->buffer_size = NGX_CONF_UNSET_SIZE;

pcf->timeout = NGX_CONF_UNSET_MSEC;

pcf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
pcf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
pcf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
Expand All @@ -582,8 +578,6 @@ ngx_tcp_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child) {

ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, (size_t) ngx_pagesize);

ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);

ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

Expand Down
8 changes: 8 additions & 0 deletions ngx_tcp.h
Expand Up @@ -102,6 +102,11 @@ typedef struct {
#endif
} ngx_tcp_conf_addr_t;

typedef struct {
in_addr_t mask;
in_addr_t addr;
ngx_uint_t deny; /* unsigned deny:1; */
} ngx_tcp_access_rule_t;

typedef struct {
ngx_array_t servers; /* ngx_tcp_core_srv_conf_t */
Expand All @@ -125,6 +130,9 @@ typedef struct {

ngx_resolver_t *resolver;

/*ACL rules*/
ngx_array_t *rules;

/* server ctx */
ngx_tcp_conf_ctx_t *ctx;
} ngx_tcp_core_srv_conf_t;
Expand Down
49 changes: 49 additions & 0 deletions ngx_tcp_access.c
@@ -0,0 +1,49 @@

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_tcp.h>


ngx_int_t
ngx_tcp_access_handler(ngx_tcp_session_t *s) {

ngx_uint_t i;
struct sockaddr_in *sin;
ngx_tcp_access_rule_t *rule;
ngx_tcp_core_srv_conf_t *cscf;

cscf = ngx_tcp_get_module_srv_conf(s, ngx_tcp_core_module);

if (cscf->rules == NULL) {
return NGX_DECLINED;
}

/* AF_INET only */

if (s->connection->sockaddr->sa_family != AF_INET) {
return NGX_DECLINED;
}

sin = (struct sockaddr_in *) s->connection->sockaddr;

rule = cscf->rules->elts;
for (i = 0; i < cscf->rules->nelts; i++) {

ngx_log_debug3(NGX_LOG_DEBUG_TCP, s->connection->log, 0,
"access: %08XD %08XD %08XD",
sin->sin_addr.s_addr, rule[i].mask, rule[i].addr);

if ((sin->sin_addr.s_addr & rule[i].mask) == rule[i].addr) {
if (rule[i].deny) {
ngx_log_error(NGX_LOG_NOTICE, s->connection->log, 0,
"access forbidden by rule");

return NGX_ERROR;
}

return NGX_OK;
}
}

return NGX_DECLINED;
}
121 changes: 98 additions & 23 deletions ngx_tcp_core_module.c
Expand Up @@ -15,10 +15,25 @@ static char *ngx_tcp_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_tcp_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);

static char *ngx_tcp_access_rule(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);

static ngx_command_t ngx_tcp_core_commands[] = {

{ ngx_string("allow"),
NGX_TCP_MAIN_CONF|NGX_TCP_SRV_CONF|NGX_CONF_TAKE1,
ngx_tcp_access_rule,
NGX_TCP_SRV_CONF_OFFSET,
0,
NULL },

{ ngx_string("deny"),
NGX_TCP_MAIN_CONF|NGX_TCP_SRV_CONF|NGX_CONF_TAKE1,
ngx_tcp_access_rule,
NGX_TCP_SRV_CONF_OFFSET,
0,
NULL },

{ ngx_string("server"),
NGX_TCP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_MULTI|NGX_CONF_NOARGS,
ngx_tcp_core_server,
Expand Down Expand Up @@ -106,9 +121,9 @@ ngx_module_t ngx_tcp_core_module = {
};


static void *
ngx_tcp_core_create_main_conf(ngx_conf_t *cf)
{
static void *
ngx_tcp_core_create_main_conf(ngx_conf_t *cf) {

ngx_tcp_core_main_conf_t *cmcf;

cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_tcp_core_main_conf_t));
Expand All @@ -133,9 +148,9 @@ ngx_tcp_core_create_main_conf(ngx_conf_t *cf)
}


static void *
ngx_tcp_core_create_srv_conf(ngx_conf_t *cf)
{
static void *
ngx_tcp_core_create_srv_conf(ngx_conf_t *cf) {

ngx_tcp_core_srv_conf_t *cscf;

cscf = ngx_pcalloc(cf->pool, sizeof(ngx_tcp_core_srv_conf_t));
Expand Down Expand Up @@ -163,9 +178,9 @@ ngx_tcp_core_create_srv_conf(ngx_conf_t *cf)
}


static char *
ngx_tcp_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
{
static char *
ngx_tcp_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) {

ngx_tcp_core_srv_conf_t *prev = parent;
ngx_tcp_core_srv_conf_t *conf = child;

Expand All @@ -184,13 +199,75 @@ ngx_tcp_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)

ngx_conf_merge_ptr_value(conf->resolver, prev->resolver, NULL);

if (conf->rules == NULL) {
conf->rules = prev->rules;
}

return NGX_CONF_OK;
}


static char *
ngx_tcp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
static char *
ngx_tcp_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_tcp_core_srv_conf_t *cscf = conf;

ngx_int_t rc;
ngx_str_t *value;
ngx_cidr_t cidr;
ngx_tcp_access_rule_t *rule;

if (cscf->rules == NULL) {
cscf->rules = ngx_array_create(cf->pool, 4,
sizeof(ngx_tcp_access_rule_t));
if (cscf->rules == NULL) {
return NGX_CONF_ERROR;
}
}

rule = ngx_array_push(cscf->rules);
if (rule == NULL) {
return NGX_CONF_ERROR;
}

value = cf->args->elts;

rule->deny = (value[0].data[0] == 'd') ? 1 : 0;

if (value[1].len == 3 && ngx_strcmp(value[1].data, "all") == 0) {
rule->mask = 0;
rule->addr = 0;

return NGX_CONF_OK;
}

rc = ngx_ptocidr(&value[1], &cidr);

if (rc == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
&value[1]);
return NGX_CONF_ERROR;
}

if (cidr.family != AF_INET) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"allow\" supports IPv4 only");
return NGX_CONF_ERROR;
}

if (rc == NGX_DONE) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"low address bits of %V are meaningless", &value[1]);
}

rule->mask = cidr.u.in.mask;
rule->addr = cidr.u.in.addr;

return NGX_CONF_OK;
}

static char *
ngx_tcp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {

char *rv;
void *mconf;
ngx_uint_t m;
Expand Down Expand Up @@ -260,10 +337,8 @@ ngx_tcp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}


static char *
ngx_tcp_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
/*ngx_tcp_core_srv_conf_t *cscf = conf;*/
static char *
ngx_tcp_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {

size_t len, off;
in_port_t port;
Expand Down Expand Up @@ -429,9 +504,9 @@ ngx_tcp_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}


static char *
ngx_tcp_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
static char *
ngx_tcp_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {

ngx_tcp_core_srv_conf_t *cscf = conf;

ngx_url_t u;
Expand Down Expand Up @@ -467,9 +542,9 @@ ngx_tcp_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}


char *
ngx_tcp_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *
ngx_tcp_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {

char *p = conf;

ngx_str_t *c, *value;
Expand Down
26 changes: 15 additions & 11 deletions ngx_tcp_session.c
Expand Up @@ -6,6 +6,7 @@


static void ngx_tcp_init_session(ngx_connection_t *c);
static void ngx_tcp_process_session(ngx_connection_t *c);

void
ngx_tcp_init_connection(ngx_connection_t *c)
Expand Down Expand Up @@ -165,21 +166,24 @@ ngx_tcp_init_session(ngx_connection_t *c)
return;
}

/*if (s->buffer == NULL) {*/
/*cscf->protocol->init_session(s, c);*/
ngx_tcp_process_session(c);
}

static void
ngx_tcp_process_session(ngx_connection_t *c)
{
ngx_tcp_session_t *s;

/*s->buffer = ngx_create_temp_buf(s->pool, 4096);*/
/*if (s->buffer == NULL) {*/
/*ngx_tcp_finalize_session(s);*/
/*return;*/
/*}*/
/*}*/
s = c->data;

/*c->write->handler = ngx_tcp_send;*/
/*process the acl*/
if (ngx_tcp_access_handler(s) == NGX_ERROR) {
ngx_tcp_finalize_session(s);
return;
}

/*Maybe I'll add more handlers module in the future*/
ngx_tcp_proxy_init_session(c, s);

/*cscf->protocol->init_session(s, c);*/
}

void
Expand Down
1 change: 1 addition & 0 deletions ngx_tcp_session.h
Expand Up @@ -72,6 +72,7 @@ void ngx_tcp_finalize_session(ngx_tcp_session_t *s);

ngx_tcp_cleanup_t * ngx_tcp_cleanup_add(ngx_tcp_session_t *s, size_t size);

ngx_int_t ngx_tcp_access_handler(ngx_tcp_session_t *s);
void ngx_tcp_proxy_init_session(ngx_connection_t *c, ngx_tcp_session_t *s);

extern ngx_module_t ngx_tcp_proxy_module;
Expand Down
131 changes: 131 additions & 0 deletions util/wiki2pod.pl
@@ -0,0 +1,131 @@
#!/usr/bin/env perl

use strict;
use warnings;
use bytes;

my @nl_counts;
my $last_nl_count_level;

my @bl_counts;
my $last_bl_count_level;

sub fmt_pos ($) {
(my $s = $_[0]) =~ s{\#(.*)}{/"$1"};
$s;
}

sub fmt_mark ($$) {
my ($tag, $s) = @_;
my $max_level = 0;
while ($s =~ /([<>])\1*/g) {
my $level = length $&;
if ($level > $max_level) {
$max_level = $level;
}
}

my $times = $max_level + 1;
if ($times > 1) {
$s = " $s ";
}
return $tag . ('<' x $times) . $s . ('>' x $times);
}

print "=encoding utf-8\n\n";

while (<>) {
if ($. == 1) {
# strip the leading U+FEFF byte in MS-DOS text files
my $first = ord(substr($_, 0, 1));
#printf STDERR "0x%x", $first;
#my $second = ord(substr($_, 2, 1));
#printf STDERR "0x%x", $second;
if ($first == 0xEF) {
substr($_, 0, 1, '');
#warn "Hit!";
}
}
s{\[(http[^ \]]+) ([^\]]*)\]}{$2 (L<$1>)}gi;
s{ \[\[ ( [^\]\|]+ ) \| ([^\]]*) \]\] }{"L<$2|" . fmt_pos($1) . ">"}gixe;
s{<code>(.*?)</code>}{fmt_mark('C', $1)}gie;
s{'''(.*?)'''}{fmt_mark('B', $1)}ge;
s{''(.*?)''}{fmt_mark('I', $1)}ge;
if (s{^\s*<[^>]+>\s*$}{}) {
next;
}
if (/^\s*$/) {
print "\n";
next;
}

=begin cmt
if ($. == 1) {
warn $_;
for my $i (0..length($_) - 1) {
my $chr = substr($_, $i, 1);
warn "chr ord($i): ".ord($chr)." \"$chr\"\n";
}
}
=end cmt
=cut

if (/(=+) (.*) \1$/) {
#warn "HERE! $_" if $. == 1;
my ($level, $title) = (length $1, $2);
collapse_lists();

print "\n=head$level $title\n\n";
} elsif (/^(\#+) (.*)/) {
my ($level, $txt) = (length($1) - 1, $2);
if (defined $last_nl_count_level && $level != $last_nl_count_level) {
print "\n=back\n\n";
}
$last_nl_count_level = $level;
$nl_counts[$level] ||= 0;
if ($nl_counts[$level] == 0) {
print "\n=over\n\n";
}
$nl_counts[$level]++;
print "\n=item $nl_counts[$level].\n\n";
print "$txt\n";
} elsif (/^(\*+) (.*)/) {
my ($level, $txt) = (length($1) - 1, $2);
if (defined $last_bl_count_level && $level != $last_bl_count_level) {
print "\n=back\n\n";
}
$last_bl_count_level = $level;
$bl_counts[$level] ||= 0;
if ($bl_counts[$level] == 0) {
print "\n=over\n\n";
}
$bl_counts[$level]++;
print "\n=item *\n\n";
print "$txt\n";
} else {
collapse_lists();
print;
}
}

collapse_lists();

sub collapse_lists {
while (defined $last_nl_count_level && $last_nl_count_level >= 0) {
print "\n=back\n\n";
$last_nl_count_level--;
}
undef $last_nl_count_level;
undef @nl_counts;

while (defined $last_bl_count_level && $last_bl_count_level >= 0) {
print "\n=back\n\n";
$last_bl_count_level--;
}
undef $last_bl_count_level;
undef @bl_counts;
}