├── Makefile ├── README.md ├── files ├── etc │ ├── config │ │ └── ngrokc │ ├── init.d │ │ └── ngrokc │ └── uci-defaults │ │ └── 40_luci-ngrokc └── usr │ └── lib │ └── lua │ └── luci │ ├── controller │ └── ngrokc.lua │ ├── model │ └── cbi │ │ └── ngrokc │ │ ├── detail.lua │ │ └── overview.lua │ └── view │ └── ngrokc │ ├── detail_script.htm │ ├── overview_enabled.htm │ └── overview_value.htm ├── i18n └── zh-cn │ └── ngrokc.zh-cn.po └── tools └── po2lmo ├── Makefile └── src ├── po2lmo ├── po2lmo.c ├── po2lmo.o ├── template_lmo.c ├── template_lmo.h └── template_lmo.o /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2015-2016 Openwrt.org 3 | # 4 | # This is free software, licensed under the Apache License, Version 2.0 . 5 | # 6 | 7 | include $(TOPDIR)/rules.mk 8 | 9 | PKG_NAME:=luci-app-ngrokc 10 | PKG_VERSION:=1.0 11 | PKG_RELEASE:=1 12 | 13 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) 14 | 15 | include $(INCLUDE_DIR)/package.mk 16 | 17 | define Package/luci-app-ngrokc 18 | CATEGORY:=Network 19 | SUBMENU:=Luci 20 | TITLE:=LuCI support for ngrokc. 21 | PKGARCH:=all 22 | DEPENDS:=+ngrokc 23 | endef 24 | 25 | 26 | define Package/luci-app-cdns/description 27 | LuCI Support for cdns. 28 | endef 29 | 30 | define Package/$(PKG_NAME)/postinst 31 | #!/bin/sh 32 | rm -rf /tmp/luci* 33 | endef 34 | 35 | define Build/Prepare 36 | $(foreach po,$(wildcard ${CURDIR}/i18n/zh-cn/*.po), \ 37 | po2lmo $(po) $(PKG_BUILD_DIR)/$(patsubst %.po,%.lmo,$(notdir $(po)));) 38 | endef 39 | 40 | define Build/Configure 41 | endef 42 | 43 | define Build/Compile 44 | endef 45 | 46 | define Package/$(PKG_NAME)/install 47 | $(INSTALL_DIR) $(1)/usr/lib/lua/luci/i18n 48 | $(INSTALL_DATA) $(PKG_BUILD_DIR)/*.*.lmo $(1)/usr/lib/lua/luci/i18n/ 49 | $(CP) ./files/* $(1)/ 50 | 51 | endef 52 | 53 | $(eval $(call BuildPackage,$(PKG_NAME))) 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ngrokc LuCI for OpenWrt/LEDE 2 | === 3 | forked from https://github.com/anti-gfw/packages/tree/master/luci/applications/luci-app-ngrok 4 | 5 | 为ImageBuilder编译[此固件][N]所需依赖包而建,非原创 6 | 7 | 简介 8 | --- 9 | 10 | 软件包为ngrok-c的luci配置页面,可配合[openwrt-ngrokc][M]使用 11 | 12 | 支持HTTP和TCP两种模式的内网穿透 13 | 14 | 依赖 15 | --- 16 | 显式依赖 `ngrokc` 17 | 18 | 编译 19 | --- 20 | 21 | - 从 OpenWrt 的 [SDK][S] 编译 22 | 23 | ```bash 24 | # 以 ar71xx 平台为例 25 | tar xjf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 26 | cd OpenWrt-SDK-ar71xx-* 27 | # 获取 Makefile 28 | git clone https://github.com/AlexZhuo/luci-app-ngrokc.git package/luci-app-ngrokc 29 | # 选择要编译的包 Network -> Luci -> luci-app-ngrokc 30 | make menuconfig 31 | # 开始编译 32 | make package/luci-app-ngrokc/compile V=99 33 | ``` 34 | 35 | --- 36 | 37 | 截图 38 | --- 39 | ![](https://github.com/AlexZhuo/BreakwallOpenWrt/raw/master/screenshots/ngrokc1.png) 40 | 41 | 42 | ![](https://github.com/AlexZhuo/BreakwallOpenWrt/raw/master/screenshots/ngrokc2.png) 43 | 44 | [N]: http://www.right.com.cn/forum/thread-198649-1-1.html 45 | [M]: https://github.com/AlexZhuo/openwrt-ngrokc 46 | [S]: http://wiki.openwrt.org/doc/howto/obtain.firmware.sdk 47 | -------------------------------------------------------------------------------- /files/etc/config/ngrokc: -------------------------------------------------------------------------------- 1 | 2 | config ngrokc 'main' 3 | option check_interval '0' 4 | 5 | config servers 'tunnel_phpor_me' 6 | option host 'tunnel.phpor.me' 7 | option port '4443' 8 | 9 | config servers 'gotunnel_org' 10 | option host 'gotunnel.org' 11 | option port '3334' 12 | 13 | config servers 'ngrok_cc' 14 | option host 'server.ngrok.cc' 15 | option port '4443' 16 | 17 | config tunnel 'tunnel1' 18 | option type 'http' 19 | option lport '80' 20 | option enabled '0' 21 | option server 'tunnel_phpor_me' 22 | option custom_domain '0' 23 | option dname 'openwrt' 24 | 25 | -------------------------------------------------------------------------------- /files/etc/init.d/ngrokc: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | # Copyright (C) 2007 OpenWrt.org 3 | 4 | START=99 5 | 6 | list_servers() { 7 | SERVER_PARAMS=" -SER[Shost:" 8 | config_get host "$1" 'host' 9 | config_get port "$1" 'port' 10 | config_get atoken "$1" 'atoken' 11 | SERVER_PARAMS="${SERVER_PARAMS}""${host}"",Sport:""${port}" 12 | [ ! $atoken ] && SERVER_PARAMS=${SERVER_PARAMS}"]" || SERVER_PARAMS=${SERVER_PARAMS}",Atoken:${atoken}]" 13 | eval ${1}_PARAMS=\$SERVER_PARAMS 14 | } 15 | 16 | list_tunnels() { 17 | echo $1 >> /tmp/ngrokc/ngrokc.list 18 | config_get_bool enabled "$1" 'enabled' 19 | config_get type "$1" 'type' 20 | config_get lhost "$1" 'lhost' 21 | config_get server "$1" 'server' 22 | config_get lport "$1" 'lport' 23 | config_get custom_domain "$1" 'custom_domain' 24 | config_get rport "$1" 'rport' 25 | config_get dname "$1" 'dname' 26 | [ -z "$lhost" ] && lhost="127.0.0.1" 27 | if [ "$enabled" = 1 ]; then 28 | TUNNEL_PARAMS=" -AddTun[Profile:${1},Type:${type},Lhost:${lhost},Lport:${lport}" 29 | [ "z""$custom_domain" = "z1" ] && DTYPE="Hostname" || DTYPE="Sdname" 30 | [ ${type} = "tcp" ] && \ 31 | TUNNEL_PARAMS=${TUNNEL_PARAMS}",Rport:${rport}," || \ 32 | TUNNEL_PARAMS=${TUNNEL_PARAMS}",${DTYPE}:${dname}" 33 | TUNNEL_PARAMS=${TUNNEL_PARAMS}"]" 34 | SERVER_PARAMS=`get_servarg $server` 35 | logger -t "ngrokc" "${server} : ngrokc ${SERVER_PARAMS} ${TUNNEL_PARAMS}" 36 | CMD_EXIST=`busybox ps -w -w|grep "Profile:${1}" |grep -v grep` 37 | if [ "z""${CMD_EXIST}" = "z" ] ; then 38 | exec /usr/bin/ngrokc $SERVER_PARAMS $TUNNEL_PARAMS 2>&1 >/dev/null & 39 | echo $! > /tmp/ngrokc/${1}.pid 40 | fi 41 | #exec $STDBUF /usr/bin/ngrokc $SERVER_PARAMS $TUNNEL_PARAMS 2>/dev/null 1>/tmp/ngrokc/${1}.log & 42 | fi 43 | } 44 | 45 | get_servarg() { 46 | eval echo \$${1}_PARAMS 47 | } 48 | 49 | start() { 50 | mkdir -p /tmp/ngrokc 51 | touch /tmp/ngrokc/running 52 | # [ -f /usr/lib/coreutils/libstdbuf.so ] && STDBUF="stdbuf -oL " || STDBUF="" 53 | config_load 'ngrokc' 54 | config_foreach list_servers 'servers' 55 | config_foreach list_tunnels 'tunnel' 56 | set_crontab 57 | } 58 | 59 | stop() { 60 | kill -2 `pidof ngrokc|sed "s/$$//g"` 2>/dev/null 61 | rm -f /tmp/ngrokc/* 62 | sleep 2 63 | } 64 | 65 | boot() { 66 | sleep 10 67 | start 68 | } 69 | 70 | set_crontab() { 71 | [ -f /etc/crontabs/root ] || touch /etc/crontabs/root 72 | interval=`uci get ngrokc.main.check_interval` 73 | ngrokc_cron=`cat /etc/crontabs/root | grep "/etc/init.d/ngrokc start"` 74 | if [ "${interval}" != "0" ] ; then 75 | ngrokc_new="*/${interval} * * * * /etc/init.d/ngrokc start" 76 | if [ "z${ngrokc_cron}" != "z${ngrokc_new}" ] ; then 77 | cat /etc/crontabs/root | grep -v "/etc/init.d/ngrokc start" > /tmp/crontabs_ngrokc 78 | echo "" >> /tmp/crontabs_oscam 79 | echo "*/${interval} * * * * /etc/init.d/ngrokc start" >> /tmp/crontabs_ngrokc 80 | cat /tmp/crontabs_ngrokc | grep -v ^$ > /etc/crontabs/root 81 | /etc/init.d/cron restart 82 | fi 83 | else 84 | if [ "z""${ngrokc_cron}" != "z" ] ; then 85 | cat /etc/crontabs/root | grep -v "/etc/init.d/ngrokc start" > /tmp/crontabs_ngrokc 86 | cat /tmp/crontabs_ngrokc | grep -v ^$ > /etc/crontabs/root 87 | /etc/init.d/cron restart 88 | fi 89 | fi 90 | } 91 | -------------------------------------------------------------------------------- /files/etc/uci-defaults/40_luci-ngrokc: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | uci -q batch <<-EOF >/dev/null 4 | delete ucitrack.@ngrokc[-1] 5 | add ucitrack ngrokc 6 | set ucitrack.@ngrokc[-1].init=ngrokc 7 | commit ucitrack 8 | EOF 9 | 10 | rm -f /tmp/luci-indexcache 11 | exit 0 12 | -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/controller/ngrokc.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | LuCI - Lua Configuration Interface - aria2 support 3 | 4 | Copyright 2014 nanpuyue 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 | 13 | module("luci.controller.ngrokc", package.seeall) 14 | 15 | function index() 16 | if not nixio.fs.access("/etc/config/ngrokc") then 17 | return 18 | end 19 | 20 | entry({"admin", "services", "ngrokc"}, cbi("ngrokc/overview"), _("Ngrok Settings")).dependent = true 21 | entry({"admin", "services", "ngrokc", "detail"}, cbi("ngrokc/detail"), nil ).leaf = true 22 | end 23 | -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/model/cbi/ngrokc/detail.lua: -------------------------------------------------------------------------------- 1 | -- Copyright 2008 Steven Barth 2 | -- Copyright 2008 Jo-Philipp Wich 3 | -- Copyright 2013 Manuel Munz 4 | -- Copyright 2014-2015 Christian Schoenebeck 5 | -- Licensed to the public under the Apache License 2.0. 6 | 7 | local UCI = (require "luci.model.uci").cursor() 8 | local NX = require "nixio" 9 | local NXFS = require "nixio.fs" 10 | local SYS = require "luci.sys" 11 | local UTIL = require "luci.util" 12 | local DISP = require "luci.dispatcher" 13 | local DTYP = require "luci.cbi.datatypes" 14 | 15 | local apply = luci.http.formvalue("cbi.apply") 16 | if apply then 17 | os.execute("/etc/init.d/ngrokc reload &") -- reload configuration 18 | end 19 | 20 | -- takeover arguments -- ####################################################### 21 | local section = arg[1] 22 | 23 | m = Map("ngrokc") 24 | m.redirect = DISP.build_url("admin", "services", "ngrokc") 25 | 26 | tunnels = m:section( NamedSection, section, "tunnel", "

" .. translate("Details") .. " : " .. section .. "

") 27 | tunnels.instance = section -- arg [1] 28 | 29 | enabled=tunnels:option(Flag, "enabled", translate("Enable")) 30 | enabled.anonymous = true 31 | enabled.addremove = false 32 | 33 | server=tunnels:option(ListValue, "server", translate("Server")) 34 | UCI.foreach("ngrokc", "servers", function(s) server:value(s['.name'], s['.name'] .. " ( " .. s.host .. ":" .. s.port .. " ) ") end) 35 | 36 | 37 | ptype=tunnels:option(ListValue, "type", translate("Type")) 38 | ptype:value("tcp", translate("TCP")) 39 | ptype:value("http", translate("HTTP")) 40 | ptype:value("https", translate("HTTPS")) 41 | 42 | lhost=tunnels:option(Value, "lhost", translate("Local Address")) 43 | lhost.rmempty = true 44 | lhost.placeholder = "127.0.0.1" 45 | lhost.datatype = "ip4addr" 46 | 47 | lport=tunnels:option(Value, "lport", translate("Local Port")) 48 | lport.datatype = "port" 49 | lport.rmempty = true 50 | 51 | custom_domain=tunnels:option(Flag, "custom_domain", translate("Use Custom Domain")) 52 | custom_domain.optional = true 53 | custom_domain:depends("type", "http") 54 | custom_domain:depends("type", "https") 55 | 56 | dname=tunnels:option(Value, "dname", translate("Custom Domain") .. "/" .. translate("SubDomain"), translate("Please set your domain's CNAME or A record to the tunnel server.")) 57 | dname.datatype = "hostname" 58 | dname.rmempty = true 59 | dname:depends("type", "http") 60 | dname:depends("type", "https") 61 | 62 | addr_port=tunnels:option(Flag, "addr_port", translate("Add Addr Port")) 63 | addr_port.optional = true 64 | addr_port:depends("type", "http") 65 | addr_port:depends("type", "https") 66 | 67 | rport=tunnels:option(Value, "rport", translate("Remote Port")) 68 | rport.datatype = "port" 69 | rport.rmempty = true 70 | rport:depends("type", "tcp") 71 | rport:depends({type = "http", addr_port = addr_port.enabled}) 72 | rport:depends({type = "https", addr_port = addr_port.enabled}) 73 | 74 | custom_html=tunnels:option(DummyValue, "none") 75 | custom_html.template = "ngrokc/detail_script" 76 | 77 | return m 78 | -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/model/cbi/ngrokc/overview.lua: -------------------------------------------------------------------------------- 1 | -- Mantainer : maz-1 < ohmygod19993 at gmail dot com > 2 | 3 | 4 | m = Map("ngrokc", translate("Ngrok"),translate("Secure tunnels to localhost.")) 5 | 6 | local apply = luci.http.formvalue("cbi.apply") 7 | if apply then 8 | os.execute("/etc/init.d/ngrokc reload &") -- reload configuration 9 | end 10 | 11 | local DISP = require "luci.dispatcher" 12 | local CTRL = require "luci.controller.ngrokc" 13 | local HTTP = require "luci.http" 14 | local UCI = (require "luci.model.uci").cursor() 15 | 16 | main=m:section(NamedSection, "main", "ngrokc", translate("Main Settings")) 17 | main.addremove=false 18 | main.anonymous=true 19 | --check_periodcally=main:option(Flag, "check_periodcally", translate("Check periodically")) 20 | check_interval=main:option(Value, "check_interval", translate("Check interval"), translate("Second(s). Set to 0 if you don't want to check")) 21 | check_interval.addremove = false 22 | --check_interval:depends("check_periodcally", "1") 23 | 24 | servers=m:section(TypedSection, "servers", translate("Servers")) 25 | servers.template = "cbi/tblsection" 26 | servers.anonymous = false 27 | servers.addremove = true 28 | 29 | 30 | nhost=servers:option(Value, "host", translate("Ngrok Host")) 31 | nhost.rmempty = false 32 | nhost.datatype = "host" 33 | 34 | hport=servers:option(Value, "port", translate("Ngrok Port")) 35 | hport.rmempty = false 36 | hport.datatype = "port" 37 | 38 | servers:option(Value, "atoken", translate("Auth Token")).rmempty = true 39 | 40 | 41 | tunnel=m:section(TypedSection, "tunnel", translate("Tunnels")) 42 | tunnel.template = "cbi/tblsection" 43 | tunnel.anonymous = false 44 | tunnel.addremove = true 45 | 46 | tunnel.extedit = DISP.build_url("admin", "services", "ngrokc", "detail", "%s") 47 | function tunnel.create(self, name) 48 | AbstractSection.create(self, name) 49 | HTTP.redirect( self.extedit:format(name) ) 50 | end 51 | 52 | ena=tunnel:option(Flag, "enabled", translate("Enabled")) 53 | ena.template = "ngrokc/overview_enabled" 54 | ena.rmempty = false 55 | 56 | lport=tunnel:option(DummyValue, "_lport", translate("Local Port")) 57 | lport.template = "ngrokc/overview_value" 58 | lport.rmempty = false 59 | function lport.set_one(self, section) 60 | local localport = self.map:get(section, "lport") or "" 61 | if localport ~= "" then 62 | return localport 63 | else 64 | return [[]] .. translate("config error") .. [[]] 65 | end 66 | end 67 | 68 | 69 | server=tunnel:option(DummyValue, "_server", translate("Server")) 70 | server.template = "ngrokc/overview_value" 71 | server.rmempty = false 72 | function server.set_one(self, section) 73 | local servername = self.map:get(section, "server") or "" 74 | local host = UCI.get("ngrokc", servername, "host") or "" 75 | local port = UCI.get("ngrokc", servername, "port") or "" 76 | if servername ~= "" or host ~= "" or port ~= "" then 77 | return host .. ":" .. port 78 | else 79 | return [[]] .. translate("config error") .. [[]] 80 | end 81 | end 82 | 83 | type=tunnel:option(DummyValue, "_type", translate("Type")) 84 | type.template = "ngrokc/overview_value" 85 | type.rmempty = false 86 | function type.set_one(self, section) 87 | local tunneltype = self.map:get(section, "type") or "" 88 | if tunneltype ~= "" then 89 | return string.upper(tunneltype) 90 | else 91 | return [[]] .. translate("config error") .. [[]] 92 | end 93 | end 94 | 95 | url=tunnel:option(DummyValue, "_url", translate("URL")) 96 | url.template = "ngrokc/overview_value" 97 | url.rmempty = false 98 | function url.set_one(self, section) 99 | local urlelement 100 | local servername = self.map:get(section, "server") or "" 101 | local host = UCI.get("ngrokc", servername, "host") or "" 102 | local tunneltype = self.map:get(section, "type") or "" 103 | local dname = self.map:get(section, "dname") or "" 104 | local cusdom = self.map:get(section, "custom_domain") or "" 105 | local addrport = self.map:get(section, "addr_port") or "" 106 | local rport = self.map:get(section, "rport") or "" 107 | if tunneltype == "tcp" and rport ~= "" then 108 | urlelement = "tcp://" .. host .. ":" .. rport 109 | elseif (tunneltype == "http" or tunneltype == "https") and dname ~= "" then 110 | if cusdom == "1" then 111 | urlelement = tunneltype .. "://" .. dname 112 | else 113 | urlelement = tunneltype .. "://" .. dname .. "." .. string.gsub(host, "www", "") 114 | end 115 | if addrport == "1" and rport ~= "" then 116 | urlelement = urlelement .. ":" .. rport 117 | end 118 | else 119 | urlelement = [[]] .. translate("config error") .. [[]] 120 | end 121 | return "" .. urlelement .. "" 122 | end 123 | 124 | return m 125 | -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/view/ngrokc/detail_script.htm: -------------------------------------------------------------------------------- 1 | 11 | -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/view/ngrokc/overview_enabled.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | <%+cbi/valueheader%> 4 | 5 | /> 8 | 9 | /> 13 | 14 | <%+cbi/valuefooter%> 15 | 16 | -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/view/ngrokc/overview_value.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | <%+cbi/valueheader%> 4 | 5 | <%=self:set_one(section)%> 6 | 7 | <%+cbi/valuefooter%> 8 | 9 | -------------------------------------------------------------------------------- /i18n/zh-cn/ngrokc.zh-cn.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "Content-Type: text/plain; charset=UTF-8\n" 3 | 4 | msgid "Ngrok Settings" 5 | msgstr "Ngrok设置" 6 | 7 | msgid "Ngrok" 8 | msgstr "Ngrok反向代理" 9 | 10 | msgid "Secure tunnels to localhost." 11 | msgstr "易用而安全的反向代理软件" 12 | 13 | msgid "Enable" 14 | msgstr "启用" 15 | 16 | msgid "Ngrok Host" 17 | msgstr "Ngrok服务器" 18 | 19 | msgid "Ngrok Port" 20 | msgstr "Ngrok服务器端口" 21 | 22 | msgid "Auth Token" 23 | msgstr "令牌" 24 | 25 | msgid "Type" 26 | msgstr "类型" 27 | 28 | msgid "Local Address" 29 | msgstr "本地地址" 30 | 31 | msgid "Local Port" 32 | msgstr "本地端口" 33 | 34 | msgid "Custom Domain" 35 | msgstr "自定义域名" 36 | 37 | msgid "SubDomain" 38 | msgstr "子域名" 39 | 40 | msgid "Use Custom Domain" 41 | msgstr "使用自定义域名" 42 | 43 | msgid "Add Addr Port" 44 | msgstr "添加地址端口" 45 | 46 | msgid "Remote Port" 47 | msgstr "远程端口" 48 | 49 | msgid "Please set your domain's CNAME or A record to the tunnel server." 50 | msgstr "请把域名的CNAME记录或A记录指向Ngrok隧道服务器。" 51 | 52 | msgid "Server" 53 | msgstr "服务器" 54 | 55 | msgid "Servers" 56 | msgstr "服务器" 57 | 58 | msgid "Tunnels" 59 | msgstr "通道" 60 | 61 | msgid "Details" 62 | msgstr "详情" 63 | 64 | msgid "Main Settings" 65 | msgstr "主要设置" 66 | 67 | msgid "Check interval" 68 | msgstr "检查间隔" 69 | 70 | msgid "Second(s). Set to 0 if you don't want to check" 71 | msgstr "单位秒。设为0可禁止每隔一段时间检查。" 72 | -------------------------------------------------------------------------------- /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-ngrokc/80a7c465cf3ee111bb9782e6020feb422731769b/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-ngrokc/80a7c465cf3ee111bb9782e6020feb422731769b/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-ngrokc/80a7c465cf3ee111bb9782e6020feb422731769b/tools/po2lmo/src/template_lmo.o --------------------------------------------------------------------------------