From 456c22e820ea782d4160c63964be0d5643c57b25 Mon Sep 17 00:00:00 2001 From: zontreck Date: Thu, 21 Nov 2024 21:27:09 -0700 Subject: [PATCH] Iniitla Commit --- .gitignore | 32 ++++++++++++++++++++ CHANGELOG.md | 3 ++ README.md | 2 ++ analysis_options.yaml | 30 +++++++++++++++++++ bin/awsparser.dart | 51 ++++++++++++++++++++++++++++++++ build.sh | 6 ++++ lib/awsparser.dart | 64 ++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 19 ++++++++++++ test/awsparser_test.dart | 8 +++++ 9 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 README.md create mode 100644 analysis_options.yaml create mode 100644 bin/awsparser.dart create mode 100644 build.sh create mode 100644 lib/awsparser.dart create mode 100644 pubspec.yaml create mode 100644 test/awsparser_test.dart diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f84766 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +# See https://www.dartlang.org/guides/libraries/private-files + +# Files and directories created by pub +.dart_tool/ +.packages +build/ +# If you're building an application, you may want to check-in your pubspec.lock +pubspec.lock + +# Directory created by dartdoc +# If you don't generate documentation locally you can remove this line. +doc/api/ + +# dotenv environment variables file +.env* + +# Avoid committing generated Javascript files: +*.dart.js +*.info.json # Produced by the --dump-info flag. +*.js # When generated by dart2js. Don't specify *.js if your + # project includes source files written in JavaScript. +*.js_ +*.js.deps +*.js.map + +.flutter-plugins +.flutter-plugins-dependencies + +ipv4_ranges.txt +ipv6_ranges.txt +awsparser +out \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..effe43c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3816eca --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +A sample command-line application with an entrypoint in `bin/`, library code +in `lib/`, and example unit test in `test/`. diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/bin/awsparser.dart b/bin/awsparser.dart new file mode 100644 index 0000000..4b38c6c --- /dev/null +++ b/bin/awsparser.dart @@ -0,0 +1,51 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:awsparser/awsparser.dart'; +import 'package:dio/dio.dart'; + +Future main(List arguments) async { + print(">> WELCOME <<"); + print(">> Downloading AWS IP Range list.."); + // First, download the AWS IP Ranges config + Dio dio = new Dio(); + Response awsResponse = + await dio.get("https://ip-ranges.amazonaws.com/ip-ranges.json"); + + if (awsResponse.statusCode! == 200) { + // Get body as string + AWSJson aws = AWSJson.parse(awsResponse.data as Map); + + print(">> Generating IP blocklist"); + + String blockListv4 = ""; + String blockListv6 = ""; + + for (var entry in aws.prefixes) { + blockListv4 += "${entry.ip_prefix} AWS IPv4\n"; + } + for (var entry in aws.ipv6_prefixes) { + blockListv6 += "${entry.ipv6_prefix} AWS IPv6\n"; + } + + print(">> Stripping trailing newline"); + blockListv4 = blockListv4.trim(); + blockListv6 = blockListv6.trim(); + + print(">> Report complete"); + print(">> Saving to ipv4_ranges.txt"); + + File fi = File("ipv4_ranges.txt"); + await fi.open(mode: FileMode.write); + await fi.writeAsString(blockListv4); + + print(">> Saving to ipv6_ranges.txt"); + fi = File("ipv6_ranges.txt"); + await fi.open(mode: FileMode.write); + await fi.writeAsString(blockListv6); + + print(">> Goodbye!"); + } else { + print("/!\\ FATAL ERROR /!\\ Failed to download the IP Range list"); + } +} diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..996e7ff --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +rm -rf out || true +mkdir -pv out || true + +dart compile exe -o out/awsparser bin/awsparser.dart \ No newline at end of file diff --git a/lib/awsparser.dart b/lib/awsparser.dart new file mode 100644 index 0000000..6b27353 --- /dev/null +++ b/lib/awsparser.dart @@ -0,0 +1,64 @@ +class AWSJson { + String syncToken = ""; + String createDate = ""; + List prefixes = []; + List ipv6_prefixes = []; + + AWSJson._(); + factory AWSJson.parse(Map js) { + AWSJson ajs = AWSJson._(); + + ajs.syncToken = js['syncToken'] as String; + ajs.createDate = js['createDate'] as String; + + List LPv4 = js['prefixes'] as List; + for (var entry in LPv4) { + ajs.prefixes.add(AWSIPv4.parse(entry as Map)); + } + + List LPv6 = js['ipv6_prefixes'] as List; + for (var entry in LPv6) { + ajs.ipv6_prefixes.add(AWSIPv6.parse(entry as Map)); + } + + return ajs; + } +} + +class AWSIPv4 { + String ip_prefix = ""; + String region = ""; + String service = ""; + String network_border_group = ""; + + AWSIPv4._(); + + factory AWSIPv4.parse(Map js) { + AWSIPv4 ip = AWSIPv4._(); + ip.ip_prefix = js['ip_prefix'] as String; + ip.region = js['region'] as String; + ip.service = js['service'] as String; + ip.network_border_group = js['network_border_group'] as String; + + return ip; + } +} + +class AWSIPv6 { + String ipv6_prefix = ""; + String region = ""; + String service = ""; + String network_border_group = ""; + + AWSIPv6._(); + + factory AWSIPv6.parse(Map js) { + AWSIPv6 ip = AWSIPv6._(); + ip.ipv6_prefix = js['ipv6_prefix'] as String; + ip.region = js['region'] as String; + ip.service = js['service'] as String; + ip.network_border_group = js['network_border_group'] as String; + + return ip; + } +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..2482b62 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,19 @@ +name: awsparser +description: A sample command-line application. +version: 1.0.0 +# repository: https://github.com/my_org/my_repo + +environment: + sdk: ^3.5.4 + +# Add regular dependencies here. +dependencies: + libac_dart: + hosted: https://git.zontreck.com/api/packages/AriasCreations/pub/ + version: 1.2.90324+0325 + dio: ^5.7.0 + # path: ^1.8.0 + +dev_dependencies: + lints: ^4.0.0 + test: ^1.24.0 diff --git a/test/awsparser_test.dart b/test/awsparser_test.dart new file mode 100644 index 0000000..e5c24ee --- /dev/null +++ b/test/awsparser_test.dart @@ -0,0 +1,8 @@ +import 'package:awsparser/awsparser.dart'; +import 'package:test/test.dart'; + +void main() { + test('calculate', () { + expect(calculate(), 42); + }); +}