Skip to content

Commit

Permalink
fix: disable ssl verification for www.stcp.pt
Browse files Browse the repository at this point in the history
  • Loading branch information
limwa committed Oct 2, 2023
1 parent 1c00610 commit 1047345
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 59 deletions.
35 changes: 0 additions & 35 deletions uni/assets/certificates/www.stcp.pt.crt

This file was deleted.

32 changes: 9 additions & 23 deletions uni/lib/controller/fetchers/departures_fetcher.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;

import 'package:html/dom.dart';
import 'package:html/parser.dart';
Expand All @@ -15,27 +14,17 @@ class DeparturesFetcher {

final String _stopCode;
final BusStopData _stopData;
static final _client = Future.microtask(() async {
// FIXME(limwa): replace `rootBundle` with `DefaultAssetBundle.of(context)`
final certificateBytes =
await rootBundle.load('assets/certificates/www.stcp.pt.crt');

final securityContext = SecurityContext(withTrustedRoots: true)
..setTrustedCertificatesBytes(certificateBytes.buffer.asUint8List());

return http.IOClient(
HttpClient(
context: securityContext,
),
);
});
static final _client = http.IOClient(
HttpClient(context: SecurityContext())
..badCertificateCallback =
(cert, host, port) => host == 'www.stcp.pt' && port == 443,
);

Future<String> _getCSRFToken() async {
final url =
'https://www.stcp.pt/en/travel/timetables/?paragem=$_stopCode&t=smsbus';

final client = await _client;
final response = await client.get(url.toUri());
final response = await _client.get(url.toUri());
final htmlResponse = parse(response.body);

final scriptText = htmlResponse
Expand Down Expand Up @@ -67,8 +56,7 @@ class DeparturesFetcher {
final url =
'https://www.stcp.pt/pt/itinerarium/soapclient.php?codigo=$_stopCode&hash123=$csrfToken';

final client = await _client;
final response = await client.get(url.toUri());
final response = await _client.get(url.toUri());
final htmlResponse = parse(response.body);

final tableEntries =
Expand Down Expand Up @@ -130,8 +118,7 @@ class DeparturesFetcher {
final url =
'https://www.stcp.pt/pt/itinerarium/callservice.php?action=srchstoplines&stopname=$stopCode';

final client = await _client;
final response = await client.post(url.toUri());
final response = await _client.post(url.toUri());
final json = jsonDecode(response.body) as List<dynamic>;
for (final busKey in json) {
final bus = busKey as Map<String, dynamic>;
Expand All @@ -155,8 +142,7 @@ class DeparturesFetcher {
final url =
'https://www.stcp.pt/pt/itinerarium/callservice.php?action=srchstoplines&stopcode=$stop';

final client = await _client;
final response = await client.post(url.toUri());
final response = await _client.post(url.toUri());

final json = jsonDecode(response.body) as List<dynamic>;

Expand Down
1 change: 0 additions & 1 deletion uni/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ flutter:
- assets/text/
- assets/text/locations/
- assets/meal-icons/
- assets/certificates/
fonts:
- family: Raleway
fonts:
Expand Down

0 comments on commit 1047345

Please sign in to comment.