diff --git a/Scripts/DCS-SRS-AutoConnectGameGUI.lua b/Scripts/DCS-SRS-AutoConnectGameGUI.lua index 2ea573ea..9b98fe1e 100644 --- a/Scripts/DCS-SRS-AutoConnectGameGUI.lua +++ b/Scripts/DCS-SRS-AutoConnectGameGUI.lua @@ -42,6 +42,7 @@ SRSAuto.unicast = true local HOST_PLAYER_ID = 1 SRSAuto.MESSAGE_PREFIX = "SRS Running @ " -- DO NOT MODIFY!!! +SRSAuto.MESSAGE_PREFIX_PORT = "SRS Running on " -- DO NOT MODIFY!!! package.path = package.path..";.\\LuaSocket\\?.lua;" package.cpath = package.cpath..";.\\LuaSocket\\?.dll;" @@ -67,16 +68,24 @@ function SRSAuto.log(str) end end +function SRSAuto.sendAutoConnectMessage(id) + SRSAuto.log(string.format("Sending auto connect message to player %d on connect ", id)) + if SRSAuto.SERVER_SRS_HOST_AUTO then + net.send_chat_to(SRSAuto.MESSAGE_PREFIX_PORT .. SRSAuto.SERVER_SRS_PORT, id) + else + net.send_chat_to(SRSAuto.MESSAGE_PREFIX .. SRSAuto.SERVER_SRS_HOST .. ":" .. SRSAuto.SERVER_SRS_PORT, id) + end +end + -- Register callbacks -- SRSAuto.onPlayerConnect = function(id) if not DCS.isServer() then return end - if SRSAuto.SERVER_SEND_AUTO_CONNECT and id ~= HOST_PLAYER_ID then - SRSAuto.log(string.format("Sending auto connect message to player %d on connect ", id)) - net.send_chat_to(string.format(SRSAuto.MESSAGE_PREFIX .. "%s", SRSAuto.SERVER_SRS_HOST..":"..SRSAuto.SERVER_SRS_PORT), id) - end + if SRSAuto.SERVER_SEND_AUTO_CONNECT and id ~= HOST_PLAYER_ID then + sendAutoConnectMessage(id) + end end SRSAuto.onPlayerChangeSlot = function(id) @@ -84,9 +93,8 @@ SRSAuto.onPlayerChangeSlot = function(id) return end if SRSAuto.SERVER_SEND_AUTO_CONNECT and id ~= HOST_PLAYER_ID then - SRSAuto.log(string.format("Sending auto connect message to player %d on switch ", id)) - net.send_chat_to(string.format(SRSAuto.MESSAGE_PREFIX .. "%s", SRSAuto.SERVER_SRS_HOST..":"..SRSAuto.SERVER_SRS_PORT), id) - end + sendAutoConnectMessage(id) + end end SRSAuto.trimStr = function(_str) @@ -119,24 +127,6 @@ end local _lastSent = 0 SRSAuto.onMissionLoadBegin = function() - local _status, _result = pcall( function() - if SRSAuto.SERVER_SRS_HOST_AUTO then - local ipLookupUrl = "https://ipv4.icanhazip.com" - local T, code, headers, status = socket.http.request(ipLookupUrl) - - if T == nil or code == nil or code < 200 or code >= 300 then - if code == nil then code = "??" end - SRSAuto.log("Failed to lookup IP from "..ipLookupUrl..". Http Status: " .. code) - else - SRSAuto.SERVER_SRS_HOST = T - SRSAuto.log("SET IP automatically to "..SRSAuto.SERVER_SRS_HOST) - end - end - end) - - if not _status then - SRSAuto.log('ERROR: ' .. _result) - end end SRSAuto.onSimulationFrame = function() diff --git a/Scripts/DCS-SRS/Scripts/DCS-SRSGameGUI.lua b/Scripts/DCS-SRS/Scripts/DCS-SRSGameGUI.lua index 13ace412..7b3bbf7e 100644 --- a/Scripts/DCS-SRS/Scripts/DCS-SRSGameGUI.lua +++ b/Scripts/DCS-SRS/Scripts/DCS-SRSGameGUI.lua @@ -101,8 +101,9 @@ SRS.sendUpdate = function(playerID) socket.try(SRS.UDPSendSocket:sendto(_jsonUpdate, "127.0.0.1", 9087)) end -SRS.MESSAGE_PREFIX_OLD = "This server is running SRS on - " -- DO NOT MODIFY!!! -SRS.MESSAGE_PREFIX = "SRS Running @ " -- DO NOT MODIFY!!! +SRS.MESSAGE_PATTERN_OLDER = "This server is running SRS on - (%d+%.%d+%.%d+%.%d+:?%d*)" -- DO NOT MODIFY!!! +SRS.MESSAGE_PATTERN_OLD = "SRS Running @ (%d+%.%d+%.%d+%.%d+:?%d*)" -- DO NOT MODIFY!!! +SRS.MESSAGE_PATTERN = "SRS Running on (%d+)" -- DO NOT MODIFY!!! function string.startsWith(string, prefix) return string.sub(string, 1, string.len(prefix)) == prefix @@ -112,16 +113,15 @@ function string.trim(_str) return string.format( "%s", _str:match( "^%s*(.-)%s*$" ) ) end -function SRS.isAutoConnectMessage(msg) - return string.startsWith(string.trim(msg), SRS.MESSAGE_PREFIX) or string.startsWith(string.trim(msg), SRS.MESSAGE_PREFIX_OLD) +function SRS.getPortFromMessage(msg) + return msg:match(SRS.MESSAGE_PATTERN) end function SRS.getHostFromMessage(msg) - if string.startsWith(string.trim(msg), SRS.MESSAGE_PREFIX_OLD) then - return string.trim(string.sub(msg, string.len(SRS.MESSAGE_PREFIX_OLD) + 1)) - else - return string.trim(string.sub(msg, string.len(SRS.MESSAGE_PREFIX) + 1)) - end + local host = msg:match(SRS.MESSAGE_PATTERN_OLD) + if host ~= nil then return host end + + return msg:match(SRS.MESSAGE_PATTERN_OLDER) end -- Register callbacks -- @@ -307,27 +307,33 @@ SRS.handleRadio = function(msg) end SRS.onChatMessage = function(msg, from) + -- Only accept auto connect message coming from host. + if SRS.CLIENT_ACCEPT_AUTO_CONNECT and from == 1 then + local host = nil + local port = SRS.getPortFromMessage(msg) + if port ~= nil then + local ip = net.get_server_host() + host = ip .. ':' .. port + else + host = SRS.getHostFromMessage(msg) + end + if host == nil then + SRS.log("Error getting host from message: " .. msg) + return + end + SRS.log(string.format("Got SRS Auto Connect message: %s", host)) - -- Only accept auto connect message coming from host. - if SRS.CLIENT_ACCEPT_AUTO_CONNECT - and from == 1 - and SRS.isAutoConnectMessage(msg) then - local host = SRS.getHostFromMessage(msg) - SRS.log(string.format("Got SRS Auto Connect message: %s", host)) - - local enabled = OptionsData.getPlugin("DCS-SRS","srsAutoLaunchEnabled") - if srs and enabled then - local path = srs.get_srs_path() - if path ~= "" then - - net.log("Trying to Launch SRS @ "..path) - srs.start_srs(host) - end - - end - SRS.sendConnect(host) - end + local enabled = OptionsData.getPlugin("DCS-SRS", "srsAutoLaunchEnabled") + if srs and enabled then + local path = srs.get_srs_path() + if path ~= "" then + net.log("Trying to Launch SRS @ " .. path) + srs.start_srs(host) + end + end + SRS.sendConnect(host) + end -- MESSAGE FROM MYSELF if from == net.get_my_player_id() then