如何在dart中运行自定义https域?

时间:2018-06-07 13:22:11

标签: dart

当我尝试使用SSL运行dart服务器时,我遇到了一些麻烦。 Dart VM版本 - 1.24.3。 Apache或其他服务禁用。我的意思是44380端口是免费的。来自letsencrypt的SSL证书

main.dart档案中:

import 'dart:io';
import "dart:isolate";

main() {
  SecurityContext context = new SecurityContext();
  var fullchain = Platform.script.resolve('/home/user/.ssl/turarabu/www/fullchain.pem').toFilePath();
  var privkey   = Platform.script.resolve('/home/user/.ssl/turarabu/www/privkey.pem').toFilePath();

  context.useCertificateChain(fullchain);
  context.usePrivateKey(privkey);

  HttpServer
      .bindSecure('www.turarabu.com', 443, context)
      .then((server) {
        server.listen((HttpRequest request) {
          request.response.write('Hello, world!');
          request.response.close();
        });
      });
}

/etc/hosts档案

35.198.81.101 www.turarabu.com

但是当我尝试跑步时:

sudo dart main.dart

出现此错误:

Unhandled exception:
SocketException: Failed to create server socket (OS Error: Cannot assign requested address, errno = 99), address = www.turarabu.com, port = 443
#0      _NativeSocket.bind.<anonymous closure> (dart:io-patch/socket_patch.dart:511)
#1      _RootZone.runUnary (dart:async/zone.dart:1371)
#2      _FutureListener.handleValue (dart:async/future_impl.dart:129)
#3      _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:636)
#4      _Future._propagateToListeners (dart:async/future_impl.dart:665)
#5      _Future._completeWithValue (dart:async/future_impl.dart:478)
#6      _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:510)
#7      _microtaskLoop (dart:async/schedule_microtask.dart:41)
#8      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
#9      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:99)
#10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:152)

ifconfig输出

ens4      Link encap:Ethernet  HWaddr 42:01:0a:9c:00:03
          inet addr:10.156.0.3  Bcast:10.156.0.3  Mask:255.255.255.255
          inet6 addr: fe80::4001:aff:fe9c:3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1460  Metric:1
          RX packets:708175 errors:0 dropped:0 overruns:0 frame:6
          TX packets:629464 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:247043081 (247.0 MB)  TX bytes:76546648 (76.5 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:15 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:5065 (5.0 KB)  TX bytes:5065 (5.0 KB)

我做错了什么?

P.S。 main.dart和SSL文件chmod permiossions是正确的

1 个答案:

答案 0 :(得分:1)

ifconfig的输出是什么?特别是,您是否可以确认其中一个NIC的地址为35.198.81.101

尝试将其更改为:.bindSecure(InternetAddress.ANY_IP_V4, 443, context)

如果该地址确实存在,请尝试.bindSecure('35.198.81.101', 443, context)

通常,最常见的用例是绑定到所有地址(使用ANY...),但通常只有一个。指定ANY意味着您不会将任何地址硬编码到源代码中。

另一种常见情况是仅对环回出价,以便只有同一台计算机上的客户端才能访问服务器,从而阻止对网络的任何访问。