Skip to content

Commit

Permalink
Merge pull request #716 from Chilledheart/harmony_show_current_ip_and…
Browse files Browse the repository at this point in the history
…_port

harmony: show current IP and port
  • Loading branch information
Chilledheart authored Jan 31, 2024
2 parents b75bdaa + 106697d commit 29560ef
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 24 deletions.
2 changes: 1 addition & 1 deletion harmony/entry/src/main/cpp/types/libyass/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const getCipherStrings: () => string[];
export const getTimeout: () => number;
export const init: (temp_dir: string, data_dir: string) => void;
export const destroy: () => void;
export const startWorker: (cb: (err_msg: string) => void) => void;
export const startWorker: (cb: (err_msg: string, port: number) => void) => void;
export const stopWorker: (cb: () => void) => void;
export const saveConfig: (server_host: string, server_sni: string, server_port: string,
username: string, password: string, cipher: string, timeout: string) => string;
73 changes: 57 additions & 16 deletions harmony/entry/src/main/ets/pages/DetailPage.ets
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { YassDataItem } from '../viewmodel/YassDataItem';
import YassViewModel from '../viewmodel/YassViewModel';
import hilog from '@ohos.hilog';
import yass from 'libyass.so';
// import wifiManager from '@ohos.wifiManager';
import wifi from '@ohos.wifi';
import UIAbility from '@ohos.app.ability.UIAbility';

/**
* Detail page. Click the item on about page to jump to the detail page.
Expand All @@ -23,14 +26,16 @@ struct DetailPage {
@State private dataParam: YassDataItem = new YassDataItem;

private state: StartState = StartState.STOPPED;
@State private status: string = "";
@State private status: Resource = $r('app.string.status_stopped', 'localhost', 0);
@State private wifi_status: Resource = $r('app.string.current_ip', '0.0.0.0', 0);

private timer_id: number;

aboutToAppear() {
this.titleParam = $r('app.string.title_name');
this.dataParam = YassViewModel.getYassData();
this.status = "Disconnected with " + this.dataParam.serverHost;
this.status = $r('app.string.status_stopped', this.dataParam.serverHost, this.dataParam.serverPort);
this.wifi_status = $r('app.string.current_ip', this.getWifiAddress(), 0)
}

build() {
Expand Down Expand Up @@ -61,30 +66,53 @@ struct DetailPage {
}
}

.margin({
left: $r('app.float.grid_row_margin_left'),
right: $r('app.float.grid_row_margin_right')
})
Row() {
Text(this.status)
.width(CommonConstants.DETAIL_LIST_WIDTH_PERCENT)
.fontSize($r('app.float.name_text_size'))
.fontColor($r('app.color.text'))
}
.margin({
left: $r('app.float.grid_row_margin_left'),
right: $r('app.float.grid_row_margin_right')
})
Row() {
Text(this.wifi_status)
.width(CommonConstants.DETAIL_LIST_WIDTH_PERCENT)
.fontSize($r('app.float.name_text_size'))
.fontColor($r('app.color.text'))
}
.margin({
left: $r('app.float.grid_row_margin_left'),
right: $r('app.float.grid_row_margin_right')
})
Row() {
Button($r('app.string.start_button'), { type: ButtonType.Capsule,
stateEffect: this.state == StartState.STOPPED }).onClick((event: ClickEvent) => {
stateEffect: this.state == StartState.STOPPED })
.width(CommonConstants.DETAIL_INPUT_WIDTH_PERCENT)
.onClick((event: ClickEvent) => {
if (this.state == StartState.STOPPED) {
this.onStartClicked();
}
})
Blank()
Button($r('app.string.stop_button'), { type: ButtonType.Capsule,
stateEffect: this.state == StartState.STARTED }).onClick((event: ClickEvent) => {
stateEffect: this.state == StartState.STARTED })
.width(CommonConstants.DETAIL_INPUT_WIDTH_PERCENT)
.onClick((event: ClickEvent) => {
if (this.state == StartState.STARTED) {
this.onStopClicked();
}
})
}
Row() {
Text(this.status)
.fontSize($r('app.float.name_text_size'))
.fontColor($r('app.color.text'))
}
.margin({
left: $r('app.float.grid_row_margin_left'),
right: $r('app.float.grid_row_margin_right')
})
}
.width(CommonConstants.DETAIL_COLUMN_WIDTH_PERCENT)
.height(CommonConstants.DETAIL_COLUMN_HEIGHT_PERCENT)
Expand Down Expand Up @@ -115,7 +143,7 @@ struct DetailPage {

onStartClicked() {
this.state = StartState.STARTING;
this.status = "Starting";
this.status = $r('app.string.status_starting');

let err_msg = yass.saveConfig(this.dataParam.serverHost, this.dataParam.serverSNI, this.dataParam.serverPort.toString(),
this.dataParam.username, this.dataParam.password, this.dataParam.cipher, this.dataParam.timeout.toString());
Expand All @@ -124,10 +152,11 @@ struct DetailPage {
return;
}

yass.startWorker((err_msg: string) => {
yass.startWorker((err_msg: string, port: number) => {
hilog.info(0x0000, 'yass', 'started %s', err_msg);
if (err_msg == '') {
this.state = StartState.STARTED;
this.wifi_status = $r('app.string.current_ip', this.getWifiAddress(), port);
this.onUpdateTransferRate();
} else {
this.onStartFailedWithMsg(err_msg);
Expand All @@ -137,7 +166,7 @@ struct DetailPage {

onUpdateTransferRate() {
let rates = yass.getTransferRate();
this.status = "Connected rx rate " + rates[0] + " tx rate " + rates[1];
this.status = $r('app.string.status_started_with_rate', rates[0], rates[1]);
hilog.warn(0x0000, 'yass', '%{public}s', 'on refresh: ' + this.status);
this.timer_id = setTimeout(() => {
this.onUpdateTransferRate();
Expand All @@ -146,18 +175,30 @@ struct DetailPage {

onStartFailedWithMsg(err_msg) {
this.state = StartState.STOPPED;
this.status = "Failed to start due to " + err_msg;
AlertDialog.show({ message: 'The start failed: ' + err_msg })
this.status = $r('app.string.status_started_with_error_msg', err_msg);
AlertDialog.show({ message: 'The start failed: ' + err_msg });
}

onStopClicked() {
this.state = StartState.STOPPING;
this.status = "Stopping"
this.status = $r('app.string.status_stopping');
clearTimeout(this.timer_id);
yass.stopWorker(() => {
hilog.info(0x0000, 'yass', 'stopped %d');
this.state = StartState.STOPPED;
this.status = "Disconnected with " + this.dataParam.serverHost;
this.status = $r('app.string.status_stopped', this.dataParam.serverHost, this.dataParam.serverPort);
})
}

getWifiAddress() {
if (wifi.isConnected()) {
return wifi.getIpInfo().ipAddress.toString();
}
/* FIXME crash on emulator
if (wifiManager.isConnected()) {
return wifiManager.getIpInfo().ipAddress.toString();
}
*/
return "0.0.0.0";
}
}
9 changes: 9 additions & 0 deletions harmony/entry/src/main/module.json5
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
],
"when": "inuse"
}
},
{
"name": "ohos.permission.GET_WIFI_INFO",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "inuse"
}
}
]
}
Expand Down
24 changes: 24 additions & 0 deletions harmony/entry/src/main/resources/base/element/string.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@
{
"name": "stop_button",
"value": "Stop"
},
{
"name": "current_ip",
"value": "Current IP: %s:%d"
},
{
"name": "status_started_with_rate",
"value": "Connected rx rate: %s tx rate: %s"
},
{
"name": "status_started_with_error_msg",
"value": "Failed to connect due to %s"
},
{
"name": "status_stopping",
"value": "STOPPING"
},
{
"name": "status_starting",
"value": "STARTING"
},
{
"name": "status_stopped",
"value": "Disconnected with %s:%d"
}
]
}
26 changes: 25 additions & 1 deletion harmony/entry/src/main/resources/en_US/element/string.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
{
"name": "copyright",
"value": "Copyright © 2022-2023 xx company all rights reserved"
"value": "Copyright © 2024 Chilledheart all rights reserved"
},
{
"name": "detail_item_title",
Expand Down Expand Up @@ -71,6 +71,30 @@
{
"name": "stop_button",
"value": "Stop"
},
{
"name": "current_ip",
"value": "Current IP: %s:%d"
},
{
"name": "status_started_with_rate",
"value": "Connected rx rate: %s tx rate: %s"
},
{
"name": "status_started_with_error_msg",
"value": "Failed to connect due to %s"
},
{
"name": "status_stopping",
"value": "STOPPING"
},
{
"name": "status_starting",
"value": "STARTING"
},
{
"name": "status_stopped",
"value": "Disconnected with %s:%d"
}
]
}
26 changes: 25 additions & 1 deletion harmony/entry/src/main/resources/zh_CN/element/string.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
{
"name": "copyright",
"value": "版权所有 ©2022-2023 xx公司保留一切权利"
"value": "版权所有 ©2024 Chilledheart 保留一切权利"
},
{
"name": "detail_item_title",
Expand Down Expand Up @@ -71,6 +71,30 @@
{
"name": "stop_button",
"value": "停止"
},
{
"name": "current_ip",
"value": "本地IP地址: %s:%d"
},
{
"name": "status_started_with_rate",
"value": "已连接 上传速率: %s 下载速率: %s"
},
{
"name": "status_started_with_error_msg",
"value": "无法连接因: %s"
},
{
"name": "status_stopping",
"value": "停止中"
},
{
"name": "status_starting",
"value": "启动中"
},
{
"name": "status_stopped",
"value": "断开连接于服务器 %s:%d"
}
]
}
27 changes: 22 additions & 5 deletions src/harmony/yass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,21 @@ static napi_value stopTun2proxy(napi_env env, napi_callback_info info) {
}

static constexpr char kAsyncStartWorkerResourceName[] = "Thread-safe StartWorker";
struct AsyncStartCtx {
asio::error_code ec;
int port_num;
};

static void startWorkerCallingJS(napi_env env, napi_value /*js_cb*/, void *context, void *data) {
napi_ref cb_ref = reinterpret_cast<napi_ref>(context);

std::unique_ptr<asio::error_code> ec(reinterpret_cast<asio::error_code*>(data));
std::unique_ptr<AsyncStartCtx> ctx(reinterpret_cast<AsyncStartCtx*>(data));
std::ostringstream ss;
if (*ec) {
ss << *ec;
if (ctx->ec) {
ss << ctx->ec;
}
std::string ec_str = ss.str();
int port_num = ctx->port_num;

if (env == nullptr) {
LOG(WARNING) << "null env";
Expand Down Expand Up @@ -476,13 +481,20 @@ static void startWorkerCallingJS(napi_env env, napi_value /*js_cb*/, void *conte

napi_value err_msg;
status = napi_create_string_utf8(env, ec_str.c_str(), NAPI_AUTO_LENGTH, &err_msg);
if (status != napi_ok) {
LOG(WARNING) << "napi_create_string_utf8: " << status;
return;
}

napi_value port;
status = napi_create_int32(env, port_num, &port);
if (status != napi_ok) {
LOG(WARNING) << "napi_create_int32: " << status;
return;
}

napi_value result;
napi_value argv[] = { err_msg };
napi_value argv[] = { err_msg, port };
status = napi_call_function(env, global, cb, std::size(argv), argv, &result);
if (status != napi_ok) {
LOG(WARNING) << "napi_call_function: " << status;
Expand Down Expand Up @@ -553,15 +565,20 @@ static napi_value startWorker(napi_env env, napi_callback_info info) {
if (!ec) {
config::SaveConfig();
}
std::unique_ptr<AsyncStartCtx> ctx = std::make_unique<AsyncStartCtx>();
ctx->ec = ec;
ctx->port_num = ec ? 0 : g_worker->GetLocalPort();

auto status = napi_acquire_threadsafe_function(startWorkerCallbackFunc);
if (status != napi_ok) {
LOG(WARNING) << "napi_acquire_threadsafe_function: " << status;
}

status = napi_call_threadsafe_function(startWorkerCallbackFunc, new asio::error_code(ec), napi_tsfn_blocking);
auto ctx_raw = ctx.release();
status = napi_call_threadsafe_function(startWorkerCallbackFunc, ctx_raw, napi_tsfn_blocking);
if (status != napi_ok) {
LOG(WARNING) << "napi_call_threadsafe_function: " << status;
delete ctx_raw;
}

status = napi_release_threadsafe_function(startWorkerCallbackFunc, napi_tsfn_release);
Expand Down

0 comments on commit 29560ef

Please sign in to comment.