├── Makefile ├── README.md ├── files ├── etc │ ├── config │ │ └── haproxy │ ├── haproxy_init.sh │ └── haproxy_start └── usr │ └── lib │ └── lua │ └── luci │ ├── controller │ └── haproxy.lua │ └── model │ └── cbi │ └── haproxy.lua ├── i18n └── zh-cn │ └── haproxy.zh-cn.po └── tools └── po2lmo ├── Makefile └── src ├── po2lmo ├── po2lmo.c ├── po2lmo.o ├── template_lmo.c ├── template_lmo.h └── template_lmo.o /Makefile: -------------------------------------------------------------------------------- 1 | include $(TOPDIR)/rules.mk 2 | 3 | PKG_NAME:=luci-app-haproxy-tcp 4 | PKG_VERSION=1.5 5 | PKG_RELEASE:=1 6 | PKG_MAINTAINER:=Alex Zhuo <1886090@gmail.com> 7 | 8 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) 9 | 10 | include $(INCLUDE_DIR)/package.mk 11 | 12 | define Package/$(PKG_NAME) 13 | CATEGORY:=Utilities 14 | SUBMENU:=Luci 15 | TITLE:=luci for haproxy and shadowsocks 16 | PKGARCH:=all 17 | DEPENDS:=+haproxy 18 | endef 19 | 20 | define Package/$(PKG_NAME)/description 21 | A luci app for haproxy with shadowsocks 22 | endef 23 | 24 | define Package/$(PKG_NAME)/postinst 25 | #!/bin/sh 26 | rm -rf /tmp/luci* 27 | echo stopping haproxy 28 | /etc/init.d/haproxy stop 29 | /etc/init.d/haproxy disable 30 | echo haproxy disabled 31 | endef 32 | 33 | define Build/Prepare 34 | $(foreach po,$(wildcard ${CURDIR}/i18n/zh-cn/*.po), \ 35 | po2lmo $(po) $(PKG_BUILD_DIR)/$(patsubst %.po,%.lmo,$(notdir $(po)));) 36 | endef 37 | 38 | define Build/Configure 39 | endef 40 | 41 | define Build/Compile 42 | endef 43 | 44 | define Package/$(PKG_NAME)/install 45 | $(INSTALL_DIR) $(1)/usr/lib/lua/luci/i18n 46 | $(INSTALL_DATA) $(PKG_BUILD_DIR)/*.*.lmo $(1)/usr/lib/lua/luci/i18n/ 47 | $(CP) ./files/* $(1)/ 48 | 49 | endef 50 | 51 | $(eval $(call BuildPackage,$(PKG_NAME))) 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # luci-app-haproxy-tcp 2 | OpenWrt HAProxy的Luci配置页面,已在[该固件][A]中使用 3 | 4 | 简介 5 | --- 6 | 7 | 本软件包为OpenWRT HAPrxoy的 LuCI 控制界面,用于Shadowsocks在多服务器条件下实现负载均衡和高可用 8 | 9 | 可以设置多个主服务器或多个备用服务器. 默认监听端口127.0.0.1:2222 后台监控页面端口0.0.0.0:1111,后台监控页面地址192.168.1.1:1111/haproxy 10 | 11 | 多主服务器是将所有TCP流量分流,并可以设置每个服务器的分流比例;多备用服务器是在检测到主服务器A宕机之后切换至备用服务器B,B宕机之后切换到服务器C...依次类推,可以防止因为单个服务器或者线路故障导致的断网问题。 12 | 使用效果和更多使用方法请[点击这里][A] 13 | 14 | 15 | 依赖 16 | --- 17 | 18 | 显式依赖 `haproxy`, 安装完毕该luci包后会stop并disable当前op的haproxy,点击“保存&应用”后会修改HAProxy默认配置文件/etc/haproxy.cfg并自动重启,支持开机自启. 19 | 20 | 21 | 配置 22 | --- 23 | 24 | 如果有需要,可以修改/etc/haproxy_init.sh ,不要直接修改/etc/haproxy.cfg,否则会被覆盖 25 | 26 | 编译 27 | --- 28 | 29 | 从 OpenWrt 的 [SDK][openwrt-sdk] 编译 30 | ```bash 31 | # 解压下载好的 SDK 32 | tar xjf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 33 | cd OpenWrt-SDK-ar71xx-* 34 | # Clone 项目 35 | git clone https://github.com/AlexZhuo/luci-app-haproxy-tcp package/luci-app-haproxy-tcp 36 | # 编译 po2lmo (如果有po2lmo可跳过) 37 | pushd package/luci-app-haproxy-tcp/tools/po2lmo 38 | make && sudo make install 39 | popd 40 | # 选择要编译的包 Utilities -> LuCI -> luci-app-haproxy-tcp 41 | make menuconfig 42 | # 开始编译 43 | make package/luci-app-haproxy-tcp/compile V=99 44 | ``` 45 | 46 | 47 | 截图 48 | --- 49 | ![](https://github.com/AlexZhuo/BreakwallOpenWrt/raw/master/screenshots/haproxy.png) 50 | 51 | [A]: http://www.right.com.cn/forum/thread-198649-1-1.html 52 | [openwrt-sdk]: http://wiki.openwrt.org/doc/howto/obtain.firmware.sdk 53 | 54 | 55 | -------------------------------------------------------------------------------- /files/etc/config/haproxy: -------------------------------------------------------------------------------- 1 | 2 | config arguments 3 | option enabled '0' 4 | 5 | config main_server 6 | option server_weight '10' 7 | option server_ip '1.2.3.4' 8 | option server_port '443' 9 | option server_name 'JP1' 10 | option validate '1' 11 | 12 | config backup_server 13 | option server_name 'JP2' 14 | option server_ip '2.2.2.2' 15 | option server_port '8038' 16 | option validate '1' 17 | 18 | config backup_server 19 | option server_name 'JP3' 20 | option server_ip '3.3.3.3' 21 | option server_port '443' 22 | option validate '1' 23 | 24 | config backup_server 25 | option server_name 'JP4' 26 | option server_ip '4.4.4.4' 27 | option server_port '443' 28 | option validate '1' 29 | 30 | -------------------------------------------------------------------------------- /files/etc/haproxy_init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | 3 | CFG_FILE=/etc/haproxy.cfg 4 | stop(){ 5 | logger -t alex stopping haproxy 6 | echo "stopping haproxy" 7 | /etc/init.d/haproxy disable 8 | /etc/init.d/haproxy stop 9 | [ -f /etc/haproxy_backup ] && { 10 | cp /etc/haproxy_backup /etc/init.d/haproxy 11 | } 12 | iptables -t nat -D OUTPUT -j HAPROXY &> /dev/null 13 | iptables -t nat -F HAPROXY &> /dev/null 14 | sleep 1 15 | iptables -t nat -X HAPROXY &> /dev/null 16 | } 17 | start(){ 18 | echo "starting haproxy" 19 | logger -t alex restarting haproxy 20 | local balance=`uci get haproxy.@arguments[0].balance 2>/dev/null` 21 | [ -z $balance ] && balance=roundrobin 22 | cat > $CFG_FILE </dev/null` 72 | local server_name=`uci get haproxy.@main_server[$COUNTER].server_name 2>/dev/null` 73 | local server_port=`uci get haproxy.@main_server[$COUNTER].server_port 2>/dev/null` 74 | local server_weight=`uci get haproxy.@main_server[$COUNTER].server_weight 2>/dev/null` 75 | local validate=`uci get haproxy.@main_server[$COUNTER].validate 2>/dev/null` 76 | if [ -z "$server_ip" ] || [ -z "$server_name" ] || [ -z "$server_port" ] || [ -z "$server_weight" ]; then 77 | echo break 78 | break 79 | fi 80 | echo the main server $COUNTER $server_ip $server_name $server_port $server_weight 81 | [ "$validate" = 1 ] && { 82 | echo server $server_name $server_ip:$server_port weight $server_weight maxconn 4096 check inter 1500 rise 3 fall 3 >> $CFG_FILE 83 | } 84 | iptables -t nat -A HAPROXY -p tcp -d $server_ip -j ACCEPT 85 | COUNTER=$(($COUNTER+1)) 86 | done 87 | COUNTER=0 88 | #添加备用服务器 89 | while true 90 | do 91 | local server_ip=`uci get haproxy.@backup_server[$COUNTER].server_ip 2>/dev/null` 92 | local server_name=`uci get haproxy.@backup_server[$COUNTER].server_name 2>/dev/null` 93 | local server_port=`uci get haproxy.@backup_server[$COUNTER].server_port 2>/dev/null` 94 | local validate=`uci get haproxy.@backup_server[$COUNTER].validate 2>/dev/null` 95 | if [ -z "$server_ip" ] || [ -z "$server_name" ] || [ -z "$server_port" ]; then 96 | echo break 97 | break 98 | fi 99 | echo the backup server $COUNTER $server_ip $server_name $server_port 100 | [ "$validate" = 1 ] && { 101 | echo server $server_name $server_ip:$server_port weight 10 check backup inter 1500 rise 3 fall 3 >> $CFG_FILE 102 | } 103 | iptables -t nat -A HAPROXY -p tcp -d $server_ip -j ACCEPT 104 | COUNTER=$(($COUNTER+1)) 105 | done 106 | iptables -t nat -I OUTPUT -j HAPROXY 107 | /etc/init.d/haproxy enable 108 | /etc/init.d/haproxy restart 109 | cp /etc/init.d/haproxy /etc/haproxy_backup 110 | cp /etc/haproxy_start /etc/init.d/haproxy 111 | } 112 | 113 | restart(){ 114 | echo luci for haproxy 115 | sleep 1s 116 | local vt_enabled=`uci get haproxy.@arguments[0].enabled 2>/dev/null` 117 | logger -t alex !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!haproxy is initializing enabled is $vt_enabled 118 | echo $vt_enabled 119 | if [ "$vt_enabled" = 1 ]; then 120 | [ -f /etc/haproxy_backup ] && { 121 | cp /etc/haproxy_backup /etc/init.d/haproxy 122 | } 123 | iptables -t nat -D OUTPUT -j HAPROXY &> /dev/null 124 | iptables -t nat -F HAPROXY &> /dev/null 125 | sleep 1 126 | iptables -t nat -X HAPROXY &> /dev/null 127 | start; 128 | else 129 | stop; 130 | fi 131 | } -------------------------------------------------------------------------------- /files/etc/haproxy_start: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | # Copyright (C) 2009-2010 OpenWrt.org 3 | 4 | START=99 5 | STOP=80 6 | 7 | SERVICE_USE_PID=1 8 | 9 | HAPROXY_BIN="/usr/sbin/haproxy" 10 | HAPROXY_CONFIG="/etc/haproxy.cfg" 11 | HAPROXY_PID="/var/run/haproxy.pid" 12 | 13 | start() { 14 | service_start $HAPROXY_BIN -q -D -f "$HAPROXY_CONFIG" -p "$HAPROXY_PID" 15 | local COUNTER=0 16 | #添加主服务器 17 | iptables -t nat -D OUTPUT -j HAPROXY &> /dev/null 18 | iptables -t nat -X HAPROXY 19 | iptables -t nat -N HAPROXY 20 | iptables -t nat -F HAPROXY 21 | 22 | while true 23 | do 24 | local server_ip=`uci get haproxy.@main_server[$COUNTER].server_ip 2>/dev/null` 25 | local server_name=`uci get haproxy.@main_server[$COUNTER].server_name 2>/dev/null` 26 | local server_port=`uci get haproxy.@main_server[$COUNTER].server_port 2>/dev/null` 27 | local server_weight=`uci get haproxy.@main_server[$COUNTER].server_weight 2>/dev/null` 28 | local validate=`uci get haproxy.@main_server[$COUNTER].validate 2>/dev/null` 29 | if [ -z "$server_ip" ] || [ -z "$server_name" ] || [ -z "$server_port" ] || [ -z "$server_weight" ]; then 30 | echo break 31 | break 32 | fi 33 | echo the main2 server $COUNTER $server_ip $server_name $server_port $server_weight 34 | [ "$validate" = 1 ] && { 35 | iptables -t nat -A HAPROXY -p tcp -d $server_ip -j ACCEPT 36 | } 37 | COUNTER=$(($COUNTER+1)) 38 | done 39 | COUNTER=0 40 | #添加备用服务器 41 | while true 42 | do 43 | local server_ip=`uci get haproxy.@backup_server[$COUNTER].server_ip 2>/dev/null` 44 | local server_name=`uci get haproxy.@backup_server[$COUNTER].server_name 2>/dev/null` 45 | local server_port=`uci get haproxy.@backup_server[$COUNTER].server_port 2>/dev/null` 46 | local validate=`uci get haproxy.@backup_server[$COUNTER].validate 2>/dev/null` 47 | if [ -z "$server_ip" ] || [ -z "$server_name" ] || [ -z "$server_port" ]; then 48 | echo break 49 | break 50 | fi 51 | echo the backup2 server $COUNTER $server_ip $server_name $server_port 52 | [ "$validate" = 1 ] && { 53 | iptables -t nat -A HAPROXY -p tcp -d $server_ip -j ACCEPT 54 | } 55 | COUNTER=$(($COUNTER+1)) 56 | done 57 | 58 | iptables -t nat -I OUTPUT -j HAPROXY 59 | } 60 | 61 | stop() { 62 | kill -9 $(cat $HAPROXY_PID | tr "\n" " ") 63 | service_stop $HAPROXY_BIN 64 | iptables -t nat -D OUTPUT -j HAPROXY &> /dev/null 65 | iptables -t nat -F HAPROXY &> /dev/null 66 | sleep 1 67 | iptables -t nat -X HAPROXY &> /dev/null 68 | } 69 | 70 | reload() { 71 | $HAPROXY_BIN -D -q -f $HAPROXY_CONFIG -p $HAPROXY_PID -sf $(cat $HAPROXY_PID | tr "\n" " ") 72 | #$HAPROXY_BIN -D -q -f $HAPROXY_CONFIG -p $HAPROXY_PID -sf $(cat $HAPROXY_PID) 73 | } 74 | -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/controller/haproxy.lua: -------------------------------------------------------------------------------- 1 | module("luci.controller.haproxy", package.seeall) 2 | function index() 3 | if not nixio.fs.access("/etc/config/haproxy") then 4 | return 5 | end 6 | entry({"admin", "services", "haproxy"}, cbi("haproxy"), _("HAProxy")).dependent = true 7 | end -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/model/cbi/haproxy.lua: -------------------------------------------------------------------------------- 1 | --Alex<1886090@gmail.com> 2 | local fs = require "nixio.fs" 3 | 4 | function sync_value_to_file(value, file) --用来写入文件的函数,目前这种方式已经弃用 5 | value = value:gsub("\r\n?", "\n") 6 | local old_value = nixio.fs.readfile(file) 7 | if value ~= old_value then 8 | nixio.fs.writefile(file, value) 9 | end 10 | end 11 | local state_msg = "" 12 | local haproxy_on = (luci.sys.call("pidof haproxy > /dev/null") == 0) 13 | local router_ip = luci.sys.exec("uci get network.lan.ipaddr") 14 | if haproxy_on then 15 | state_msg = "" .. translate("Running") .. "" 16 | else 17 | state_msg = "" .. translate("Not running") .. "" 18 | end 19 | m=Map("haproxy",translate("HAProxy"),translate("HAProxy能够检测Shadowsocks服务器的连通情况,从而实现负载均衡和高可用的功能,支持主备用服务器宕机自动切换,并且可以设置多个主服务器用于分流,规定每个分流节点的流量比例等。前提条件是你的所有Shadowsocks服务器的【加密方式】和【密码】一致。

使用方法:配置好你的Shadowsocks服务器ip地址和端口,然后开启Shadowsocks服务,将服务器地址填写为【127.0.0.1】,端口【2222】,其他参数和之前一样即可,你可以通过访问【路由器的IP:1111/haproxy】输入用户名admin,密码root来观察各节点健康状况,红色为宕机,绿色正常,使用说明请点击这里") .. "

后台监控页面:" .. router_ip .. ":1111/haproxy 用户名admin,密码root" .. "

状态 - " .. state_msg) 20 | s=m:section(TypedSection,"arguments","") 21 | s.addremove=false 22 | s.anonymous=true 23 | view_enable = s:option(Flag,"enabled",translate("Enable")) 24 | balance=s:option(ListValue,"balance",translate("Balance Strategy")) 25 | balance:value("roundrobin",translate("roundrobin")) 26 | balance:value("leastconn",translate("leastconn")) 27 | balance:value("source",translate("source")) 28 | --通过读写配置文件控制HAProxy这种方式已经弃用 29 | --view_cfg = s:option(TextValue, "1", nil) 30 | --view_cfg.rmempty = false 31 | --view_cfg.rows = 43 32 | 33 | --function view_cfg.cfgvalue() 34 | -- return nixio.fs.readfile("/etc/haproxy.cfg") or "" 35 | --end 36 | --function view_cfg.write(self, section, value) 37 | -- sync_value_to_file(value, "/etc/haproxy.cfg") 38 | --end 39 | s=m:section(TypedSection,"main_server","" .. translate("Main Server List") .. "") 40 | s.anonymous=true 41 | s.addremove=true 42 | o=s:option(Value,"server_name",translate("Display Name"),translate("Only English Characters,No spaces")) 43 | o.rmempty = false 44 | 45 | o=s:option(Flag,"validate",translate("validate")) 46 | 47 | o=s:option(Value,"server_ip",translate("Proxy Server IP")) 48 | o.datatype="ip4addr" 49 | o=s:option(Value,"server_port",translate("Proxy Server Port")) 50 | o.datatype="uinteger" 51 | o=s:option(Value,"server_weight",translate("Weight")) 52 | o.datatype="uinteger" 53 | 54 | s=m:section(TypedSection,"backup_server","" .. translate("Backup Server List") .. "") 55 | s.anonymous=true 56 | s.addremove=true 57 | o=s:option(Value,"server_name",translate("Display Name"),translate("Only English Characters,No spaces")) 58 | o.rmempty = false 59 | 60 | o=s:option(Flag,"validate",translate("validate")) 61 | 62 | o=s:option(Value,"server_ip",translate("Proxy Server IP")) 63 | o.datatype="ip4addr" 64 | o=s:option(Value,"server_port",translate("Proxy Server Port")) 65 | o.datatype="uinteger" 66 | -- --------------------------------------------------- 67 | local apply = luci.http.formvalue("cbi.apply") 68 | if apply then 69 | os.execute("/etc/haproxy_init.sh restart >/dev/null 2>&1 &") 70 | end 71 | return m 72 | -------------------------------------------------------------------------------- /i18n/zh-cn/haproxy.zh-cn.po: -------------------------------------------------------------------------------- 1 | msgid "Running" 2 | msgstr "运行中" 3 | 4 | msgid "Not running" 5 | msgstr "未运行" 6 | 7 | msgid "Main Server List" 8 | msgstr "主服务器列表" 9 | 10 | msgid "Display Name" 11 | msgstr "服务器名称" 12 | 13 | msgid "Only English Characters,No spaces" 14 | msgstr "仅限英文字母,不要有空格" 15 | 16 | msgid "Proxy Server IP" 17 | msgstr "代理服务器IP" 18 | 19 | msgid "Proxy Server Port" 20 | msgstr "代理服务器端口" 21 | 22 | msgid "Weight" 23 | msgstr "分流权重" 24 | 25 | msgid "Backup Server List" 26 | msgstr "备用服务器列表" 27 | 28 | msgid "validate" 29 | msgstr "生效" 30 | 31 | msgid "Balance Strategy" 32 | msgstr "负载均衡模式" 33 | 34 | msgid "roundrobin" 35 | msgstr "轮询" 36 | 37 | msgid "leastconn" 38 | msgstr "最少连接数优先" 39 | 40 | msgid "source" 41 | msgstr "源IP地址" 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /tools/po2lmo/Makefile: -------------------------------------------------------------------------------- 1 | 2 | INSTALL = install 3 | PREFIX = /usr/bin 4 | 5 | po2lmo: src/po2lmo.o src/template_lmo.o 6 | $(CC) $(LDFLAGS) -o src/po2lmo src/po2lmo.o src/template_lmo.o 7 | 8 | install: 9 | $(INSTALL) -m 755 src/po2lmo $(PREFIX) 10 | 11 | clean: 12 | $(RM) src/po2lmo src/*.o 13 | -------------------------------------------------------------------------------- /tools/po2lmo/src/po2lmo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexZhuo/luci-app-haproxy-tcp/9e682880c7334b86f1e1f5209eb93813e512193a/tools/po2lmo/src/po2lmo -------------------------------------------------------------------------------- /tools/po2lmo/src/po2lmo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lmo - Lua Machine Objects - PO to LMO conversion tool 3 | * 4 | * Copyright (C) 2009-2012 Jo-Philipp Wich 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include "template_lmo.h" 20 | 21 | static void die(const char *msg) 22 | { 23 | fprintf(stderr, "Error: %s\n", msg); 24 | exit(1); 25 | } 26 | 27 | static void usage(const char *name) 28 | { 29 | fprintf(stderr, "Usage: %s input.po output.lmo\n", name); 30 | exit(1); 31 | } 32 | 33 | static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream) 34 | { 35 | if( fwrite(ptr, size, nmemb, stream) == 0 ) 36 | die("Failed to write stdout"); 37 | } 38 | 39 | static int extract_string(const char *src, char *dest, int len) 40 | { 41 | int pos = 0; 42 | int esc = 0; 43 | int off = -1; 44 | 45 | for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ ) 46 | { 47 | if( (off == -1) && (src[pos] == '"') ) 48 | { 49 | off = pos + 1; 50 | } 51 | else if( off >= 0 ) 52 | { 53 | if( esc == 1 ) 54 | { 55 | switch (src[pos]) 56 | { 57 | case '"': 58 | case '\\': 59 | off++; 60 | break; 61 | } 62 | dest[pos-off] = src[pos]; 63 | esc = 0; 64 | } 65 | else if( src[pos] == '\\' ) 66 | { 67 | dest[pos-off] = src[pos]; 68 | esc = 1; 69 | } 70 | else if( src[pos] != '"' ) 71 | { 72 | dest[pos-off] = src[pos]; 73 | } 74 | else 75 | { 76 | dest[pos-off] = '\0'; 77 | break; 78 | } 79 | } 80 | } 81 | 82 | return (off > -1) ? strlen(dest) : -1; 83 | } 84 | 85 | static int cmp_index(const void *a, const void *b) 86 | { 87 | uint32_t x = ((const lmo_entry_t *)a)->key_id; 88 | uint32_t y = ((const lmo_entry_t *)b)->key_id; 89 | 90 | if (x < y) 91 | return -1; 92 | else if (x > y) 93 | return 1; 94 | 95 | return 0; 96 | } 97 | 98 | static void print_uint32(uint32_t x, FILE *out) 99 | { 100 | uint32_t y = htonl(x); 101 | print(&y, sizeof(uint32_t), 1, out); 102 | } 103 | 104 | static void print_index(void *array, int n, FILE *out) 105 | { 106 | lmo_entry_t *e; 107 | 108 | qsort(array, n, sizeof(*e), cmp_index); 109 | 110 | for (e = array; n > 0; n--, e++) 111 | { 112 | print_uint32(e->key_id, out); 113 | print_uint32(e->val_id, out); 114 | print_uint32(e->offset, out); 115 | print_uint32(e->length, out); 116 | } 117 | } 118 | 119 | int main(int argc, char *argv[]) 120 | { 121 | char line[4096]; 122 | char key[4096]; 123 | char val[4096]; 124 | char tmp[4096]; 125 | int state = 0; 126 | int offset = 0; 127 | int length = 0; 128 | int n_entries = 0; 129 | void *array = NULL; 130 | lmo_entry_t *entry = NULL; 131 | uint32_t key_id, val_id; 132 | 133 | FILE *in; 134 | FILE *out; 135 | 136 | if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) ) 137 | usage(argv[0]); 138 | 139 | memset(line, 0, sizeof(key)); 140 | memset(key, 0, sizeof(val)); 141 | memset(val, 0, sizeof(val)); 142 | 143 | while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) ) 144 | { 145 | if( state == 0 && strstr(line, "msgid \"") == line ) 146 | { 147 | switch(extract_string(line, key, sizeof(key))) 148 | { 149 | case -1: 150 | die("Syntax error in msgid"); 151 | case 0: 152 | state = 1; 153 | break; 154 | default: 155 | state = 2; 156 | } 157 | } 158 | else if( state == 1 || state == 2 ) 159 | { 160 | if( strstr(line, "msgstr \"") == line || state == 2 ) 161 | { 162 | switch(extract_string(line, val, sizeof(val))) 163 | { 164 | case -1: 165 | state = 4; 166 | break; 167 | default: 168 | state = 3; 169 | } 170 | } 171 | else 172 | { 173 | switch(extract_string(line, tmp, sizeof(tmp))) 174 | { 175 | case -1: 176 | state = 2; 177 | break; 178 | default: 179 | strcat(key, tmp); 180 | } 181 | } 182 | } 183 | else if( state == 3 ) 184 | { 185 | switch(extract_string(line, tmp, sizeof(tmp))) 186 | { 187 | case -1: 188 | state = 4; 189 | break; 190 | default: 191 | strcat(val, tmp); 192 | } 193 | } 194 | 195 | if( state == 4 ) 196 | { 197 | if( strlen(key) > 0 && strlen(val) > 0 ) 198 | { 199 | key_id = sfh_hash(key, strlen(key)); 200 | val_id = sfh_hash(val, strlen(val)); 201 | 202 | if( key_id != val_id ) 203 | { 204 | n_entries++; 205 | array = realloc(array, n_entries * sizeof(lmo_entry_t)); 206 | entry = (lmo_entry_t *)array + n_entries - 1; 207 | 208 | if (!array) 209 | die("Out of memory"); 210 | 211 | entry->key_id = key_id; 212 | entry->val_id = val_id; 213 | entry->offset = offset; 214 | entry->length = strlen(val); 215 | 216 | length = strlen(val) + ((4 - (strlen(val) % 4)) % 4); 217 | 218 | print(val, length, 1, out); 219 | offset += length; 220 | } 221 | } 222 | 223 | state = 0; 224 | memset(key, 0, sizeof(key)); 225 | memset(val, 0, sizeof(val)); 226 | } 227 | 228 | memset(line, 0, sizeof(line)); 229 | } 230 | 231 | print_index(array, n_entries, out); 232 | 233 | if( offset > 0 ) 234 | { 235 | print_uint32(offset, out); 236 | fsync(fileno(out)); 237 | fclose(out); 238 | } 239 | else 240 | { 241 | fclose(out); 242 | unlink(argv[2]); 243 | } 244 | 245 | fclose(in); 246 | return(0); 247 | } 248 | -------------------------------------------------------------------------------- /tools/po2lmo/src/po2lmo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexZhuo/luci-app-haproxy-tcp/9e682880c7334b86f1e1f5209eb93813e512193a/tools/po2lmo/src/po2lmo.o -------------------------------------------------------------------------------- /tools/po2lmo/src/template_lmo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lmo - Lua Machine Objects - Base functions 3 | * 4 | * Copyright (C) 2009-2010 Jo-Philipp Wich 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include "template_lmo.h" 20 | 21 | /* 22 | * Hash function from http://www.azillionmonkeys.com/qed/hash.html 23 | * Copyright (C) 2004-2008 by Paul Hsieh 24 | */ 25 | 26 | uint32_t sfh_hash(const char *data, int len) 27 | { 28 | uint32_t hash = len, tmp; 29 | int rem; 30 | 31 | if (len <= 0 || data == NULL) return 0; 32 | 33 | rem = len & 3; 34 | len >>= 2; 35 | 36 | /* Main loop */ 37 | for (;len > 0; len--) { 38 | hash += sfh_get16(data); 39 | tmp = (sfh_get16(data+2) << 11) ^ hash; 40 | hash = (hash << 16) ^ tmp; 41 | data += 2*sizeof(uint16_t); 42 | hash += hash >> 11; 43 | } 44 | 45 | /* Handle end cases */ 46 | switch (rem) { 47 | case 3: hash += sfh_get16(data); 48 | hash ^= hash << 16; 49 | hash ^= data[sizeof(uint16_t)] << 18; 50 | hash += hash >> 11; 51 | break; 52 | case 2: hash += sfh_get16(data); 53 | hash ^= hash << 11; 54 | hash += hash >> 17; 55 | break; 56 | case 1: hash += *data; 57 | hash ^= hash << 10; 58 | hash += hash >> 1; 59 | } 60 | 61 | /* Force "avalanching" of final 127 bits */ 62 | hash ^= hash << 3; 63 | hash += hash >> 5; 64 | hash ^= hash << 4; 65 | hash += hash >> 17; 66 | hash ^= hash << 25; 67 | hash += hash >> 6; 68 | 69 | return hash; 70 | } 71 | 72 | uint32_t lmo_canon_hash(const char *str, int len) 73 | { 74 | char res[4096]; 75 | char *ptr, prev; 76 | int off; 77 | 78 | if (!str || len >= sizeof(res)) 79 | return 0; 80 | 81 | for (prev = ' ', ptr = res, off = 0; off < len; prev = *str, off++, str++) 82 | { 83 | if (isspace(*str)) 84 | { 85 | if (!isspace(prev)) 86 | *ptr++ = ' '; 87 | } 88 | else 89 | { 90 | *ptr++ = *str; 91 | } 92 | } 93 | 94 | if ((ptr > res) && isspace(*(ptr-1))) 95 | ptr--; 96 | 97 | return sfh_hash(res, ptr - res); 98 | } 99 | 100 | lmo_archive_t * lmo_open(const char *file) 101 | { 102 | int in = -1; 103 | uint32_t idx_offset = 0; 104 | struct stat s; 105 | 106 | lmo_archive_t *ar = NULL; 107 | 108 | if (stat(file, &s) == -1) 109 | goto err; 110 | 111 | if ((in = open(file, O_RDONLY)) == -1) 112 | goto err; 113 | 114 | if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL) 115 | { 116 | memset(ar, 0, sizeof(*ar)); 117 | 118 | ar->fd = in; 119 | ar->size = s.st_size; 120 | 121 | fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC); 122 | 123 | if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED) 124 | goto err; 125 | 126 | idx_offset = ntohl(*((const uint32_t *) 127 | (ar->mmap + ar->size - sizeof(uint32_t)))); 128 | 129 | if (idx_offset >= ar->size) 130 | goto err; 131 | 132 | ar->index = (lmo_entry_t *)(ar->mmap + idx_offset); 133 | ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t); 134 | ar->end = ar->mmap + ar->size; 135 | 136 | return ar; 137 | } 138 | 139 | err: 140 | if (in > -1) 141 | close(in); 142 | 143 | if (ar != NULL) 144 | { 145 | if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) 146 | munmap(ar->mmap, ar->size); 147 | 148 | free(ar); 149 | } 150 | 151 | return NULL; 152 | } 153 | 154 | void lmo_close(lmo_archive_t *ar) 155 | { 156 | if (ar != NULL) 157 | { 158 | if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) 159 | munmap(ar->mmap, ar->size); 160 | 161 | close(ar->fd); 162 | free(ar); 163 | 164 | ar = NULL; 165 | } 166 | } 167 | 168 | 169 | lmo_catalog_t *_lmo_catalogs = NULL; 170 | lmo_catalog_t *_lmo_active_catalog = NULL; 171 | 172 | int lmo_load_catalog(const char *lang, const char *dir) 173 | { 174 | DIR *dh = NULL; 175 | char pattern[16]; 176 | char path[PATH_MAX]; 177 | struct dirent *de = NULL; 178 | 179 | lmo_archive_t *ar = NULL; 180 | lmo_catalog_t *cat = NULL; 181 | 182 | if (!lmo_change_catalog(lang)) 183 | return 0; 184 | 185 | if (!dir || !(dh = opendir(dir))) 186 | goto err; 187 | 188 | if (!(cat = malloc(sizeof(*cat)))) 189 | goto err; 190 | 191 | memset(cat, 0, sizeof(*cat)); 192 | 193 | snprintf(cat->lang, sizeof(cat->lang), "%s", lang); 194 | snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang); 195 | 196 | while ((de = readdir(dh)) != NULL) 197 | { 198 | if (!fnmatch(pattern, de->d_name, 0)) 199 | { 200 | snprintf(path, sizeof(path), "%s/%s", dir, de->d_name); 201 | ar = lmo_open(path); 202 | 203 | if (ar) 204 | { 205 | ar->next = cat->archives; 206 | cat->archives = ar; 207 | } 208 | } 209 | } 210 | 211 | closedir(dh); 212 | 213 | cat->next = _lmo_catalogs; 214 | _lmo_catalogs = cat; 215 | 216 | if (!_lmo_active_catalog) 217 | _lmo_active_catalog = cat; 218 | 219 | return 0; 220 | 221 | err: 222 | if (dh) closedir(dh); 223 | if (cat) free(cat); 224 | 225 | return -1; 226 | } 227 | 228 | int lmo_change_catalog(const char *lang) 229 | { 230 | lmo_catalog_t *cat; 231 | 232 | for (cat = _lmo_catalogs; cat; cat = cat->next) 233 | { 234 | if (!strncmp(cat->lang, lang, sizeof(cat->lang))) 235 | { 236 | _lmo_active_catalog = cat; 237 | return 0; 238 | } 239 | } 240 | 241 | return -1; 242 | } 243 | 244 | static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash) 245 | { 246 | unsigned int m, l, r; 247 | uint32_t k; 248 | 249 | l = 0; 250 | r = ar->length - 1; 251 | 252 | while (1) 253 | { 254 | m = l + ((r - l) / 2); 255 | 256 | if (r < l) 257 | break; 258 | 259 | k = ntohl(ar->index[m].key_id); 260 | 261 | if (k == hash) 262 | return &ar->index[m]; 263 | 264 | if (k > hash) 265 | { 266 | if (!m) 267 | break; 268 | 269 | r = m - 1; 270 | } 271 | else 272 | { 273 | l = m + 1; 274 | } 275 | } 276 | 277 | return NULL; 278 | } 279 | 280 | int lmo_translate(const char *key, int keylen, char **out, int *outlen) 281 | { 282 | uint32_t hash; 283 | lmo_entry_t *e; 284 | lmo_archive_t *ar; 285 | 286 | if (!key || !_lmo_active_catalog) 287 | return -2; 288 | 289 | hash = lmo_canon_hash(key, keylen); 290 | 291 | for (ar = _lmo_active_catalog->archives; ar; ar = ar->next) 292 | { 293 | if ((e = lmo_find_entry(ar, hash)) != NULL) 294 | { 295 | *out = ar->mmap + ntohl(e->offset); 296 | *outlen = ntohl(e->length); 297 | return 0; 298 | } 299 | } 300 | 301 | return -1; 302 | } 303 | 304 | void lmo_close_catalog(const char *lang) 305 | { 306 | lmo_archive_t *ar, *next; 307 | lmo_catalog_t *cat, *prev; 308 | 309 | for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next) 310 | { 311 | if (!strncmp(cat->lang, lang, sizeof(cat->lang))) 312 | { 313 | if (prev) 314 | prev->next = cat->next; 315 | else 316 | _lmo_catalogs = cat->next; 317 | 318 | for (ar = cat->archives; ar; ar = next) 319 | { 320 | next = ar->next; 321 | lmo_close(ar); 322 | } 323 | 324 | free(cat); 325 | break; 326 | } 327 | } 328 | } 329 | -------------------------------------------------------------------------------- /tools/po2lmo/src/template_lmo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * lmo - Lua Machine Objects - General header 3 | * 4 | * Copyright (C) 2009-2012 Jo-Philipp Wich 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _TEMPLATE_LMO_H_ 20 | #define _TEMPLATE_LMO_H_ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #if (defined(__GNUC__) && defined(__i386__)) 38 | #define sfh_get16(d) (*((const uint16_t *) (d))) 39 | #else 40 | #define sfh_get16(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ 41 | +(uint32_t)(((const uint8_t *)(d))[0]) ) 42 | #endif 43 | 44 | 45 | struct lmo_entry { 46 | uint32_t key_id; 47 | uint32_t val_id; 48 | uint32_t offset; 49 | uint32_t length; 50 | } __attribute__((packed)); 51 | 52 | typedef struct lmo_entry lmo_entry_t; 53 | 54 | 55 | struct lmo_archive { 56 | int fd; 57 | int length; 58 | uint32_t size; 59 | lmo_entry_t *index; 60 | char *mmap; 61 | char *end; 62 | struct lmo_archive *next; 63 | }; 64 | 65 | typedef struct lmo_archive lmo_archive_t; 66 | 67 | 68 | struct lmo_catalog { 69 | char lang[6]; 70 | struct lmo_archive *archives; 71 | struct lmo_catalog *next; 72 | }; 73 | 74 | typedef struct lmo_catalog lmo_catalog_t; 75 | 76 | 77 | uint32_t sfh_hash(const char *data, int len); 78 | uint32_t lmo_canon_hash(const char *data, int len); 79 | 80 | lmo_archive_t * lmo_open(const char *file); 81 | void lmo_close(lmo_archive_t *ar); 82 | 83 | 84 | extern lmo_catalog_t *_lmo_catalogs; 85 | extern lmo_catalog_t *_lmo_active_catalog; 86 | 87 | int lmo_load_catalog(const char *lang, const char *dir); 88 | int lmo_change_catalog(const char *lang); 89 | int lmo_translate(const char *key, int keylen, char **out, int *outlen); 90 | void lmo_close_catalog(const char *lang); 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /tools/po2lmo/src/template_lmo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexZhuo/luci-app-haproxy-tcp/9e682880c7334b86f1e1f5209eb93813e512193a/tools/po2lmo/src/template_lmo.o --------------------------------------------------------------------------------