Add http/https support to socketio url valid

This commit is contained in:
moonrailgun
2020-03-05 11:15:07 +08:00
parent 08e578b4b4
commit 94b2688073
2 changed files with 27 additions and 14 deletions

View File

@@ -74,7 +74,7 @@
</template> </template>
<script> <script>
import { wsValid } from "~/functions/utils/valid" import { socketioValid } from "~/functions/utils/valid"
import io from "socket.io-client" import io from "socket.io-client"
import realtimeLog from "./log" import realtimeLog from "./log"
@@ -97,7 +97,7 @@ export default {
}, },
computed: { computed: {
urlValid() { urlValid() {
return wsValid(this.url) return socketioValid(this.url)
}, },
}, },
methods: { methods: {

View File

@@ -1,14 +1,21 @@
function generateIPRE(protocol) {
return new RegExp(
`${protocol}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`
)
}
function generateHostnameRE(protocol) {
return new RegExp(
`${protocol}(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9/])$`
)
}
/** /**
* valid url for ws/wss * valid url for ws/wss
*/ */
export function wsValid(url) { export function wsValid(url) {
const protocol = "^(wss?:\\/\\/)?" const protocol = "^(wss?:\\/\\/)?"
const validIP = new RegExp( const validIP = generateIPRE(protocol)
`${protocol}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$` const validHostname = generateHostnameRE(protocol)
)
const validHostname = new RegExp(
`${protocol}(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9/])$`
)
return validIP.test(url) || validHostname.test(url) return validIP.test(url) || validHostname.test(url)
} }
@@ -17,11 +24,17 @@ export function wsValid(url) {
*/ */
export function sseValid(url) { export function sseValid(url) {
const protocol = "^(https?:\\/\\/)?" const protocol = "^(https?:\\/\\/)?"
const validIP = new RegExp( const validIP = generateIPRE(protocol)
`${protocol}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$` const validHostname = generateHostnameRE(protocol)
) return validIP.test(url) || validHostname.test(url)
const validHostname = new RegExp( }
`${protocol}(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9/])$`
) /**
* valid url for ws/wss/http/https
*/
export function socketioValid(url) {
const protocol = "^((wss?:\\/\\/)|(https?:\\/\\/))?"
const validIP = generateIPRE(protocol)
const validHostname = generateHostnameRE(protocol)
return validIP.test(url) || validHostname.test(url) return validIP.test(url) || validHostname.test(url)
} }