Skip to content
Browse files

add check type of smtp and some test cases

  • Loading branch information...
1 parent 2c053b7 commit dd222aecc72a38162719564aa7854067442d3c7c @yaoweibin committed
View
1 ngx_tcp.h
@@ -20,6 +20,7 @@ typedef struct ngx_tcp_upstream_resolved_s ngx_tcp_upstream_resolved_t;
typedef struct ngx_tcp_check_peer_conf_s ngx_tcp_check_peer_conf_t;
typedef struct ngx_tcp_check_peers_conf_s ngx_tcp_check_peers_conf_t;
+typedef struct check_conf_s check_conf_t;
/* make nginx-0.8.22+ happy */
#if defined(nginx_version) && nginx_version >= 8022
View
44 ngx_tcp_upstream.c
@@ -56,14 +56,21 @@ static ngx_command_t ngx_tcp_upstream_commands[] = {
NGX_TCP_UPS_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_TCP_SRV_CONF_OFFSET,
- offsetof(ngx_tcp_upstream_srv_conf_t, check_http_conf.send),
+ offsetof(ngx_tcp_upstream_srv_conf_t, send),
+ NULL },
+
+ { ngx_string("check_smtp_send"),
+ NGX_TCP_UPS_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_str_slot,
+ NGX_TCP_SRV_CONF_OFFSET,
+ offsetof(ngx_tcp_upstream_srv_conf_t, send),
NULL },
{ ngx_string("check_http_expect_alive"),
NGX_TCP_UPS_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_TCP_SRV_CONF_OFFSET,
- offsetof(ngx_tcp_upstream_srv_conf_t, check_http_conf.status_alive),
+ offsetof(ngx_tcp_upstream_srv_conf_t, status_alive),
&ngx_check_http_expect_alive_masks },
{ ngx_string("check_shm_size"),
@@ -923,7 +930,7 @@ ngx_tcp_upstream_check(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_tcp_upstream_srv_conf_t *uscf = conf;
ngx_str_t *value, s;
- ngx_uint_t i, rise, fall, type;
+ ngx_uint_t i, rise, fall;
ngx_msec_t interval, timeout;
/*set default*/
@@ -931,7 +938,6 @@ ngx_tcp_upstream_check(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
fall = 5;
interval = 30000;
timeout = 1000;
- type = NGX_TCP_CHECK_TCP;
value = cf->args->elts;
@@ -941,25 +947,13 @@ ngx_tcp_upstream_check(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
s.len = value[i].len - 5;
s.data = value[i].data + 5;
- if (ngx_strncmp(s.data, "tcp", s.len) == 0) {
- type = NGX_TCP_CHECK_TCP;
- continue;
- }
- else if (ngx_strncmp(s.data, "http", s.len) == 0 ) {
- type = NGX_TCP_CHECK_HTTP;
- continue;
- }
- else if (ngx_strncmp(s.data, "ssl_hello", s.len) == 0 ) {
- type = NGX_TCP_CHECK_SSL_HELLO;
- continue;
- }
- else if (ngx_strncmp(s.data, "smtp", s.len) == 0 ) {
- type = NGX_TCP_CHECK_SMTP;
- continue;
- }
- else {
+ uscf->check_type_conf = ngx_tcp_get_check_type_conf(&s);
+
+ if ( uscf->check_type_conf == NULL) {
goto invalid_check_parameter;
}
+
+ continue;
}
if (ngx_strncmp(value[i].data, "interval=", 9) == 0) {
@@ -1013,12 +1007,18 @@ ngx_tcp_upstream_check(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
goto invalid_check_parameter;
}
- uscf->check_type = type;
uscf->check_interval = interval;
uscf->check_timeout = timeout;
uscf->fall_count = fall;
uscf->rise_count = rise;
+ if (uscf->check_type_conf == NULL) {
+ s.len = sizeof("tcp") - 1;
+ s.data =(u_char *) "tcp";
+
+ uscf->check_type_conf = ngx_tcp_get_check_type_conf(&s);
+ }
+
return NGX_CONF_OK;
invalid_check_parameter:
View
29 ngx_tcp_upstream.h
@@ -93,26 +93,8 @@ typedef struct {
#define NGX_TCP_UPSTREAM_SRUN_ID 0x0040
#define NGX_TCP_UPSTREAM_MAX_BUSY 0x0080
-
-#define NGX_TCP_CHECK_TCP 0x0001
-#define NGX_TCP_CHECK_HTTP 0x0002
-#define NGX_TCP_CHECK_SSL_HELLO 0x0004
-#define NGX_TCP_CHECK_SMTP 0x0008
-
-
-#define NGX_CHECK_HTTP_2XX 0x0002
-#define NGX_CHECK_HTTP_3XX 0x0004
-#define NGX_CHECK_HTTP_4XX 0x0008
-#define NGX_CHECK_HTTP_5XX 0x0010
-#define NGX_CHECK_HTTP_6XX 0x0020
-#define NGX_CHECK_HTTP_ERR 0x8000
-
-typedef struct {
- ngx_str_t send;
- ngx_uint_t status_alive;
-} http_check_conf_t;
-
struct ngx_tcp_upstream_srv_conf_s {
+
ngx_tcp_upstream_peer_t peer;
void **srv_conf;
@@ -130,9 +112,14 @@ struct ngx_tcp_upstream_srv_conf_s {
ngx_uint_t rise_count;
ngx_msec_t check_interval;
ngx_msec_t check_timeout;
- ngx_uint_t check_type;
- http_check_conf_t check_http_conf;
+ check_conf_t *check_type_conf;
+ ngx_str_t send;
+
+ union {
+ ngx_uint_t return_code;
+ ngx_uint_t status_alive;
+ };
};
View
335 ngx_tcp_upstream_check.c
@@ -11,9 +11,117 @@ static ngx_int_t ngx_tcp_check_get_shm_name(ngx_str_t *shm_name, ngx_pool_t *poo
static ngx_int_t ngx_tcp_upstream_check_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data);
static ngx_int_t ngx_tcp_check_init_process(ngx_cycle_t *cycle);
+static void ngx_tcp_check_peek_handler(ngx_event_t *event);
+
+static void ngx_tcp_check_send_handler(ngx_event_t *event);
+static void ngx_tcp_check_recv_handler(ngx_event_t *event);
+
+static ngx_int_t ngx_tcp_check_http_init(ngx_tcp_check_peer_conf_t *peer_conf);
+static ngx_int_t ngx_tcp_check_http_parse(ngx_tcp_check_peer_conf_t *peer_conf);
+static void ngx_tcp_check_http_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
+
+static ngx_int_t ngx_tcp_check_ssl_hello_init(ngx_tcp_check_peer_conf_t *peer_conf);
+static ngx_int_t ngx_tcp_check_ssl_hello_parse(ngx_tcp_check_peer_conf_t *peer_conf);
+static void ngx_tcp_check_ssl_hello_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
+
+static ngx_int_t ngx_tcp_check_smtp_init(ngx_tcp_check_peer_conf_t *peer_conf);
+static ngx_int_t ngx_tcp_check_smtp_parse(ngx_tcp_check_peer_conf_t *peer_conf);
+static void ngx_tcp_check_smtp_reinit(ngx_tcp_check_peer_conf_t *peer_conf);
+
static char * ngx_tcp_upstream_check_status_set_status(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
+#define RANDOM "NGX_TCP_CHECK_SSL_HELLO\n\n\n\n\n"
+
+/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
+ * check type of ssl_hello to ensure that the remote server speaks SSL.
+ *
+ * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
+ *
+ * Some codes copy from HAProxy 1.4.1
+ */
+const char sslv3_client_hello_pkt[] = {
+ "\x16" /* ContentType : 0x16 = Hanshake */
+ "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
+ "\x00\x79" /* ContentLength : 0x79 bytes after this one */
+ "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
+ "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
+ "\x03\x00" /* Hello Version : 0x0300 = v3 */
+ "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
+ RANDOM /* Random : must be exactly 28 bytes */
+ "\x00" /* Session ID length : empty (no session ID) */
+ "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
+ "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
+ "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
+ "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
+ "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
+ "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
+ "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
+ "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
+ "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
+ "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
+ "\x00\x38" "\x00\x39" "\x00\x3A"
+ "\x01" /* Compression Length : 0x01 = 1 byte for types */
+ "\x00" /* Compression Type : 0x00 = NULL compression */
+};
+
+
+#define HANDSHAKE 0x16
+#define SERVER_HELLO 0x02
+
+static check_conf_t ngx_check_types[] = {
+ {
+ NGX_TCP_CHECK_TCP,
+ "tcp",
+ ngx_null_string,
+ 0,
+ ngx_tcp_check_peek_handler,
+ ngx_tcp_check_peek_handler,
+ NULL,
+ NULL,
+ NULL,
+ 0
+ },
+ {
+ NGX_TCP_CHECK_HTTP,
+ "http",
+ ngx_string("GET / HTTP/1.0\r\n\r\n"),
+ NGX_CONF_BITMASK_SET | NGX_CHECK_HTTP_2XX | NGX_CHECK_HTTP_3XX,
+ ngx_tcp_check_send_handler,
+ ngx_tcp_check_recv_handler,
+ ngx_tcp_check_http_init,
+ ngx_tcp_check_http_parse,
+ ngx_tcp_check_http_reinit,
+ 1
+ },
+ {
+ NGX_TCP_CHECK_SSL_HELLO,
+ "ssl_hello",
+ ngx_string(sslv3_client_hello_pkt),
+ 0,
+ ngx_tcp_check_send_handler,
+ ngx_tcp_check_recv_handler,
+ ngx_tcp_check_ssl_hello_init,
+ ngx_tcp_check_ssl_hello_parse,
+ ngx_tcp_check_ssl_hello_reinit,
+ 1
+ },
+ {
+ NGX_TCP_CHECK_SMTP,
+ "smtp",
+ ngx_string("HELO localhost\r\n"),
+ 0,
+ ngx_tcp_check_send_handler,
+ ngx_tcp_check_recv_handler,
+ ngx_tcp_check_smtp_init,
+ ngx_tcp_check_smtp_parse,
+ ngx_tcp_check_smtp_reinit,
+ 1
+ },
+
+ {0, "", ngx_null_string, 0, NULL, NULL, NULL, NULL, NULL, 0}
+};
+
static ngx_command_t ngx_tcp_upstream_check_status_commands[] = {
{ ngx_string("check_status"),
@@ -59,6 +167,24 @@ ngx_module_t ngx_tcp_upstream_check_status_module = {
static ngx_uint_t ngx_tcp_check_shm_generation = 0;
static ngx_tcp_check_peers_conf_t *check_peers_ctx = NULL;
+check_conf_t *
+ngx_tcp_get_check_type_conf(ngx_str_t *str) {
+
+ ngx_uint_t i;
+
+ for (i = 0; ;i++) {
+
+ if (ngx_check_types[i].type == 0) {
+ break;
+ }
+
+ if (ngx_strncmp(str->data, (u_char *)ngx_check_types[i].name, str->len) == 0) {
+ return &ngx_check_types[i];
+ }
+ }
+
+ return NULL;
+}
ngx_uint_t
ngx_tcp_check_peer_down(ngx_uint_t index){
@@ -485,8 +611,8 @@ ngx_tcp_check_http_init(ngx_tcp_check_peer_conf_t *peer_conf) {
ctx = peer_conf->check_data;
uscf = peer_conf->conf;
- ctx->send.start = ctx->send.pos = (u_char *)uscf->check_http_conf.send.data;
- ctx->send.end = ctx->send.last = ctx->send.start + uscf->check_http_conf.send.len;
+ ctx->send.start = ctx->send.pos = (u_char *)uscf->send.data;
+ ctx->send.end = ctx->send.last = ctx->send.start + uscf->send.len;
ctx->recv.start = ctx->recv.pos = NULL;
ctx->recv.end = ctx->recv.last = NULL;
@@ -530,12 +656,12 @@ ngx_tcp_check_http_parse(ngx_tcp_check_peer_conf_t *peer_conf) {
ngx_log_debug2(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
"http_parse: hp->status_code_n: %d, conf: %d",
- hp->status_code_n, uscf->check_http_conf.status_alive);
+ hp->status_code_n, uscf->status_alive);
if (hp->status_code_n == 0) {
return NGX_AGAIN;
}
- else if (hp->status_code_n & uscf->check_http_conf.status_alive) {
+ else if (hp->status_code_n & uscf->status_alive) {
return NGX_OK;
}
else {
@@ -565,53 +691,18 @@ ngx_tcp_check_http_reinit(ngx_tcp_check_peer_conf_t *peer_conf) {
}
-#define RANDOM "NGX_TCP_CHECK_SSL_HELLO\n\n\n\n\n"
-
-/* This is the SSLv3 CLIENT HELLO packet used in conjunction with the
- * check type of ssl_hello to ensure that the remote server speaks SSL.
- *
- * Check RFC 2246 (TLSv1.0) sections A.3 and A.4 for details.
- *
- * Some codes copy from HAProxy 1.4.1
- */
-const char sslv3_client_hello_pkt[] = {
- "\x16" /* ContentType : 0x16 = Hanshake */
- "\x03\x00" /* ProtocolVersion : 0x0300 = SSLv3 */
- "\x00\x79" /* ContentLength : 0x79 bytes after this one */
- "\x01" /* HanshakeType : 0x01 = CLIENT HELLO */
- "\x00\x00\x75" /* HandshakeLength : 0x75 bytes after this one */
- "\x03\x00" /* Hello Version : 0x0300 = v3 */
- "\x00\x00\x00\x00" /* Unix GMT Time (s) : filled with <now> (@0x0B) */
- RANDOM /* Random : must be exactly 28 bytes */
- "\x00" /* Session ID length : empty (no session ID) */
- "\x00\x4E" /* Cipher Suite Length : 78 bytes after this one */
- "\x00\x01" "\x00\x02" "\x00\x03" "\x00\x04" /* 39 most common ciphers : */
- "\x00\x05" "\x00\x06" "\x00\x07" "\x00\x08" /* 0x01...0x1B, 0x2F...0x3A */
- "\x00\x09" "\x00\x0A" "\x00\x0B" "\x00\x0C" /* This covers RSA/DH, */
- "\x00\x0D" "\x00\x0E" "\x00\x0F" "\x00\x10" /* various bit lengths, */
- "\x00\x11" "\x00\x12" "\x00\x13" "\x00\x14" /* SHA1/MD5, DES/3DES/AES... */
- "\x00\x15" "\x00\x16" "\x00\x17" "\x00\x18"
- "\x00\x19" "\x00\x1A" "\x00\x1B" "\x00\x2F"
- "\x00\x30" "\x00\x31" "\x00\x32" "\x00\x33"
- "\x00\x34" "\x00\x35" "\x00\x36" "\x00\x37"
- "\x00\x38" "\x00\x39" "\x00\x3A"
- "\x01" /* Compression Length : 0x01 = 1 byte for types */
- "\x00" /* Compression Type : 0x00 = NULL compression */
-};
-
-
-#define HANDSHAKE 0x16
-#define SERVER_HELLO 0x02
static ngx_int_t
ngx_tcp_check_ssl_hello_init(ngx_tcp_check_peer_conf_t *peer_conf) {
ngx_tcp_check_ctx *ctx;
-
+ ngx_tcp_upstream_srv_conf_t *uscf;
+
ctx = peer_conf->check_data;
+ uscf = peer_conf->conf;
- ctx->send.start = ctx->send.pos = (u_char *)sslv3_client_hello_pkt;
- ctx->send.end = ctx->send.last = ctx->send.start + sizeof(sslv3_client_hello_pkt);
+ ctx->send.start = ctx->send.pos = (u_char *)uscf->send.data;
+ ctx->send.end = ctx->send.last = ctx->send.start + uscf->send.len;
ctx->recv.start = ctx->recv.pos = NULL;
ctx->recv.end = ctx->recv.last = NULL;
@@ -666,6 +757,64 @@ ngx_tcp_check_ssl_hello_reinit(ngx_tcp_check_peer_conf_t *peer_conf) {
ctx->recv.pos = ctx->recv.last = ctx->recv.start;
}
+
+static ngx_int_t
+ngx_tcp_check_smtp_init(ngx_tcp_check_peer_conf_t *peer_conf) {
+
+ ngx_tcp_check_ctx *ctx;
+ ngx_tcp_upstream_srv_conf_t *uscf;
+
+ ctx = peer_conf->check_data;
+ uscf = peer_conf->conf;
+
+ ctx->send.start = ctx->send.pos = (u_char *)uscf->send.data;
+ ctx->send.end = ctx->send.last = ctx->send.start + uscf->send.len;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
+ "smtp_init: send:%V", &uscf->send);
+
+ ctx->recv.start = ctx->recv.pos = NULL;
+ ctx->recv.end = ctx->recv.last = NULL;
+
+ return NGX_OK;
+}
+
+static ngx_int_t
+ngx_tcp_check_smtp_parse(ngx_tcp_check_peer_conf_t *peer_conf) {
+
+ u_char ch;
+ ngx_tcp_check_ctx *ctx;
+
+ ctx = peer_conf->check_data;
+
+ if (ctx->recv.last - ctx->recv.pos <= 0 ) {
+ return NGX_AGAIN;
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_TCP, ngx_cycle->log, 0,
+ "smtp_parse: recv:%s", ctx->recv.pos);
+
+ ch = (u_char) *(ctx->recv.pos);
+
+ if (ch != '2') {
+ return NGX_ERROR;
+ }
+
+ return NGX_OK;
+}
+
+static void
+ngx_tcp_check_smtp_reinit(ngx_tcp_check_peer_conf_t *peer_conf) {
+
+ ngx_tcp_check_ctx *ctx;
+
+ ctx = peer_conf->check_data;
+
+ ctx->send.pos = ctx->send.start;
+ ctx->send.last = ctx->send.end;
+
+ ctx->recv.pos = ctx->recv.last = ctx->recv.start;
+}
static void
ngx_tcp_check_send_handler(ngx_event_t *event) {
@@ -799,7 +948,7 @@ ngx_tcp_check_recv_handler(ngx_event_t *event) {
}
size = c->recv(c, ctx->recv.last, n);
- err = (size >=0) ? 0 : ngx_socket_errno;
+ err = (size >= 0) ? 0 : ngx_socket_errno;
ngx_log_debug2(NGX_LOG_DEBUG_TCP, c->log, err,
"tcp check recv size: %d, peer: %V", size, &peer_conf->peer->name);
@@ -888,27 +1037,8 @@ ngx_tcp_check_connect_handler(ngx_event_t *event) {
peer_conf->state = NGX_TCP_CHECK_CONNECT_DONE;
- switch (uscf->check_type) {
- case NGX_TCP_CHECK_TCP:
- c->write->handler = ngx_tcp_check_peek_handler;
- c->read->handler = ngx_tcp_check_peek_handler;
- break;
- case NGX_TCP_CHECK_HTTP:
- c->write->handler = ngx_tcp_check_send_handler;
- c->read->handler = ngx_tcp_check_recv_handler;
- break;
- case NGX_TCP_CHECK_SSL_HELLO:
- c->write->handler = ngx_tcp_check_send_handler;
- c->read->handler = ngx_tcp_check_recv_handler;
- break;
- case NGX_TCP_CHECK_SMTP:
- c->write->handler = ngx_tcp_check_peek_handler;
- c->read->handler = ngx_tcp_check_peek_handler;
- break;
- default:
- c->write->handler = ngx_tcp_check_peek_handler;
- c->read->handler = ngx_tcp_check_peek_handler;
- }
+ c->write->handler = peer_conf->send_handler;
+ c->read->handler = peer_conf->recv_handler;
ngx_add_timer(&peer_conf->check_timeout_ev, uscf->check_timeout);
}
@@ -956,20 +1086,21 @@ ngx_tcp_check_begin_handler(ngx_event_t *event) {
static void
ngx_tcp_upstream_init_check_conf(ngx_tcp_upstream_srv_conf_t *uscf) {
- /*set default value*/
+ check_conf_t *cf;
- if (uscf->check_type | NGX_TCP_CHECK_HTTP) {
+ cf = uscf->check_type_conf;
- if (uscf->check_http_conf.send.data == NULL) {
- uscf->check_http_conf.send.data = (u_char *) "GET / HTTP/1.0\r\n\r\n";
- uscf->check_http_conf.send.len = sizeof("GET / HTTP/1.0\r\n\r\n") - 1;
- }
+ if (uscf->send.len == 0) {
+ uscf->send.data = cf->default_send.data;
+ uscf->send.len = cf->default_send.len;
+ }
+
+ if (cf->type & NGX_TCP_CHECK_HTTP) {
- if ((uscf->check_http_conf.status_alive < NGX_CHECK_HTTP_2XX) ||
- (uscf->check_http_conf.status_alive >= NGX_CHECK_HTTP_6XX)) {
+ if ((uscf->status_alive < NGX_CHECK_HTTP_2XX) ||
+ (uscf->status_alive >= NGX_CHECK_HTTP_6XX)) {
- uscf->check_http_conf.status_alive =
- NGX_CONF_BITMASK_SET | NGX_CHECK_HTTP_2XX | NGX_CHECK_HTTP_3XX;
+ uscf->status_alive = cf->default_status_alive;
}
}
}
@@ -1031,6 +1162,7 @@ ngx_tcp_check_init_process(ngx_cycle_t *cycle) {
ngx_msec_t t, delay;
ngx_str_t shm_name;
ngx_shm_zone_t *shm_zone;
+ check_conf_t *cf;
ngx_tcp_check_peers_conf_t *peers_conf;
ngx_tcp_check_peer_conf_t *peer_conf;
ngx_tcp_check_peers_shm_t *peers_shm;
@@ -1073,35 +1205,21 @@ ngx_tcp_check_init_process(ngx_cycle_t *cycle) {
peer_conf[i].check_timeout_ev.timer_set = 0;
uscf = peer_conf[i].conf;
+ cf = uscf->check_type_conf;
- if (uscf->check_type | NGX_TCP_CHECK_SSL_HELLO | NGX_TCP_CHECK_SSL_HELLO ) {
+ if (cf->need_pool) {
peer_conf[i].pool = ngx_create_pool(ngx_pagesize, cycle->log);
if (peer_conf[i].pool == NULL) {
return NGX_ERROR;
}
}
- switch (uscf->check_type) {
+ peer_conf[i].send_handler = cf->send_handler;
+ peer_conf[i].recv_handler = cf->recv_handler;
- case NGX_TCP_CHECK_HTTP:
- peer_conf[i].init = ngx_tcp_check_http_init;
- peer_conf[i].parse = ngx_tcp_check_http_parse;
- peer_conf[i].reinit = ngx_tcp_check_http_reinit;
- break;
-
- case NGX_TCP_CHECK_SSL_HELLO:
- peer_conf[i].init = ngx_tcp_check_ssl_hello_init;
- peer_conf[i].parse = ngx_tcp_check_ssl_hello_parse;
- peer_conf[i].reinit = ngx_tcp_check_ssl_hello_reinit;
- break;
-
- case NGX_TCP_CHECK_TCP:
- case NGX_TCP_CHECK_SMTP:
- default:
- peer_conf[i].init = NULL;
- peer_conf[i].parse = NULL;
- peer_conf[i].reinit = NULL;
- }
+ peer_conf[i].init = cf->init;
+ peer_conf[i].parse = cf->parse;
+ peer_conf[i].reinit = cf->reinit;
/* Default delay interval is 1 second.
I don't want to trigger the check event too close. */
@@ -1114,27 +1232,6 @@ ngx_tcp_check_init_process(ngx_cycle_t *cycle) {
return NGX_OK;
}
-static char *
-get_check_type(ngx_uint_t type) {
-
- switch (type) {
- case NGX_TCP_CHECK_HTTP:
- return "http";
-
- case NGX_TCP_CHECK_SSL_HELLO:
- return "ssl_hello";
-
- case NGX_TCP_CHECK_SMTP:
- return "smtp";
-
- case NGX_TCP_CHECK_TCP:
- default:
- return "tcp";
- }
-
- return NULL;
-}
-
static ngx_int_t
ngx_tcp_upstream_check_status_handler(ngx_http_request_t *r) {
@@ -1212,7 +1309,7 @@ ngx_tcp_upstream_check_status_handler(ngx_http_request_t *r) {
"server %ui: name=%V, down=%ui, rise=%ui, fall=%ui, type=%s\n",
i, &peer_conf[i].peer->name, peer_shm[i].down,
peer_shm[i].rise_count, peer_shm[i].fall_count,
- get_check_type(peer_conf[i].conf->check_type));
+ peer_conf[i].conf->check_type_conf->name);
}
r->headers_out.status = NGX_HTTP_OK;
View
38 ngx_tcp_upstream_check.h
@@ -72,6 +72,39 @@ typedef ngx_int_t (*ngx_tcp_check_packet_init_pt)(ngx_tcp_check_peer_conf_t *pee
typedef ngx_int_t (*ngx_tcp_check_packet_parse_pt)(ngx_tcp_check_peer_conf_t *peer_conf);
typedef void (*ngx_tcp_check_packet_clean_pt)(ngx_tcp_check_peer_conf_t *peer_conf);
+#define NGX_TCP_CHECK_TCP 0x0001
+#define NGX_TCP_CHECK_HTTP 0x0002
+#define NGX_TCP_CHECK_SSL_HELLO 0x0004
+#define NGX_TCP_CHECK_SMTP 0x0008
+
+
+#define NGX_CHECK_HTTP_2XX 0x0002
+#define NGX_CHECK_HTTP_3XX 0x0004
+#define NGX_CHECK_HTTP_4XX 0x0008
+#define NGX_CHECK_HTTP_5XX 0x0010
+#define NGX_CHECK_HTTP_6XX 0x0020
+#define NGX_CHECK_HTTP_ERR 0x8000
+
+struct check_conf_s {
+ ngx_uint_t type;
+
+ char *name;
+
+ ngx_str_t default_send;
+
+ /*HTTP*/
+ ngx_uint_t default_status_alive;
+
+ ngx_event_handler_pt send_handler;
+ ngx_event_handler_pt recv_handler;
+
+ ngx_tcp_check_packet_init_pt init;
+ ngx_tcp_check_packet_parse_pt parse;
+ ngx_tcp_check_packet_clean_pt reinit;
+
+ unsigned need_pool;
+};
+
struct ngx_tcp_check_peer_conf_s {
ngx_flag_t state;
@@ -84,6 +117,9 @@ struct ngx_tcp_check_peer_conf_s {
ngx_peer_connection_t pc;
void * check_data;
+ ngx_event_handler_pt send_handler;
+ ngx_event_handler_pt recv_handler;
+
ngx_tcp_check_packet_init_pt init;
ngx_tcp_check_packet_parse_pt parse;
ngx_tcp_check_packet_clean_pt reinit;
@@ -99,6 +135,7 @@ struct ngx_tcp_check_peers_conf_s {
};
+/*HTTP parser*/
typedef void (*element_cb)(void *data, const char *at, size_t length);
typedef void (*field_cb)(void *data, const char *field, size_t flen, const char *value, size_t vlen);
@@ -140,6 +177,7 @@ ngx_uint_t ngx_tcp_check_add_peer(ngx_conf_t *cf, ngx_tcp_upstream_srv_conf_t *u
ngx_uint_t ngx_tcp_check_peer_down(ngx_uint_t index);
+check_conf_t *ngx_tcp_get_check_type_conf(ngx_str_t *str);
#endif //_NGX_TCP_UPSTREAM_CHECK_H_INCLUDED_
View
2 test/lib/Test/Nginx/Util.pm
@@ -412,7 +412,7 @@ start_nginx:
}
}
- sleep 0.1;
+ sleep 6;
}
}
View
57 test/t/http_check.t
@@ -0,0 +1,57 @@
+#
+#===============================================================================
+#
+# FILE: http_check.t
+#
+# DESCRIPTION: test
+#
+# FILES: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Weibin Yao (http://yaoweibin.cn/), yaoweibin@gmail.com
+# COMPANY:
+# VERSION: 1.0
+# CREATED: 03/02/2010 03:18:28 PM
+# REVISION: ---
+#===============================================================================
+
+
+# vi:filetype=perl
+
+use lib 'lib';
+use Test::Nginx::LWP;
+
+plan tests => repeat_each() * 2 * blocks();
+
+#no_diff;
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: the http_check test
+--- config
+ upstream test{
+ server 172.19.0.129;
+ server 172.19.0.130;
+ server 172.19.0.131;
+ server 172.19.0.132;
+ server 172.19.0.235;
+ server 172.19.0.236;
+ server 172.19.0.237;
+ server 172.19.0.238;
+ server 172.19.0.239;
+ #ip_hash;
+ check interval=3000 rise=2 fall=5 timeout=1000 type=http;
+ check_http_send "GET /test HTTP/1.0\r\n\r\n";
+ check_http_expect_alive http_2xx http_3xx http_4xx;
+ }
+
+ server {
+ listen 1982;
+
+ proxy_pass test;
+ }
+--- request
+GET /
+--- response_body_like: ^<(.*)>$
View
2 test/t/sample.t
@@ -51,4 +51,4 @@ __DATA__
}
--- request
GET /
---- response_body_like: ^.*$
+--- response_body_like: ^<(.*)>$
View
49 test/t/smtp_check.t
@@ -0,0 +1,49 @@
+#
+#===============================================================================
+#
+# FILE: smtp_check.t
+#
+# DESCRIPTION: test
+#
+# FILES: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Weibin Yao (http://yaoweibin.cn/), yaoweibin@gmail.com
+# COMPANY:
+# VERSION: 1.0
+# CREATED: 03/02/2010 03:18:28 PM
+# REVISION: ---
+#===============================================================================
+
+
+# vi:filetype=perl
+
+use lib 'lib';
+use Test::Nginx::LWP;
+
+plan tests => repeat_each() * 2 * blocks();
+
+#no_diff;
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: the smtp_check
+--- config
+ upstream test{
+ server 127.0.0.1:25;
+
+ #ip_hash;
+ check interval=3000 rise=2 fall=5 timeout=1000 type=smtp;
+ check_smtp_send "HELO localhost\r\n";
+ }
+
+ server {
+ listen 1982;
+
+ proxy_pass test;
+ }
+--- request
+GET /
+--- response_body_like: ^2(.*)$
View
48 test/t/ssl_hello_check.t
@@ -0,0 +1,48 @@
+#
+#===============================================================================
+#
+# FILE: ssl_hello_check.t
+#
+# DESCRIPTION: test
+#
+# FILES: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Weibin Yao (http://yaoweibin.cn/), yaoweibin@gmail.com
+# COMPANY:
+# VERSION: 1.0
+# CREATED: 03/02/2010 03:18:28 PM
+# REVISION: ---
+#===============================================================================
+
+
+# vi:filetype=perl
+
+use lib 'lib';
+use Test::Nginx::LWP;
+
+plan tests => repeat_each() * 2 * blocks();
+
+#no_diff;
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: the ssl_hello_check test
+--- config
+ upstream test{
+ server www.battlenet.com.cn:443;
+
+ #ip_hash;
+ check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
+ }
+
+ server {
+ listen 1982;
+
+ proxy_pass test;
+ }
+--- request
+GET /
+--- response_body_like: ^<(.*)>$
View
55 test/t/tcp_check.t
@@ -0,0 +1,55 @@
+#
+#===============================================================================
+#
+# FILE: tcp_check.t
+#
+# DESCRIPTION: test
+#
+# FILES: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Weibin Yao (http://yaoweibin.cn/), yaoweibin@gmail.com
+# COMPANY:
+# VERSION: 1.0
+# CREATED: 03/02/2010 03:18:28 PM
+# REVISION: ---
+#===============================================================================
+
+
+# vi:filetype=perl
+
+use lib 'lib';
+use Test::Nginx::LWP;
+
+plan tests => repeat_each() * 2 * blocks();
+
+#no_diff;
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: the tcp_check test
+--- config
+ upstream test{
+ server 172.19.0.129;
+ server 172.19.0.130;
+ server 172.19.0.131;
+ server 172.19.0.132;
+ server 172.19.0.235;
+ server 172.19.0.236;
+ server 172.19.0.237;
+ server 172.19.0.238;
+ server 172.19.0.239;
+ #ip_hash;
+ check interval=3000 rise=2 fall=5 timeout=1000;
+ }
+
+ server {
+ listen 1982;
+
+ proxy_pass test;
+ }
+--- request
+GET /
+--- response_body_like: ^<(.*)>$
View
54 test/t/upstream_ip_hash.t
@@ -0,0 +1,54 @@
+#
+#===============================================================================
+#
+# FILE: sample.t
+#
+# DESCRIPTION: test
+#
+# FILES: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Weibin Yao (http://yaoweibin.cn/), yaoweibin@gmail.com
+# COMPANY:
+# VERSION: 1.0
+# CREATED: 03/02/2010 03:18:28 PM
+# REVISION: ---
+#===============================================================================
+
+
+# vi:filetype=perl
+
+use lib 'lib';
+use Test::Nginx::LWP;
+
+plan tests => repeat_each() * 2 * blocks();
+
+#no_diff;
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: the upstream_ip_hash command
+--- config
+ upstream test{
+ server 172.19.0.129;
+ server 172.19.0.130;
+ server 172.19.0.131;
+ server 172.19.0.132;
+ server 172.19.0.235;
+ server 172.19.0.236;
+ server 172.19.0.237;
+ server 172.19.0.238;
+ server 172.19.0.239;
+ ip_hash;
+ }
+
+ server {
+ listen 1982;
+
+ proxy_pass test;
+ }
+--- request
+GET /
+--- response_body_like: ^<(.*)>$

0 comments on commit dd222ae

Please sign in to comment.
Something went wrong with that request. Please try again.