瀏覽代碼

perf(luci-app-ssr-plus): Use curl instead of socket when transport is "ws"

nixio.socket() couldn't test the websocket connection correctly ,so use curl instead of nixio.socket()
acooler15 4 年之前
父節點
當前提交
c981dcf256

+ 20 - 9
luci-app-ssr-plus/luasrc/controller/shadowsocksr.lua

@@ -46,17 +46,28 @@ function act_ping()
 	local e = {}
 	local domain = luci.http.formvalue("domain")
 	local port = luci.http.formvalue("port")
+	local transport = luci.http.formvalue("transport")
+	local wsPath = luci.http.formvalue("wsPath")
+	local tls = luci.http.formvalue("tls")
 	e.index = luci.http.formvalue("index")
 	local iret = luci.sys.call("ipset add ss_spec_wan_ac " .. domain .. " 2>/dev/null")
-	local socket = nixio.socket("inet", "stream")
-	socket:setopt("socket", "rcvtimeo", 3)
-	socket:setopt("socket", "sndtimeo", 3)
-	e.socket = socket:connect(domain, port)
-	socket:close()
-	-- 	e.ping = luci.sys.exec("ping -c 1 -W 1 %q 2>&1 | grep -o 'time=[0-9]*.[0-9]' | awk -F '=' '{print$2}'" % domain)
-	-- 	if (e.ping == "") then
-	e.ping = luci.sys.exec(string.format("echo -n $(tcping -q -c 1 -i 1 -t 2 -p %s %s 2>&1 | grep -o 'time=[0-9]*' | awk -F '=' '{print $2}') 2>/dev/null", port, domain))
-	-- 	end
+	if transport == "ws" then
+		local prefix = tls=='1' and "https://" or "http://"
+		local address = prefix..domain..':'..port..wsPath
+		local result = luci.sys.exec("curl --http1.1 -m 3 -s  -i -N -o /dev/null -w 'time_connect=%{time_connect}\nhttp_code=%{http_code}' -H 'Connection: Upgrade' -H 'Upgrade: websocket' -H 'Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==' -H 'Sec-WebSocket-Version: 13' "..address)
+		e.socket = string.match(result,"http_code=(%d+)")=="101"
+		e.ping = tonumber(string.match(result, "time_connect=(%d+.%d%d%d)"))*1000
+	else
+		local socket = nixio.socket("inet", "stream")
+		socket:setopt("socket", "rcvtimeo", 3)
+		socket:setopt("socket", "sndtimeo", 3)
+		e.socket = socket:connect(domain, port)
+		socket:close()
+		-- 	e.ping = luci.sys.exec("ping -c 1 -W 1 %q 2>&1 | grep -o 'time=[0-9]*.[0-9]' | awk -F '=' '{print$2}'" % domain)
+		-- 	if (e.ping == "") then
+		e.ping = luci.sys.exec(string.format("echo -n $(tcping -q -c 1 -i 1 -t 2 -p %s %s 2>&1 | grep -o 'time=[0-9]*' | awk -F '=' '{print $2}') 2>/dev/null", port, domain))
+		-- 	end
+	end
 	if (iret == 0) then
 		luci.sys.call(" ipset del ss_spec_wan_ac " .. domain)
 	end

+ 8 - 0
luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/servers.lua

@@ -104,6 +104,14 @@ end
 o = s:option(DummyValue, "server_port", translate("Socket Connected"))
 o.template = "shadowsocksr/socket"
 o.width = "10%"
+o.render = function(self, section, scope)
+	self.transport = s:cfgvalue(section).transport
+	if self.transport == 'ws' then
+		self.ws_path = s:cfgvalue(section).ws_path
+		self.tls = s:cfgvalue(section).tls
+	end
+	DummyValue.render(self, section, scope)
+end
 
 o = s:option(DummyValue, "server", translate("Ping Latency"))
 o.template = "shadowsocksr/ping"

+ 11 - 2
luci-app-ssr-plus/luasrc/view/shadowsocksr/server_list.htm

@@ -5,17 +5,26 @@
 <script type="text/javascript">
 	//<![CDATA[
 	const doms = document.getElementsByClassName('pingtime');
-	const ports = document.getElementsByClassName("socket-connected")
+	const ports = document.getElementsByClassName("socket-connected");
+	const transports = document.getElementsByClassName("transport");
+	const wsPaths = document.getElementsByClassName("wsPath");
+	const tlss = document.getElementsByClassName("tls");
 	const xhr = (index) => {
 		return new Promise((res) => {
 			const dom = doms[index];
 			const port = ports[index];
+			const transport = transports[index];
+			const wsPath = wsPaths[index];
+			const tls = tlss[index];
 			if (!dom) res()
 			port.innerHTML = '<font color="#0072c3">connect</font>';
 			XHR.get('<%=luci.dispatcher.build_url("admin/services/shadowsocksr/ping")%>', {
 				index,
 				domain: dom.getAttribute("hint"),
-				port: port.getAttribute("hint")
+				port: port.getAttribute("hint"),
+				transport: transport.getAttribute("hint"),
+				wsPath: wsPath.getAttribute("hint"),
+				tls: tls.getAttribute("hint")
 			},
 			(x, result) => {
 				let col = '#ff0000';

+ 3 - 0
luci-app-ssr-plus/luasrc/view/shadowsocksr/socket.htm

@@ -1,3 +1,6 @@
 <%+cbi/valueheader%>
 <span class="socket-connected" hint="<%=self:cfgvalue(section)%>">wait</span>
+<span class="transport" hint="<%=self.transport%>"></span>
+<span class="wsPath" hint="<%=self.ws_path%>"></span>
+<span class="tls" hint="<%=self.tls%>"></span>
 <%+cbi/valuefooter%>