Added code to get a token and register our public key with PIA

master
Ishan Jain 4 months ago
commit 0e7857626c
  1. 3
      .gitignore
  2. 1163
      Cargo.lock
  3. 13
      Cargo.toml
  4. 44
      ca.rsa.4096.crt
  5. 104
      src/main.rs

3
.gitignore vendored

@ -0,0 +1,3 @@
/target
env

1163
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,13 @@
[package]
name = "pia-mikrotik"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version= "0.11.11", features=["json", "rustls-tls"]}
serde = { version = "1.0.143", features = ["derive"]}
thiserror = "1.0.32"
tokio = { version = "1.20.1", features = ["full"] }
urlencoding = "2.1.0"

@ -0,0 +1,44 @@
-----BEGIN CERTIFICATE-----
MIIHqzCCBZOgAwIBAgIJAJ0u+vODZJntMA0GCSqGSIb3DQEBDQUAMIHoMQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExEzARBgNVBAcTCkxvc0FuZ2VsZXMxIDAeBgNV
BAoTF1ByaXZhdGUgSW50ZXJuZXQgQWNjZXNzMSAwHgYDVQQLExdQcml2YXRlIElu
dGVybmV0IEFjY2VzczEgMB4GA1UEAxMXUHJpdmF0ZSBJbnRlcm5ldCBBY2Nlc3Mx
IDAeBgNVBCkTF1ByaXZhdGUgSW50ZXJuZXQgQWNjZXNzMS8wLQYJKoZIhvcNAQkB
FiBzZWN1cmVAcHJpdmF0ZWludGVybmV0YWNjZXNzLmNvbTAeFw0xNDA0MTcxNzQw
MzNaFw0zNDA0MTIxNzQwMzNaMIHoMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0Ex
EzARBgNVBAcTCkxvc0FuZ2VsZXMxIDAeBgNVBAoTF1ByaXZhdGUgSW50ZXJuZXQg
QWNjZXNzMSAwHgYDVQQLExdQcml2YXRlIEludGVybmV0IEFjY2VzczEgMB4GA1UE
AxMXUHJpdmF0ZSBJbnRlcm5ldCBBY2Nlc3MxI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0ZSBJbnRl
cm5ldCBBY2Nlc3MxIDAeBgNVBAsTF1ByaXZhdGUgSW50ZXJuZXQgQWNjZXNzMSAw
HgYDVQQDExdQcml2YXRlIEludGVybmV0IEFjY2VzczEgMB4GA1UEKRMXUHJpdmF0
ZSBJbnRlcm5ldCBBY2Nlc3MxLzAtBgkqhkiG9w0BCQEWIHNlY3VyZUBwcml2YXRl
aW50ZXJuZXRhY2Nlc3MuY29tggkAnS7684Nkme0wDAYDVR0TBAUwAwEB/zANBgkq
hkiG9w0BAQ0FAAOCAgEAJsfhsPk3r8kLXLxY+v+vHzbr4ufNtqnL9/1Uuf8NrsCt
pXAoyZ0YqfbkWx3NHTZ7OE9ZRhdMP/RqHQE1p4N4Sa1nZKhTKasV6KhHDqSCt/dv
Em89xWm2MVA7nyzQxVlHa9AkcBaemcXEiyT19XdpiXOP4Vhs+J1R5m8zQOxZlV1G
tF9vsXmJqWZpOVPmZ8f35BCsYPvv4yMewnrtAC8PFEK/bOPeYcKN50bol22QYaZu
LfpkHfNiFTnfMh8sl/ablPyNY7DUNiP5DRcMdIwmfGQxR5WEQoHL3yPJ42LkB5zs
6jIm26DGNXfwura/mi105+ENH1CaROtRYwkiHb08U6qLXXJz80mWJkT90nr8Asj3
5xN2cUppg74nG3YVav/38P48T56hG1NHbYF5uOCske19F6wi9maUoto/3vEr0rnX
JUp2KODmKdvBI7co245lHBABWikk8VfejQSlCtDBXn644ZMtAdoxKNfR2WTFVEwJ
iyd1Fzx0yujuiXDROLhISLQDRjVVAvawrAtLZWYK31bY7KlezPlQnl/D9Asxe85l
8jO5+0LdJ6VyOs/Hd4w52alDW/MFySDZSfQHMTIc30hLBJ8OnCEIvluVQQ2UQvoW
+no177N9L2Y+M9TcTA62ZyMXShHQGeh20rb4kK8f+iFX8NxtdHVSkxMEFSfDDyQ=
-----END CERTIFICATE-----

@ -0,0 +1,104 @@
use reqwest::{Certificate, Client, ClientBuilder};
use serde::{Deserialize, Serialize};
use std::io;
use thiserror::Error;
use tokio::{fs::File, io::AsyncReadExt};
#[derive(Debug, Error)]
pub enum PError {
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
#[error(transparent)]
IoError(#[from] io::Error),
}
#[tokio::main]
async fn main() -> Result<(), PError> {
let token = get_token().await?;
println!("{:?}", token);
// TODO(ishan): In future, give an option to specify exit node.
// for now, we assume de-frankfurt, would prefer SG but some issues in the path to SG right now
let response = register_wireguard_pub_key(&token.token).await?;
println!("{:?}", response);
Ok(())
}
async fn read_certificate() -> Result<Certificate, PError> {
let mut buf = vec![];
let mut file = File::open("ca.rsa.4096.crt").await?;
file.read_to_end(&mut buf).await?;
Ok(Certificate::from_pem(&buf)?)
}
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RegisterWireguardPubKeyResponse {
pub status: String,
#[serde(rename = "server_key")]
pub server_key: String,
#[serde(rename = "server_port")]
pub server_port: i64,
#[serde(rename = "server_ip")]
pub server_ip: String,
#[serde(rename = "server_vip")]
pub server_vip: String,
#[serde(rename = "peer_ip")]
pub peer_ip: String,
#[serde(rename = "peer_pubkey")]
pub peer_pubkey: String,
#[serde(rename = "dns_servers")]
pub dns_servers: Vec<String>,
}
async fn register_wireguard_pub_key(
token: &str,
) -> Result<RegisterWireguardPubKeyResponse, PError> {
let pubkey = std::env::var("PUBKEY").unwrap();
let certificate = read_certificate().await?;
let client = ClientBuilder::new()
.danger_accept_invalid_certs(true)
.add_root_certificate(certificate)
.build()
.unwrap();
let response = client
.get(format!(
"https://de-frankfurt.privacy.network:1337/addKey?pt={}&pubkey={}",
urlencoding::encode(token),
urlencoding::encode(&pubkey)
))
.header("Content-Type", "application/x-www-form-urlencoded")
.send()
.await?;
Ok(response.json().await?)
}
#[derive(Debug, Deserialize)]
pub struct GetTokenOutput {
#[allow(unused)]
status: String,
token: String,
}
async fn get_token() -> Result<GetTokenOutput, PError> {
let username = std::env::var("PIA_USER").unwrap();
let password = Some(std::env::var("PIA_PASS").unwrap());
let client = Client::new();
let response = client
.get("https://www.privateinternetaccess.com/gtoken/generateToken")
.basic_auth(username, password)
.send();
Ok(response.await?.json::<GetTokenOutput>().await?)
}
Loading…
Cancel
Save