windows 带有flutter窗口的HttpServer

c9qzyr3d  于 2023-04-22  发布在  Windows
关注(0)|答案(1)|浏览(155)

我有一个flutter windows HttpServer这样的

HttpServer server = await HttpServer.bind(InternetAddress.anyIPv4, 2345);
  print('Listening on ${server.port}');
  server.listen((req) {
    req.response
      ..write('request received successfully')
      ..close();
  });

当尝试在Windows Flutter应用程序上运行它时,它只适用于Windows机器本身的本地连接。
但是当试图从android设备访问它时,例如从同一个本地网络,它不起作用。
我做了一些搜索,发现我必须为我的应用程序添加一个防火墙入站规则,以便能够侦听外部连接
我从Windows Defender防火墙手动执行了此操作,并且成功了。
我需要从我的Flutter代码编程。
然后尝试了这个代码

final processResult = Process.runSync(
  'netsh',
  [
    'advfirewall',
    'firewall',
    'add',
    'rule',
    'name="My App Rule Name"',
    'action=allow',
    'program="${Platform.resolvedExecutable}"',
    'dir=in',
    'protocol=tcp',
  ],
);

但我得到了这个结果

A specified value is not valid.

  Usage: add rule name=<string>
  dir=in|out
  action=allow|block|bypass
  [program=<program path>]
  [service=<service short name>|any]
  [description=<string>]
  [enable=yes|no (default=yes)]
  [profile=public|private|domain|any[,...]]
  [localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  [remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|
     <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
  [localport=0-65535|<port range>[,...]|RPC|RPC-EPMap|IPHTTPS|any (default=any)]
  [remoteport=0-65535|<port range>[,...]|any (default=any)]
  [protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|
     tcp|udp|any (default=any)]
  [interfacetype=wireless|lan|ras|any]
  [rmtcomputergrp=<SDDL string>]
  [rmtusrgrp=<SDDL string>]
  [edge=yes|deferapp|deferuser|no (default=no)]
  [security=authenticate|authenc|authdynenc|authnoencap|notrequired 
     (default=notrequired)]

  Remarks:

  - Add a new inbound or outbound rule to the firewall policy.
  - Rule name should be unique and cannot be "all".
  - If a remote computer or user group is specified, security must be
    authenticate, authenc, authdynenc, or authnoencap.
  - Setting security to authdynenc allows systems to dynamically
    negotiate the use of encryption for traffic that matches
    a given Windows Defender Firewall rule. Encryption is negotiated based on
    existing connection security rule properties. This option
    enables the ability of a machine to accept the first TCP
    or UDP packet of an inbound IPsec connection as long as
    it is secured, but not encrypted, using IPsec.
    Once the first packet is processed, the server will
    re-negotiate the connection and upgrade it so that
    all subsequent communications are fully encrypted.
  - If action=bypass, the remote computer group must be specified when dir=in.
  - If service=any, the rule applies only to services.
  - ICMP type or code can be "any".
  - Edge can only be specified for inbound rules.
  - AuthEnc and authnoencap cannot be used together.
  - Authdynenc is valid only when dir=in.
  - When authnoencap is set, the security=authenticate option becomes an
    optional parameter.

  Examples:

  Add an inbound rule with no encapsulation security for browser.exe:
  netsh advfirewall firewall add rule name="allow browser"
  dir=in program="c:\programfiles\browser\browser.exe"
  security=authnoencap action=allow

  Add an outbound rule for port 80:
  netsh advfirewall firewall add rule name="allow80"
  protocol=TCP dir=out localport=80 action=block

  Add an inbound rule requiring security and encryption
  for TCP port 80 traffic:
  netsh advfirewall firewall add rule
  name="Require Encryption for Inbound TCP/80"
  protocol=TCP dir=in localport=80 security=authdynenc
  action=allow

  Add an inbound rule for browser.exe and require security
  netsh advfirewall firewall add rule name="allow browser"
  dir=in program="c:\program files\browser\browser.exe"
  security=authenticate action=allow

  Add an authenticated firewall bypass rule for group
  acmedomain\scanners identified by a SDDL string:
  netsh advfirewall firewall add rule name="allow scanners"
  dir=in rmtcomputergrp=<SDDL string> action=bypass
  security=authenticate

  Add an outbound allow rule for local ports 5000-5010 for udp-
  Add rule name="Allow port range" dir=out protocol=udp localport=5000-5010 action=allow

而且问题没有解决!
也许我需要请求防火墙权限才能打开端口,但我不知道如何操作

2cmtqfgy

2cmtqfgy1#

我添加了一个.bat脚本,如下所示

@echo off

set programPath=%1

set ruleName="Put Your Rule Name Here"

echo %date% %time%: Starting script with arguments %* >>C:\log.log

netsh advfirewall firewall show rule name=%ruleName% >nul
if %errorlevel%==0 (
  echo Rule "%ruleName%" already exists. Exiting...
  echo %date% %time%: Rule "%ruleName%" already exists. Exiting... >>C:\log.log
  exit /b
)

echo Rule "%ruleName%" not found. Adding...
echo %date% %time%: Rule "%ruleName%" not found. Adding... >>C:\log.log

netsh advfirewall firewall add rule name=%ruleName% action=allow enable=yes dir=in profile=domain,private,public localip=any remoteip=any protocol=any program=%programPath%
netsh advfirewall firewall add rule name=%ruleName% action=allow enable=yes dir=out profile=domain,private,public localip=any remoteip=any protocol=any program=%programPath%

echo Rule "%ruleName%" added successfully.
echo %date% %time%: Rule "%ruleName%" added successfully. >>C:\log.log

exit /b

我保存了firewall_rule.bat并使用Windows高级安装程序将其与我的软件包打包在一起
然后使脚本与应用程序安装一起运行,以在应用程序安装时使用管理员权限激活规则。

相关问题