(addr: string, require: "http" | "ws")
| 35 | } |
| 36 | |
| 37 | export function parseForwardAddress(addr: string, require: "http" | "ws") { |
| 38 | // save its protocol header |
| 39 | //ws://127.0.0.1:25565 |
| 40 | let protocol = `${window.location.protocol}//`; |
| 41 | const addrProtocolString = addr.toLocaleLowerCase(); |
| 42 | if (require === "http") { |
| 43 | if (addrProtocolString.indexOf("ws://") === 0) protocol = "http://"; |
| 44 | else if (addrProtocolString.indexOf("wss://") === 0) protocol = "https://"; |
| 45 | else if (addrProtocolString.indexOf("http://") === 0) protocol = "http://"; |
| 46 | else if (addrProtocolString.indexOf("https://") === 0) protocol = "https://"; |
| 47 | else if (protocol === "https://") protocol = "https://"; |
| 48 | else protocol = "http://"; |
| 49 | } |
| 50 | if (require === "ws") { |
| 51 | if (addrProtocolString.indexOf("http://") === 0) protocol = "ws://"; |
| 52 | else if (addrProtocolString.indexOf("https://") === 0) protocol = "wss://"; |
| 53 | else if (addrProtocolString.indexOf("ws://") === 0) protocol = "ws://"; |
| 54 | else if (addrProtocolString.indexOf("wss://") === 0) protocol = "wss://"; |
| 55 | else if (protocol === "https://") protocol = "wss://"; |
| 56 | else protocol = "ws://"; |
| 57 | } |
| 58 | |
| 59 | // remove potentially redundant headers |
| 60 | addr = deleteWebsocketHeader(deleteHttpHeader(addr)); |
| 61 | |
| 62 | // port and ip are separated |
| 63 | let daemonPort = null; |
| 64 | let onlyAddr = null; |
| 65 | if (addr.split(":").length === 2) { |
| 66 | onlyAddr = addr.split(":")[0]; |
| 67 | daemonPort = parseInt(addr.split(":")[1].split("/")[0]); |
| 68 | if (isNaN(daemonPort)) |
| 69 | throw new Error(`The address ${addr} failed to resolve, the port is incorrect`); |
| 70 | } else { |
| 71 | onlyAddr = addr; |
| 72 | } |
| 73 | |
| 74 | let path = null; |
| 75 | if (addr.indexOf("/") != -1) { |
| 76 | path = addr.slice(addr.indexOf("/")); |
| 77 | } |
| 78 | |
| 79 | // Reassemble the address based on the separated port and ip |
| 80 | const checkAddr = onlyAddr.toLocaleLowerCase(); |
| 81 | if (checkAddr.indexOf("localhost") === 0 || checkAddr.indexOf("127.0.0.") === 0) { |
| 82 | addr = `${protocol}${window.location.hostname}${daemonPort ? `:${daemonPort}` : ""}${ |
| 83 | path ?? "" |
| 84 | }`; |
| 85 | } else { |
| 86 | addr = `${protocol}${onlyAddr}${daemonPort ? `:${daemonPort}` : ""}${path ?? ""}`; |
| 87 | } |
| 88 | return addr; |
| 89 | } |
| 90 | |
| 91 | // The ws address on the Daemon side is converted into an http address |
| 92 | export function daemonWsAddressToHttp(wsAddr = "") { |
no test coverage detected