Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement auth #322

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"defaultHostPort": 10000,
"defaultProxy": "",
"defaultProxyPort": 0,
"defaultVersion": "1.18.2"
"defaultVersion": "1.18.2",
"extension_id": "ncghkfgegekillgpmbpacghgfbekcdkj"
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ async function connect (options) {
viewDistance: 'tiny',
checkTimeoutInterval: 240 * 1000,
noPongTimeout: 240 * 1000,
closeTimeout: 240 * 1000
closeTimeout: 240 * 1000,
session: options.onlineMode ? options.auth : undefined,
skipValidation: true
})

bot.on('error', (err) => {
Expand Down
30 changes: 29 additions & 1 deletion lib/menus/play_screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,37 @@ class PlayScreen extends LitElement {
this.username = window.localStorage.getItem('username') ?? 'pviewer' + (Math.floor(Math.random() * 1000))
this.password = ''
this.version = ''
this.onlineMode = false
this.auth = {}

window.fetch('config.json').then(res => res.json()).then(config => {
this.server = config.defaultHost
this.serverport = config.defaultHostPort ?? 25565
this.proxy = config.defaultProxy
this.proxyport = !config.defaultProxy && !config.defaultProxyPort ? '' : config.defaultProxyPort ?? 443
this.version = config.defaultVersion
if (chrome && chrome.runtime && config.extension_id) {
try {
chrome.runtime.sendMessage(config.extension_id, { type: 'login' }, response => {
if (response) {
const username = this.shadowRoot.getElementById('username')
username.shadowRoot.getElementById('username').disabled = true
username.value = response.name
this.username = response.name
this.onlineMode = true

this.auth = {
accessToken: response.access_token,
clientToken: response.client_token,
selectedProfile: {
id: response.uuid,
name: response.name
}
}
}
})
} catch (e) {}
}
})
}

Expand Down Expand Up @@ -124,13 +148,15 @@ class PlayScreen extends LitElement {
pmui-width="150px"
pmui-label="Username"
pmui-id="username"
id="username"
pmui-value="${this.username}"
@input=${e => { this.username = e.target.value }}
></pmui-editbox>
<pmui-editbox
pmui-width="150px"
pmui-label="Bot Version"
pmui-id="botversion"
id="botversion"
pmui-value="${this.version}"
@input=${e => { this.version = e.target.value }}
></pmui-editbox>
Expand All @@ -154,7 +180,9 @@ class PlayScreen extends LitElement {
proxy: `${this.proxy}${this.proxy !== '' ? `:${this.proxyport}` : ''}`,
username: this.username,
password: this.password,
botVersion: this.version
botVersion: this.version,
onlineMode: this.onlineMode,
auth: this.auth
}
}))
}
Expand Down
31 changes: 31 additions & 0 deletions modules/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const config = require('../config.json')

let hasExtension
if (chrome && chrome.runtime && config.extension_id) {
try {
chrome.runtime.sendMessage(config.extension_id, { type: 'version' }, response => {
hasExtension = true
})
} catch (e) {
hasExtension = false
}
} else {
hasExtension = false
}

module.exports = (...args) => {
if (!hasExtension) {
return window.fetch
}

return new Promise(resolve => {
chrome.runtime.sendMessage(config.extension_id || '', { type: 'fetch', req: args }, response => {
resolve({
status: response.status,
text: async function () {
return response.text
}
})
})
})
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"compression": "^1.7.4",
"express": "^4.17.1",
"fetch-no-cors": "^1.0.13",
"ismobilejs": "^1.1.1",
"lit": "^2.0.2",
"net-browserify": "PrismarineJS/net-browserify",
Expand Down Expand Up @@ -74,5 +75,8 @@
"webpack-dev-server": "^4.0.0",
"webpack-merge": "^5.7.3",
"workbox-webpack-plugin": "^6.1.2"
},
"standard": {
"globals": [ "chrome" ]
}
}
1 change: 1 addition & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const config = {
express: false,
net: 'net-browserify',
fs: 'memfs',
'node-fetch': require.resolve('./modules/fetch.js'),
jose: false
},
fallback: {
Expand Down