VPN 无法创建 HarmonyOS 鸿蒙Next

VPN 无法创建 HarmonyOS 鸿蒙Next 工程设置:

创建代码:

主进程中调用VPN 服务:

cke_580.png

VPNExtAbility 实现:

export default class VPNExtAbility extends VpnExtensionAbility {

  vpnConnection?: vpnExtension.VpnConnection;
  udpServer?: socket.UDPSocket;
  g_tunnelFd: number = -1;
  tunFd: number = -1;

  onCreate(want: Want): void {
    console.log("onCreate")

    this.udpServer = socket.constructUDPSocketInstance();
    let bindAddr: socket.NetAddress = {
      address: '127.0.0.1',
      port: 8081
    }
    this.udpServer.bind(bindAddr).then(() => {

    }).catch((err: BusinessError) => {
      console.log("error: " + err.message)
    });
    this.udpServer.on('message', (value: socket.SocketMessageInfo) => {

      let array: Uint8Array = new Uint8Array(value.message);
      console.log('on message message: ' + JSON.stringify(array));

      let version = (array[0] & 0xFF) >> 4;
      console.log('on message version: ' + version);

      let ip_protocol = array[6] & 0xFF;
      console.log('on message ip_protocol: ' + ip_protocol)

      let ip_hdrlen  = 40;
      let dport = (array[ip_hdrlen + 2] & 255) << 8 | array[ip_hdrlen + 3] & 255;

      console.log('on message dport: ' + dport)

      let hex: string = "";
      for (let i: number = 0; i < array.length; i++) {
        hex += array[i].toString(16).padStart(2, '0');
      }
      console.log("hex======" ,hex);

      console.log('remoteInfo: ' + JSON.stringify(value.remoteInfo));
    });
    this.udpServer.on('error', (err: BusinessError) => {
      console.log('on error message: ' + JSON.stringify(err));
    })


    this.vpnConnection = vpnExtension.createVpnConnection(this.context)

    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[] = [];
      routes: vpnExtension.RouteInfo[] = [];
      searchDomains: string[] = [];
      trustedApplications: string[];
      constructor() {
        this.addresses = [
          new AddressWithPrefix(new Address("1.0.1.3", 1), 24)
        ];
        this.trustedApplications = ["com.huawei.hmos.browser","com.huawei.browser"];
        this.mtu = 1500;
        this.dnsAddresses = ["10.21.121.188"];
        this.routes = [
          {
            interface: "tun",
            destination: {
              address: {
                address: "10.0.0.0",
                family: 1
              },
              prefixLength: 8
            },
            gateway: {
              address: "10.0.0.0",
              family:1
            },
            hasGateway: true,
            isDefaultRoute: false
          }
        ]
      }
    }

    let config = new Config();

    console.log("setupVpn===" ,JSON.stringify(config));

    this.vpnConnection.create(config).then((result: number) => {
      console.log("create result: " + result);
      this.tunFd = result;
      this.g_tunnelFd = vpn_client.tcpConnect("127.0.0.1", 8081);
      vpn_client.startVpn(this.tunFd,this.g_tunnelFd);
      this.vpnConnection?.protect(this.g_tunnelFd);
    }).catch((error: BusinessError) => {
      console.log("error: " + error.message)
    });
  }

  onDestroy(): void {
    this.vpnConnection?.destroy();
    vpn_client.stopVpn(this.g_tunnelFd);
    this.tunFd = -1;
    this.g_tunnelFd = -1;
  }

}

当创建成功后,收到如下2条数据:

hex====== 6000000000240001fe80000000000000e11fae16e3d1e7e4ff0200000000000000000000000000163a000502000001008f002d660000000104000000ff0200000000000000000001ffd1e7e4
hex====== 6000000000083afffe80000000000000e11fae16e3d1e7e4ff0200000000000000000000000000028500224a00000000

当使用系统方法查看路由信息得倒如下信息:路由没有正确设置

cke_27465.png


更多关于VPN 无法创建 HarmonyOS 鸿蒙Next的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于VPN 无法创建 HarmonyOS 鸿蒙Next的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next系统中,VPN无法创建可能是由于系统权限、网络配置或应用兼容性等问题导致。首先,确保设备的系统版本支持VPN功能,并且已经授予相关应用必要的权限。其次,检查网络设置,确保设备连接到稳定的网络,并且没有防火墙或安全软件阻止VPN连接。此外,某些VPN应用可能与鸿蒙Next系统不完全兼容,建议尝试使用其他VPN应用或更新当前应用至最新版本。如果问题仍然存在,可能是系统层面的限制或Bug,需要等待官方更新修复。

回到顶部