This commit is contained in:
Sina Blattmann 2023-03-24 13:07:17 +01:00
commit 2a664ac213
5 changed files with 79 additions and 12 deletions

View File

@ -0,0 +1,17 @@
using System.Diagnostics.CodeAnalysis;
#pragma warning disable CS8618
namespace InnovEnergy.App.OpenVpnCertificatesServer;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[SuppressMessage("ReSharper", "InconsistentNaming")]
internal class AccToken
{
public String token { get; init; }
public String bearer { get; init; }
public UInt64 idUser { get; init; }
public String verification_mode { get; init; }
public String idAccessToken { get; init; }
public Boolean verification_sent { get; init; }
public Boolean success { get; init; }
}

View File

@ -13,4 +13,10 @@
<ProjectReference Include="../../Lib/Utils/Utils.csproj" /> <ProjectReference Include="../../Lib/Utils/Utils.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="token.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -1,5 +1,7 @@
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text; using System.Text;
using System.Text.Json;
using Flurl; using Flurl;
using ICSharpCode.SharpZipLib.Tar; using ICSharpCode.SharpZipLib.Tar;
using InnovEnergy.App.OpenVpnCertificatesServer.PKI; using InnovEnergy.App.OpenVpnCertificatesServer.PKI;
@ -10,7 +12,10 @@ using static InnovEnergy.App.OpenVpnCertificatesServer.PKI.CertificateAuthority;
namespace InnovEnergy.App.OpenVpnCertificatesServer; namespace InnovEnergy.App.OpenVpnCertificatesServer;
// export SolutionDir=$(pwd)
// dotnet publish OpenVpnCertificatesServer.csproj -c Release -r linux-x64 -p:PublishSingleFile=true --self-contained true && scp bin/Release/net6.0/linux-x64/publish/OpenVpnCertificatesServer ig@salidomo.innovenergy.ch:~/get_cert/get_cert // dotnet publish OpenVpnCertificatesServer.csproj -c Release -r linux-x64 -p:PublishSingleFile=true --self-contained true && scp bin/Release/net6.0/linux-x64/publish/OpenVpnCertificatesServer ig@salidomo.innovenergy.ch:~/get_cert/get_cert
// scp bin/Release/net6.0/linux-x64/publish/token.json ig@salidomo.innovenergy.ch:~/get_cert/token.json
// http://localhost:4000/get_cert?machine_serial=HQ2032UAWYM // http://localhost:4000/get_cert?machine_serial=HQ2032UAWYM
// http://localhost:4000/get_cert?unique_id=985dadd0cf29 // http://localhost:4000/get_cert?unique_id=985dadd0cf29
@ -18,6 +23,7 @@ namespace InnovEnergy.App.OpenVpnCertificatesServer;
// quotes!! // quotes!!
// wget 'http://localhost:4000/get_cert?name=MYNAME&pw=MwBRbQb3QaX7l9XIaakq' // wget 'http://localhost:4000/get_cert?name=MYNAME&pw=MwBRbQb3QaX7l9XIaakq'
[SuppressMessage("Trimming", "IL2026:Members annotated with \'RequiresUnreferencedCodeAttribute\' require dynamic access otherwise can break functionality when trimming application code")]
public static class Program public static class Program
{ {
// TODO: use fody weaver to obfuscate strings? // TODO: use fody weaver to obfuscate strings?
@ -29,8 +35,14 @@ public static class Program
private const String CcdDir = VpnDir + "/ccd"; private const String CcdDir = VpnDir + "/ccd";
private const String CertsDir = VpnDir + "/certs"; private const String CertsDir = VpnDir + "/certs";
private const String Endpoint = "http://localhost:4000/"; private const String Endpoint = "http://localhost:4000/";
private const String VrmUser = "victron@innov.energy";
private const String VrmPwd = "NnoVctr201002"; private static AccToken ReadAccessToken()
{
var content = File.ReadAllText("./token.json");
return JsonSerializer.Deserialize<AccToken>(content)!;
}
private const String ManualPw = "MwBRbQb3QaX7l9XIaakq"; private const String ManualPw = "MwBRbQb3QaX7l9XIaakq";
private const String QueryPath = "/get_cert"; private const String QueryPath = "/get_cert";
@ -133,8 +145,8 @@ public static class Program
Console.WriteLine($"looking up unique id {uniqueId} on VRM"); Console.WriteLine($"looking up unique id {uniqueId} on VRM");
//var installationName = await LookupInstallationNameByUniqueId(ccdName); //var installationName = await LookupInstallationNameByUniqueId(ccdName);
var token = ReadAccessToken();
using var vrm = await VrmAccount.Login(VrmUser, VrmPwd); // TODO: use token using var vrm = VrmAccount.Token(token.idUser, token.token);
var installations = await vrm.GetInstallations(); var installations = await vrm.GetInstallations();
var installationName = installations var installationName = installations
@ -150,8 +162,8 @@ public static class Program
private static async Task<(String ccdName, String humanReadableName)?> LookupInstallationNameByMachineSerial(String ccdName) private static async Task<(String ccdName, String humanReadableName)?> LookupInstallationNameByMachineSerial(String ccdName)
{ {
Console.WriteLine($"looking up {ccdName} on VRM"); Console.WriteLine($"looking up {ccdName} on VRM");
var token = ReadAccessToken();
using var vrm = await VrmAccount.Login(VrmUser, VrmPwd); // TODO: use token using var vrm = VrmAccount.Token(token.idUser, token.token);
var installations = await vrm.GetInstallations(); var installations = await vrm.GetInstallations();
foreach (var installation in installations) foreach (var installation in installations)

31
csharp/App/OpenVpnCertificatesServer/newToken.sh Normal file → Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
## USAGE: ./newToken.sh -u Username -p Password -n UniqueTokenName
while getopts u:p:n: flag
do
case "${flag}" in
u) username=${OPTARG};;
p) password=${OPTARG};;
n) name=${OPTARG};;
esac
done
response=$(curl --request POST \
--url https://vrmapi.victronenergy.com/v2/auth/login \
--header 'Content-Type: application/json' \
--data '{"username":"'$username'","password":"'$password'","sms_token":null,"remember_me":false,"language":""}')
echo -n '{"bearer' > token.json
token=$(echo $response | jq ".token")
uid=$(echo $response | jq ".idUser")
echo -n ${response:7:-1} >> token.json
response2=$(curl --request POST \
--url https://vrmapi.victronenergy.com/v2/users/$uid/accesstokens/create \
--header 'Content-Type: application/json' \
--header 'x-authorization: Bearer '${token:1:-1} \
--data '{"name":"'$name'"}')
echo -n , >> token.json
echo -n ${response2:1} >> token.json

View File

@ -0,0 +1 @@
{"bearer":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.eyJ1aWQiOiI1NTQ1MCIsInRva2VuX3R5cGUiOiJkZWZhdWx0IiwiaXNzIjoidnJtYXBpLnZpY3Ryb25lbmVyZ3kuY29tIiwiYXVkIjoiaHR0cHM6Ly92cm1hcGkudmljdHJvbmVuZXJneS5jb20vIiwiaWF0IjoxNjc5NTg2ODU0LCJleHAiOjE2Nzk2NzMyNTQsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.AsLJU7qDPBHO-_FjVo9a8RbyoxhYrDrwZX7V3z4Xq8EoUulv2VyTqy9OXLpez4JI2FVAfSO5a7Amj4XvK1AWtDr9MxP07IBfPyMu5LLGTzjPMAJ6fvZbvZ-eRsP1-aARCFekMGoeSvEEBDrZB9_0kps4h9idQwwGwAby2Tya0vNGu6QEw9WmHcbL8qjvJzxavg8bK6Lttv2-3l_11iZoqiYLdXbwBi32GYK_sdwp-fbGvPE1d6g6eVll94JfSqNLZl9baijtOksx_Qouu7YB8knCgFNrx535d4iJtCkMv9xWztWXbevpSQiy9S8pCgLSpmHNztlVDjacEYyduwUzyw","idUser":55450,"verification_mode":"password","verification_sent":false,"success":true,"token":"108a1407da84afea182b8102a202a2c6b73e9bd6ad919f521033b09837e6e564","idAccessToken":"385119"}