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

fea: Add HTTPS support #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion replace.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
nohup google-chrome --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 --no-sandbox --disable-dev-shm-usage --headless --disable-gpu >/dev/null 2>&1 &
sed -i "s/SONIC_SERVER_HOST/${SONIC_SERVER_HOST}/g" /usr/share/nginx/html/assets/*.js
sed -i "s/SONIC_SERVER_PORT/${SONIC_SERVER_PORT}/g" /usr/share/nginx/html/assets/*.js
sed -i "s/SONIC_SERVER_HTTPS/${SONIC_SERVER_HTTPS}/g" /usr/share/nginx/html/assets/*.js
NGINXCONF=/etc/nginx/nginx.conf
if [ "${SONIC_SERVER_HTTPS}" = "true" ]; then
NGINXCONF=/etc/nginx/nginx-https.conf
WSORIGINAL="ws:\/\/\${e}:\${t}\/websockets"
WSREPLACED="wss:\/\/${SONIC_SERVER_HOST}:${SONIC_SERVER_PORT}\/server\/websockets\/hub\/\${e}\/\${t}"
PROTORIGINAL="\`http:\/\/\`"
PROTREPLACED="\`https:\/\/\`"
sed -i "s/$PROTORIGINAL/$PROTREPLACED/g" /usr/share/nginx/html/assets/*.js
sed -i "s/$WSORIGINAL/$WSREPLACED/g" /usr/share/nginx/html/assets/*.js
fi
/usr/sbin/nginx -c $NGINXCONF -g 'daemon off;'
12 changes: 12 additions & 0 deletions src/config/sysdefines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default class SysDefines {

getWebProtocol() {
// Replace to return "https://" for HTTPS Support
return `http://`;
Copy link

@756434644 756434644 Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why return http://? Dosen‘t return https://?

Copy link
Author

@hazmi-e205 hazmi-e205 Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be replaced when the container run.
https://github.com/hazmi-e205/sonic-client-web/blob/6c9dd1a3543aaaedb00aaa3b3ae4528f1edc7911/replace.sh#L11

because the maintainer want to make it easier to switch between http and https

}

getWebsocketUrl(host, port) {
// Replace to return "wss://SONIC_SERVER_HOST:SONIC_SERVER_PORT/server/websockets/hub/${host}/${port}" for HTTPS Support
return `ws://${host}:${port}/websockets`;
}
}
5 changes: 4 additions & 1 deletion src/http/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import qs from 'qs';
import { ElMessage } from 'element-plus';
import { i18n, $tc } from '@/locales/setupI18n';
import { router } from '../router/index.js';
import SysDefines from '@/config/sysdefines';

let sysDef = new SysDefines();
let baseURL = '';
if (process.env.NODE_ENV === 'development') {
baseURL = 'http://localhost:3000/server/api';
Expand All @@ -30,9 +32,10 @@ if (process.env.NODE_ENV === 'production') {
}
const $http = axios.create();
baseURL = baseURL.replace(':80/', '/');
baseURL = baseURL.replace('http://', sysDef.getWebProtocol());
$http.defaults.baseURL = baseURL;
// $http.defaults.timeout = 20000;
$http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
// $http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$http.defaults.withCredentials = true;
$http.defaults.paramsSerializer = (params) =>
qs.stringify(params, { arrayFormat: 'brackets' });
Expand Down
14 changes: 9 additions & 5 deletions src/views/RemoteEmulator/AndroidRemote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import {
} from '@element-plus/icons';

import { useI18n } from 'vue-i18n';

import SysDefines from '@/config/sysdefines';
import AudioProcessor from '@/lib/audio-processor';
import wifiLogo from '@/assets/img/wifi.png';
import RenderDeviceName from '../../components/RenderDeviceName.vue';
Expand Down Expand Up @@ -496,17 +496,20 @@ const setImgData = () => {
};
isShowImg.value = true;
};

const sysDef = new SysDefines();
const openSocket = (host, port, key, udId) => {
if ('WebSocket' in window) {
baseUrl = sysDef.getWebsocketUrl(host, port);
//
websocket = new WebSocket(
`ws://${host}:${port}/websockets/android/${key}/${udId}/${localStorage.getItem(
`${baseUrl}/android/${key}/${udId}/${localStorage.getItem(
'SonicToken'
)}`
);
//
__Scrcpy = new Scrcpy({
socketURL: `ws://${host}:${port}/websockets/android/screen/${key}/${udId}/${localStorage.getItem(
socketURL: `${baseUrl}/android/screen/${key}/${udId}/${localStorage.getItem(
'SonicToken'
)}`,
node: 'scrcpy-video',
Expand All @@ -517,7 +520,7 @@ const openSocket = (host, port, key, udId) => {
changeScreenMode(screenMode.value, 1);
//
terminalWebsocket = new WebSocket(
`ws://${host}:${port}/websockets/android/terminal/${key}/${udId}/${localStorage.getItem(
`${baseUrl}/android/terminal/${key}/${udId}/${localStorage.getItem(
'SonicToken'
)}`
);
Expand Down Expand Up @@ -1650,9 +1653,10 @@ const getDeviceById = (id) => {
let audioPlayer = null;
const isConnectAudio = ref(false);
const initAudioPlayer = () => {
baseUrl = sysDef.getWebsocketUrl(agent.value.host, agent.value.port);
audioPlayer = new AudioProcessor({
node: 'audio-player',
wsUrl: `ws://${agent.value.host}:${agent.value.port}/websockets/audio/${agent.value.secretKey}/${device.value.udId}`,
wsUrl: `${baseUrl}/audio/${agent.value.secretKey}/${device.value.udId}`,
onReady() {
isConnectAudio.value = true;
},
Expand Down
9 changes: 6 additions & 3 deletions src/views/RemoteEmulator/IOSRemote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
InfoFilled,
} from '@element-plus/icons';
import { useI18n } from 'vue-i18n';
import SysDefines from '@/config/sysdefines';
import PackageList from '@/components/PackageList.vue';
import RenderDeviceName from '../../components/RenderDeviceName.vue';
import PocoPane from '../../components/PocoPane.vue';
Expand Down Expand Up @@ -490,20 +491,22 @@ const setImgData = (data) => {
};
isShowImg.value = true;
};
const sysDef = new SysDefines();
const openSocket = (host, port, key, udId) => {
if ('WebSocket' in window) {
baseUrl = sysDef.getWebsocketUrl(host, port);
websocket = new WebSocket(
`ws://${host}:${port}/websockets/ios/${key}/${udId}/${localStorage.getItem(
`${baseUrl}/ios/${key}/${udId}/${localStorage.getItem(
'SonicToken'
)}`
);
terminalWebsocket = new WebSocket(
`ws://${host}:${port}/websockets/ios/terminal/${key}/${udId}/${localStorage.getItem(
`${baseUrl}/ios/terminal/${key}/${udId}/${localStorage.getItem(
'SonicToken'
)}`
);
screenWebsocket = new WebSocket(
`ws://${host}:${port}/websockets/ios/screen/${key}/${udId}/${localStorage.getItem(
`${baseUrl}/ios/screen/${key}/${udId}/${localStorage.getItem(
'SonicToken'
)}`
);
Expand Down