-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionAPI.lua
More file actions
161 lines (151 loc) · 6.29 KB
/
SessionAPI.lua
File metadata and controls
161 lines (151 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
local KrissyUtil = require "KrissyUtil"
local LovenseSession = {}
do
local baseUrl = "https://c.lovense-api.com"
local wsUrl = "wss://c.lovense-api.com"
--local request = syn and syn.request or request
--local connect = syn and syn.websocket.connect or WebSocket.connect
local pollnet = require("pollnet")
local JSON = require("json")
local mt = {
__index = LovenseSession
}
local function poll_blocking(sock)
while sock:poll() do
if sock:last_message() then
return sock:last_message()
end
pollnet.sleep_ms(20)
end
error("Socket closed?", sock:last_message())
sock:close()
end
local function getCLID(shortCode)
print(shortCode)
local httpBoundary = "---------------------------" .. math.random(1, 1000000)
local sock = pollnet.http_post(baseUrl .. "/anon/longtimecontrollink/init", "Content-Type: multipart/form-data; boundary=" .. httpBoundary, "--" .. httpBoundary .. "\r\n" .. 'Content-Disposition: form-data; name="shortCode"\r\n\r\n' .. shortCode .. "\r\n" .. "--" .. httpBoundary .. "--\r\n")
local status = poll_blocking(sock)
local headers = poll_blocking(sock)
local body = poll_blocking(sock)
sock:close()
if JSON.decode(body).message then
print("[LOVENSE] " .. JSON.decode(body).message)
print("rip bozo")
os.exit()
end
print(JSON.decode(body).data.id)
return {
Status = status,
Headers = headers,
Body = body
}
end
local function getSessionData(accessCode) -- to be fixed
local httpBoundary = "---------------------------" .. math.random(1, 1000000)
local CLID = JSON.decode(getCLID(accessCode).Body).data.id
local sock = pollnet.http_post(baseUrl .. "/anon/controllink/join", "Content-Type: multipart/form-data; boundary=" .. httpBoundary, "--" .. httpBoundary .. "\r\n" .. 'Content-Disposition: form-data; name="id"\r\n\r\n' .. CLID .. "\r\n" .. "--" .. httpBoundary .. "\r\n" .. 'Content-Disposition: form-data; name="historyUrl"\r\n\r\n\r\n' .. "--" .. httpBoundary .. "--\r\n")
local status = poll_blocking(sock)
local headers = poll_blocking(sock)
local body = poll_blocking(sock)
sock:close()
return {
Status = status,
Headers = headers,
Body = body
}
end
function LovenseSession:new(accessCode)
local self = setmetatable({}, mt)
self.debuggingOn = true -- you can change this
self.accessCode = accessCode
return self
end
function LovenseSession:Connect()
local data = JSON.decode(getSessionData(self.accessCode).Body)
if data.message then
if self.debuggingOn then
print("[LOVENSE] " .. data.message)
end
return false
end
local ws_url =
data.data.wsUrl:gsub("https://", "wss://"):gsub("%.com%?", ".com/anon.io/?") .. "&EIO=3&transport=websocket"
if self.debuggingOn then
print("[LOVENSE] Access code is valid! Attempting to connect..")
end
local ws = pollnet.open_ws(ws_url)
self.active = true
self.ws = ws
self.sessionData = data
self.connected = false
self.initDone = false
self.hbStarted = false
return true
end
function LovenseSession:handle_message(msg)
if KrissyUtil:startsWith(msg, "0") then
if self.debuggingOn then
print("[LOVENSE] Init payload received")
end
local wsData = JSON.decode(msg:sub(2))
self.wsData = wsData
self.initDone = true
elseif KrissyUtil:startsWith(msg, "40") and self.initDone and not self.hbStarted then
if self.debuggingOn then
print("[LOVENSE] Connected!")
end
print(self.sessionData.data.controlLinkData.creator.toys[1].type)
self.toys = self.sessionData.data.controlLinkData.creator.toys
self.connected = true
self.hbStarted = true
local c = coroutine.create(
function()
while self.active do
ws:send(tostring(self.wsData.pingCode))
KrissyUtil:sleep(self.wsData.pingInterval)
end
end
)
self.ws:send('42["anon_open_control_panel_ts",{"linkId":"' .. self.sessionData.data.controlLinkData.linkId .. '"}]')
elseif KrissyUtil:startsWith(msg, '42') then
if KrissyUtil:startsWith(msg, '42["anon_link_is_end_tc') then
self:Disconnect("Control Link ended")
elseif KrissyUtil:startsWith(msg, '42["which_app_page_open_now_tc"') then
local msgData = JSON.decode(JSON.decode(msg:sub(3))[2])
self.ws:send('42["app_open_this_page_now_ts",{"pepsiId":"' .. msgData.pepsiId .. '","webPage":"control_link"}]')
end
end
while not self.connected or not self.initDone do
self:get_and_handle_message()
end
end
function LovenseSession:get_and_handle_message()
local happy, message = self.ws:poll()
if not happy then
return message
end
if message and string.len(message) > 1 then
print("< " .. message)
self:handle_message(message)
end
end
function LovenseSession:Disconnect(cause)
self.active = false
self.connected = false
self.ws:close()
print("[LOVENSE] Disconnected: " .. cause)
end
function LovenseSession:Vibrate(strength)
if not self.connected then
print("[LOVENSE] Not connected!")
return
end
self.ws:send(
'42["anon_command_link_ts",{"toyCommandJson":"{\\"version\\":5,\\"cate\\":\\"id\\",\\"id\\":{\\"' ..
self.toys[1].id ..
'\\":{\\"v\\":' .. strength .. ',\\"v1\\":-1,\\"v2\\":-1,\\"v3\\":-1,\\"s\\":-1,\\"p\\":-1,\\"r\\":-1,\\"f\\":-1,\\"t\\":-1}}}","linkId":"' ..
self.sessionData.data.controlLinkData.linkId .. '","userTouch":false}]'
)
end
end
return LovenseSession