A Rate-Limiting HTTP Proxy(6)Cookie/HTTPS/TIMER

A Rate-Limiting HTTP Proxy(6)Cookie/HTTPS/TIMER

Cookie Operation
https://github.com/362228416/openresty-web-dev/tree/master/demo12
https://github.com/cloudflare/lua-resty-cookie

HTTPS
https://github.com/362228416/openresty-web-dev/tree/master/demo13

TIMER
https://github.com/362228416/openresty-web-dev/tree/master/demo14

NGX document
https://github.com/openresty/lua-nginx-module#nginx-api-for-lua

Package
try with the example of MD5
https://openresty.org/cn/using-luarocks.html
https://github.com/blunt-object/openresty-luarocks

Install lua ENV
http://sillycat.iteye.com/admin/blogs/2376002
https://www.lua.org/download.html

On MAC
>curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz
unzip the file
>make macosx test
>sudo make macosx install
Verify the installation
>lua -v
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio

Where is the file
>which lua
/usr/local/bin/lua


On CentOS
>curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz
unzip the file
http://jackatlinux.blogspot.com/2013/11/installing-lua-and-luarocks-in-centos.html
>make linux
Exception:
lua.c:82:31: fatal error:
readline/readline.h: No such file or directory
#include <readline/readline.h>

Solution:
>sudo yum install readline-devel
>make linux
>sudo make install
>lua -v
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio

>which lua
/usr/local/bin/lua

On RaspberryPi
>curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz
unzip the file
Same Exception as CentOS when I do
>make linux test
lua.c:82:31: fatal error: readline/readline.h: No such file or directory
#include <readline/readline.h>

Solution:
>sudo apt-get install libreadline-dev
>make linux test
>sudo make install

>lua -v
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio

>which lua
/usr/local/bin/lua


Install luarocks
On MAC
>wget http://luarocks.github.io/luarocks/releases/luarocks-2.4.2.tar.gz
>./configure --prefix=/Users/carl/tool/luarocks-2.4.2
>make build
>sudo make install
Link the bin directory and add to PATH
Verify the installation
>luarocks help

On CentOS
>wget http://luarocks.github.io/luarocks/releases/luarocks-2.4.2.tar.gz
>./configure --prefix=/home/ec2-user/tool/luarocks-2.4.2
>make build
>sudo make install
Lin the bin directory and add to PATH
>sudo ln -s /home/ec2-user/tool/luarocks-2.4.2 /opt/luarocks-2.4.2
>sudo ln -s /opt/luarocks-2.4.2 /opt/luarocks
>luarocks help

On RaspberryPI
>wget http://luarocks.github.io/luarocks/releases/luarocks-2.4.2.tar.gz
>./configure --prefix=/home/carl/tool/luarocks-2.4.2 --with-lua=/usr/local
>make build
>sudo make install
>luarocks help

Search Package
>luarocks search vanilla

Search results:
===============


Rockspecs and source rocks:
---------------------------

vanilla
0.1.0.rc4-1 (rockspec) - https://luarocks.org
0.1.0.rc4-1 (src) - https://luarocks.org
0.1.0.rc3-1 (rockspec) - https://luarocks.org
0.1.0.rc3-1 (src) - https://luarocks.org
0.1.0.rc2-1 (rockspec) - https://luarocks.org
0.1.0.rc2-1 (src) - https://luarocks.org
0.1.rc1-1 (rockspec) - https://luarocks.org
0.1.rc1-1 (src) - https://luarocks.org
0.0.1-1 (rockspec) - https://luarocks.org
0.0.1-1 (src) - https://luarocks.org

Install Package on Local
>luarocks install json4lua --local

List the packages
>luarocks list

Installed rocks:
----------------

json4lua
0.9.30-1 (installed) - /Users/carl/.luarocks/lib/luarocks/rocks

Install MD5 package
>sudo luarocks install md5

Update luarocks
>sudo luarocks install luarocks

Check path
> lua -e "print(package.cpath)"
/usr/local/lib/lua/5.3/?.so;/usr/local/lib/lua/5.3/loadall.so;./?.so

>lua -e "print(package.path)"
/usr/local/share/lua/5.3/?.lua;
/usr/local/share/lua/5.3/?/init.lua;
/usr/local/lib/lua/5.3/?.lua;
/usr/local/lib/lua/5.3/?/init.lua;
./?.lua;
./?/init.lua

>sudo find /usr/local -name md5
Password:
/usr/local/go/src/crypto/md5

>luarocks list

Installed rocks:
----------------
luarocks
2.4.2-1 (installed) - /Users/carl/tool/luarocks-2.4.2/lib/luarocks/rocks
md5
1.2-1 (installed) - /Users/carl/tool/luarocks-2.4.2/lib/luarocks/rocks

https://stackoverflow.com/questions/6803582/luajit-not-seeing-rocks-installed-by-luarocks
http://wgxsoft.blog.163.com/blog/static/17381917120146234053774/

Reinstall luarocks
>sudo ./configure --with-lua="/opt/openresty/luajit" --lua-suffix="jit" --with-lua-include="/opt/openresty/luajit/include/luajit-2.1”
>sudo make build
>sudo make install
>luarocks --version
/usr/local/bin/luarocks 2.4.2
LuaRocks main command-line interface

>luarocks list

Installed rocks:
----------------
md5
1.2-1 (installed) - /usr/local/lib/luarocks/rocks

In my lua Code, I will do
local rocks = require "luarocks.loader"
local md5 = require “md5"

function _M
.index()

-- ngx.say(md5.sumhexa("testforfun"));

ngx.say(
cjson.encode(users))end

It will work pretty well.

package.sh will compile all lua to binary
>cat package.sh
#!/usr/bin/env bash

rm -rf ./dist
mkdir -p dist/app/logs
cp -rf conf dist/app
cp -rf html dist/app
cp -rf lua dist/app
cp -rf lualib dist/app

luajit=/opt/openresty/luajit/bin/luajit

function compile() {
for file in $1
do
if test -f $file
then
$luajit -b $file $file
fi
done
}

compile "./dist/app/lua/web/*"
compile "./dist/app/lua/*"


References:
https://github.com/362228416/openresty-web-dev/tree/master/demo12
https://github.com/362228416/openresty-web-dev/tree/master/demo13

package 1
https://my.oschina.net/362228416/blog/846741
https://luarocks.org/
https://openresty.org/cn/using-luarocks.html
http://blog.leanote.com/post/wechat-fee3/b06945bcb7cc
http://dhq.me/luarocks-a-deployment-and-management-system-for-lua-modules
https://segmentfault.com/a/1190000003920034

package 2
https://luarocks.org/#quick-start

lua notes
http://nanxiao.me/category/%E6%8A%80%E6%9C%AF/lua%E7%AC%94%E8%AE%B0/
//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include <SI_C8051F300_Register_Enums.h> #include <stdio.h> //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define SYSCLK 24500000 // SYSCLK frequency in Hz #define BAUDRATE 115200 // Baud rate of UART in bps SI_SBIT(DE_RE, SFR_P0, 6); //定義P0.6 控制名稱為DE_RE SI_SBIT(LED2, SFR_P0, 1); //定義P0.1 控制名稱為LED2 SI_SBIT(LED1, SFR_P0, 0); //定義P0.0 控制名稱為LED1 SI_SBIT(LED3, SFR_P0, 2); //定義P0.2 控制名稱為LED3 SI_SBIT(LED4, SFR_P0, 3); //定義P0.3 控制名稱為LED4 SI_SBIT(LED5, SFR_P0, 7); //定義P0.7 控制名稱為LED5 //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- SI_INTERRUPT_PROTO(UART0_Interrupt, 4); void SYSCLK_Init (void); void UART0_Init (void); void PORT_Init (void); void Timer2_Init (int16_t); void MyDelay (uint16_t Value); #define KEY_DOWN 1 #define KEY_UP 0 #define LED_ON 0 #define LED_OFF 1 //----------------------------------------------------------------------------- // Global Variables //----------------------------------------------------------------------------- #define UART_BUFFERSIZE 6 uint8_t UART_Buffer[UART_BUFFERSIZE]; uint8_t UART_Buffer_Size = 0; uint8_t UART_Input_First = 0; uint8_t UART_Output_First = 0; uint8_t TX_Ready =1; uint8_t RX_Ready =0; static char Byte; uint8_t SnapDown_TimeCnt =0; uint8_t fSnap = 0; uint8_t RecDown_TimeCnt =0; uint8_t fRec = 0; //----------------------------------------------------------------------------- // SiLabs_Startup() Routine // ---------------------------------------------------------------------------- // This function is called immediately after reset, before the initialization // code is run in SILABS_STARTUP.A51 (which runs before main() ). This is a // useful place to disable the watchdog timer, which is enable by default // and may trigger before main() in some instances. //----------------------------------------------------------------------------- void SiLabs_Startup (void) { PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer } //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void) { // enable) PORT_Init(); // Initialize Port I/O SYSCLK_Init (); // Initialize Oscillator UART0_Init(); IE_EA = 1; DE_RE =0; Timer2_Init (SYSCLK / 12 / 1000); // Init Timer2 to generate // 每1ms 計時中斷 while(1) { if(RX_Ready) { RX_Ready = 0; UART_Buffer_Size =0; if(UART_Buffer[3]==0x01)//任一鍵按下 { if(UART_Buffer[4]==0x01)//REC { fRec = KEY_DOWN; RecDown_TimeCnt=0; } else if(UART_Buffer[4]==0x02)//SNAP { fSnap = KEY_DOWN; SnapDown_TimeCnt=0; } else if(UART_Buffer[4]==0x03)//LED UP LED3 = LED_ON; else if(UART_Buffer[4]==0x04)//LED DO LED4 = LED_ON; else if(UART_Buffer[4]==0x05)//Mirror LED5 = LED_ON; } if(UART_Buffer[3]==0x00)//任一鍵放開 { if(UART_Buffer[4]==0x01)//REC { fRec = KEY_UP; } else if(UART_Buffer[4]==0x02)//SNAP { fSnap = KEY_UP; } else if(UART_Buffer[4]==0x03)//LED UP LED3 = LED_OFF; else if(UART_Buffer[4]==0x04)//LED DO LED4 = LED_OFF; else if(UART_Buffer[4]==0x05)//Mirror LED5 = LED_OFF; } IE_ES0 =1; } if(SnapDown_TimeCnt >55 && fSnap == KEY_DOWN) { LED1 = LED_ON; } else if(SnapDown_TimeCnt >55 && fSnap == KEY_UP) { LED1 =LED_OFF; SnapDown_TimeCnt =0; } else if(SnapDown_TimeCnt >45 && SnapDown_TimeCnt <55 && fSnap == KEY_UP) { LED1 = LED_ON; LED2 = LED_ON; MyDelay(1100);//延遲約0.5秒 LED1 = LED_OFF; LED2 = LED_OFF; SnapDown_TimeCnt =0; } if(RecDown_TimeCnt >55 && fRec == KEY_DOWN) { LED2 = LED_ON; } else if(RecDown_TimeCnt >55 && fRec == KEY_UP) { LED2 = LED_OFF; RecDown_TimeCnt =0; } else if(RecDown_TimeCnt >45 && RecDown_TimeCnt <55 && fRec == KEY_UP) { LED4 = LED_ON; LED5 = LED_ON; MyDelay(1100);//延遲約0.5秒 LED4 = LED_OFF; LED5 = LED_OFF; RecDown_TimeCnt =0; } } } //----------------------------------------------------------------------------- // Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure the Crossbar and GPIO ports. // // P0.4 digital push-pull UART TX // P0.5 digital open-drain UART RX // //----------------------------------------------------------------------------- void PORT_Init (void) { P0MDOUT |= 0x10; // set UART TX to push-pull output XBR1 = 0x03; // Enable UTX, URX as push-pull output XBR2 = 0x40; // Enable crossbar, weak pull-ups // disabled XBR0 = 0x40; P0MDOUT |= 0x40; } //----------------------------------------------------------------------------- // SYSCLK_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This routine initializes the system clock to use the internal oscillator // at its maximum frequency. // Also enables the Missing Clock Detector. //----------------------------------------------------------------------------- void SYSCLK_Init (void) { OSCICN |= 0x03; // Configure internal oscillator for // its maximum frequency RSTSRC = 0x04; // Enable missing clock detector } //----------------------------------------------------------------------------- // UART0_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1. //----------------------------------------------------------------------------- void UART0_Init (void) { SCON0 = 0x10; // SCON0: 8-bit variable bit rate // level of STOP bit is ignored // RX enabled // ninth bits are zeros // clear SCON0_RI and SCON0_TI bits if (SYSCLK/BAUDRATE/2/256 < 1) { TH1 = -(SYSCLK/BAUDRATE/2); CKCON |= 0x10; // T1M = 1; SCA1:0 = xx } else if (SYSCLK/BAUDRATE/2/256 < 4) { TH1 = -(SYSCLK/BAUDRATE/2/4); CKCON |= 0x01; // T1M = 0; SCA1:0 = 01 CKCON &= ~0x12; } else if (SYSCLK/BAUDRATE/2/256 < 12) { TH1 = -(SYSCLK/BAUDRATE/2/12); CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00 } else { TH1 = -(SYSCLK/BAUDRATE/2/48); CKCON |= 0x02; // T1M = 0; SCA1:0 = 10 CKCON &= ~0x11; } TL1 = 0xff; // set Timer1 to overflow immediately TMOD |= 0x20; // TMOD: timer 1 in 8-bit autoreload TMOD &= ~0xD0; // mode TCON_TR1 = 1; // START Timer1 TX_Ready = 1; // Flag showing that UART can transmit IP |= 0x10; // Make UART high priority IE_ES0 = 1; // Enable UART0 interrupts } //----------------------------------------------------------------------------- // Interrupt Service Routines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // UART0_Interrupt //----------------------------------------------------------------------------- // // This routine is invoked whenever a character is entered or displayed on the // Hyperterminal. // //----------------------------------------------------------------------------- SI_INTERRUPT(UART0_Interrupt, 4) { if (SCON0_RI == 1) { if( UART_Buffer_Size == 0) { // If new word is entered UART_Input_First = 0; } SCON0_RI = 0; // Clear interrupt flag Byte = SBUF0; // Read a character from UART if (UART_Buffer_Size < UART_BUFFERSIZE) { UART_Buffer[UART_Input_First] = Byte; // Store in array UART_Buffer_Size++; // Update array's size UART_Input_First++; // Update counter } if(UART_Buffer_Size >= UART_BUFFERSIZE) { RX_Ready =1; UART_Buffer_Size =0; IE_ES0 = 0; } } if (SCON0_TI == 1) // Check if transmit flag is set { SCON0_TI = 0; // Clear interrupt flag if (UART_Buffer_Size != 1) // If buffer not empty { // If a new word is being output if ( UART_Buffer_Size == UART_Input_First ) { UART_Output_First = 0; } // Store a character in the variable byte Byte = UART_Buffer[UART_Output_First]; if ((Byte >= 0x61) && (Byte <= 0x7A)) { // If upper case letter Byte -= 32; } SBUF0 = Byte; // Transmit to Hyperterminal UART_Output_First++; // Update counter UART_Buffer_Size--; // Decrease array size } else { UART_Buffer_Size = 0; // Set the array size to 0 TX_Ready = 1; // Indicate transmission complete } } } //----------------------------------------------------------------------------- // Timer2_Init //----------------------------------------------------------------------------- // // Configure Timer2 to 16-bit auto-reload and generate an interrupt at // interval specified by <counts> using SYSCLK/12 as its time base. // void Timer2_Init (int16_t counts) { TMR2CN = 0x00; // Stop Timer2; Clear TF2; // use SYSCLK/12 as timebase CKCON &= ~(CKCON_T2MH__BMASK | CKCON_T2ML__BMASK ); // Timer2 clocked based on TMR2CN_T2XCLK TMR2RL = -counts; // Init reload values TMR2 = 0xffff; // set to reload immediately IE_ET2 = 1; // enable Timer2 interrupts TMR2CN_TR2 = 1; // start Timer2 } //----------------------------------------------------------------------------- // Timer2_ISR //----------------------------------------------------------------------------- // This routine changes the state of the LED whenever Timer2 overflows. // SI_INTERRUPT(Timer2_ISR, TIMER2_IRQn) { TMR2CN_TF2H = 0; // clear Timer2 interrupt flag if(fSnap == KEY_DOWN) { SnapDown_TimeCnt++; // change state of LED if(SnapDown_TimeCnt==255) SnapDown_TimeCnt = 254; } else if(fRec == KEY_DOWN) { RecDown_TimeCnt++; // change state of LED if(RecDown_TimeCnt==255) RecDown_TimeCnt = 254; } } void MyDelay(uint16_t Value) { uint16_t i,j; for(i=0;i<Value;i++) { for(j=0;j<1000;j++) _nop_(); } } //----------------------------------------------------------------------------- // End Of File //-----------------------------------------------------------------------------
07-04
### UART Communication and LED Control on C8051F300 Microcontroller The C8051F300 microcontroller is a popular choice for embedded systems due to its integrated peripherals, including UART communication capabilities. Understanding how to implement UART communication and control LEDs can be essential for various applications, such as data logging, sensor interfacing, and user feedback mechanisms. To establish UART communication, the microcontroller must be configured with appropriate settings for baud rate, data bits, parity, and stop bits. The following code snippet demonstrates how to initialize the UART module: ```c #include <C8051F300.h> void UART_Init(void) { SCON = 0x50; // Set UART mode to 8-bit, variable baud rate TMOD |= 0x20; // Timer 1 in mode 2 (8-bit auto-reload) TH1 = 0xFD; // Baud rate setting for 9600 bps TR1 = 1; // Start Timer 1 TI = 1; // Indicate that transmitter is ready } ``` In this initialization function, `SCON` is set to configure the UART for 8-bit data format with variable baud rate. The `TMOD` register is configured to operate Timer 1 in mode 2, which allows for an 8-bit auto-reload feature necessary for generating accurate baud rates. The value loaded into `TH1` determines the baud rate, where `0xFD` corresponds to a baud rate of 9600 bps[^1]. For LED control, the microcontroller's GPIO pins can be manipulated to turn LEDs on or off based on specific conditions within the program. Typically, an LED might be connected to a port pin through a current-limiting resistor to ground. Here's an example of how to toggle an LED connected to P1.0: ```c void Toggle_LED(void) { P1 ^= 0x01; // Toggle P1.0 } ``` This function uses bitwise XOR operation to invert the state of P1.0, effectively toggling the LED connected to it. Debugging issues related to UART communication often involves checking the configuration registers (`SCON`, `TMOD`, `TH1`) and ensuring that the correct baud rate is being used. Additionally, verifying the physical connections between devices and using oscilloscopes or logic analyzers can help identify timing issues or incorrect signal levels. When dealing with LED control, common problems include incorrect pin configurations, faulty wiring, or misinterpretation of active-high versus active-low signals. It's crucial to double-check the microcontroller's datasheet to ensure proper configuration of the I/O ports and any necessary pull-up or pull-down resistors.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值