1
+ import net from 'net'
1
2
import { Client } from 'minecraft-protocol'
2
3
import { appQueryParams } from '../appParams'
3
4
import { downloadAllMinecraftData , getVersionAutoSelect } from '../connect'
4
5
import { gameAdditionalState } from '../globalState'
5
6
import { ProgressReporter } from '../core/progressReporter'
7
+ import { parseServerAddress } from '../parseServerAddress'
8
+ import { getCurrentProxy } from '../react/ServersList'
6
9
import { pingServerVersion , validatePacket } from './minecraft-protocol-extra'
7
10
import { getWebsocketStream } from './websocket-core'
8
11
@@ -35,14 +38,16 @@ setInterval(() => {
35
38
} , 1000 )
36
39
37
40
38
- export const getServerInfo = async ( ip : string , port ?: number , preferredVersion = getVersionAutoSelect ( ) , ping = false , progressReporter ?: ProgressReporter ) => {
41
+ export const getServerInfo = async ( ip : string , port ?: number , preferredVersion = getVersionAutoSelect ( ) , ping = false , progressReporter ?: ProgressReporter , setProxyParams ?: ProxyParams ) => {
39
42
await downloadAllMinecraftData ( )
40
43
const isWebSocket = ip . startsWith ( 'ws://' ) || ip . startsWith ( 'wss://' )
41
44
let stream
42
45
if ( isWebSocket ) {
43
46
progressReporter ?. setMessage ( 'Connecting to WebSocket server' )
44
47
stream = ( await getWebsocketStream ( ip ) ) . mineflayerStream
45
48
progressReporter ?. setMessage ( 'WebSocket connected. Ping packet sent, waiting for response' )
49
+ } else if ( setProxyParams ) {
50
+ setProxy ( setProxyParams )
46
51
}
47
52
window . setLoadingMessage = ( message ?: string ) => {
48
53
if ( message === undefined ) {
@@ -59,3 +64,45 @@ export const getServerInfo = async (ip: string, port?: number, preferredVersion
59
64
window . setLoadingMessage = undefined
60
65
} )
61
66
}
67
+
68
+ globalThis . debugTestPing = async ( ip : string ) => {
69
+ const parsed = parseServerAddress ( ip , false )
70
+ const result = await getServerInfo ( parsed . host , parsed . port ? Number ( parsed . port ) : undefined , undefined , true , undefined , { address : getCurrentProxy ( ) , } )
71
+ console . log ( 'result' , result )
72
+ return result
73
+ }
74
+
75
+ export const getDefaultProxyParams = ( ) => {
76
+ return {
77
+ headers : {
78
+ Authorization : `Bearer ${ new URLSearchParams ( location . search ) . get ( 'token' ) ?? '' } `
79
+ }
80
+ }
81
+ }
82
+
83
+ export type ProxyParams = {
84
+ address ?: string
85
+ headers ?: Record < string , string >
86
+ }
87
+
88
+ export const setProxy = ( proxyParams : ProxyParams ) => {
89
+ if ( proxyParams . address ?. startsWith ( ':' ) ) {
90
+ proxyParams . address = `${ location . protocol } //${ location . hostname } ${ proxyParams . address } `
91
+ }
92
+ if ( proxyParams . address && location . port !== '80' && location . port !== '443' && ! / : \d + $ / . test ( proxyParams . address ) ) {
93
+ const https = proxyParams . address . startsWith ( 'https://' ) || location . protocol === 'https:'
94
+ proxyParams . address = `${ proxyParams . address } :${ https ? 443 : 80 } `
95
+ }
96
+
97
+ const parsedProxy = parseServerAddress ( proxyParams . address , false )
98
+ const proxy = { host : parsedProxy . host , port : parsedProxy . port }
99
+ proxyParams . headers ??= getDefaultProxyParams ( ) . headers
100
+ net [ 'setProxy' ] ( {
101
+ hostname : proxy . host ,
102
+ port : proxy . port ,
103
+ headers : proxyParams . headers
104
+ } )
105
+ return {
106
+ proxy
107
+ }
108
+ }
0 commit comments