Compare commits
3 Commits
v3.0.0
...
python-bin
| Author | SHA1 | Date | |
|---|---|---|---|
| 9075ab5967 | |||
| 125d113ade | |||
| e7e0641f31 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -43,6 +43,9 @@ p2prc.PublicKeyBareMetal
|
||||
# Ignore pem files
|
||||
*.pem
|
||||
|
||||
# ignore virtual env file
|
||||
venv
|
||||
|
||||
# Nix and Nix flake files
|
||||
result
|
||||
result-*
|
||||
|
||||
2
Bindings/Haskell/.mhsi
Normal file
2
Bindings/Haskell/.mhsi
Normal file
@@ -0,0 +1,2 @@
|
||||
exit
|
||||
Exit
|
||||
4
Bindings/Haskell/dist-mcabal/autogen/Paths_p2prc.hs
Normal file
4
Bindings/Haskell/dist-mcabal/autogen/Paths_p2prc.hs
Normal file
@@ -0,0 +1,4 @@
|
||||
module Paths_p2prc where
|
||||
import Data.Version
|
||||
version :: Version; version = makeVersion [0,1,0,0]
|
||||
getDataDir :: IO FilePath; getDataDir = return "/Users/akilan/.mcabal/mhs-0.12.3.0/packages/p2prc-0.1.0.0/data"
|
||||
1
Bindings/Haskell/out.comb
Normal file
1
Bindings/Haskell/out.comb
Normal file
@@ -0,0 +1 @@
|
||||
z
|
||||
BIN
Bindings/python/.DS_Store
vendored
BIN
Bindings/python/.DS_Store
vendored
Binary file not shown.
102
Bindings/python/library.py
Normal file
102
Bindings/python/library.py
Normal file
@@ -0,0 +1,102 @@
|
||||
import ctypes
|
||||
from ctypes import Structure, c_char_p, c_int, cdll
|
||||
from dataclasses import dataclass, astuple
|
||||
import time
|
||||
import json
|
||||
from urllib.parse import urlparse
|
||||
from typing import List
|
||||
import subprocess
|
||||
import uuid
|
||||
import dacite
|
||||
import os
|
||||
import sqlalchemy
|
||||
import dataclasses
|
||||
from typing import Union
|
||||
import schedule
|
||||
import threading
|
||||
import requests
|
||||
|
||||
p2prc = ctypes.CDLL("SharedObjects/p2prc.so")
|
||||
|
||||
p2prc.Init("")
|
||||
|
||||
# Start running schedule
|
||||
schedule.run_pending()
|
||||
|
||||
# # Create Sqlite database to track processes
|
||||
# engine=sqlalchemy.create_engine(f'sqlite:///homeserver.db')
|
||||
|
||||
# Global variable
|
||||
# A global variable will be populated on runtime.
|
||||
# It is read from P2PRC directly or from a local
|
||||
# database.
|
||||
|
||||
# Node information
|
||||
# Generated using: https://jsonformatter.org/json-to-python
|
||||
@dataclass
|
||||
class Node:
|
||||
Name: str
|
||||
MachineUsername: str
|
||||
IPV4: str
|
||||
IPV6: str
|
||||
Latency: int
|
||||
Download: int
|
||||
Upload: int
|
||||
ServerPort: str
|
||||
BareMetalSSHPort: str
|
||||
NAT: str
|
||||
EscapeImplementation: str
|
||||
ProxyServer: str
|
||||
UnSafeMode: bool
|
||||
PublicKey: str
|
||||
CustomInformation: str
|
||||
|
||||
@dataclass
|
||||
class IPAddress:
|
||||
ip_address: List[Node]
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# ----------------------------- Helper functions -----------------------------
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
def StartServer():
|
||||
# Starting P2PRC as a server mode
|
||||
p2prc.Server()
|
||||
|
||||
# Class to create string to pass as string function
|
||||
# parameter to shared object file
|
||||
class go_string(Structure):
|
||||
_fields_ = [
|
||||
("p", c_char_p),
|
||||
("n", c_int)]
|
||||
|
||||
# Ensuring local port can escape NAT and responds
|
||||
# with the public ip and port no.
|
||||
# If the domain name is specified then the public IP
|
||||
# can be used as an A name entry.
|
||||
def P2PRCMapPort(port="",domainname="",serveraddress=""):
|
||||
port = go_string(c_char_p(port.encode('utf-8')), len(port))
|
||||
domainname = go_string(c_char_p(domainname.encode('utf-8')), len(domainname))
|
||||
serveraddress = go_string(c_char_p(serveraddress.encode('utf-8')), len(serveraddress))
|
||||
|
||||
# Defining the response type of the GoLang function
|
||||
# function
|
||||
p2prc.MapPort.restype = c_char_p
|
||||
# Calling the Go function
|
||||
address = p2prc.MapPort(port,domainname,serveraddress)
|
||||
res = str(address).strip("b'")
|
||||
return res
|
||||
|
||||
# Lists all avaliable nodes in the network
|
||||
def ListNodes():
|
||||
# View IP Table information
|
||||
p2prc.ViewIPTable.restype = c_char_p
|
||||
ipTable = p2prc.ViewIPTable()
|
||||
# View IP Table as
|
||||
ipTableObject = json.loads((str(ipTable).strip("b'")))
|
||||
dat: IPAddress = dacite.from_dict(IPAddress,ipTableObject)
|
||||
return dat
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import ctypes
|
||||
|
||||
p2prc = ctypes.CDLL("SharedOBjects/p2prc.so")
|
||||
|
||||
p2prc.Init("")
|
||||
|
||||
def StartServer():
|
||||
# Starting P2PRC as a server mode
|
||||
p2prc.Server()
|
||||
for _ in iter(int, 1):
|
||||
pass
|
||||
4
Bindings/python/requirements.txt
Normal file
4
Bindings/python/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
dacite
|
||||
schedule
|
||||
sqlalchemy
|
||||
requests
|
||||
7
Bindings/python/test.py
Normal file
7
Bindings/python/test.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from library import *
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
P2PRCNodes = ListNodes()
|
||||
# Print nodes in the network
|
||||
print(P2PRCNodes)
|
||||
@@ -641,7 +641,7 @@ cd Bindings/python/export/
|
||||
# list files
|
||||
ls
|
||||
# Expected output
|
||||
SharedObjects/ p2prc.py
|
||||
SharedObjects/ library.py requirements.txt
|
||||
#+end_src
|
||||
|
||||
Above shows a generated folder which consists of a folder called
|
||||
|
||||
@@ -6,12 +6,19 @@ mkdir Bindings/python/export
|
||||
# Creating SharedObjects directory for python
|
||||
mkdir Bindings/python/export/SharedObjects
|
||||
|
||||
# Builds p2prc.h and p2prc.so file
|
||||
# as apart of Go FFI to interacted
|
||||
# with later on.
|
||||
sh build-bindings.sh
|
||||
|
||||
# Copy the shared object files as well to ensure
|
||||
# that python can interact with the latest snapshot
|
||||
# of P2PRC
|
||||
cp Bindings/p2prc.h Bindings/python/export/SharedObjects/
|
||||
cp Bindings/p2prc.so Bindings/python/export/SharedObjects/
|
||||
|
||||
cp Bindings/python/p2prc.py Bindings/python/export/
|
||||
# Copy python library and tests as export as well
|
||||
cp Bindings/python/* Bindings/python/export/
|
||||
|
||||
echo "Output is in the Directory Bindings/python/export/"
|
||||
|
||||
@@ -25,4 +32,4 @@ echo "Output is in the Directory Bindings/python/export/"
|
||||
# env GOOS=linux GOARCH=${arch} go build -buildmode=c-shared -o python/export/SharedObjects/linux-${arch}/p2prc.so
|
||||
# echo "GOOS=linux GOARCH=${arch} go build -buildmode=c-shared -o python/export/SharedObjects/linux-${arch}/p2prc.so"
|
||||
# cd ..
|
||||
#done
|
||||
#done
|
||||
|
||||
Reference in New Issue
Block a user