Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ĞMixer-py
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pascal Engélibert
ĞMixer-py
Commits
87fcbb86
Commit
87fcbb86
authored
5 years ago
by
Pascal Engélibert
Browse files
Options
Downloads
Patches
Plain Diff
Standardize function names
parent
bb015a71
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#5634
passed
5 years ago
Stage: deploy
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
client.py
+5
-5
5 additions, 5 deletions
client.py
server.py
+86
-86
86 additions, 86 deletions
server.py
utils.py
+4
-4
4 additions, 4 deletions
utils.py
with
95 additions
and
95 deletions
client.py
+
5
−
5
View file @
87fcbb86
...
...
@@ -253,17 +253,17 @@ def mix(db_txs, amount, base, sender, path, host, proxy=None, proxy_onion_only=F
if
send_tx
:
try
:
send
T
ransaction
(
sender
,
path
[
0
],
amount
,
gen_comment
(
comment_seeds
[
0
]))
send
_t
ransaction
(
sender
,
path
[
0
],
amount
,
gen_comment
(
comment_seeds
[
0
]))
message
[
"
sent
"
]
=
True
db_txs
.
put
(
comment_seeds
[
0
][
1
],
PublicKey
(
sender
.
pubkey
).
encrypt_seal
(
ubjson
.
dumpb
(
message
)))
except
socket
.
timeout
:
log
P
rint
(
"
Error when sending tx: timeout
"
,
LOG_ERROR
)
log
p
rint
(
"
Error when sending tx: timeout
"
,
LOG_ERROR
)
except
Exception
as
e
:
log
P
rint
(
"
Error when sending tx:
"
+
str
(
e
),
LOG_ERROR
)
log
p
rint
(
"
Error when sending tx:
"
+
str
(
e
),
LOG_ERROR
)
return
async
def
test1
(
db_txs
,
host
,
receiver
,
amount
=
1000
,
layers
=
3
,
proxy
=
None
,
proxy_onion_only
=
False
,
send_tx
=
True
):
async
def
main
(
db_txs
,
host
,
receiver
,
amount
=
1000
,
layers
=
3
,
proxy
=
None
,
proxy_onion_only
=
False
,
send_tx
=
True
):
if
amount
<
100
:
print
(
"
!! Warning !!
\n
You are going to send less than 1.00, all this money will be destroyed by Duniter.
\n
Please always send 1.00 or more.
"
)
if
input
(
"
Do it anyway? [yn]:
"
).
lower
()
!=
"
y
"
:
...
...
@@ -337,4 +337,4 @@ python3 client.py -h svetsae7j3usrycn.onion 10951 -r 78ZwwgpgdH5uLZLbThUQH7LKwPg
db_txs
=
plyvel
.
DB
(
DIR
+
"
/client_db_txs
"
,
create_if_missing
=
True
)
asyncio
.
get_event_loop
().
run_until_complete
(
test1
(
db_txs
,
(
host
,
port
),
receiver
,
amount
,
layers
,
proxy
,
proxy_onion_only
,
send_tx
))
asyncio
.
get_event_loop
().
run_until_complete
(
main
(
db_txs
,
(
host
,
port
),
receiver
,
amount
,
layers
,
proxy
,
proxy_onion_only
,
send_tx
))
This diff is collapsed.
Click to expand it.
server.py
+
86
−
86
View file @
87fcbb86
...
...
@@ -55,7 +55,7 @@ MIX_INTERVAL = 60
MIX_MIN_TXS
=
5
# minimum amount of txs to mix
MIX_REQ_AGE_MAX
=
604800
# maximum mix request age before return to sender
def
send
R
esponse
(
client
,
code
,
resp
,
dataformat
=
"
ubjson
"
):
def
send
_r
esponse
(
client
,
code
,
resp
,
dataformat
=
"
ubjson
"
):
if
dataformat
==
"
ubjson
"
:
content_raw
=
ubjson
.
dumpb
(
resp
)
mime
=
"
application/ubjson
"
...
...
@@ -90,7 +90,7 @@ class TX:
self
.
confirms
=
b
""
self
.
tx_sent
=
False
def
gen
MixC
onfirm
(
self
,
keys
):
def
gen
_mix_c
onfirm
(
self
,
keys
):
message
=
{
"
document
"
:
"
gmixer-mixconfirm1
"
,
"
sender_pubkey
"
:
self
.
sender_pubkey
,
...
...
@@ -152,14 +152,14 @@ def load_txs(db_txs, pool, tx_in_index, tx_out_index):
tx_in_index
[
tx
.
in_seeds
[
1
]]
=
tx
tx_out_index
[
tx
.
out_seeds
[
1
]]
=
tx
utils
.
log
P
rint
(
"
Loaded
"
+
str
(
len
(
pool
))
+
"
txs
"
,
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Loaded
"
+
str
(
len
(
pool
))
+
"
txs
"
,
utils
.
LOG_TRACE
)
def
save_txs
(
db_txs
,
pool
):
for
tx
in
pool
:
tx
.
export_ubjson
(
db_txs
)
# Read ini config file
def
read
C
onfig
(
cdir
,
conf_overwrite
=
{}):
def
read
_c
onfig
(
cdir
,
conf_overwrite
=
{}):
if
not
os
.
path
.
isfile
(
cdir
+
"
/config.json
"
):
configfile
=
open
(
cdir
+
"
/config.json
"
,
"
w
"
)
configfile
.
write
(
"
{}
"
)
...
...
@@ -169,7 +169,7 @@ def readConfig(cdir, conf_overwrite={}):
try
:
conf
=
json
.
load
(
configfile
)
except
json
.
JSONDecodeError
:
utils
.
log
P
rint
(
"
Config: bad JSON => abort
"
,
utils
.
LOG_ERROR
)
utils
.
log
p
rint
(
"
Config: bad JSON => abort
"
,
utils
.
LOG_ERROR
)
exit
(
1
)
conf
.
setdefault
(
"
server
"
,
{})
...
...
@@ -227,7 +227,7 @@ class ServerThread(Thread):
self
.
sock
.
settimeout
(
5
)
self
.
sock
.
bind
(
server_addr
)
self
.
sock
.
listen
(
1
)
utils
.
log
P
rint
(
"
Server started at
"
+
str
(
server_addr
),
utils
.
LOG_INFO
)
utils
.
log
p
rint
(
"
Server started at
"
+
str
(
server_addr
),
utils
.
LOG_INFO
)
while
self
.
work
:
try
:
...
...
@@ -275,7 +275,7 @@ class ServerThread(Thread):
try
:
url
=
httpreq
[
0
].
split
(
b
"
"
)[
1
].
decode
().
split
(
"
/
"
)
except
IndexError
:
send
R
esponse
(
client
,
"
400 Bad Request
"
,
{
"
error
"
:
"
bad_http
"
})
send
_r
esponse
(
client
,
"
400 Bad Request
"
,
{
"
error
"
:
"
bad_http
"
})
continue
while
""
in
url
:
url
.
remove
(
""
)
...
...
@@ -297,26 +297,26 @@ class ServerThread(Thread):
in_amount
=
int
(
utils
.
getargv
(
"
mix
"
,
""
,
2
,
url
))
in_base
=
int
(
utils
.
getargv
(
"
mix
"
,
""
,
3
,
url
))
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_amount_base
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_amount_base
"
},
resp_format
)
continue
send_confirm
=
not
"
client
"
in
url
try
:
sender_keys
=
PublicKey
(
sender_pubkey
)
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_sender_pubkey
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_sender_pubkey
"
},
resp_format
)
continue
try
:
raw
=
libnacl
.
sign
.
Verifier
(
sender_keys
.
hex_pk
()).
verify
(
content
)
# Verify
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_signature
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_signature
"
},
resp_format
)
continue
try
:
data
=
self
.
keys
.
decrypt_seal
(
raw
[
32
:])
# Decrypt
except
libnacl
.
CryptError
:
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_encryption
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_encryption
"
},
resp_format
)
continue
try
:
...
...
@@ -328,7 +328,7 @@ class ServerThread(Thread):
assert
"
message
"
in
data
and
type
(
data
[
"
message
"
])
==
bytes
assert
(
data
[
"
message
"
]
==
b
""
and
type
(
data
[
"
out_seeds
"
][
2
])
==
bytes
and
len
(
data
[
"
out_seeds
"
][
2
])
==
32
)
or
(
data
[
"
message
"
]
!=
b
""
and
data
[
"
out_seeds
"
][
2
]
==
None
)
except
(
ubjson
.
decoder
.
DecoderException
,
AssertionError
):
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_ubjson
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_ubjson
"
},
resp_format
)
continue
receiver_pubkey
=
data
[
"
receiver
"
]
# receiver pubkey
...
...
@@ -344,13 +344,13 @@ class ServerThread(Thread):
try
:
PublicKey
(
receiver_pubkey
)
except
ValueError
:
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_rec_pubkey
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_rec_pubkey
"
},
resp_format
)
continue
try
:
PublicKey
(
onetime_pubkey
)
except
ValueError
:
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_onetime_pubkey
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_onetime_pubkey
"
},
resp_format
)
continue
# Save tx in pool
...
...
@@ -359,7 +359,7 @@ class ServerThread(Thread):
last_node
=
len
(
message
)
==
0
tx
.
need_send
=
not
last_node
tx
.
can_confirm
=
last_node
utils
.
log
P
rint
(
"
TX
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
TX
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
self
.
tx_out_index
[
out_seeds
[
1
]]
=
tx
self
.
tx_in_index
[
in_seeds
[
1
]]
=
tx
self
.
pool
.
append
(
tx
)
...
...
@@ -372,21 +372,21 @@ class ServerThread(Thread):
try
:
out_seed1
=
bytes
.
fromhex
(
utils
.
getargv
(
"
confirm
"
,
""
,
2
,
url
))
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_url
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_url
"
},
resp_format
)
continue
if
not
out_seed1
in
self
.
tx_out_index
:
send
R
esponse
(
client
,
"
404 Not Found
"
,
{
"
error
"
:
"
unknown_tx
"
},
resp_format
)
send
_r
esponse
(
client
,
"
404 Not Found
"
,
{
"
error
"
:
"
unknown_tx
"
},
resp_format
)
continue
tx
=
self
.
tx_out_index
[
out_seed1
]
if
len
(
tx
.
confirms
)
>
0
or
tx
.
can_confirm
or
not
tx
.
need_confirm
or
tx
.
need_send
:
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
cannot_confirm
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
cannot_confirm
"
},
resp_format
)
continue
if
receiver_pubkey
!=
tx
.
receiver_pubkey
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_rec_pubkey
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_rec_pubkey
"
},
resp_format
)
continue
receiver_keys
=
PublicKey
(
tx
.
receiver_pubkey
)
...
...
@@ -394,23 +394,23 @@ class ServerThread(Thread):
try
:
data
=
libnacl
.
sign
.
Verifier
(
receiver_keys
.
hex_pk
()).
verify
(
content
)
# Verify
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_signature
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_signature
"
},
resp_format
)
continue
try
:
data
=
self
.
keys
.
decrypt_seal
(
data
)
# Decrypt
except
libnacl
.
CryptError
:
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_encryption
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_encryption
"
},
resp_format
)
continue
if
data
[:
32
]
!=
tx
.
out_seeds
[
1
]
or
len
(
data
)
<
64
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_seed
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_seed
"
},
resp_format
)
continue
tx
.
confirms
=
data
[
64
:]
# TODO check size
tx
.
out_seeds
[
2
]
=
data
[
32
:
64
]
utils
.
log
P
rint
(
"
Rec confirm
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Rec confirm
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
tx
.
can_confirm
=
True
resp
[
"
confirm_ok
"
]
=
tx
.
out_seeds
[
2
]
...
...
@@ -420,28 +420,28 @@ class ServerThread(Thread):
try
:
in_seed1
=
bytes
.
fromhex
(
utils
.
getargv
(
"
getconfirm
"
,
""
,
2
,
url
))
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_url
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_url
"
},
resp_format
)
continue
if
not
in_seed1
in
self
.
tx_in_index
:
send
R
esponse
(
client
,
"
404 Not Found
"
,
{
"
error
"
:
"
unknown_tx
"
},
resp_format
)
send
_r
esponse
(
client
,
"
404 Not Found
"
,
{
"
error
"
:
"
unknown_tx
"
},
resp_format
)
continue
tx
=
self
.
tx_in_index
[
in_seed1
]
if
not
tx
.
can_confirm
or
not
tx
.
need_confirm
or
tx
.
need_send
:
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
cannot_confirm
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
cannot_confirm
"
},
resp_format
)
continue
if
sender_pubkey
!=
tx
.
sender_pubkey
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_rec_pubkey
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_rec_pubkey
"
},
resp_format
)
continue
message
=
tx
.
gen
MixC
onfirm
(
self
.
keys
)
message
=
tx
.
gen
_mix_c
onfirm
(
self
.
keys
)
resp
[
"
confirm
"
]
=
message
tx
.
need_confirm
=
False
tx
.
export_ubjson
(
self
.
db_txs
)
utils
.
log
P
rint
(
"
Confirmed
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Confirmed
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
if
"
list
"
in
url
:
peers_list
=
[]
...
...
@@ -454,22 +454,22 @@ class ServerThread(Thread):
try
:
new_keys
=
PublicKey
(
new_pubkey
)
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_pubkey
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_pubkey
"
},
resp_format
)
continue
try
:
message
=
libnacl
.
sign
.
Verifier
(
new_keys
.
hex_pk
()).
verify
(
content
)
# Verify
except
ValueError
:
send
R
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_signature
"
},
resp_format
)
send
_r
esponse
(
client
,
"
401 Unauthorized
"
,
{
"
error
"
:
"
bad_signature
"
},
resp_format
)
continue
try
:
message
=
self
.
keys
.
decrypt_seal
(
message
)
# Decrypt
except
libnacl
.
CryptError
:
send
R
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_encryption
"
},
resp_format
)
send
_r
esponse
(
client
,
"
403 Forbidden
"
,
{
"
error
"
:
"
bad_encryption
"
},
resp_format
)
continue
try
:
message
=
ubjson
.
loadb
(
message
)
except
ubjson
.
decoder
.
DecoderException
:
send
R
esponse
(
client
,
"
400 Bad Request
"
,
{
"
error
"
:
"
bad_ubjson
"
},
resp_format
)
send
_r
esponse
(
client
,
"
400 Bad Request
"
,
{
"
error
"
:
"
bad_ubjson
"
},
resp_format
)
continue
if
new_pubkey
==
message
[
"
pubkey
"
]:
...
...
@@ -477,13 +477,13 @@ class ServerThread(Thread):
peer
=
utils
.
Peer
(
new_pubkey
,
message
[
"
host
"
],
message
[
"
port
"
],
True
)
self
.
peers
.
append
(
peer
)
self
.
peers_index
[
new_pubkey
]
=
peer
utils
.
log
P
rint
(
"
Add
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Add
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
else
:
self
.
peers_index
[
new_pubkey
].
up
=
True
utils
.
log
P
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
# Send response
send
R
esponse
(
client
,
"
200 OK
"
,
resp
,
resp_format
)
send
_r
esponse
(
client
,
"
200 OK
"
,
resp
,
resp_format
)
self
.
sock
.
close
()
def
stop
(
self
):
...
...
@@ -506,8 +506,8 @@ class ClientThread(Thread):
self
.
bma_endpoints
=
[
"
BMAS
"
+
host
for
host
in
conf
[
"
client
"
][
"
bma_hosts
"
]]
self
.
work
=
True
def
detect
P
eers
(
self
):
# Check known peers and ask them for their known peer list
utils
.
log
P
rint
(
"
Start peers detection
"
,
utils
.
LOG_TRACE
)
def
detect
_p
eers
(
self
):
# Check known peers and ask them for their known peer list
utils
.
log
p
rint
(
"
Start peers detection
"
,
utils
.
LOG_TRACE
)
modified
=
True
asked
=
[]
...
...
@@ -527,42 +527,42 @@ class ClientThread(Thread):
})
message
=
peer
.
keys
.
encrypt_seal
(
message
)
# Encrypt
message
=
self
.
keys
.
sign
(
message
)
# Sign
utils
.
log
P
rint
(
"
Ask
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Ask
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
try
:
header
,
content
=
utils
.
sdata
((
peer
.
host
,
peer
.
port
),
"
POST
"
,
"
/list/new/
"
+
self
.
keys
.
pubkey
,
message
,
proxy
=
self
.
conf
[
"
client
"
][
"
proxy
"
],
proxy_onion_only
=
self
.
conf
[
"
client
"
][
"
proxy_onion_only
"
])
# Send
except
(
ConnectionRefusedError
,
socks
.
GeneralProxyError
,
socket
.
gaierror
,
socket
.
timeout
):
peer
.
up
=
False
utils
.
log
P
rint
(
"
Down
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Down
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
continue
peer
.
up
=
True
utils
.
log
P
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
try
:
message
=
ubjson
.
loadb
(
content
)
assert
"
peers
"
in
message
assert
type
(
message
[
"
peers
"
])
==
list
except
(
ubjson
.
decoder
.
DecoderException
,
AssertionError
):
utils
.
log
P
rint
(
"
Bad json from
"
+
str
(
peer
),
utils
.
LOG_ERROR
)
utils
.
log
p
rint
(
"
Bad json from
"
+
str
(
peer
),
utils
.
LOG_ERROR
)
continue
for
i_peer
in
message
[
"
peers
"
]:
try
:
assert
"
pubkey
"
in
i_peer
and
"
host
"
in
i_peer
and
"
port
"
in
i_peer
int
(
i_peer
[
"
port
"
])
except
(
AssertionError
,
ValueError
):
utils
.
log
P
rint
(
"
Bad json from
"
+
str
(
peer
),
utils
.
LOG_ERROR
)
utils
.
log
p
rint
(
"
Bad json from
"
+
str
(
peer
),
utils
.
LOG_ERROR
)
continue
if
i_peer
[
"
pubkey
"
]
in
self
.
peers_index
or
i_peer
[
"
pubkey
"
]
==
self
.
keys
.
pubkey
:
continue
try
:
new_peer
=
utils
.
Peer
(
i_peer
[
"
pubkey
"
],
i_peer
[
"
host
"
],
i_peer
[
"
port
"
],
None
)
except
ValueError
:
utils
.
log
P
rint
(
"
Bad pubkey from
"
+
str
(
peer
),
utils
.
LOG_ERROR
)
utils
.
log
p
rint
(
"
Bad pubkey from
"
+
str
(
peer
),
utils
.
LOG_ERROR
)
continue
self
.
peers
.
append
(
new_peer
)
self
.
peers_index
[
new_peer
.
pubkey
]
=
new_peer
modified
=
True
utils
.
log
P
rint
(
"
Add
"
+
str
(
new_peer
),
utils
.
LOG_TRACE
)
utils
.
log
P
rint
(
"
Finished peers detection
"
,
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Add
"
+
str
(
new_peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Finished peers detection
"
,
utils
.
LOG_TRACE
)
async
def
mix
(
self
):
can_mix
=
False
...
...
@@ -573,7 +573,7 @@ class ClientThread(Thread):
if
not
can_mix
:
return
utils
.
log
P
rint
(
"
Starting mix
"
,
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Starting mix
"
,
utils
.
LOG_TRACE
)
t
=
time
.
time
()
client
=
None
for
bma_endpoint
in
self
.
bma_endpoints
:
...
...
@@ -582,11 +582,11 @@ class ClientThread(Thread):
await
client
(
bma
.
node
.
summary
)
break
except
:
utils
.
log
P
rint
(
"
BMA down:
"
+
bma_endpoint
,
utils
.
LOG_WARN
)
utils
.
log
p
rint
(
"
BMA down:
"
+
bma_endpoint
,
utils
.
LOG_WARN
)
await
client
.
close
()
client
=
None
if
client
:
utils
.
log
P
rint
(
"
BMA up:
"
+
bma_endpoint
,
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
BMA up:
"
+
bma_endpoint
,
utils
.
LOG_TRACE
)
ready_txs
=
[]
# Get txs history
...
...
@@ -624,24 +624,24 @@ class ClientThread(Thread):
random
.
shuffle
(
txs
)
for
tx
in
txs
:
try
:
utils
.
send
T
ransaction
(
self
.
keys
,
tx
.
receiver_pubkey
,
tx
.
out_amount
,
utils
.
gen_comment
(
tx
.
out_seeds
))
utils
.
send
_t
ransaction
(
self
.
keys
,
tx
.
receiver_pubkey
,
tx
.
out_amount
,
utils
.
gen_comment
(
tx
.
out_seeds
))
tx
.
tx_sent
=
True
tx
.
export_ubjson
(
self
.
db_txs
)
except
socket
.
timeout
:
utils
.
log
P
rint
(
"
Error when sending tx: timeout
"
,
utils
.
LOG_ERROR
)
utils
.
log
p
rint
(
"
Error when sending tx: timeout
"
,
utils
.
LOG_ERROR
)
except
Exception
as
e
:
utils
.
log
P
rint
(
"
Error when sending tx:
"
+
str
(
e
),
utils
.
LOG_ERROR
)
utils
.
log
p
rint
(
"
Error when sending tx:
"
+
str
(
e
),
utils
.
LOG_ERROR
)
else
:
utils
.
log
P
rint
(
"
Not enough ready txs to mix (
"
+
str
(
amount
)
+
"
u) (only
"
+
str
(
len
(
txs
))
+
"
)
"
,
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Not enough ready txs to mix (
"
+
str
(
amount
)
+
"
u) (only
"
+
str
(
len
(
txs
))
+
"
)
"
,
utils
.
LOG_TRACE
)
utils
.
log
P
rint
(
"
Mix finished
"
,
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Mix finished
"
,
utils
.
LOG_TRACE
)
else
:
utils
.
log
P
rint
(
"
All BMA endpoints down: cannot mix!
"
,
utils
.
LOG_ERROR
)
utils
.
log
p
rint
(
"
All BMA endpoints down: cannot mix!
"
,
utils
.
LOG_ERROR
)
def
run
(
self
):
asyncio
.
new_event_loop
().
run_until_complete
(
self
.
start
C
lient
())
asyncio
.
new_event_loop
().
run_until_complete
(
self
.
start
_c
lient
())
async
def
start
C
lient
(
self
):
async
def
start
_c
lient
(
self
):
next_peers_detection
=
0
next_mix
=
time
.
time
()
+
self
.
conf
[
"
mix
"
][
"
mix_interval
"
]
...
...
@@ -650,7 +650,7 @@ class ClientThread(Thread):
# Detect peers
if
t
>
next_peers_detection
:
self
.
detect
P
eers
()
self
.
detect
_p
eers
()
next_peers_detection
=
time
.
time
()
+
120
# Mix
...
...
@@ -661,7 +661,7 @@ class ClientThread(Thread):
for
tx
in
self
.
pool
:
if
tx
.
need_send
:
utils
.
log
P
rint
(
"
Send
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Send
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
if
tx
.
receiver_pubkey
in
self
.
peers_index
:
peer
=
self
.
peers_index
[
tx
.
receiver_pubkey
]
message
=
self
.
keys
.
sign
(
tx
.
out_seeds
[
1
]
+
tx
.
message
)
# Sign
...
...
@@ -673,22 +673,22 @@ class ClientThread(Thread):
tx
.
need_send
=
False
tx
.
export_ubjson
(
self
.
db_txs
)
peer
.
up
=
True
utils
.
log
P
rint
(
"
Sent
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
P
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Sent
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
except
(
ConnectionRefusedError
,
socks
.
GeneralProxyError
,
socket
.
gaierror
,
socket
.
timeout
):
peer
.
up
=
False
utils
.
log
P
rint
(
"
Down
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Down
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
except
(
ubjson
.
decoder
.
DecoderException
,
KeyError
,
AssertionError
):
utils
.
log
P
rint
(
"
Error: bad response from
"
+
str
(
peer
),
utils
.
LOG_WARN
)
utils
.
log
p
rint
(
"
Error: bad response from
"
+
str
(
peer
),
utils
.
LOG_WARN
)
else
:
utils
.
log
P
rint
(
"
Unknown peer:
"
+
tx
.
receiver_pubkey
,
utils
.
LOG_WARN
)
utils
.
log
p
rint
(
"
Unknown peer:
"
+
tx
.
receiver_pubkey
,
utils
.
LOG_WARN
)
elif
tx
.
can_confirm
and
tx
.
need_confirm
and
tx
.
send_confirm
:
utils
.
log
P
rint
(
"
Confirm
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Confirm
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
if
tx
.
sender_pubkey
in
self
.
peers_index
:
peer
=
self
.
peers_index
[
tx
.
sender_pubkey
]
message
=
tx
.
gen
MixC
onfirm
(
self
.
keys
)
message
=
tx
.
gen
_mix_c
onfirm
(
self
.
keys
)
try
:
header
,
content
=
utils
.
sdata
((
peer
.
host
,
peer
.
port
),
"
POST
"
,
"
/confirm/
"
+
self
.
keys
.
pubkey
+
"
/
"
+
tx
.
in_seeds
[
1
].
hex
(),
message
,
proxy
=
self
.
conf
[
"
client
"
][
"
proxy
"
],
proxy_onion_only
=
self
.
conf
[
"
client
"
][
"
proxy_onion_only
"
])
...
...
@@ -697,16 +697,16 @@ class ClientThread(Thread):
tx
.
need_confirm
=
False
tx
.
export_ubjson
(
self
.
db_txs
)
peer
.
up
=
True
utils
.
log
P
rint
(
"
Confirmed
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
P
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Confirmed
"
+
tx
.
sender_pubkey
[:
8
]
+
"
->
"
+
tx
.
receiver_pubkey
[:
8
]
+
"
=
"
+
str
(
tx
.
in_amount
)
+
"
:
"
+
str
(
tx
.
in_base
)
+
"
->
"
+
str
(
tx
.
out_amount
)
+
"
:
"
+
str
(
tx
.
out_base
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Up
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
except
(
ConnectionRefusedError
,
socks
.
GeneralProxyError
,
socket
.
gaierror
,
socket
.
timeout
):
peer
.
up
=
False
utils
.
log
P
rint
(
"
Down
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Down
"
+
str
(
peer
),
utils
.
LOG_TRACE
)
except
(
ubjson
.
decoder
.
DecoderException
,
KeyError
,
AssertionError
):
utils
.
log
P
rint
(
"
Error: bad response from
"
+
str
(
peer
),
utils
.
LOG_WARN
)
utils
.
log
p
rint
(
"
Error: bad response from
"
+
str
(
peer
),
utils
.
LOG_WARN
)
else
:
utils
.
log
P
rint
(
"
Unknown peer:
"
+
tx
.
sender_pubkey
,
utils
.
LOG_WARN
)
utils
.
log
p
rint
(
"
Unknown peer:
"
+
tx
.
sender_pubkey
,
utils
.
LOG_WARN
)
# Remove expired requests
expire_txs
=
[]
...
...
@@ -720,11 +720,11 @@ class ClientThread(Thread):
self
.
db_txs
.
delete
(
tx
.
in_seeds
[
2
])
self
.
pool
.
remove
(
tx
)
if
len
(
expire_txs
)
>
0
:
utils
.
log
P
rint
(
"
Removed
"
+
str
(
len
(
expire_txs
))
+
"
expired txs
"
,
utils
.
LOG_TRACE
)
utils
.
log
p
rint
(
"
Removed
"
+
str
(
len
(
expire_txs
))
+
"
expired txs
"
,
utils
.
LOG_TRACE
)
time
.
sleep
(
5
)
def
get
C
redentials
(
conf
):
def
get
_c
redentials
(
conf
):
salt
=
conf
[
"
crypto
"
][
"
id_salt
"
]
if
salt
==
""
:
salt
=
getpass
.
getpass
(
"
Enter your passphrase (salt):
"
)
...
...
@@ -736,8 +736,8 @@ def getCredentials(conf):
# Main function
def
main
():
# Load conf & peers
conf
=
read
C
onfig
(
DIR
)
peers
,
peers_index
=
utils
.
read
P
eers
(
DIR
)
conf
=
read
_c
onfig
(
DIR
)
peers
,
peers_index
=
utils
.
read
_p
eers
(
DIR
)
# Load txs
pool
=
[]
...
...
@@ -747,9 +747,9 @@ def main():
load_txs
(
db_txs
,
pool
,
tx_in_index
,
tx_out_index
)
# Get private key
salt
,
password
=
get
C
redentials
(
conf
)
salt
,
password
=
get
_c
redentials
(
conf
)
keys
=
SigningKey
.
from_credentials
(
salt
,
password
)
utils
.
log
P
rint
(
"
Pubkey:
"
+
keys
.
pubkey
,
utils
.
LOG_INFO
)
utils
.
log
p
rint
(
"
Pubkey:
"
+
keys
.
pubkey
,
utils
.
LOG_INFO
)
# Start threads
clientThread
=
ClientThread
(
conf
,
peers
,
peers_index
,
keys
,
pool
,
tx_in_index
,
tx_out_index
,
db_txs
)
...
...
@@ -763,7 +763,7 @@ def main():
while
True
:
input
()
except
KeyboardInterrupt
:
utils
.
log
P
rint
(
"
Stopping (^C)...
"
,
utils
.
LOG_INFO
)
utils
.
log
p
rint
(
"
Stopping (^C)...
"
,
utils
.
LOG_INFO
)
# Stop threads
serverThread
.
stop
()
...
...
@@ -772,7 +772,7 @@ def main():
clientThread
.
join
()
# Save
utils
.
write
P
eers
(
DIR
,
peers
)
utils
.
write
_p
eers
(
DIR
,
peers
)
save_txs
(
db_txs
,
pool
)
db_txs
.
close
()
...
...
@@ -793,23 +793,23 @@ if __name__ == "__main__":
PUBLIC_HOST
=
subprocess
.
run
([
'
curl
'
,
'
-4
'
,
'
https://zettascript.org/tux/ip/
'
],
stdout
=
subprocess
.
PIPE
).
stdout
.
decode
(
"
utf-8
"
)
print
(
"
Public host:
"
+
PUBLIC_HOST
)
conf_overwrite
[
"
server.public_host
"
]
=
PUBLIC_HOST
read
C
onfig
(
DIR
,
conf_overwrite
)
read
_c
onfig
(
DIR
,
conf_overwrite
)
if
"
-s
"
in
sys
.
argv
:
main
()
elif
"
-i
"
in
sys
.
argv
:
conf
=
read
C
onfig
(
DIR
)
utils
.
read
P
eers
(
DIR
)
conf
=
read
_c
onfig
(
DIR
)
utils
.
read
_p
eers
(
DIR
)
elif
"
-I
"
in
sys
.
argv
:
ID_SALT
,
ID_PASSWORD
=
gen_keys
()
conf
=
read
C
onfig
(
DIR
)
utils
.
read
P
eers
(
DIR
)
conf
=
read
_c
onfig
(
DIR
)
utils
.
read
_p
eers
(
DIR
)
elif
"
-k
"
in
sys
.
argv
:
conf
=
read
C
onfig
(
DIR
)
salt
,
password
=
get
C
redentials
(
conf
)
conf
=
read
_c
onfig
(
DIR
)
salt
,
password
=
get
_c
redentials
(
conf
)
keys
=
SigningKey
.
from_credentials
(
salt
,
password
)
print
(
keys
.
pubkey
)
...
...
This diff is collapsed.
Click to expand it.
utils.py
+
4
−
4
View file @
87fcbb86
...
...
@@ -109,7 +109,7 @@ LOG_ERROR = 8
LOGMSG_TYPES
=
{
LOG_INFO
:
"
\033
[96minfo
\033
[0m
"
,
LOG_TRACE
:
"
\033
[39mtrace
\033
[0m
"
,
LOG_WARN
:
"
\033
[93mwarn
\033
[0m
"
,
LOG_ERROR
:
"
\033
[91merror
\033
[0m
"
}
VERBOSITY
=
LOG_INFO
|
LOG_WARN
|
LOG_ERROR
def
log
P
rint
(
msg
:
str
,
msgtype
:
int
):
def
log
p
rint
(
msg
:
str
,
msgtype
:
int
):
if
msgtype
&
VERBOSITY
:
print
(
time
.
strftime
(
"
%Y-%m-%d %H:%M:%S
"
)
+
"
[
"
+
LOGMSG_TYPES
[
msgtype
]
+
"
]
"
+
msg
)
...
...
@@ -138,7 +138,7 @@ class Peer:
return
self
.
pubkey
+
"
"
+
self
.
host
+
"
"
+
str
(
self
.
port
)
# Read peers list
def
read
P
eers
(
cdir
:
str
)
->
(
list
,
dict
):
def
read
_p
eers
(
cdir
:
str
)
->
(
list
,
dict
):
if
not
os
.
path
.
isfile
(
cdir
+
"
/peers
"
):
open
(
cdir
+
"
/peers
"
,
"
w
"
).
close
()
...
...
@@ -158,13 +158,13 @@ def readPeers(cdir:str) -> (list, dict):
return
peers
,
peers_index
# Save peers list
def
write
P
eers
(
cdir
:
str
,
peers
:
list
):
def
write
_p
eers
(
cdir
:
str
,
peers
:
list
):
peersfile
=
open
(
cdir
+
"
/peers
"
,
"
w
"
)
for
peer
in
peers
:
peersfile
.
write
(
peer
.
export_str
()
+
"
\n
"
)
peersfile
.
close
()
def
send
T
ransaction
(
sender_keys
:
SigningKey
,
receiver_pubkey
:
str
,
amount
:
int
,
comment
:
str
):
def
send
_t
ransaction
(
sender_keys
:
SigningKey
,
receiver_pubkey
:
str
,
amount
:
int
,
comment
:
str
):
sender_amount
=
silkaj
.
money
.
get_amount_from_pubkey
(
sender_keys
.
pubkey
)[
0
]
assert
sender_amount
>=
amount
,
"
not enough money
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment