Skip to content

Commit

Permalink
Live Simulator: 2 v2.0
Browse files Browse the repository at this point in the history
AquaShineDownload HTTPS support using LuaSec

LLP beatmap download code is now published (HTTPS mode only)
- Proxy mode code is still proprietary
  • Loading branch information
MikuAuahDark committed Feb 16, 2018
1 parent 7ba8882 commit a30b754
Show file tree
Hide file tree
Showing 8 changed files with 3,549 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ Makefile
jit/
external/
make*
ssl.lua
https.lua
6 changes: 6 additions & 0 deletions AquaShine/Download.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
local AquaShine = ...
local class = AquaShine.Class
local love = require("love")
local supportHTTPS = pcall(require, "ssl")
local Download = class("AquaShine.Download")
local DownloadList = setmetatable({}, {__mode = "k"})

Expand Down Expand Up @@ -69,6 +70,10 @@ function Download.DefaultErrorCallback(this, data)
error(data)
end

function Download.HasHTTPS()
return supportHTTPS
end

function Download.Create()
return Download()
end
Expand Down Expand Up @@ -136,3 +141,4 @@ function love.handlers.aqs_download(input, name, data)
end

AquaShine.Download = Download
return Download
35 changes: 32 additions & 3 deletions AquaShine/DownloadThread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
-- See copyright notice in AquaShine.lua

require("love.thread")
local lf = require("love.filesystem")

local cin = ...
local hasSSL, ssl = pcall(require, "ssl")
local socket = require("socket")
local http = require("socket.http")
local lt = require("love.timer")
local sslParams
http.TIMEOUT = 60

-- If LuaSec is available, use it
if hasSSL then
sslParams = ssl.newcontext {
mode = "client",
protocol = "tlsv1_2",
cafile = assert(love.filesystem.getRealDirectory("cacert.pem")).."/cacert.pem",
verify = "peer",
}
end

--function print() lt.sleep(0.001) end

-- AquaShine download event
Expand All @@ -26,6 +39,7 @@ local function custom_sink(chunk, err)
print("QUIT requested")
return nil, "QUIT Requested"
elseif chunk then
print("recv", chunk:sub(1, 40))
push_event("RECV", chunk)
end

Expand All @@ -38,10 +52,24 @@ local lasthttp

local function get_http(url, force)
local h
local dest, uri = assert(url:match("http://([^/]+)(/?.*)"))
local s, dest, uri = socket.try(url:match("http(s?)://([^/]+)(/?.*)"))
local host, port = dest:match("([^:]+):?(%d*)")
port = #port > 0 and port or "80"
h = http.open(host, assert(tonumber(port)))

-- Check if we passed "https" but doesn't have LuaSec
socket.try(#s > 0 and hasSSL or #s == 0, "HTTPS is not supported")

-- Open connection
port = #port > 0 and port or (#s > 0 and "443" or "80")
port = socket.try(tonumber(port), "Invalid port")
h = http.open(host, port)

-- If HTTPS is used, do wrap it to LuaSec SSL connection and do handshake
if #s > 0 then
h.c = ssl.wrap(h.c, sslParams)
print("attempt handshake")
socket.try(h.c:dohandshake())
print("handshake ok")
end

return h, #uri == 0 and "/" or uri, dest
end
Expand All @@ -52,6 +80,7 @@ end

local request_http = socket.protect(function(url, headers)
-- Build request table
print("get http")
local h, uri, dest = get_http(url)
print("http hand", h, uri)

Expand Down
3,467 changes: 3,467 additions & 0 deletions ca_cert.pem

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion dl_wrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ local BackgroundImage = AquaShine.LoadModule("uielement.background_image")
local BackNavigation = AquaShine.LoadModule("uielement.backnavigation")
local DLWrap = {}

function DLWrap.IsLLPOK()
return AquaShine.Download.HasHTTPS() or love.filesystem.getInfo("external/download_beatmap_llp.lua")
end

function DLWrap.GetLLPDestination()
if AquaShine.Download.HasHTTPS() then
return "download_beatmap_llp.lua"
else
return "external/download_beatmap_llp.lua"
end
end

function DLWrap.Start()
DLWrap.Delay = 10000
DLWrap.Download = AquaShine.GetCachedData("BeatmapDLHandle", AquaShine.Download)
Expand All @@ -16,7 +28,7 @@ function DLWrap.Start()
end

function DLWrap.Update(deltaT)
local dest = love.filesystem.getInfo("external/download_beatmap_llp.lua") and "external/download_beatmap_llp.lua"
local dest = DLWrap.IsLLPOK() and DLWrap.GetLLPDestination()
DLWrap.Delay = DLWrap.Delay - deltaT
if not(DLWrap.Download:IsDownloading()) then
AquaShine.LoadEntryPoint(AquaShine.LoadConfig("DL_CURRENT", dest or "download_beatmap_sif.lua"))
Expand Down
19 changes: 16 additions & 3 deletions download_beatmap_sif.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ function DLBeatmap.MovePage(inc)
DLBeatmap.MainNode.brother = DLBeatmap.NodePageList[DLBeatmap.CurrentPage + 1]
end

function DLBeatmap.IsLLPOK()
return AquaShine.Download.HasHTTPS() or love.filesystem.getInfo("external/download_beatmap_llp.lua")
end

function DLBeatmap.GetLLPDestination()
if AquaShine.Download.HasHTTPS() then
return "download_beatmap_llp.lua"
else
return "external/download_beatmap_llp.lua"
end
end

function DLBeatmap.Start(arg)
local maps_info = love.filesystem.getInfo("maps.json")
local s_button_03 = AquaShine.LoadImage("assets/image/ui/s_button_03.png")
Expand All @@ -173,10 +185,11 @@ function DLBeatmap.Start(arg)
:setShadow(1, 1, true)
)

if love.filesystem.getInfo("external/download_beatmap_llp.lua") then
if DLBeatmap.IsLLPOK() then
DLBeatmap.MainNode:addChild(SimpleButton(s_button_03, s_button_03se, function()
AquaShine.SaveConfig("DL_CURRENT", "external/download_beatmap_llp.lua")
AquaShine.LoadEntryPoint("external/download_beatmap_llp.lua")
local dest = DLBeatmap.GetLLPDestination()
AquaShine.SaveConfig("DL_CURRENT", dest)
AquaShine.LoadEntryPoint(dest)
end, 0.5)
:setPosition(696, 18)
:initText(AquaShine.LoadFont("MTLmr3m.ttf", 14), "LLP Download Beatmap")
Expand Down
11 changes: 9 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
--]]---------------------------------------------------------------------------

-- Version string
DEPLS_VERSION = "2.0-RC2"
DEPLS_VERSION = "2.0.0"
-- Version number
-- In form xxyyzzww. x = major, y = minor, z = patch, w = pre-release counter (99 = not a pre release)
DEPLS_VERSION_NUMBER = 01020002
DEPLS_VERSION_NUMBER = 02000099

-- We don't want to protect the global table if we run it from LuaJIT/Terra
if love._exe then
Expand Down Expand Up @@ -153,6 +153,13 @@ ls2.setstreamwrapper {
local LuaStoryboard = require("luastoryboard2")
LuaStoryboard._SetAquaShine(AquaShine)

----------------------
-- Certificate List --
----------------------
if not(love.filesystem.getInfo("cacert.pem")) then
assert(love.filesystem.write("cacert.pem", assert(love.filesystem.read("ca_cert.pem"))))
end

----------------------------
-- Force Create Directory --
----------------------------
Expand Down
4 changes: 4 additions & 0 deletions main_menu2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ function MainMenu.ConstructVersionText()
bld[#bld + 1] = "FFX "
end

if AquaShine.Download.HasHTTPS() then
bld[#bld + 1] = "HTTPS "
end

bld[#bld + 1] = "\nRenderer: "
bld[#bld + 1] = AquaShine.RendererInfo[1]

Expand Down

0 comments on commit a30b754

Please sign in to comment.