[16977] 2021-07-02_WAF开发之灰度转发

文档创建者:s7ckTeam
浏览次数:2
最后更新:2025-01-18
2021-07-02_WAF开发之灰度转发 W A F   F r e e B u f   2 0 2 1 - 0 7 - 0 2 l u a o p e n r e s t y b a l a n c e r n g i n x u p s t r e a m u p s t r e a m 使 l u a I P H T T P u p s t r e a m   b a c k e n d   {     s e r v e r   t e s t . c o m     t e s t ; } # o p e n r e s t y s y n t a x : o k ,   e r r   =   b a l a n c e r . s e t _ c u r r e n t _ p e e r ( h o s t ,   p o r t ) # s y n t a x : o k ,   e r r   =   b a l a n c e r . s e t _ m o r e _ t r i e s ( c o u n t ) # s y n t a x : s t a t e _ n a m e ,   s t a t u s _ c o d e   =   b a l a n c e r . g e t _ l a s t _ f a i l u r e ( ) # s y n t a x : o k ,   e r r   =   b a l a n c e r . s e t _ t i m e o u t s ( c o n n e c t _ t i m e o u t ,   s e n d _ t i m e o u t ,   r e a d _ t i m e o u t - - i p _ l i s t s , 1 9 2 . 1 6 8 . 1 . 2 1 9 2 . 1 6 8 . 1 . 3 1 9 2 . 1 6 8 . 1 . 4 p o r t l o c a l   f u n c t i o n   f o r w a r d _ s e r v e r ( i p _ l i s t s ,   p o r t ) - -         i f   n o t   n g x . c t x . t r i e s   t h e n                 n g x . c t x . t r i e s   =   0         e n d #         i f   n g x . c t x . t r i e s   <   # i p _ l i s t s   t h e n                 l o c a l   s e t _ m o r e _ t r i e s _ o k ,   s e t _ m o r e _ t r i e s _ e r r   =   b a l a n c e r . s e t _ m o r e _ t r i e s ( 1 )                 i f   n o t   s e t _ m o r e _ t r i e s _ o k   t h e n                         n g x . l o g ( n g x . E R R ,   " f a i l e d   t o   s e t   t h e   c u r r e n t   p e e r :   " ,   s e t _ m o r e _ t r i e s _ e r r )
使 { 1 9 2 . 1 6 8 . 1 . 2 0 2 0 1 9 2 . 1 6 8 . 1 . 3 0 3 0 }                         n g x . l o g ( n g x . E R R ,   " f a i l e d   t o   s e t   t h e   c u r r e n t   p e e r :   " ,   s e t _ m o r e _ t r i e s _ e r r )                 e l s e i f   s e t _ m o r e _ t r i e s _ e r r   t h e n                         n g x . l o g ( n g x . A L E R T ,   " s e t   m o r e   t r i e s :   " ,   s e t _ m o r e _ t r i e s _ e r r )                 e n d         e n d         n g x . c t x . t r i e s   =   n g x . c t x . t r i e s   +   1 #         i f   n o t   n g x . c t x . i p _ l i s t s   t h e n                 n g x . c t x . i p _ l i s t s   =   i p _ l i s t s         e n d # I P         l o c a l   f i r s t _ c o u n t   =   { }         t a b l e . i n s e r t ( f i r s t _ c o u n t ,   s t r i n g . s u b ( n g x . v a r . r e m o t e _ a d d r ,   1 ,   1 ) )         t a b l e . i n s e r t ( f i r s t _ c o u n t ,   s t r i n g . s u b ( n g x . v a r . r e m o t e _ a d d r ,   - 1 ) )         l o c a l   i p _ c o u n t   =   ( t o n u m b e r ( t a b l e . c o n c a t ( f i r s t _ c o u n t ) )   %   # n g x . c t x . i p _ l i s t s )   +   1         l o c a l   _ h o s t   =   n g x . c t x . i p _ l i s t s [ i p _ c o u n t ]         l o c a l   s t a t e _ n a m e ,   s t a t e _ c o d e   =   b a l a n c e r . g e t _ l a s t _ f a i l u r e ( ) #         i f   s t a t e _ n a m e   = =   " f a i l e d "   t h e n                 f o r   k ,   v   i n   i p a i r s ( n g x . c t x . i p _ l i s t s )   d o                         i f   v   = =   _ h o s t   t h e n                                 i f   n o t   ( # n g x . c t x . i p _ l i s t s   = =   1 )   t h e n                                         t a b l e . r e m o v e ( n g x . c t x . i p _ l i s t s ,   k )                                         i p _ c o u n t   =   ( s t r i n g . s u b ( n g x . v a r . r e m o t e _ a d d r ,   - 1 )   %   # n g x . c t x . i p _ l i s t s )   +   1                                         _ h o s t   =   n g x . c t x . i p _ l i s t s [ i p _ c o u n t ]                                 e n d                         e n d                 e n d         e n d #         l o c a l   o k ,   e r r   =   b a l a n c e r . s e t _ c u r r e n t _ p e e r ( _ h o s t ,   p o r t )         i f   n o t   o k   t h e n                 n g x . l o g ( n g x . E R R ,   " f a i l e d   t o   s e t   t h e   c u r r e n t   p e e r :   " ,   e r r )         e n d e n d l o c a l   w e i g h t _ l i s t   =   { } l o c a l   i p l i s t   =   { } i p l i s t [ ' 1 9 2 . 1 6 8 . 1 . 2 0 ' ] = 2 0 i p l i s t [ ' 1 9 2 . 1 6 8 . 1 . 3 0 ' ] = 3 0
I P i p u t i l s   I P I P i p l i s t [ ' 1 9 2 . 1 6 8 . 1 . 3 0 ' ] = 3 0 f o r   k e y , v a l u e   i n   p a i r s ( i p l i s t )   d o         f o r   i = 1 ,   v a l u e   d o                 t a b l e . i n s e r t ( w e i g h t _ l i s t , k e y )           e n d e n d l o c a l   r a n d o m _ v a l u e   =   m a t h . r a n d o m ( 1 ,   # w e i g h t _ l i s t ) p r i n t ( r a n d o m _ v a l u e ) p r i n t ( w e i g h t _ l i s t [ r a n d o m _ v a l u e ] ) l o c a l   i p u t i l s   =   r e q u i r e   " r e s t y . w a f . i p u t i l s " l o c a l   i p _ f o r w a r d _ l i s t = { " 1 9 2 . 1 6 8 . 1 . 2 " , " 1 9 2 . 1 6 8 . 2 0 . 0 / 2 4 " } l o c a l   i p _ l i s t   =   i p u t i l s . p a r s e _ c i d r s ( i p _ f o r w a r d _ l i s t ) i f   i p u t i l s . i p _ i n _ c i d r s ( r e m o t e _ i p ,   i p _ l i s t )   t h e n       - - I P         r e t u r n     f o r w a r d _ s e r v e r ( g r a y _ s e r v e r ,   g r a y _ p o r t ) e n d l o c a l   g e o   =   r e q u i r e   ' r e s t y . w a f . m a x m i n d d b ' i f   n o t   g e o . i n i t t e d ( )   t h e n         g e o . i n i t ( " / o p t / G e o L i t e 2 - C i t y . m m d b " )   - - g e o e n d l o c a l   r e s ,   e r r   =   g e o . l o o k u p ( r e m o t e _ i p ) i f   r e s   t h e n         i f   r e s [ ' c i t y ' ]   t h e n                 l o c a l   c i t y _ n a m e   =   r e s [ ' c i t y ' ] [ ' n a m e s ' ] [ ' z h - C N ' ]                 - - I P                 - - n g x . l o g ( n g x . E R R , c i t y _ n a m e )                 f o r   _ ,   _ v a l u e   i n   p a i r s ( r e g i o n _ f o r w a r d _ d a t a )   d o
j x w a f H T T P j x w a f P S C C                         l o c a l   g r a y _ s e r v e r   =   _ v a l u e [ ' g r a y _ s e r v e r ' ]                         l o c a l   g r a y _ p o r t   =   _ v a l u e [ ' g r a y _ p o r t ' ]                         l o c a l   r e g i o n   =   _ v a l u e [ ' c o n t e n t ' ] [ 1 ] [ ' c o n t e n t ' ]                         i f   r e g i o n   = =   c i t y _ n a m e   t h e n                                 r e t u r n   f o r w a r d _ s e r v e r ( g r a y _ s e r v e r ,   g r a y _ p o r t )   - -                         e n d                 e n d         e n d e n d l o c a l   w a f   =   r e q u i r e   " r e s t y . w a f . w a f " l o c a l   r e q u e s t   =   r e q u i r e   " r e s t y . w a f . r e q u e s t " l o c a l   o p e r a t o r   =   r e q u i r e   " r e s t y . w a f . o p e r a t o r " l o c a l   t r a n s f o r m   =   r e q u i r e   " r e s t y . w a f . t r a n s f o r m "

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则