AfRApay/AfRApay.FTM/Program.cs

150 lines
6.1 KiB
C#
Raw Normal View History

2023-02-08 13:05:51 +01:00
using System.CommandLine;
using PCSC;
using PCSC.Monitoring;
using PCSC.Iso7816;
var rootCommand = new RootCommand("Fancy Test Machine for AfRApay");
var listReadersOption = new Option<bool>("--list-readers", "List card readers and exit");
rootCommand.Add(listReadersOption);
2023-02-08 15:03:16 +01:00
var webAddrOption = new Option<Uri>("--web-addr", "Base URL for AfRApay.Web");
webAddrOption.SetDefaultValue(new Uri("http://127.0.0.1:5296"));
rootCommand.Add(webAddrOption);
rootCommand.SetHandler((listReaders, webAddr) => {
2023-02-08 13:05:51 +01:00
using (var context = ContextFactory.Instance.Establish(SCardScope.System)) {
// We need at least one card reader or this won't work!
2023-02-08 13:05:51 +01:00
var readerNames = context.GetReaders();
if (readerNames.Length == 0) {
Console.Error.WriteLine("Error: no card reader detected");
Environment.Exit(1);
}
// If --list-readers is passed, list readers and exit.
if (listReaders) {
Console.Error.WriteLine("----------- Connected Readers ----------");
2023-02-08 13:05:51 +01:00
foreach (var name in readerNames) {
Console.WriteLine(name);
}
Console.Error.WriteLine("----------------------------------------");
2023-02-08 13:05:51 +01:00
Environment.Exit(0);
}
2023-02-08 15:03:16 +01:00
using HttpClient httpClient = new();
httpClient.BaseAddress = webAddr;
Console.Error.WriteLine("----------------------------------------");
Console.Error.WriteLine("--- AfRApay FTM - Fancy Test Machine ---");
Console.Error.WriteLine("----------------------------------------");
Console.Error.WriteLine();
2023-02-08 15:03:16 +01:00
Console.Error.WriteLine("AfRApay.Web: {0}", httpClient.BaseAddress);
Console.Error.WriteLine();
Console.Error.WriteLine("Hotkeys (case insensitive):");
2023-02-08 15:35:21 +01:00
Console.Error.WriteLine(" [L] Link card (initiate from web UI)");
Console.Error.WriteLine(" [B] Balance query");
Console.Error.WriteLine(" [Esc] Cancel, return to default state");
Console.Error.WriteLine();
Console.Error.WriteLine("----------------------------------------");
2023-02-08 15:03:16 +01:00
2023-02-08 13:05:51 +01:00
// Listen for events on all connected readers.
using (var monitor = MonitorFactory.Instance.Create(SCardScope.System)) {
var state = TerminalState.Default;
2023-02-08 13:05:51 +01:00
monitor.Initialized += (_, args) => Console.WriteLine("[ Reader Initialized: {0} ]", args.ReaderName);
monitor.MonitorException += (_, args) => {
Console.Error.WriteLine("! ERROR: {0}", args);
Environment.Exit(1);
};
monitor.StatusChanged += (_, args) => Console.WriteLine("~ {0} -> {1}", args.LastState, args.NewState);
monitor.CardInserted += (_, args) => {
Console.WriteLine("> TAP: {0}", Convert.ToHexString(args.Atr));
var reader = new IsoReader(context, args.ReaderName, SCardShareMode.Shared, SCardProtocol.Any);
2023-02-08 15:03:16 +01:00
HandleTap(reader, httpClient, state);
2023-02-08 13:05:51 +01:00
};
monitor.CardRemoved += (_, args) => {
Console.WriteLine("< OFF");
Console.WriteLine(); // Write a blank line between card taps for readability.
};
Console.WriteLine("[ Starting... ]");
monitor.Start(readerNames);
while (true) {
var key = Console.ReadKey();
switch (key.Key) {
case ConsoleKey.L:
state = TerminalState.Link;
Console.Error.WriteLine("=> Mode: Link");
break;
2023-02-08 15:35:21 +01:00
case ConsoleKey.B:
state = TerminalState.Balance;
Console.Error.WriteLine("=> Mode: Balance Query");
break;
case ConsoleKey.Escape:
state = TerminalState.Default;
2023-02-08 15:03:16 +01:00
Console.Error.WriteLine("\b => Mode: Default"); // Hack: the \b eats the escape character.
break;
default:
Console.Error.WriteLine(" => UNRECOGNISED KEY");
break;
};
2023-02-08 13:05:51 +01:00
}
}
}
2023-02-08 15:03:16 +01:00
}, listReadersOption, webAddrOption);
2023-02-08 13:05:51 +01:00
return await rootCommand.InvokeAsync(args);
// Queries a card for data when one is tapped.
2023-02-08 15:03:16 +01:00
static async void HandleTap(IsoReader reader, HttpClient httpClient, TerminalState state) {
2023-02-08 13:05:51 +01:00
// Send a PCSC pseudo-APDU to query the ISO 14443 UID.
2023-02-08 15:03:16 +01:00
var uidRsp = reader.Transmit(new CommandApdu(IsoCase.Case2Short, SCardProtocol.Any) {
2023-02-08 13:05:51 +01:00
CLA = 0xFF,
Instruction = InstructionCode.GetData,
P1 = 0x00,
P2 = 0x00,
});
2023-02-08 15:03:16 +01:00
if (!IsSucc(uidRsp)) {
Console.Error.WriteLine("--> Card Error: SW1={0} SW2={1}", (SW1Code)uidRsp.SW1, uidRsp.SW2);
2023-02-08 13:05:51 +01:00
return;
}
2023-02-08 15:03:16 +01:00
var uid = uidRsp.GetData();
2023-02-08 13:05:51 +01:00
Console.WriteLine(" UID: {0}", Convert.ToHexString(uid));
2023-02-08 15:03:16 +01:00
// Query the backend, which endpoint depending on terminal state.
switch (state) {
case TerminalState.Default:
await CallGet(httpClient, String.Format("/api/card/transaction?card={0}&amount=1.50", Convert.ToHexString(uid)));
2023-02-08 15:03:16 +01:00
break;
case TerminalState.Link:
await CallGet(httpClient, String.Format("/api/card/link?card={0}", Convert.ToHexString(uid)));
break;
2023-02-08 15:35:21 +01:00
case TerminalState.Balance:
await CallGet(httpClient, String.Format("/api/card/balance?card={0}", Convert.ToHexString(uid)));
break;
2023-02-08 15:03:16 +01:00
default:
Console.Error.WriteLine("UNKNOWN TERMINAL STATE: {0}", state);
break;
}
2023-02-08 13:05:51 +01:00
}
// Was the command successful?
static bool IsSucc(Response rsp) {
return rsp.SW1 == (byte)SW1Code.Normal && rsp.SW2 == 0x00;
}
2023-02-08 15:03:16 +01:00
static async Task<string> CallGet(HttpClient client, string path) {
Console.WriteLine(" -> GET {0}", path);
var rsp = await client.GetStringAsync(path);
Console.WriteLine(" <- {0}", rsp);
return rsp;
}
// Terminal State.
enum TerminalState {
Default,
Link,
2023-02-08 15:35:21 +01:00
Balance,
};