HarmonyOS 鸿蒙Next VPN添加路由求大佬帮帮忙

发布于 1周前 作者 sinazl 最后一次编辑是 5天前 来自 鸿蒙OS

使用 VpnExtensionAbility,已成功开启了VPN。但现在我有一个需要本地访问192.168.31.106:8080时,使其走我本地的127.0.0.1:4321的本地TCP代理服务。应该如果添加路由呢。且我发现开启VPN(未设置路由),会导致我无法访问网络。是因为被我的VPN全部拦截导致的吗?

如下是我的VpnExtensionAbility:

import Want from '@ohos.app.ability.Want';
import VpnExtensionAbility from '@ohos.app.ability.VpnExtensionAbility';
import vpnExt from '@ohos.net.vpnExtension';
import hilog from '@ohos.hilog';

const TAG: string = "[VpnExtAbility]";

export default class VpnExtAbility extends VpnExtensionAbility {
  private VpnConnection: vpnExt.VpnConnection = {} as vpnExt.VpnConnection;
  private tunIp: string = '10.0.0.5';
  private blockedAppName: string = 'com.example.ndktest';

  onCreate(want: Want) {
    console.info(TAG, `onCreate, want: ${want.abilityName}`);
    this.VpnConnection = vpnExt.createVpnConnection(this.context);
    console.info("createVpnConnection success");
    this.CreateTunnel();
    this.Protect();
    this.SetupVpn();
  }

  onRequest(want: Want, startId: number) {
    console.info(TAG, `onRequest, want: ${want.abilityName}`);
  }

  onConnect(want: Want) {
    console.info(TAG, `onConnect, want: ${want.abilityName}`);
    return null;
  }

  onDisconnect(want: Want) {
    console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
  }

  onDestroy() {
    this.Destroy();
    console.info(TAG, `onDestroy`);
  }

  CreateTunnel() {
    console.info("CreateTunnel");
  }

  Protect() {
    hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Protect');
  }

  SetupVpn() {
    hilog.info(0x0000, 'developTag', '%{public}s', 'vpn SetupVpn');

    class Address {
      address: string;
      family: number;

      constructor(address: string, family: number) {
        this.address = address;
        this.family = family;
      }
    }

    class AddressWithPrefix {
      address: Address;
      prefixLength: number;

      constructor(address: Address, prefixLength: number) {
        this.address = address;
        this.prefixLength = prefixLength;
      }
    }

    class Config {
      addresses: AddressWithPrefix[];
      mtu: number;
      dnsAddresses: string[];
      trustedApplications: string[];
      blockedApplications: string[];
      routes:Array<vpnExt.RouteInfo>;
      constructor(
        tunIp: string,
        blockedAppName: string
      ) {
        this.addresses = [
          new AddressWithPrefix(new Address(tunIp, 1), 24)
        ];
        this.mtu = 1400;
        this.dnsAddresses = ["114.114.114.114"];
        this.trustedApplications = [];
        this.blockedApplications = [blockedAppName];
        this.routes = [];
      }
    }

    let config = new Config(this.tunIp, this.blockedAppName);
    config.routes.push( {
      interface : "eth0",
      destination : {
        address: {
          address : "192.168.31.106",family:1,port:8080
        },
        prefixLength:32
      },
      gateway : {
        address : "127.0.0.1"
      },
      hasGateway : true,
      isDefaultRoute: true
    } as vpnExt.RouteInfo)
    try {
      this.VpnConnection.create(config).then((data) => {
        hilog.error(0x0000, 'developTag', 'tunfd: %{public}s', JSON.stringify(data) ?? '');
      })
    } catch (error) {
      hilog.error(0x0000, 'developTag', 'vpn setUp fail: %{public}s', JSON.stringify(error) ?? '');
    }
  }

  Destroy() {
    hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Destroy');
    this.VpnConnection.destroy().then(() => {
      hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Destroy Success');
    }).catch((err : Error) => {
      hilog.error(0x0000, 'developTag', 'vpn Destroy Failed: %{public}s', JSON.stringify(err) ?? '');
    })
  }
}

更多关于HarmonyOS 鸿蒙Next VPN添加路由求大佬帮帮忙的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复
let config = new Config(this.tunIp, this.blockedAppName);
config.routes.push( {
  interface : "",
  destination : {
    address: {
      address : "192.168.31.106",family:1
    },
    prefixLength:32
  },
  gateway : {
    address : "10.0.0.1"
  },
  hasGateway : false,
  isDefaultRoute: false
} as vpnExt.RouteInfo)
 

更多关于HarmonyOS 鸿蒙Next VPN添加路由求大佬帮帮忙的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙系统中添加VPN路由,可以通过以下步骤进行操作:

  1. 获取VPN配置文件:首先,确保你有一个有效的VPN配置文件,这通常包括VPN服务器地址、认证方式、密钥等必要信息。

  2. 打开VPN设置:进入HarmonyOS系统的“设置”应用,找到并点击“网络和互联网”或类似名称的选项,然后选择“VPN”。

  3. 添加VPN:在VPN设置页面,点击“添加VPN网络”。按照提示输入VPN的名称、类型(通常是PPTP、L2TP/IPSec或OpenVPN,具体取决于你的VPN服务),以及之前获取的VPN配置文件中的相关信息。

  4. 配置路由:在VPN设置的高级选项中,找到“路由选项”。根据需要选择是否“总是通过此VPN发送数据”或者指定哪些应用或网络地址通过此VPN。

  5. 保存并连接:完成配置后,保存设置并尝试连接VPN。系统将会根据配置尝试建立VPN连接。

  6. 验证连接:连接成功后,可以通过访问特定的测试网站或IP地址来验证VPN是否正常工作。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部