Openwrt自定義CGI實現

此文已由做者吳志勐受權網易雲社區發佈。
html

歡迎訪問網易雲社區,瞭解更多網易技術產品運營經驗。react

安裝uhttpd。

在編譯openwrt前,輸入make memuconfig,查找Network -> Web Servers/Proxies -> uhttpd,若是沒勾選則勾選。而後編譯固件。nginx


修改uhttpd配置。

安裝運行openwrt後,經過ssh登錄,修改/etc/config/uhttpd配置文件,在文件末尾添加新的web服務。添加數據基本格式以下:web

config 'uhttpd' 'ServerName' 

 option 'home' '/root/' 

 list 'listen_http' '0.0.0.0:8080' 

 option 'cgi_prefix' '/cgi-bin'

其中:django


名稱 說明
uhttpd 服務名稱,不要和系統的重名便可
home 服務的根目錄
listen_http CGI前綴,用來區分是否調用的CGI
cgi_prefix CGI前綴,用來區分是否調用的CGI


還有其餘配置,不一一列舉,好比index_page 、error_page 、listen_https、cert、key、script_timeout、network_timeout、tcp_keepalive等。
修改後須要重啓uhttpd,使用命令:瀏覽器

/etc/init.d/uhttpd restart

配置靜態頁面。

在服務的根目錄下配置web頁面,好比index.html(若是uhttpd配置未指定,則爲默認頁面)。以下是一個網上找到的登錄網頁:app

<html> 
<head><title>..................</title></head> 
<body> 
<form name="form1" action="/cgi-bin/helloworld"> 
<table align="center"> 
<tr><td align="center" colspan="2"></td></tr> 
<tr> 
<td align="right">.........</td> 
<td><input type="text" name="Username"></td> 
</tr> 
<tr> 
<td align="right">...  ...</td> 
<td><input type="password" name="Password"></td> 
</tr> 
<tr> 
<td><input type="submit" value="...  ..."></td> 
<td><input type="reset" value="...  ..."></td> 
</tr> 
</table> 
</form> 
</body> 
</html>

當用戶點擊登錄時,會跳轉訪問當前目錄下的/cgi-bin/helloworld。ssh


編寫CGI程序。

例如上一步的/cgi-bin/helloworld即爲web服務的CGI程序。本文介紹經過ipk的方式安裝CGI程序的方法。
生成ipk須要藉助openwrt的交叉編譯,首先在openwrt目錄下的package目錄下創建一個目錄,本例爲:helloworld。而後在helloworld目錄下創建Makefile文件和src目錄。其中Makefile文件的內如以下:tcp

include (TOPDIR)/rules.mkPKGNAME:=helloworldPKGRELEASE:=1PKGBUILDDIR:=(BUILD_DIR)/(PKGNAME)include(INCLUDE_DIR)/package.mk 
define Package/helloworld 

 SECTION:=utils 

 CATEGORY:=Utilities 

 TITLE:=Helloworld – prints a snarky message 
endef 
define Package/helloworld/description 

 If you can't figure out what this program does, you're  probablybrain-dead and need immediate medical attention. 
endef 
define Build/Prepare 

 mkdir -p (PKGBUILDDIR)(CP) ./src/* $(PKG_BUILD_DIR)/ 
endef 
define Package/helloworld/install 

 (INSTALLDIR)(1)/root/cgi-bin/ 

 $(INSTALL_BIN) (PKGBUILDDIR)/helloworld(1)/root/cgi-bin/ 
endef 
(eval(call BuildPackage,helloworld))

其中制定安裝路徑爲web服務的CGI路徑。
src目錄下放入本身的程序及Makefile,本例中有:
helloworld.c內容以下:測試

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

 void list_argument(void) 
{ 

  char *env_var[] = { 

    "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE", 

    "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING", 

    "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION", 

    "HTTP_HOST", "HTTP_USER_AGENT", "PATH", 

    "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT", 

    "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME", 

    "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN", 

    "SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL", 

    "SERVER_SIGNATURE","SERVER_SOFTWARE", "CONTENT_LENGTH" }; 

  char *value, i; 

 

  for (i=0; i<25; i++) { 

    value = getenv(env_var[i]); 

 

    if (value) 

    { 

 printf("<p>"); 

        printf ( "%s = %s \n", env_var[i], value); 

 printf("<\p>"); 

    } 

  } 
} 

 
int main(){ 
printf("Content-Type:text/html;charset=utf-8\n\n"); 
printf("<html>\n"); 
printf("<head>\n<title>test</title>\n</head>\n"); 
printf("<body>\n"); 
printf("<p>"); 
printf("hello~\n"); 
printf("</p>"); 
list_argument(); 
printf("</body>\n"); 
printf("</html>\n"); 
}

Makefile內容以下:

helloworld: helloworld.o 

 (CC)(LDFLAGS) helloworld.o -o helloworld 
helloworld.o: helloworld.c 

 (CC)(CFLAGS) -c helloworld.c 
clean: 

 rm *.o helloworldo

使用openwrt編譯好後,把ipa經過scp上傳到路由器下(或直接編譯到固件中),安裝:

opkg install helloworld_1_x86.ipk

以後就會在/root/cgi-bin下看見helloworld程序。調用程序能正常輸出網頁:

root@OpenWrt:~/cgi-bin# ./helloworld 
Content-Type:text/html;charset=utf-8 

 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<P>hello</p></body> 
</html>

結果輸出

瀏覽器就會打開默認頁面(192.168.8.106是測試路由器地址),點擊默認頁面的肯定按鈕,就會返回CGI的輸出,以下圖:

hello~  
DOCUMENT_ROOT = /root  
GATEWAY_INTERFACE = CGI/1.1  
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8  
HTTP_ACCEPT_ENCODING = gzip, deflate  
HTTP_ACCEPT_LANGUAGE = zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3  
HTTP_CONNECTION = keep-alive  
HTTP_HOST = 192.168.8.106:8080  
HTTP_USER_AGENT = Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:29.0) Gecko/20100101 Firefox/29.0  
PATH = /sbin:/usr/sbin:/bin:/usr/bin  
QUERY_STRING = Username=11&Password=222  
REMOTE_ADDR = 192.168.8.103  
REMOTE_PORT = 53874  
REQUEST_METHOD = GET  
REQUEST_URI = /cgi-bin/helloworld?Username=11&Password=222  
SCRIPT_FILENAME = /root/cgi-bin/helloworld  
SCRIPT_NAME = /cgi-bin/helloworld  
SERVER_ADDR = 192.168.8.106  
SERVER_NAME = 192.168.8.106  
SERVER_PORT = 8080  
SERVER_PROTOCOL = HTTP/1.1  
SERVER_SOFTWARE = uHTTPd



網易雲免費體驗館,0成本體驗20+款雲產品! 

更多網易技術、產品、運營經驗分享請點擊


相關文章:
【推薦】 MongoDB複製集成員及狀態轉換
【推薦】 django項目在uwsgi+nginx上部署遇到的坑
【推薦】 react技術棧實踐(1)

相關文章
相關標籤/搜索